@@ -72,6 +72,7 @@ public enum Format {JSON, GROOVY, NONE}
7272 public static final String SYNTHETIC_METHODS_COMPLETION = "syntheticMethodsCompletion" ;
7373
7474 private static final String VAR_GROOVY_OPTIONS = "GROOVY_OPTIONS" ;
75+ private static final String DEFAULT_NANORC_SYNTAX = "classpath:/org/jline/groovy/java.nanorc" ;
7576 private static final String REGEX_SYSTEM_VAR = "[A-Z]+[A-Z_]*" ;
7677 private static final String REGEX_VAR = "[a-zA-Z_]+[a-zA-Z0-9_]*" ;
7778 private static final Pattern PATTERN_FUNCTION_DEF = Pattern .compile (
@@ -100,6 +101,8 @@ public enum Format {JSON, GROOVY, NONE}
100101 private final Map <String ,Class <?>> nameClass ;
101102 private Cloner objectCloner = new ObjectCloner ();
102103 protected final EngineClassLoader classLoader ;
104+ private SyntaxHighlighter syntaxHighlighter ;
105+ private String syntaxHighlighterStyle ;
103106
104107 public interface Cloner {
105108 Object clone (Object obj );
@@ -530,6 +533,28 @@ protected static <T>T groovyOption(Map<String,Object> options, String option, T
530533 return out ;
531534 }
532535
536+ public boolean refresh () {
537+ syntaxHighlighter = null ;
538+ return true ;
539+ }
540+
541+ protected SyntaxHighlighter getSyntaxHighlighter () {
542+ String syntax = groovyOption (NANORC_SYNTAX , DEFAULT_NANORC_SYNTAX );
543+ if (syntaxHighlighter == null || syntax == null || !syntax .equals (syntaxHighlighterStyle )) {
544+ String nanorcString = (String ) get (VAR_NANORC );
545+ Path nanorc = nanorcString != null ? Paths .get (nanorcString ) : null ;
546+ if (syntax == null ) {
547+ syntaxHighlighter = SyntaxHighlighter .build ("" );
548+ } else if (syntax .contains (":" ) || nanorc == null ) {
549+ syntaxHighlighter = SyntaxHighlighter .build (syntax );
550+ } else {
551+ syntaxHighlighter = SyntaxHighlighter .build (nanorc , syntax );
552+ }
553+ syntaxHighlighterStyle = syntax ;
554+ }
555+ return syntaxHighlighter ;
556+ }
557+
533558 private Completer compileCompleter () {
534559 List <Completer > completers = new ArrayList <>();
535560 completers .add (new ArgumentCompleter (new StringsCompleter ("def" ), new StringsCompleter (methods ::keySet )
@@ -1320,7 +1345,6 @@ private static class Inspector {
13201345 static final Pattern PATTERN_FUNCTION = Pattern .compile ("\\ s*def\\ s+\\ w+\\ s*\\ ((.*?)\\ ).*" );
13211346 static final Pattern PATTERN_CLOSURE = Pattern .compile (".*\\ {(.*?)->.*" );
13221347 static final Pattern PATTERN_TYPE_VAR = Pattern .compile ("(\\ w+)\\ s+(\\ w+)" );
1323- static final String DEFAULT_NANORC_SYNTAX = "classpath:/org/jline/groovy/java.nanorc" ;
13241348 static final String DEFAULT_GROOVY_COLORS = "ti=1;34:me=31" ;
13251349
13261350 private final GroovyShell shell ;
@@ -1336,21 +1360,20 @@ private static class Inspector {
13361360 private final AccessRules access ;
13371361 private String [] equationLines ;
13381362 private int cuttedSize ;
1339- private final String nanorcSyntax ;
13401363 private final String groovyColors ;
13411364 private Object involvedObject = null ;
1342- private final Path nanorc ;
1365+ private final SyntaxHighlighter syntaxHighlighter ;
13431366
13441367 public Inspector (GroovyEngine groovyEngine ) {
13451368 this .imports = groovyEngine .imports ;
13461369 this .nameClass = groovyEngine .nameClass ;
13471370 this .canonicalNames = groovyEngine .groovyOption (CANONICAL_NAMES , false );
1348- this .nanorcSyntax = groovyEngine .groovyOption (NANORC_SYNTAX , DEFAULT_NANORC_SYNTAX );
13491371 this .noSyntaxCheck = groovyEngine .groovyOption (NO_SYNTAX_CHECK , false );
13501372 this .restrictedCompletion = groovyEngine .groovyOption (RESTRICTED_COMPLETION , false );
13511373 this .metaMethodsCompletion = groovyEngine .groovyOption (META_METHODS_COMPLETION , false );
13521374 this .syntheticCompletion = groovyEngine .groovyOption (SYNTHETIC_METHODS_COMPLETION , false );
13531375 this .access = new AccessRules (groovyEngine .groovyOptions ());
1376+ this .syntaxHighlighter = groovyEngine .getSyntaxHighlighter ();
13541377 String gc = groovyEngine .groovyOption (GROOVY_COLORS , null );
13551378 groovyColors = gc != null && Styles .isStylePattern (gc ) ? gc : DEFAULT_GROOVY_COLORS ;
13561379 groovyEngine .getObjectCloner ().markCache ();
@@ -1374,8 +1397,6 @@ public Inspector(GroovyEngine groovyEngine) {
13741397 sharedData .setVariable (entry .getKey (), execute ("{" + m .group (1 ) + "->" + m .group (2 ) + "}" ));
13751398 }
13761399 }
1377- String nanorcString = (String )groovyEngine .get (VAR_NANORC );
1378- nanorc = nanorcString != null ? Paths .get (nanorcString ) : null ;
13791400 }
13801401
13811402 public Object getInvolvedObject () {
@@ -1603,18 +1624,6 @@ private String accessModifier(int modifier, boolean all) {
16031624 return out ;
16041625 }
16051626
1606- private SyntaxHighlighter buildHighlighter (String syntax ) {
1607- SyntaxHighlighter out ;
1608- if (syntax == null ) {
1609- out = SyntaxHighlighter .build ("" );
1610- } else if (syntax .contains (":" ) || nanorc == null ) {
1611- out = SyntaxHighlighter .build (syntax );
1612- } else {
1613- out = SyntaxHighlighter .build (nanorc , syntax );
1614- }
1615- return out ;
1616- }
1617-
16181627 private CmdDesc methodDescription (CmdLine line ) {
16191628 CmdDesc out = new CmdDesc ();
16201629 List <String > args = line .getArgs ();
@@ -1647,8 +1656,7 @@ && new Brackets(args.get(args.size() - 1)).openRound()) {
16471656 }
16481657 List <AttributedString > mainDesc = new ArrayList <>();
16491658 if (clazz != null ) {
1650- SyntaxHighlighter java = buildHighlighter (nanorcSyntax );
1651- mainDesc .add (java .highlight (clazz .toString ()));
1659+ mainDesc .add (syntaxHighlighter .highlight (clazz .toString ()));
16521660 if (constructor ) {
16531661 for (Constructor <?> m : access .allConstructors ? clazz .getDeclaredConstructors ()
16541662 : clazz .getConstructors ()) {
@@ -1680,7 +1688,7 @@ && new Brackets(args.get(args.size() - 1)).openRound()) {
16801688 sb .append (canonicalNames ? e .getCanonicalName () : e .getSimpleName ());
16811689 first = false ;
16821690 }
1683- mainDesc .add (java .highlight (trimMethodDescription (sb )));
1691+ mainDesc .add (syntaxHighlighter .highlight (trimMethodDescription (sb )));
16841692 }
16851693 } else {
16861694 List <String > addedMethods = new ArrayList <>();
@@ -1707,7 +1715,7 @@ && new Brackets(args.get(args.size() - 1)).openRound()) {
17071715 sb .append (")" );
17081716 if (!addedMethods .contains (sb .toString ())) {
17091717 addedMethods .add (sb .toString ());
1710- mainDesc .add (java .highlight (trimMethodDescription (sb )));
1718+ mainDesc .add (syntaxHighlighter .highlight (trimMethodDescription (sb )));
17111719 }
17121720 }
17131721 }
@@ -1748,7 +1756,7 @@ && new Brackets(args.get(args.size() - 1)).openRound()) {
17481756 }
17491757 if (!addedMethods .contains (sb .toString ())) {
17501758 addedMethods .add (sb .toString ());
1751- mainDesc .add (java .highlight (trimMethodDescription (sb )));
1759+ mainDesc .add (syntaxHighlighter .highlight (trimMethodDescription (sb )));
17521760 }
17531761 }
17541762 }
@@ -1855,10 +1863,9 @@ private CmdDesc checkSyntax(CmdLine line) {
18551863
18561864 private List <AttributedString > doExceptionMessage (Exception exception ) {
18571865 List <AttributedString > out = new ArrayList <>();
1858- SyntaxHighlighter java = buildHighlighter (nanorcSyntax );
18591866 StyleResolver resolver = Styles .style (groovyColors );
18601867 Pattern header = Pattern .compile ("^[a-zA-Z() ]{3,}:(\\ s+|$)" );
1861- out .add (java .highlight (exception .getClass ().getCanonicalName ()));
1868+ out .add (syntaxHighlighter .highlight (exception .getClass ().getCanonicalName ()));
18621869 if (exception .getMessage () != null ) {
18631870 for (String s : exception .getMessage ().split ("\\ r?\\ n" )) {
18641871 if (s .trim ().length () == 0 ) {
0 commit comments