You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/08/06 18:31:56 UTC

[groovy] branch master updated: Highlight class info in object browser

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b76b2e6034 Highlight class info in object browser
b76b2e6034 is described below

commit b76b2e6034703caf85620cf14dca864a298f3131
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Aug 7 02:25:11 2022 +0800

    Highlight class info in object browser
---
 .../groovy/groovy/console/ui/ObjectBrowser.groovy  | 31 ++++++++++++++++++----
 .../console/ui/text/SmartDocumentFilter.java       | 13 ++++-----
 2 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ObjectBrowser.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ObjectBrowser.groovy
index 3c436b0b9f..b0d2b7d30c 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ObjectBrowser.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ObjectBrowser.groovy
@@ -32,6 +32,7 @@ import java.awt.datatransfer.StringSelection
 import java.awt.event.MouseAdapter
 import java.awt.event.MouseEvent
 
+import static groovy.console.ui.text.SmartDocumentFilter.HIGHLIGHTED_TOKEN_TYPE_LIST
 import static groovy.inspect.Inspector.MEMBER_DECLARER_IDX
 import static groovy.inspect.Inspector.MEMBER_EXCEPTIONS_IDX
 import static groovy.inspect.Inspector.MEMBER_MODIFIER_IDX
@@ -41,7 +42,7 @@ import static groovy.inspect.Inspector.MEMBER_PARAMS_IDX
 import static groovy.inspect.Inspector.MEMBER_TYPE_IDX
 import static groovy.inspect.Inspector.MEMBER_VALUE_IDX
 import static javax.swing.ListSelectionModel.SINGLE_SELECTION
-
+import static org.apache.groovy.parser.antlr4.GroovyLexer.VOCABULARY
 /**
  * A little GUI to show some of the Inspector capabilities.
  * Starting this script opens the ObjectBrowser on "some String".
@@ -130,15 +131,35 @@ class ObjectBrowser {
         builder.panel {
             borderLayout()
             panel(name: 'Class Info',
-                    border: emptyBorder([5, 10, 5, 10]),
+                    border: titledBorder('Class Info'),
                     constraints: NORTH) {
                 flowLayout(alignment: FlowLayout.LEFT)
-                def props = inspector.classProps.findAll{ it != 'implements ' }
+                def props = inspector.classProps.findAll { it != 'implements ' }
                 try {
                     def module = inspector.object.class.module.name
-                    if (module) props.add(0, 'module ' + module)
+                    if (module) props.add(0, "module ${module}")
                 } catch(Exception ignore) {}
-                def classLabel = '<html>' + props.join('<br>')
+
+                final implementsPrefix = 'implements '
+                def content = props.collect {
+                    String p = it.trim()
+                    if (p.startsWith(implementsPrefix)) {
+                        return "${implementsPrefix}${p.substring(implementsPrefix.length()).replace(' ', ', ')}"
+                    }
+                    if (p.startsWith('is ')) {
+                        return "(${p})"
+                    }
+                    return p
+                }.join('\n').trim()
+
+                for (def ln in (HIGHLIGHTED_TOKEN_TYPE_LIST.collect {
+                                    VOCABULARY.getLiteralName(it)?.replace("'", '')
+                                }.grep { it } + ['module', 'true', 'false'])) {
+                    content = content.replaceAll(/\b(${ln})\b/, '<b>$1</b>')
+                                        .replace(',', '<b>,</b>')
+                                        .replace(':', '<b>:</b>')
+                }
+                def classLabel = "<html>${content.replace('\n', '<br/>')}</html>"
                 label(classLabel)
             }
             tabbedPane(constraints: CENTER) {
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/text/SmartDocumentFilter.java b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/text/SmartDocumentFilter.java
index 26ef0dae0c..31a22c99f1 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/text/SmartDocumentFilter.java
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/text/SmartDocumentFilter.java
@@ -123,6 +123,12 @@ import static org.apache.groovy.parser.antlr4.GroovyLexer.YIELD;
  * @since 3.0.0
  */
 public class SmartDocumentFilter extends DocumentFilter {
+    public static final List<Integer> HIGHLIGHTED_TOKEN_TYPE_LIST = Arrays.asList(AS, DEF, IN, TRAIT, THREADSAFE,
+            VAR, BuiltInPrimitiveType, ABSTRACT, ASSERT, BREAK, CASE, CATCH, CLASS, CONST, CONTINUE, DEFAULT, DO,
+            ELSE, ENUM, EXTENDS, FINAL, FINALLY, FOR, IF, GOTO, IMPLEMENTS, IMPORT, INSTANCEOF, INTERFACE,
+            NATIVE, NEW, NON_SEALED, NOT_IN, NOT_INSTANCEOF, PACKAGE, PERMITS, PRIVATE, PROTECTED, PUBLIC,
+            RECORD, RETURN, SEALED, STATIC, STRICTFP, SUPER, SWITCH, SYNCHRONIZED,
+            THIS, THROW, THROWS, TRANSIENT, TRY, VOID, VOLATILE, WHILE, YIELD, NullLiteral, BooleanLiteral);
     private static final String MONOSPACED = "Monospaced";
     private final DefaultStyledDocument styledDocument;
     private final StyleContext styleContext;
@@ -381,12 +387,7 @@ public class SmartDocumentFilter extends DocumentFilter {
         }
 
         // reserved keywords, null literals, boolean literals
-        for (int t : Arrays.asList(AS, DEF, IN, TRAIT, THREADSAFE,
-                VAR, BuiltInPrimitiveType, ABSTRACT, ASSERT, BREAK, CASE, CATCH, CLASS, CONST, CONTINUE, DEFAULT, DO,
-                ELSE, ENUM, EXTENDS, FINAL, FINALLY, FOR, IF, GOTO, IMPLEMENTS, IMPORT, INSTANCEOF, INTERFACE,
-                NATIVE, NEW, NON_SEALED, NOT_IN, NOT_INSTANCEOF, PACKAGE, PERMITS, PRIVATE, PROTECTED, PUBLIC,
-                RECORD, RETURN, SEALED, STATIC, STRICTFP, SUPER, SWITCH, SYNCHRONIZED,
-                THIS, THROW, THROWS, TRANSIENT, TRY, VOID, VOLATILE, WHILE, YIELD, NullLiteral, BooleanLiteral)) {
+        for (int t : HIGHLIGHTED_TOKEN_TYPE_LIST) {
             Style style = createDefaultStyleByTokenType(t);
             StyleConstants.setBold(style, true);
             StyleConstants.setForeground(style, Color.BLUE.darker().darker());