groovy
  1. groovy
  2. GROOVY-3328

Automatic imports for selected text execution

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.3.0-beta-1
    • Component/s: Groovy Console
    • Labels:
      None
    • Testcase included:
      yes
    • Patch Submitted:
      Yes
    • Number of attachments :
      3

      Description

      As a Groovy Console user I would like to improve execution of selected text, so that all imports of script were automatically applied to selected text.
      So user will be eable to execute part of script without being forced to copy-paste import section. That would make running of a part of script much easier.
      Also I think Imports from selected text should have higher priority than imports from script.

      Attached patch implements changes that are required for this improvement.
      Any comments are welcome!

      1. AutomaticImports.2.patch
        12 kB
        Sergey Bondarenko
      2. AutomaticImports.3.patch
        13 kB
        Sergey Bondarenko
      3. AutomaticImports.patch
        11 kB
        Sergey Bondarenko

        Issue Links

          Activity

          Hide
          Sergey Bondarenko added a comment -

          Some fixes in suggested patch.
          Now unresolved imports from surrounding script are silently ignored.
          Also unit-test class was moved to src/test folder.

          Show
          Sergey Bondarenko added a comment - Some fixes in suggested patch. Now unresolved imports from surrounding script are silently ignored. Also unit-test class was moved to src/test folder.
          Hide
          Sergey Bondarenko added a comment -

          Minor fixes (default imports now are not applied by mistake in other call after the first one).

          Show
          Sergey Bondarenko added a comment - Minor fixes (default imports now are not applied by mistake in other call after the first one).
          Hide
          Pascal Schumacher added a comment -

          Jose Osinde comment copied from the closed duplicate (GROOVY-5205):

          I'm integrating a groovy applet on my own java application. For me it would be very useful to be able to run part of a loaded script using the "Run Selection" option in the groovy applet's main menu but this is most of the times impossible because the selected code include java classes that must be explicitly imported into the selection. To solve this problem I successfully included the following update in the groovy.ui.HistoryRecord (groovy) class:

          package groovy.ui
          
          class HistoryRecord {
          
              ...
          
              public String getTextToRun(boolean useSelection) {
                  if (useSelection && selectionStart != selectionEnd) {
                      // Retrieve all the imports included in the script before the current selection
                      def before = allText[0 ..< selectionStart].split("\n")
                      def importLines = before.findAll { it.trim().startsWith("import") }
                      def imports = importLines.join("\n")
                      def code = imports + "\n" + allText[selectionStart ..< selectionEnd] 
                      //println "code:\n" + code + "\n"
                      return code
                  }
                  return allText
              }
          

          Maybe it's also useful for other people.

          Show
          Pascal Schumacher added a comment - Jose Osinde comment copied from the closed duplicate ( GROOVY-5205 ): I'm integrating a groovy applet on my own java application. For me it would be very useful to be able to run part of a loaded script using the "Run Selection" option in the groovy applet's main menu but this is most of the times impossible because the selected code include java classes that must be explicitly imported into the selection. To solve this problem I successfully included the following update in the groovy.ui.HistoryRecord (groovy) class: package groovy.ui class HistoryRecord { ... public String getTextToRun( boolean useSelection) { if (useSelection && selectionStart != selectionEnd) { // Retrieve all the imports included in the script before the current selection def before = allText[0 ..< selectionStart].split( "\n" ) def importLines = before.findAll { it.trim().startsWith( " import " ) } def imports = importLines.join( "\n" ) def code = imports + "\n" + allText[selectionStart ..< selectionEnd] //println "code:\n" + code + "\n" return code } return allText } Maybe it's also useful for other people.
          Hide
          Pascal Schumacher added a comment -

          I was not able to adapt Sergey Bondarenko patch to current groovy, but I created a pull request using (some) of his tests and the solution suggested by Jose Osinde.

          Show
          Pascal Schumacher added a comment - I was not able to adapt Sergey Bondarenko patch to current groovy, but I created a pull request using (some) of his tests and the solution suggested by Jose Osinde.
          Hide
          blackdrag blackdrag added a comment -

          I applied the pull request

          Show
          blackdrag blackdrag added a comment - I applied the pull request

            People

            • Assignee:
              Pascal Schumacher
              Reporter:
              Sergey Bondarenko
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: