@@ -72,6 +72,7 @@ public enum Format {JSON, GROOVY, NONE}
72
72
public static final String SYNTHETIC_METHODS_COMPLETION = "syntheticMethodsCompletion" ;
73
73
74
74
private static final String VAR_GROOVY_OPTIONS = "GROOVY_OPTIONS" ;
75
+ private static final String DEFAULT_NANORC_SYNTAX = "classpath:/org/jline/groovy/java.nanorc" ;
75
76
private static final String REGEX_SYSTEM_VAR = "[A-Z]+[A-Z_]*" ;
76
77
private static final String REGEX_VAR = "[a-zA-Z_]+[a-zA-Z0-9_]*" ;
77
78
private static final Pattern PATTERN_FUNCTION_DEF = Pattern .compile (
@@ -100,6 +101,8 @@ public enum Format {JSON, GROOVY, NONE}
100
101
private final Map <String ,Class <?>> nameClass ;
101
102
private Cloner objectCloner = new ObjectCloner ();
102
103
protected final EngineClassLoader classLoader ;
104
+ private SyntaxHighlighter syntaxHighlighter ;
105
+ private String syntaxHighlighterStyle ;
103
106
104
107
public interface Cloner {
105
108
Object clone (Object obj );
@@ -530,6 +533,28 @@ protected static <T>T groovyOption(Map<String,Object> options, String option, T
530
533
return out ;
531
534
}
532
535
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
+
533
558
private Completer compileCompleter () {
534
559
List <Completer > completers = new ArrayList <>();
535
560
completers .add (new ArgumentCompleter (new StringsCompleter ("def" ), new StringsCompleter (methods ::keySet )
@@ -1320,7 +1345,6 @@ private static class Inspector {
1320
1345
static final Pattern PATTERN_FUNCTION = Pattern .compile ("\\ s*def\\ s+\\ w+\\ s*\\ ((.*?)\\ ).*" );
1321
1346
static final Pattern PATTERN_CLOSURE = Pattern .compile (".*\\ {(.*?)->.*" );
1322
1347
static final Pattern PATTERN_TYPE_VAR = Pattern .compile ("(\\ w+)\\ s+(\\ w+)" );
1323
- static final String DEFAULT_NANORC_SYNTAX = "classpath:/org/jline/groovy/java.nanorc" ;
1324
1348
static final String DEFAULT_GROOVY_COLORS = "ti=1;34:me=31" ;
1325
1349
1326
1350
private final GroovyShell shell ;
@@ -1336,21 +1360,20 @@ private static class Inspector {
1336
1360
private final AccessRules access ;
1337
1361
private String [] equationLines ;
1338
1362
private int cuttedSize ;
1339
- private final String nanorcSyntax ;
1340
1363
private final String groovyColors ;
1341
1364
private Object involvedObject = null ;
1342
- private final Path nanorc ;
1365
+ private final SyntaxHighlighter syntaxHighlighter ;
1343
1366
1344
1367
public Inspector (GroovyEngine groovyEngine ) {
1345
1368
this .imports = groovyEngine .imports ;
1346
1369
this .nameClass = groovyEngine .nameClass ;
1347
1370
this .canonicalNames = groovyEngine .groovyOption (CANONICAL_NAMES , false );
1348
- this .nanorcSyntax = groovyEngine .groovyOption (NANORC_SYNTAX , DEFAULT_NANORC_SYNTAX );
1349
1371
this .noSyntaxCheck = groovyEngine .groovyOption (NO_SYNTAX_CHECK , false );
1350
1372
this .restrictedCompletion = groovyEngine .groovyOption (RESTRICTED_COMPLETION , false );
1351
1373
this .metaMethodsCompletion = groovyEngine .groovyOption (META_METHODS_COMPLETION , false );
1352
1374
this .syntheticCompletion = groovyEngine .groovyOption (SYNTHETIC_METHODS_COMPLETION , false );
1353
1375
this .access = new AccessRules (groovyEngine .groovyOptions ());
1376
+ this .syntaxHighlighter = groovyEngine .getSyntaxHighlighter ();
1354
1377
String gc = groovyEngine .groovyOption (GROOVY_COLORS , null );
1355
1378
groovyColors = gc != null && Styles .isStylePattern (gc ) ? gc : DEFAULT_GROOVY_COLORS ;
1356
1379
groovyEngine .getObjectCloner ().markCache ();
@@ -1374,8 +1397,6 @@ public Inspector(GroovyEngine groovyEngine) {
1374
1397
sharedData .setVariable (entry .getKey (), execute ("{" + m .group (1 ) + "->" + m .group (2 ) + "}" ));
1375
1398
}
1376
1399
}
1377
- String nanorcString = (String )groovyEngine .get (VAR_NANORC );
1378
- nanorc = nanorcString != null ? Paths .get (nanorcString ) : null ;
1379
1400
}
1380
1401
1381
1402
public Object getInvolvedObject () {
@@ -1603,18 +1624,6 @@ private String accessModifier(int modifier, boolean all) {
1603
1624
return out ;
1604
1625
}
1605
1626
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
-
1618
1627
private CmdDesc methodDescription (CmdLine line ) {
1619
1628
CmdDesc out = new CmdDesc ();
1620
1629
List <String > args = line .getArgs ();
@@ -1647,8 +1656,7 @@ && new Brackets(args.get(args.size() - 1)).openRound()) {
1647
1656
}
1648
1657
List <AttributedString > mainDesc = new ArrayList <>();
1649
1658
if (clazz != null ) {
1650
- SyntaxHighlighter java = buildHighlighter (nanorcSyntax );
1651
- mainDesc .add (java .highlight (clazz .toString ()));
1659
+ mainDesc .add (syntaxHighlighter .highlight (clazz .toString ()));
1652
1660
if (constructor ) {
1653
1661
for (Constructor <?> m : access .allConstructors ? clazz .getDeclaredConstructors ()
1654
1662
: clazz .getConstructors ()) {
@@ -1680,7 +1688,7 @@ && new Brackets(args.get(args.size() - 1)).openRound()) {
1680
1688
sb .append (canonicalNames ? e .getCanonicalName () : e .getSimpleName ());
1681
1689
first = false ;
1682
1690
}
1683
- mainDesc .add (java .highlight (trimMethodDescription (sb )));
1691
+ mainDesc .add (syntaxHighlighter .highlight (trimMethodDescription (sb )));
1684
1692
}
1685
1693
} else {
1686
1694
List <String > addedMethods = new ArrayList <>();
@@ -1707,7 +1715,7 @@ && new Brackets(args.get(args.size() - 1)).openRound()) {
1707
1715
sb .append (")" );
1708
1716
if (!addedMethods .contains (sb .toString ())) {
1709
1717
addedMethods .add (sb .toString ());
1710
- mainDesc .add (java .highlight (trimMethodDescription (sb )));
1718
+ mainDesc .add (syntaxHighlighter .highlight (trimMethodDescription (sb )));
1711
1719
}
1712
1720
}
1713
1721
}
@@ -1748,7 +1756,7 @@ && new Brackets(args.get(args.size() - 1)).openRound()) {
1748
1756
}
1749
1757
if (!addedMethods .contains (sb .toString ())) {
1750
1758
addedMethods .add (sb .toString ());
1751
- mainDesc .add (java .highlight (trimMethodDescription (sb )));
1759
+ mainDesc .add (syntaxHighlighter .highlight (trimMethodDescription (sb )));
1752
1760
}
1753
1761
}
1754
1762
}
@@ -1855,10 +1863,9 @@ private CmdDesc checkSyntax(CmdLine line) {
1855
1863
1856
1864
private List <AttributedString > doExceptionMessage (Exception exception ) {
1857
1865
List <AttributedString > out = new ArrayList <>();
1858
- SyntaxHighlighter java = buildHighlighter (nanorcSyntax );
1859
1866
StyleResolver resolver = Styles .style (groovyColors );
1860
1867
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 ()));
1862
1869
if (exception .getMessage () != null ) {
1863
1870
for (String s : exception .getMessage ().split ("\\ r?\\ n" )) {
1864
1871
if (s .trim ().length () == 0 ) {
0 commit comments