This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY-9468
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY-9468 by this push:
     new 29b1398  Minor refactoring
29b1398 is described below

commit 29b13983ec9b618d885219a72e98cc3a458f4507
Author: Daniel Sun <[email protected]>
AuthorDate: Mon Mar 16 01:08:59 2020 +0800

    Minor refactoring
---
 .../main/groovy/groovy/console/ui/Console.groovy   | 137 +++++++++++++++------
 .../groovy/groovy/console/ui/ConsoleActions.groovy |   2 +-
 2 files changed, 99 insertions(+), 40 deletions(-)

diff --git 
a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy 
b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
index 9617d39..37ec862 100644
--- 
a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
+++ 
b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
@@ -18,7 +18,7 @@
  */
 package groovy.console.ui
 
-
+import com.github.javaparser.ParseProblemException
 import com.github.javaparser.StaticJavaParser
 import com.github.javaparser.ast.CompilationUnit
 import com.github.javaparser.ast.Modifier
@@ -919,15 +919,8 @@ class Console implements CaretListener, HyperlinkListener, 
ComponentListener, Fo
                     int errorLine = se.line
                     String message = se.originalMessage
 
-                    String scriptFileName = scriptFile?.name ?: 
DEFAULT_SCRIPT_NAME_START
-
                     def doc = outputArea.styledDocument
-
-                    def style = hyperlinkStyle
-                    def hrefAttr = new SimpleAttributeSet()
-                    // don't pass a GString as it won't be coerced to String 
as addAttribute takes an Object
-                    hrefAttr.addAttribute(HTML.Attribute.HREF, 'file://' + 
scriptFileName + ':' + errorLine)
-                    style.addAttribute(HTML.Tag.A, hrefAttr)
+                    Style style = createLinkStyle(errorLine)
 
                     insertString(doc, doc.length, message + ' at ', 
stacktraceStyle)
                     insertString(doc, doc.length, "line: ${se.line}, column: 
${se.startColumn}\n\n", style)
@@ -940,6 +933,29 @@ class Console implements CaretListener, HyperlinkListener, 
ComponentListener, Fo
                     insertString(doc, doc.length, "${error.message}\n", new 
SimpleAttributeSet())
                 }
             }
+        } else if (t instanceof ParseProblemException) {
+            def problemList = ((ParseProblemException) t).getProblems()
+            int count = problemList.size()
+            appendOutputNl("${count} compilation error${count > 1 ? 's' : 
''}:\n\n", commandStyle)
+
+            def doc = outputArea.styledDocument
+            problemList.each { p ->
+                insertString(doc, doc.length, "${p.message}", stacktraceStyle)
+
+                if (p.location.isPresent()) {
+                    def range = p.location.get().begin.range
+                    if (range.isPresent()) {
+                        def position = range.get().begin
+                        def errorLine = position.line
+                        def errorCol = position.column
+                        Style style = createLinkStyle(errorLine)
+
+                        insertString(doc, doc.length, " at ", stacktraceStyle)
+                        insertString(doc, doc.length, "line: ${errorLine}, 
column: ${errorCol}\n\n", style)
+                    }
+                }
+
+            }
         } else {
             reportException(t)
         }
@@ -957,6 +973,16 @@ class Console implements CaretListener, HyperlinkListener, 
ComponentListener, Fo
         }
     }
 
+    private Style createLinkStyle(int errorLine) {
+        String scriptFileName = scriptFile?.name ?: DEFAULT_SCRIPT_NAME_START
+        def style = hyperlinkStyle
+        def hrefAttr = new SimpleAttributeSet()
+        // don't pass a GString as it won't be coerced to String as 
addAttribute takes an Object
+        hrefAttr.addAttribute(HTML.Attribute.HREF, 'file://' + scriptFileName 
+ ':' + errorLine)
+        style.addAttribute(HTML.Tag.A, hrefAttr)
+        return style
+    }
+
     private calcPreferredSize(a, b, c) {
         [c, [a, b].min()].max()
     }
@@ -1142,20 +1168,69 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
         return consoleControllers.find { it.consoleId == consoleId }
     }
 
-    private enum SourceType {
-        GROOVY, JAVA
+    @CompileStatic
+    private class GroovySourceType extends SourceType {
+        GroovySourceType() {
+            super('groovy')
+        }
+
+        @Override
+        Object run(String src) {
+            String name = ((File) Console.this.scriptFile)?.name ?: 
(DEFAULT_SCRIPT_NAME_START + Console.this.scriptNameCounter++)
+            Console.this.shell.run(src, name, [])
+        }
     }
 
-    void runJava(EventObject evt = null) {
-        doRun(evt, SourceType.JAVA)
+    @CompileStatic
+    private class JavaSourceType extends SourceType {
+        JavaSourceType() {
+            super('java')
+        }
+
+        @Override
+        Object run(String src) {
+            Optional<String> optionalPrimaryClassName = 
findPrimaryClassName(src)
+            if (optionalPrimaryClassName.isPresent()) {
+                def js = new 
JavaShell(Thread.currentThread().contextClassLoader)
+                js.run(optionalPrimaryClassName.get(), src)
+            }
+            return null
+        }
     }
 
-    // actually run the script
-    void runScript(EventObject evt = null) {
-        doRun(evt, SourceType.GROOVY)
+    @CompileStatic
+    private abstract class SourceType {
+        String extension
+        SourceType(String extension) {
+            this.extension = extension
+        }
+
+        abstract Object run(String src)
+
+        @Override
+        boolean equals(o) {
+            if (this.is(o)) return true
+            if (!(o instanceof SourceType)) return false
+
+            SourceType that = (SourceType) o
+
+            if (extension != that.extension) return false
+
+            return true
+        }
+
+        @Override
+        int hashCode() {
+            return extension.hashCode()
+        }
     }
 
-    private doRun(EventObject evt = null, SourceType st) {
+    void runJava(EventObject evt = null) {
+        runScript(evt, new JavaSourceType())
+    }
+
+    // actually run the script
+    void runScript(EventObject evt = null, SourceType st = new 
GroovySourceType()) {
         saveInputAreaContentHash()
         if (saveOnRun && scriptFile != null) {
             if (fileSave(evt)) runScriptImpl(false, st)
@@ -1199,19 +1274,14 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
     }
 
     void runSelectedJava(EventObject evt = null) {
-        doRunSelected(evt, SourceType.JAVA)
+        runSelectedScript(evt, new JavaSourceType())
     }
 
-    void runSelectedScript(EventObject evt = null) {
-        doRunSelected(evt, SourceType.GROOVY)
-    }
-
-    private void doRunSelected(EventObject evt = null, SourceType st) {
+    void runSelectedScript(EventObject evt = null, SourceType st = new 
GroovySourceType()) {
         saveInputAreaContentHash()
         runScriptImpl(true, st)
     }
 
-
     void addClasspathJar(EventObject evt = null) {
         def fc = new JFileChooser(currentClasspathJarDir)
         fc.fileSelectionMode = JFileChooser.FILES_ONLY
@@ -1278,7 +1348,7 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
         inputAreaContentHash = inputArea.getText().hashCode()
     }
 
-    private void runScriptImpl(boolean selected, SourceType st = 
SourceType.GROOVY) {
+    private void runScriptImpl(boolean selected, SourceType st = new 
GroovySourceType()) {
         if (scriptRunning) {
             statusLabel.text = 'Cannot run script now as a script is already 
running. Please wait or use "Interrupt Script" option.'
             return
@@ -1296,9 +1366,9 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
 
         // Print the input text
         if (showScriptInOutput) {
-            final promptPrfix = SourceType.JAVA === st ? 'java> ' : 'groovy> '
+            final promptPrefix = "${st.extension}> "
             for (line in record.getTextToRun(selected).tokenize('\n')) {
-                appendOutputNl(promptPrfix, promptStyle)
+                appendOutputNl(promptPrefix, promptStyle)
                 appendOutput(line, commandStyle)
             }
             appendOutputNl(' \n', promptStyle)
@@ -1358,19 +1428,8 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
 
     @CompileStatic
     private Object doRun(boolean selected, SourceType st, HistoryRecord 
record) {
-        def result
         def src = record.getTextToRun(selected)
-        if (SourceType.JAVA === st) {
-            Optional<String> optionalPrimaryClassName = 
findPrimaryClassName(src)
-            if (optionalPrimaryClassName.isPresent()) {
-                def js = new 
JavaShell(Thread.currentThread().contextClassLoader)
-                js.run(optionalPrimaryClassName.get(), src)
-            }
-        } else {
-            String name = ((File) scriptFile)?.name ?: 
(DEFAULT_SCRIPT_NAME_START + scriptNameCounter++)
-            result = shell.run(src, name, [])
-        }
-        return result
+        return st.run(src)
     }
 
     @CompileStatic
diff --git 
a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
 
b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
index fb05c2c..6e06db3 100644
--- 
a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
+++ 
b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
@@ -228,7 +228,7 @@ runSelectionAction = action(
 )
 
 runJavaSelectionAction = action(
-        name: 'Run Java Selection',
+        name: 'Run Selection as Java',
         closure: controller.&runSelectedJava
 )
 

Reply via email to