@@ -38,6 +38,7 @@ public class SystemHighlighter extends DefaultHighlighter {
38
38
protected final SyntaxHighlighter langHighlighter ;
39
39
protected final SystemRegistry systemRegistry ;
40
40
protected final Map <String , FileHighlightCommand > fileHighlight = new HashMap <>();
41
+ private int commandIndex ;
41
42
42
43
public SystemHighlighter (SyntaxHighlighter commandHighlighter , SyntaxHighlighter argsHighlighter
43
44
, SyntaxHighlighter langHighlighter ) {
@@ -71,7 +72,9 @@ private boolean doDefaultHighlight(LineReader reader) {
71
72
protected AttributedString systemHighlight (LineReader reader , String buffer ) {
72
73
AttributedString out ;
73
74
Parser parser = reader .getParser ();
74
- String command = parser .getCommand (buffer );
75
+ ParsedLine pl = parser .parse (buffer , 0 , Parser .ParseContext .COMPLETE );
76
+ String command = pl .words ().size () > 0 ? parser .getCommand (pl .words ().get (0 )) : "" ;
77
+ commandIndex = buffer .indexOf (command );
75
78
if (buffer .trim ().isEmpty ()) {
76
79
out = new AttributedStringBuilder ().append (buffer ).toAttributedString ();
77
80
} else if (fileHighlight .containsKey (command )) {
@@ -81,29 +84,28 @@ protected AttributedString systemHighlight(LineReader reader, String buffer) {
81
84
} else {
82
85
out = doFileOptsHighlight (reader , buffer , fhc );
83
86
}
84
- } else if (systemRegistry .isCommandOrScript (command ) || systemRegistry .isCommandAlias (command )) {
87
+ } else if (systemRegistry .isCommandOrScript (command ) || systemRegistry .isCommandAlias (command ) || command . isEmpty () ) {
85
88
out = doCommandHighlight (buffer );
86
89
} else if (langHighlighter != null ) {
87
- out = langHighlighter .highlight (buffer );
90
+ out = langHighlighter .reset (). highlight (buffer );
88
91
} else {
89
92
out = new AttributedStringBuilder ().append (buffer ).toAttributedString ();
90
93
}
91
94
return out ;
92
95
}
93
96
94
97
protected AttributedString doFileOptsHighlight (LineReader reader , String buffer , FileHighlightCommand fhc ) {
95
- int idx0 = commandIndex (buffer );
96
98
AttributedStringBuilder asb = new AttributedStringBuilder ();
97
- if (idx0 < 0 ) {
99
+ if (commandIndex < 0 ) {
98
100
highlightCommand (buffer , asb );
99
101
} else {
100
- highlightCommand (buffer .substring (0 , idx0 ), asb );
102
+ highlightCommand (buffer .substring (0 , commandIndex ), asb );
101
103
ParsedLine parsedLine = reader .getParser ().parse (buffer , buffer .length () + 1 , Parser .ParseContext .COMPLETE );
102
104
List <String > words = parsedLine .words ();
103
105
if (!fhc .isSubcommand () || (words .size () > 2 && fhc .getSubcommand ().equals (words .get (1 )))) {
104
106
int firstArg = fhc .isSubcommand () ? 1 : 0 ;
105
107
int idx = buffer .indexOf (words .get (firstArg )) + words .get (firstArg ).length () + 1 ;
106
- highlightArgs (buffer .substring (idx0 , idx ), asb );
108
+ highlightArgs (buffer .substring (commandIndex , idx ), asb );
107
109
boolean fileOption = false ;
108
110
for (int i = firstArg + 1 ; i < words .size (); i ++) {
109
111
int nextIdx = buffer .substring (idx ).indexOf (words .get (i )) + idx ;
@@ -126,25 +128,24 @@ protected AttributedString doFileOptsHighlight(LineReader reader, String buffer,
126
128
idx = nextIdx + word .length ();
127
129
}
128
130
} else {
129
- highlightArgs (buffer .substring (idx0 ), asb );
131
+ highlightArgs (buffer .substring (commandIndex ), asb );
130
132
}
131
133
}
132
134
return asb .toAttributedString ();
133
135
}
134
136
135
137
protected AttributedString doFileArgsHighlight (LineReader reader , String buffer , FileHighlightCommand fhc ) {
136
- int idx0 = commandIndex (buffer );
137
138
AttributedStringBuilder asb = new AttributedStringBuilder ();
138
- if (idx0 < 0 ) {
139
+ if (commandIndex < 0 ) {
139
140
highlightCommand (buffer , asb );
140
141
} else {
141
- highlightCommand (buffer .substring (0 , idx0 ), asb );
142
+ highlightCommand (buffer .substring (0 , commandIndex ), asb );
142
143
ParsedLine parsedLine = reader .getParser ().parse (buffer , buffer .length () + 1 , Parser .ParseContext .COMPLETE );
143
144
List <String > words = parsedLine .words ();
144
145
if (!fhc .isSubcommand () || (words .size () > 2 && fhc .getSubcommand ().equals (words .get (1 )))) {
145
146
int firstArg = fhc .isSubcommand () ? 1 : 0 ;
146
147
int idx = buffer .indexOf (words .get (firstArg )) + words .get (firstArg ).length ();
147
- highlightArgs (buffer .substring (idx0 , idx ), asb );
148
+ highlightArgs (buffer .substring (commandIndex , idx ), asb );
148
149
for (int i = firstArg + 1 ; i < words .size (); i ++) {
149
150
int nextIdx = buffer .substring (idx ).indexOf (words .get (i )) + idx ;
150
151
for (int j = idx ; j < nextIdx ; j ++) {
@@ -154,7 +155,7 @@ protected AttributedString doFileArgsHighlight(LineReader reader, String buffer,
154
155
idx = nextIdx + words .get (i ).length ();
155
156
}
156
157
} else {
157
- highlightArgs (buffer .substring (idx0 ), asb );
158
+ highlightArgs (buffer .substring (commandIndex ), asb );
158
159
}
159
160
}
160
161
return asb .toAttributedString ();
@@ -163,13 +164,12 @@ protected AttributedString doFileArgsHighlight(LineReader reader, String buffer,
163
164
protected AttributedString doCommandHighlight (String buffer ) {
164
165
AttributedString out ;
165
166
if (commandHighlighter != null || argsHighlighter != null ) {
166
- int idx = commandIndex (buffer );
167
167
AttributedStringBuilder asb = new AttributedStringBuilder ();
168
- if (idx < 0 ) {
168
+ if (commandIndex < 0 ) {
169
169
highlightCommand (buffer , asb );
170
170
} else {
171
- highlightCommand (buffer .substring (0 , idx ), asb );
172
- highlightArgs (buffer .substring (idx ), asb );
171
+ highlightCommand (buffer .substring (0 , commandIndex ), asb );
172
+ highlightArgs (buffer .substring (commandIndex ), asb );
173
173
}
174
174
out = asb .toAttributedString ();
175
175
} else {
@@ -178,21 +178,6 @@ protected AttributedString doCommandHighlight(String buffer) {
178
178
return out ;
179
179
}
180
180
181
- private int commandIndex (String buffer ) {
182
- int idx = -1 ;
183
- boolean cmdFound = false ;
184
- for (int i = 0 ; i < buffer .length (); i ++) {
185
- char c = buffer .charAt (i );
186
- if (c != ' ' ) {
187
- cmdFound = true ;
188
- } else if (cmdFound ) {
189
- idx = i ;
190
- break ;
191
- }
192
- }
193
- return idx ;
194
- }
195
-
196
181
private void highlightFileArg (LineReader reader , String arg , AttributedStringBuilder asb ) {
197
182
if (arg .startsWith ("-" )) {
198
183
highlightArgs (arg , asb );
@@ -256,15 +241,15 @@ private void highlightFile(Path path, AttributedStringBuilder asb) {
256
241
257
242
private void highlightArgs (String args , AttributedStringBuilder asb ) {
258
243
if (argsHighlighter != null ) {
259
- asb .append (argsHighlighter .highlight (args ));
244
+ asb .append (argsHighlighter .reset (). highlight (args ));
260
245
} else {
261
246
asb .append (args );
262
247
}
263
248
}
264
249
265
250
private void highlightCommand (String command , AttributedStringBuilder asb ) {
266
251
if (commandHighlighter != null ) {
267
- asb .append (commandHighlighter .highlight (command ));
252
+ asb .append (commandHighlighter .reset (). highlight (command ));
268
253
} else {
269
254
asb .append (command );
270
255
}
0 commit comments