|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.model; |
|
38 |
| |
|
39 |
| import edu.rice.cs.drjava.DrJava; |
|
40 |
| import edu.rice.cs.drjava.config.OptionConstants; |
|
41 |
| import edu.rice.cs.drjava.config.OptionEvent; |
|
42 |
| import edu.rice.cs.drjava.config.OptionListener; |
|
43 |
| |
|
44 |
| import edu.rice.cs.drjava.model.definitions.indent.Indenter; |
|
45 |
| import edu.rice.cs.drjava.model.definitions.reducedmodel.BraceInfo; |
|
46 |
| import edu.rice.cs.drjava.model.definitions.reducedmodel.ReducedModelControl; |
|
47 |
| import edu.rice.cs.drjava.model.definitions.reducedmodel.HighlightStatus; |
|
48 |
| import edu.rice.cs.drjava.model.definitions.reducedmodel.ReducedModelState; |
|
49 |
| |
|
50 |
| import edu.rice.cs.util.OperationCanceledException; |
|
51 |
| import edu.rice.cs.util.StringOps; |
|
52 |
| import edu.rice.cs.util.UnexpectedException; |
|
53 |
| import edu.rice.cs.util.swing.Utilities; |
|
54 |
| import edu.rice.cs.util.text.SwingDocument; |
|
55 |
| |
|
56 |
| import java.awt.EventQueue; |
|
57 |
| import java.util.ArrayList; |
|
58 |
| import java.util.Set; |
|
59 |
| import java.util.HashMap; |
|
60 |
| import java.util.HashSet; |
|
61 |
| import java.util.LinkedList; |
|
62 |
| import java.util.List; |
|
63 |
| import java.util.SortedMap; |
|
64 |
| import java.util.StringTokenizer; |
|
65 |
| import java.util.TreeMap; |
|
66 |
| import javax.swing.ProgressMonitor; |
|
67 |
| import javax.swing.text.AbstractDocument; |
|
68 |
| import javax.swing.text.AttributeSet; |
|
69 |
| import javax.swing.text.BadLocationException; |
|
70 |
| import javax.swing.text.Position; |
|
71 |
| |
|
72 |
| import static edu.rice.cs.drjava.model.definitions.reducedmodel.ReducedModelStates.*; |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| public abstract class AbstractDJDocument extends SwingDocument implements DJDocument, OptionConstants { |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| protected static final String delimiters = " \t\n\r{}()[].+-/*;:=!@#$%^&*~<>?,\"`'<>|"; |
|
92 |
| protected static final char newline = '\n'; |
|
93 |
| |
|
94 |
| protected static final HashSet<String> _normEndings = _makeNormEndings(); |
|
95 |
| |
|
96 |
| protected final HashSet<String> _keywords = new HashSet<String> |
|
97 |
| (edu.rice.cs.drjava.model.compiler.JavacCompiler.JAVA_KEYWORDS); |
|
98 |
| |
|
99 |
| protected static final HashSet<String> _primTypes = _makePrimTypes(); |
|
100 |
| |
|
101 |
| protected volatile int _indent = 2; |
|
102 |
| |
|
103 |
| private static final int INIT_CACHE_SIZE = 0x10000; |
|
104 |
| |
|
105 |
| public static final int POS_THRESHOLD = 10000; |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| public static final char[] CLOSING_BRACES = new char[] {'}', ')'}; |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| public final ReducedModelControl _reduced = new ReducedModelControl(); |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| protected volatile int _currentLocation = 0; |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| private volatile HashMap<Query, Object> _queryCache; |
|
132 |
| |
|
133 |
| |
|
134 |
| private volatile SortedMap<Integer, List<Query>> _offsetToQueries; |
|
135 |
| |
|
136 |
| |
|
137 |
| private volatile Indenter _indenter; |
|
138 |
| |
|
139 |
| |
|
140 |
| private volatile OptionListener<Integer> _listener1; |
|
141 |
| private volatile OptionListener<Boolean> _listener2; |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
759
| protected AbstractDJDocument() {
|
|
147 |
759
| this(new Indenter(DrJava.getConfig().getSetting(INDENT_LEVEL).intValue()));
|
|
148 |
| } |
|
149 |
| |
|
150 |
| |
|
151 |
143
| protected AbstractDJDocument(int indentLevel) {
|
|
152 |
143
| this(new Indenter(indentLevel));
|
|
153 |
| } |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
1351
| protected AbstractDJDocument(Indenter indenter) {
|
|
158 |
1351
| _indenter = indenter;
|
|
159 |
1351
| _queryCache = null;
|
|
160 |
1351
| _offsetToQueries = null;
|
|
161 |
1351
| _initNewIndenter();
|
|
162 |
| |
|
163 |
| |
|
164 |
| } |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
236
| private Indenter getIndenter() { return _indenter; }
|
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
0
| public int getIndent() { return _indent; }
|
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
11
| public void setIndent(final int indent) {
|
|
182 |
11
| DrJava.getConfig().setSetting(INDENT_LEVEL, indent);
|
|
183 |
11
| this._indent = indent;
|
|
184 |
| } |
|
185 |
| |
|
186 |
117
| protected void _removeIndenter() {
|
|
187 |
| |
|
188 |
117
| DrJava.getConfig().removeOptionListener(INDENT_LEVEL, _listener1);
|
|
189 |
117
| DrJava.getConfig().removeOptionListener(AUTO_CLOSE_COMMENTS, _listener2);
|
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
1351
| private void _initNewIndenter() {
|
|
194 |
| |
|
195 |
| |
|
196 |
1351
| final Indenter indenter = _indenter;
|
|
197 |
| |
|
198 |
1351
| _listener1 = new OptionListener<Integer>() {
|
|
199 |
124
| public void optionChanged(OptionEvent<Integer> oce) {
|
|
200 |
| |
|
201 |
124
| indenter.buildTree(oce.value);
|
|
202 |
| } |
|
203 |
| }; |
|
204 |
| |
|
205 |
1351
| _listener2 = new OptionListener<Boolean>() {
|
|
206 |
0
| public void optionChanged(OptionEvent<Boolean> oce) {
|
|
207 |
| |
|
208 |
0
| indenter.buildTree(DrJava.getConfig().getSetting(INDENT_LEVEL));
|
|
209 |
| } |
|
210 |
| }; |
|
211 |
| |
|
212 |
1351
| DrJava.getConfig().addOptionListener(INDENT_LEVEL, _listener1);
|
|
213 |
1351
| DrJava.getConfig().addOptionListener(AUTO_CLOSE_COMMENTS, _listener2);
|
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
56
| protected static HashSet<String> _makeNormEndings() {
|
|
221 |
56
| HashSet<String> normEndings = new HashSet<String>();
|
|
222 |
56
| normEndings.add(";");
|
|
223 |
56
| normEndings.add("{");
|
|
224 |
56
| normEndings.add("}");
|
|
225 |
56
| normEndings.add("(");
|
|
226 |
56
| return normEndings;
|
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
361
| public void setKeywords(Set<String> keywords) {
|
|
232 |
361
| _keywords.clear();
|
|
233 |
361
| _keywords.addAll(keywords);
|
|
234 |
| } |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
56
| protected static HashSet<String> _makePrimTypes() {
|
|
240 |
56
| final String[] words = {
|
|
241 |
| "boolean", "char", "byte", "short", "int", "long", "float", "double", "void", |
|
242 |
| }; |
|
243 |
56
| HashSet<String> prims = new HashSet<String>();
|
|
244 |
504
| for (String w: words) { prims.add(w); }
|
|
245 |
56
| return prims;
|
|
246 |
| } |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
0
| public ArrayList<HighlightStatus> getHighlightStatus(int start, int end) {
|
|
255 |
| |
|
256 |
0
| assert EventQueue.isDispatchThread();
|
|
257 |
| |
|
258 |
0
| if (start == end) return new ArrayList<HighlightStatus>(0);
|
|
259 |
0
| ArrayList<HighlightStatus> v;
|
|
260 |
| |
|
261 |
0
| setCurrentLocation(start);
|
|
262 |
| |
|
263 |
0
| v = _reduced.getHighlightStatus(start, end - start);
|
|
264 |
| |
|
265 |
| |
|
266 |
0
| for (int i = 0; i < v.size(); i++) {
|
|
267 |
0
| HighlightStatus stat = v.get(i);
|
|
268 |
0
| if (stat.getState() == HighlightStatus.NORMAL) i = _highlightKeywords(v, i);
|
|
269 |
| } |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
0
| return v;
|
|
277 |
| } |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
0
| private int _highlightKeywords(ArrayList<HighlightStatus> v, int i) {
|
|
288 |
| |
|
289 |
0
| final HighlightStatus original = v.get(i);
|
|
290 |
0
| final String text;
|
|
291 |
| |
|
292 |
| |
|
293 |
0
| try { text = getText(original.getLocation(), original.getLength()); }
|
|
294 |
0
| catch (BadLocationException e) { throw new UnexpectedException(e); }
|
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
0
| StringTokenizer tokenizer = new StringTokenizer(text, delimiters, true);
|
|
299 |
| |
|
300 |
| |
|
301 |
0
| int start = original.getLocation();
|
|
302 |
0
| int length = 0;
|
|
303 |
| |
|
304 |
| |
|
305 |
0
| v.remove(i);
|
|
306 |
| |
|
307 |
| |
|
308 |
0
| int index = i;
|
|
309 |
| |
|
310 |
0
| boolean process;
|
|
311 |
0
| int state = 0;
|
|
312 |
0
| while (tokenizer.hasMoreTokens()) {
|
|
313 |
0
| String token = tokenizer.nextToken();
|
|
314 |
| |
|
315 |
| |
|
316 |
0
| process = false;
|
|
317 |
0
| if (_isType(token)) {
|
|
318 |
| |
|
319 |
0
| state = HighlightStatus.TYPE;
|
|
320 |
0
| process = true;
|
|
321 |
| } |
|
322 |
0
| else if (_keywords.contains(token)) {
|
|
323 |
0
| state = HighlightStatus.KEYWORD;
|
|
324 |
0
| process = true;
|
|
325 |
| } |
|
326 |
0
| else if (_isNum(token)) {
|
|
327 |
0
| state = HighlightStatus.NUMBER;
|
|
328 |
0
| process = true;
|
|
329 |
| } |
|
330 |
| |
|
331 |
0
| if (process) {
|
|
332 |
| |
|
333 |
0
| if (length != 0) {
|
|
334 |
0
| HighlightStatus newStat = new HighlightStatus(start, length, original.getState());
|
|
335 |
0
| v.add(index, newStat);
|
|
336 |
0
| index++;
|
|
337 |
0
| start += length;
|
|
338 |
0
| length = 0;
|
|
339 |
| } |
|
340 |
| |
|
341 |
| |
|
342 |
0
| int keywordLength = token.length();
|
|
343 |
0
| v.add(index, new HighlightStatus(start, keywordLength, state));
|
|
344 |
0
| index++;
|
|
345 |
| |
|
346 |
0
| start += keywordLength;
|
|
347 |
| } |
|
348 |
| else { |
|
349 |
| |
|
350 |
0
| length += token.length();
|
|
351 |
| } |
|
352 |
| } |
|
353 |
| |
|
354 |
0
| if (length != 0) {
|
|
355 |
0
| HighlightStatus newStat = new HighlightStatus(start, length, original.getState());
|
|
356 |
0
| v.add(index, newStat);
|
|
357 |
0
| index++;
|
|
358 |
0
| length = 0;
|
|
359 |
| } |
|
360 |
| |
|
361 |
0
| return index - 1;
|
|
362 |
| } |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
106
| static boolean _isNum(String x) {
|
|
368 |
106
| try {
|
|
369 |
106
| Double.parseDouble(x);
|
|
370 |
46
| return true;
|
|
371 |
| } |
|
372 |
| catch (NumberFormatException e) { |
|
373 |
60
| int radix = 10;
|
|
374 |
60
| int begin = 0;
|
|
375 |
60
| int end = x.length();
|
|
376 |
60
| int bits = 32;
|
|
377 |
60
| if (end-begin>1) {
|
|
378 |
| |
|
379 |
60
| char ch = x.charAt(end-1);
|
|
380 |
60
| if ((ch=='l')||(ch=='L')) {
|
|
381 |
44
| --end;
|
|
382 |
44
| bits = 64;
|
|
383 |
| } |
|
384 |
60
| if (end-begin>1) {
|
|
385 |
| |
|
386 |
56
| if (x.charAt(0) == '0') {
|
|
387 |
54
| ++begin;
|
|
388 |
54
| radix = 8;
|
|
389 |
54
| if (end-begin>1) {
|
|
390 |
| |
|
391 |
50
| ch = x.charAt(1);
|
|
392 |
50
| if ((ch=='x')||(ch=='X')) {
|
|
393 |
48
| ++begin;
|
|
394 |
48
| radix = 16;
|
|
395 |
| } |
|
396 |
| } |
|
397 |
| } |
|
398 |
| } |
|
399 |
| } |
|
400 |
60
| try {
|
|
401 |
| |
|
402 |
60
| java.math.BigInteger val = new java.math.BigInteger(x.substring(begin, end), radix);
|
|
403 |
54
| return (val.bitLength() <= bits);
|
|
404 |
| } |
|
405 |
| catch (NumberFormatException e2) { |
|
406 |
6
| return false;
|
|
407 |
| } |
|
408 |
| } |
|
409 |
| } |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
0
| private boolean _isType(String x) {
|
|
415 |
0
| if (_primTypes.contains(x)) return true;
|
|
416 |
| |
|
417 |
0
| try { return Character.isUpperCase(x.charAt(0)); }
|
|
418 |
0
| catch (IndexOutOfBoundsException e) { return false; }
|
|
419 |
| } |
|
420 |
| |
|
421 |
| |
|
422 |
35
| public static boolean hasOnlySpaces(String text) { return (text.trim().length() == 0); }
|
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
| protected abstract void _styleChanged(); |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
61964
| private void _addCharToReducedModel(char curChar) {
|
|
445 |
| |
|
446 |
61964
| _reduced.insertChar(curChar);
|
|
447 |
| } |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
2866
| public int getCurrentLocation() { return _currentLocation; }
|
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
413262
| public void setCurrentLocation(int loc) {
|
|
460 |
413262
| if (loc < 0) {
|
|
461 |
0
| throw new UnexpectedException("Illegal location " + loc);
|
|
462 |
| } |
|
463 |
413262
| if (loc > getLength()) {
|
|
464 |
0
| throw new UnexpectedException("Illegal location " + loc);
|
|
465 |
| } |
|
466 |
413262
| int dist = loc - _currentLocation;
|
|
467 |
413262
| _currentLocation = loc;
|
|
468 |
413262
| _reduced.move(dist);
|
|
469 |
| |
|
470 |
| } |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
504
| public void move(int dist) {
|
|
478 |
504
| int newLocation = _currentLocation + dist;
|
|
479 |
504
| if (0 <= newLocation && newLocation <= getLength()) {
|
|
480 |
504
| _reduced.move(dist);
|
|
481 |
504
| _currentLocation = newLocation;
|
|
482 |
| } |
|
483 |
0
| else throw new IllegalArgumentException("AbstractDJDocument.move(" + dist + ") places the cursor at " +
|
|
484 |
| newLocation + " which is out of range"); |
|
485 |
| } |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
| |
|
490 |
| |
|
491 |
84
| public int balanceBackward() {
|
|
492 |
84
| int origPos = _currentLocation;
|
|
493 |
84
| try {
|
|
494 |
0
| if (_currentLocation < 2) return -1;
|
|
495 |
84
| char prevChar = _getText(_currentLocation - 1, 1).charAt(0);
|
|
496 |
| |
|
497 |
0
| if (prevChar != '}' && prevChar != ')' && prevChar != ']') return -1;
|
|
498 |
84
| return _reduced.balanceBackward();
|
|
499 |
| } |
|
500 |
84
| finally { setCurrentLocation(origPos); }
|
|
501 |
| } |
|
502 |
| |
|
503 |
| |
|
504 |
| |
|
505 |
| |
|
506 |
| |
|
507 |
3972
| public int balanceForward() {
|
|
508 |
3972
| int origPos = _currentLocation;
|
|
509 |
3972
| try {
|
|
510 |
0
| if (_currentLocation == 0) return -1;
|
|
511 |
3972
| char prevChar = _getText(_currentLocation - 1, 1).charAt(0);
|
|
512 |
| |
|
513 |
0
| if (prevChar != '{' && prevChar != '(' && prevChar != '[') return -1;
|
|
514 |
| |
|
515 |
3972
| return _reduced.balanceForward() ;
|
|
516 |
| } |
|
517 |
3972
| finally { setCurrentLocation(origPos); }
|
|
518 |
| } |
|
519 |
| |
|
520 |
| |
|
521 |
| |
|
522 |
| |
|
523 |
12
| public ReducedModelControl getReduced() { return _reduced; }
|
|
524 |
| |
|
525 |
| |
|
526 |
273
| public ReducedModelState stateAtRelLocation(int dist) { return _reduced.moveWalkerGetState(dist); }
|
|
527 |
| |
|
528 |
| |
|
529 |
646
| public ReducedModelState getStateAtCurrent() {
|
|
530 |
646
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
531 |
646
| return _reduced.getStateAtCurrent();
|
|
532 |
| } |
|
533 |
| |
|
534 |
| |
|
535 |
24
| public void resetReducedModelLocation() { _reduced.resetLocation(); }
|
|
536 |
| |
|
537 |
| |
|
538 |
| |
|
539 |
| |
|
540 |
| |
|
541 |
| |
|
542 |
| |
|
543 |
| |
|
544 |
| |
|
545 |
12210
| public int findPrevEnclosingBrace(final int pos, final char opening, final char closing) throws BadLocationException {
|
|
546 |
| |
|
547 |
| |
|
548 |
| |
|
549 |
12210
| final Query key = new Query.PrevEnclosingBrace(pos, opening, closing);
|
|
550 |
12210
| final Integer cached = (Integer) _checkCache(key);
|
|
551 |
0
| if (cached != null) return cached.intValue();
|
|
552 |
| |
|
553 |
2
| if (pos >= getLength() || pos <= 0) { return -1; }
|
|
554 |
| |
|
555 |
12208
| final char[] delims = {opening, closing};
|
|
556 |
12208
| int reducedPos = pos;
|
|
557 |
12208
| int i;
|
|
558 |
12208
| int braceBalance = 0;
|
|
559 |
| |
|
560 |
12208
| String text = getText(0, pos);
|
|
561 |
| |
|
562 |
12208
| final int origPos = _currentLocation;
|
|
563 |
| |
|
564 |
12208
| setCurrentLocation(pos);
|
|
565 |
| |
|
566 |
| |
|
567 |
12208
| for (i = pos - 1; i >= 0; i--) {
|
|
568 |
| |
|
569 |
| |
|
570 |
| |
|
571 |
3253595
| if (match(text.charAt(i), delims)) {
|
|
572 |
| |
|
573 |
79941
| setCurrentLocation(i);
|
|
574 |
79941
| reducedPos = i;
|
|
575 |
| |
|
576 |
| |
|
577 |
| |
|
578 |
24
| if (isShadowed()) continue;
|
|
579 |
| else { |
|
580 |
| |
|
581 |
34899
| if (text.charAt(i) == closing) ++braceBalance;
|
|
582 |
| else { |
|
583 |
10119
| if (braceBalance == 0) break;
|
|
584 |
34899
| --braceBalance;
|
|
585 |
| } |
|
586 |
| } |
|
587 |
| } |
|
588 |
| } |
|
589 |
| |
|
590 |
| |
|
591 |
| |
|
592 |
12208
| setCurrentLocation(origPos);
|
|
593 |
| |
|
594 |
2089
| if (i == -1) reducedPos = -1;
|
|
595 |
12208
| _storeInCache(key, reducedPos, pos - 1);
|
|
596 |
| |
|
597 |
| |
|
598 |
12208
| return reducedPos;
|
|
599 |
| } |
|
600 |
| |
|
601 |
| |
|
602 |
181721
| public boolean isShadowed() { return _reduced.isShadowed(); }
|
|
603 |
| |
|
604 |
| |
|
605 |
70
| public boolean isShadowed(int pos) {
|
|
606 |
70
| int origPos = _currentLocation;
|
|
607 |
70
| setCurrentLocation(pos);
|
|
608 |
70
| boolean result = isShadowed();
|
|
609 |
70
| setCurrentLocation(origPos);
|
|
610 |
70
| return result;
|
|
611 |
| } |
|
612 |
| |
|
613 |
| |
|
614 |
| |
|
615 |
| |
|
616 |
| |
|
617 |
| |
|
618 |
| |
|
619 |
| |
|
620 |
3958
| public int findNextEnclosingBrace(final int pos, final char opening, final char closing) throws BadLocationException {
|
|
621 |
3958
| assert EventQueue.isDispatchThread();
|
|
622 |
| |
|
623 |
| |
|
624 |
3958
| final Query key = new Query.NextEnclosingBrace(pos, opening, closing);
|
|
625 |
3958
| final Integer cached = (Integer) _checkCache(key);
|
|
626 |
| |
|
627 |
0
| if (cached != null) return cached.intValue();
|
|
628 |
0
| if (pos >= getLength() - 1) { return -1; }
|
|
629 |
| |
|
630 |
3958
| final char[] delims = {opening, closing};
|
|
631 |
3958
| int reducedPos = pos;
|
|
632 |
3958
| int i;
|
|
633 |
3958
| int braceBalance = 0;
|
|
634 |
| |
|
635 |
3958
| String text = getText();
|
|
636 |
| |
|
637 |
3958
| final int origPos = _currentLocation;
|
|
638 |
| |
|
639 |
3958
| setCurrentLocation(pos);
|
|
640 |
| |
|
641 |
| |
|
642 |
76966
| for (i = pos + 1; i < text.length(); i++) {
|
|
643 |
| |
|
644 |
| |
|
645 |
| |
|
646 |
76966
| if (match(text.charAt(i),delims)) {
|
|
647 |
| |
|
648 |
7244
| setCurrentLocation(i);
|
|
649 |
7244
| reducedPos = i;
|
|
650 |
| |
|
651 |
| |
|
652 |
544
| if (isShadowed()) continue;
|
|
653 |
| else { |
|
654 |
| |
|
655 |
1371
| if (text.charAt(i) == opening) ++braceBalance;
|
|
656 |
| else { |
|
657 |
3958
| if (braceBalance == 0) break;
|
|
658 |
1371
| --braceBalance;
|
|
659 |
| } |
|
660 |
| } |
|
661 |
| } |
|
662 |
| } |
|
663 |
| |
|
664 |
| |
|
665 |
| |
|
666 |
3958
| setCurrentLocation(origPos);
|
|
667 |
| |
|
668 |
0
| if (i == text.length()) reducedPos = -1;
|
|
669 |
3958
| _storeInCache(key, reducedPos, reducedPos);
|
|
670 |
| |
|
671 |
3958
| return reducedPos;
|
|
672 |
| } |
|
673 |
| |
|
674 |
| |
|
675 |
| |
|
676 |
| |
|
677 |
| |
|
678 |
| |
|
679 |
| |
|
680 |
| |
|
681 |
| |
|
682 |
6529
| public int findPrevDelimiter(int pos, char[] delims) throws BadLocationException {
|
|
683 |
6529
| return findPrevDelimiter(pos, delims, true);
|
|
684 |
| } |
|
685 |
| |
|
686 |
| |
|
687 |
| |
|
688 |
| |
|
689 |
| |
|
690 |
| |
|
691 |
| |
|
692 |
| |
|
693 |
| |
|
694 |
13299
| public int findPrevDelimiter(final int pos, final char[] delims, final boolean skipBracePhrases)
|
|
695 |
| throws BadLocationException { |
|
696 |
| |
|
697 |
13299
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
698 |
| |
|
699 |
| |
|
700 |
13299
| final Query key = new Query.PrevDelimiter(pos, delims, skipBracePhrases);
|
|
701 |
13299
| final Integer cached = (Integer) _checkCache(key);
|
|
702 |
13299
| if (cached != null) {
|
|
703 |
| |
|
704 |
152
| return cached.intValue();
|
|
705 |
| } |
|
706 |
| |
|
707 |
13147
| int reducedPos = pos;
|
|
708 |
13147
| int i;
|
|
709 |
13147
| int lineStartPos = _getLineStartPos(pos);
|
|
710 |
0
| if (lineStartPos < 0) lineStartPos = 0;
|
|
711 |
| |
|
712 |
2470
| if (lineStartPos >= pos) i = lineStartPos - 1;
|
|
713 |
| else { |
|
714 |
10677
| assert lineStartPos < pos;
|
|
715 |
10677
| String line = getText(lineStartPos, pos - lineStartPos);
|
|
716 |
10677
| final int origPos = _currentLocation;
|
|
717 |
| |
|
718 |
| |
|
719 |
10677
| for (i = pos - 1; i >= lineStartPos; i--) {
|
|
720 |
| |
|
721 |
| |
|
722 |
| |
|
723 |
94132
| int irel = i - lineStartPos;
|
|
724 |
94132
| setCurrentLocation(i);
|
|
725 |
94132
| if (isShadowed() || isCommentOpen(line, irel)) {
|
|
726 |
| |
|
727 |
1104
| continue;
|
|
728 |
| } |
|
729 |
93028
| char ch = line.charAt(irel);
|
|
730 |
| |
|
731 |
93028
| if (match(ch, delims) ) {
|
|
732 |
6493
| reducedPos = i;
|
|
733 |
6493
| break;
|
|
734 |
| } |
|
735 |
| |
|
736 |
86535
| if (skipBracePhrases && match(ch, CLOSING_BRACES) ) {
|
|
737 |
| |
|
738 |
24
| setCurrentLocation(i + 1);
|
|
739 |
| |
|
740 |
24
| int dist = balanceBackward();
|
|
741 |
24
| if (dist == -1) {
|
|
742 |
0
| i = -1;
|
|
743 |
| |
|
744 |
0
| break;
|
|
745 |
| } |
|
746 |
24
| assert dist > 0;
|
|
747 |
| |
|
748 |
| |
|
749 |
24
| setCurrentLocation(i + 1 - dist);
|
|
750 |
24
| i = _currentLocation;
|
|
751 |
| |
|
752 |
24
| continue;
|
|
753 |
| } |
|
754 |
| } |
|
755 |
| |
|
756 |
10677
| setCurrentLocation(origPos);
|
|
757 |
| } |
|
758 |
| |
|
759 |
| |
|
760 |
| |
|
761 |
13147
| if (i < lineStartPos) {
|
|
762 |
154
| if (i <= 0) reducedPos = -1;
|
|
763 |
6500
| else reducedPos = findPrevDelimiter(i, delims, skipBracePhrases);
|
|
764 |
| } |
|
765 |
| |
|
766 |
13147
| _storeInCache(key, reducedPos, pos - 1);
|
|
767 |
| |
|
768 |
| |
|
769 |
| |
|
770 |
13147
| return reducedPos;
|
|
771 |
| } |
|
772 |
| |
|
773 |
3565655
| private static boolean match(char c, char[] delims) {
|
|
774 |
115718
| for (char d : delims) { if (c == d) return true; }
|
|
775 |
3449937
| return false;
|
|
776 |
| } |
|
777 |
| |
|
778 |
| |
|
779 |
| |
|
780 |
| |
|
781 |
73
| public boolean findCharInStmtBeforePos(char findChar, int position) {
|
|
782 |
| |
|
783 |
73
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
784 |
| |
|
785 |
73
| if (position == -1) {
|
|
786 |
0
| String msg =
|
|
787 |
| "Argument endChar to QuestionExistsCharInStmt must be a char that exists on the current line."; |
|
788 |
0
| throw new UnexpectedException(new IllegalArgumentException(msg));
|
|
789 |
| } |
|
790 |
| |
|
791 |
73
| char[] findCharDelims = {findChar, ';', '{', '}'};
|
|
792 |
73
| int prevFindChar;
|
|
793 |
| |
|
794 |
| |
|
795 |
73
| boolean found;
|
|
796 |
| |
|
797 |
73
| try {
|
|
798 |
73
| prevFindChar = this.findPrevDelimiter(position, findCharDelims, false);
|
|
799 |
| |
|
800 |
12
| if ((prevFindChar == -1) || (prevFindChar < 0)) return false;
|
|
801 |
| |
|
802 |
| |
|
803 |
61
| String foundString = getText(prevFindChar, 1);
|
|
804 |
61
| char foundChar = foundString.charAt(0);
|
|
805 |
61
| found = (foundChar == findChar);
|
|
806 |
| } |
|
807 |
0
| catch (Throwable t) { throw new UnexpectedException(t); }
|
|
808 |
61
| return found;
|
|
809 |
| } |
|
810 |
| |
|
811 |
| |
|
812 |
| |
|
813 |
| |
|
814 |
| |
|
815 |
| |
|
816 |
| |
|
817 |
| |
|
818 |
| |
|
819 |
| |
|
820 |
| |
|
821 |
| |
|
822 |
| |
|
823 |
| |
|
824 |
| |
|
825 |
| |
|
826 |
| |
|
827 |
6353
| public int _findPrevCharPos(final int pos, final char[] whitespace) throws BadLocationException {
|
|
828 |
| |
|
829 |
6353
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
830 |
| |
|
831 |
| |
|
832 |
6353
| final Query key = new Query.PrevCharPos(pos, whitespace);
|
|
833 |
6353
| final Integer cached = (Integer) _checkCache(key);
|
|
834 |
37
| if (cached != null) return cached.intValue();
|
|
835 |
| |
|
836 |
6316
| int reducedPos = pos;
|
|
837 |
6316
| int i = pos - 1;
|
|
838 |
6316
| String text;
|
|
839 |
6316
| text = getText(0, pos);
|
|
840 |
| |
|
841 |
6316
| final int oldPos = _currentLocation;
|
|
842 |
| |
|
843 |
6316
| setCurrentLocation(reducedPos);
|
|
844 |
| |
|
845 |
| |
|
846 |
| |
|
847 |
6316
| while (i >= 0) {
|
|
848 |
| |
|
849 |
| |
|
850 |
| |
|
851 |
13260
| if (match(text.charAt(i), whitespace)) {
|
|
852 |
| |
|
853 |
6808
| i--;
|
|
854 |
6808
| continue;
|
|
855 |
| } |
|
856 |
| |
|
857 |
| |
|
858 |
6452
| setCurrentLocation(i);
|
|
859 |
6452
| reducedPos = i;
|
|
860 |
| |
|
861 |
| |
|
862 |
6452
| if ((_reduced.getStateAtCurrent().equals(INSIDE_LINE_COMMENT)) ||
|
|
863 |
| (_reduced.getStateAtCurrent().equals(INSIDE_BLOCK_COMMENT))) { |
|
864 |
128
| i--;
|
|
865 |
128
| continue;
|
|
866 |
| } |
|
867 |
| |
|
868 |
6324
| if (i > 0 && _isStartOfComment(text, i - 1)) {
|
|
869 |
| |
|
870 |
12
| i = i - 2;
|
|
871 |
12
| continue;
|
|
872 |
| } |
|
873 |
| |
|
874 |
| |
|
875 |
6312
| break;
|
|
876 |
| } |
|
877 |
| |
|
878 |
| |
|
879 |
6316
| setCurrentLocation(oldPos);
|
|
880 |
| |
|
881 |
6316
| int result = reducedPos;
|
|
882 |
4
| if (i < 0) result = -1;
|
|
883 |
6316
| _storeInCache(key, result, pos - 1);
|
|
884 |
6316
| return result;
|
|
885 |
| } |
|
886 |
| |
|
887 |
| |
|
888 |
| |
|
889 |
| |
|
890 |
| |
|
891 |
112852
| protected Object _checkCache(final Query key) {
|
|
892 |
106343
| if (_queryCache == null) return null;
|
|
893 |
6509
| return _queryCache.get(key);
|
|
894 |
| } |
|
895 |
| |
|
896 |
| |
|
897 |
| |
|
898 |
| |
|
899 |
| |
|
900 |
| |
|
901 |
| |
|
902 |
108419
| protected void _storeInCache(final Query query, final Object answer, final int offset) {
|
|
903 |
106180
| if (_queryCache == null) return;
|
|
904 |
2239
| _queryCache.put(query, answer);
|
|
905 |
2239
| _addToOffsetsToQueries(query, offset);
|
|
906 |
| } |
|
907 |
| |
|
908 |
| |
|
909 |
| |
|
910 |
| |
|
911 |
1839
| protected void _clearCache(int offset) {
|
|
912 |
1704
| if (_queryCache == null) return;
|
|
913 |
| |
|
914 |
135
| if (offset <= 0) {
|
|
915 |
1
| _queryCache.clear();
|
|
916 |
1
| _offsetToQueries.clear();
|
|
917 |
1
| return;
|
|
918 |
| } |
|
919 |
| |
|
920 |
134
| Integer[] deadOffsets = _offsetToQueries.tailMap(offset).keySet().toArray(new Integer[0]);
|
|
921 |
134
| for (int i: deadOffsets) {
|
|
922 |
384
| for (Query query: _offsetToQueries.get(i)) _queryCache.remove(query);
|
|
923 |
308
| _offsetToQueries.remove(i);
|
|
924 |
| } |
|
925 |
| } |
|
926 |
| |
|
927 |
| |
|
928 |
2239
| private void _addToOffsetsToQueries(final Query query, final int offset) {
|
|
929 |
2239
| List<Query> selectedQueries = _offsetToQueries.get(offset);
|
|
930 |
2239
| if (selectedQueries == null) {
|
|
931 |
891
| selectedQueries = new LinkedList<Query>();
|
|
932 |
891
| _offsetToQueries.put(offset, selectedQueries);
|
|
933 |
| } |
|
934 |
2239
| selectedQueries.add(query);
|
|
935 |
| } |
|
936 |
| |
|
937 |
| |
|
938 |
| |
|
939 |
| |
|
940 |
| |
|
941 |
54
| public void indentLines(int selStart, int selEnd) {
|
|
942 |
54
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
943 |
54
| try { indentLines(selStart, selEnd, Indenter.IndentReason.OTHER, null); }
|
|
944 |
| catch (OperationCanceledException oce) { |
|
945 |
| |
|
946 |
0
| throw new UnexpectedException(oce);
|
|
947 |
| } |
|
948 |
| } |
|
949 |
| |
|
950 |
| |
|
951 |
| |
|
952 |
| |
|
953 |
| |
|
954 |
| |
|
955 |
| |
|
956 |
| |
|
957 |
| |
|
958 |
| |
|
959 |
66
| public void indentLines(int selStart, int selEnd, Indenter.IndentReason reason, ProgressMonitor pm)
|
|
960 |
| throws OperationCanceledException { |
|
961 |
| |
|
962 |
66
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
963 |
| |
|
964 |
| |
|
965 |
| |
|
966 |
| |
|
967 |
66
| try {
|
|
968 |
66
| if (selStart == selEnd) {
|
|
969 |
| |
|
970 |
45
| Position oldPosition = createUnwrappedPosition(_currentLocation);
|
|
971 |
45
| int lineStart = _getLineStartPos(selStart);
|
|
972 |
0
| if (lineStart < 0) lineStart = 0;
|
|
973 |
45
| setCurrentLocation(lineStart);
|
|
974 |
| |
|
975 |
| |
|
976 |
45
| if (_indentLine(reason)) {
|
|
977 |
45
| setCurrentLocation(oldPosition.getOffset());
|
|
978 |
20
| if (onlyWhiteSpaceBeforeCurrent()) move(_getWhiteSpace());
|
|
979 |
| } |
|
980 |
| } |
|
981 |
21
| else _indentBlock(selStart, selEnd, reason, pm);
|
|
982 |
| } |
|
983 |
0
| catch (BadLocationException e) { throw new UnexpectedException(e); }
|
|
984 |
| |
|
985 |
| |
|
986 |
| |
|
987 |
66
| endLastCompoundEdit();
|
|
988 |
| } |
|
989 |
| |
|
990 |
| |
|
991 |
| |
|
992 |
| |
|
993 |
| |
|
994 |
| |
|
995 |
| |
|
996 |
| |
|
997 |
21
| private void _indentBlock(final int start, final int end, Indenter.IndentReason reason, ProgressMonitor pm)
|
|
998 |
| throws OperationCanceledException, BadLocationException { |
|
999 |
| |
|
1000 |
| |
|
1001 |
21
| _queryCache = new HashMap<Query, Object>(INIT_CACHE_SIZE);
|
|
1002 |
21
| _offsetToQueries = new TreeMap<Integer, List<Query>>();
|
|
1003 |
| |
|
1004 |
| |
|
1005 |
| |
|
1006 |
21
| final Position endPos = this.createUnwrappedPosition(end);
|
|
1007 |
| |
|
1008 |
21
| int walker = start;
|
|
1009 |
| |
|
1010 |
21
| while (walker < endPos.getOffset()) {
|
|
1011 |
187
| setCurrentLocation(walker);
|
|
1012 |
| |
|
1013 |
187
| Position walkerPos = this.createUnwrappedPosition(walker);
|
|
1014 |
| |
|
1015 |
| |
|
1016 |
187
| _indentLine(reason);
|
|
1017 |
| |
|
1018 |
187
| setCurrentLocation(walkerPos.getOffset());
|
|
1019 |
187
| walker = walkerPos.getOffset();
|
|
1020 |
| |
|
1021 |
187
| if (pm != null) {
|
|
1022 |
0
| pm.setProgress(walker);
|
|
1023 |
0
| if (pm.isCanceled()) throw new OperationCanceledException();
|
|
1024 |
| } |
|
1025 |
| |
|
1026 |
| |
|
1027 |
| |
|
1028 |
187
| walker += _reduced.getDistToNextNewline() + 1;
|
|
1029 |
| |
|
1030 |
| } |
|
1031 |
| |
|
1032 |
| |
|
1033 |
21
| _queryCache = null;
|
|
1034 |
21
| _offsetToQueries = null;
|
|
1035 |
| } |
|
1036 |
| |
|
1037 |
| |
|
1038 |
236
| public boolean _indentLine(Indenter.IndentReason reason) { return getIndenter().indent(this, reason); }
|
|
1039 |
| |
|
1040 |
| |
|
1041 |
| |
|
1042 |
| |
|
1043 |
| |
|
1044 |
| |
|
1045 |
| |
|
1046 |
13
| public int getIntelligentBeginLinePos(int currPos) throws BadLocationException {
|
|
1047 |
13
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1048 |
| |
|
1049 |
13
| String prefix;
|
|
1050 |
13
| int firstChar;
|
|
1051 |
13
| firstChar = _getLineStartPos(currPos);
|
|
1052 |
13
| prefix = getText(firstChar, currPos-firstChar);
|
|
1053 |
| |
|
1054 |
| |
|
1055 |
13
| int i;
|
|
1056 |
13
| int len = prefix.length();
|
|
1057 |
| |
|
1058 |
7
| for (i = 0; i < len; i++ ) { if (! Character.isWhitespace(prefix.charAt(i))) break; }
|
|
1059 |
| |
|
1060 |
| |
|
1061 |
13
| if (i < len) {
|
|
1062 |
7
| int firstRealChar = firstChar + i;
|
|
1063 |
7
| if (firstRealChar < currPos) return firstRealChar;
|
|
1064 |
| } |
|
1065 |
| |
|
1066 |
6
| return firstChar;
|
|
1067 |
| } |
|
1068 |
| |
|
1069 |
| |
|
1070 |
| |
|
1071 |
| |
|
1072 |
| |
|
1073 |
117
| public int _getIndentOfCurrStmt(int pos) {
|
|
1074 |
117
| char[] delims = {';', '{', '}'};
|
|
1075 |
117
| char[] whitespace = {' ', '\t', '\n', ','};
|
|
1076 |
117
| return _getIndentOfCurrStmt(pos, delims, whitespace);
|
|
1077 |
| } |
|
1078 |
| |
|
1079 |
| |
|
1080 |
| |
|
1081 |
| |
|
1082 |
| |
|
1083 |
59
| public int _getIndentOfCurrStmt(int pos, char[] delims) {
|
|
1084 |
59
| char[] whitespace = {' ', '\t', '\n',','};
|
|
1085 |
59
| return _getIndentOfCurrStmt(pos, delims, whitespace);
|
|
1086 |
| } |
|
1087 |
| |
|
1088 |
| |
|
1089 |
| |
|
1090 |
| |
|
1091 |
| |
|
1092 |
| |
|
1093 |
| |
|
1094 |
197
| public int _getIndentOfCurrStmt(final int pos, final char[] delims, final char[] whitespace) {
|
|
1095 |
197
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1096 |
| |
|
1097 |
197
| try {
|
|
1098 |
| |
|
1099 |
197
| int lineStart = _getLineStartPos(pos);
|
|
1100 |
| |
|
1101 |
197
| final Query key = new Query.IndentOfCurrStmt(lineStart, delims, whitespace);
|
|
1102 |
197
| final Integer cached = (Integer) _checkCache(key);
|
|
1103 |
47
| if (cached != null) return cached;
|
|
1104 |
| |
|
1105 |
| |
|
1106 |
| |
|
1107 |
150
| boolean reachedStart = false;
|
|
1108 |
150
| int prevDelim = findPrevDelimiter(lineStart, delims, true);
|
|
1109 |
| |
|
1110 |
73
| if (prevDelim == -1) reachedStart = true;
|
|
1111 |
| |
|
1112 |
| |
|
1113 |
150
| int nextNonWSChar;
|
|
1114 |
73
| if (reachedStart) nextNonWSChar = getFirstNonWSCharPos(0);
|
|
1115 |
77
| else nextNonWSChar = getFirstNonWSCharPos(prevDelim + 1, whitespace, false);
|
|
1116 |
| |
|
1117 |
| |
|
1118 |
0
| if (nextNonWSChar == -1) nextNonWSChar = getLength();
|
|
1119 |
| |
|
1120 |
| |
|
1121 |
| |
|
1122 |
| |
|
1123 |
| |
|
1124 |
150
| int newLineStart = _getLineStartPos(nextNonWSChar);
|
|
1125 |
| |
|
1126 |
| |
|
1127 |
150
| int firstNonWS = _getLineFirstCharPos(newLineStart);
|
|
1128 |
150
| int wSPrefix = firstNonWS - newLineStart;
|
|
1129 |
150
| _storeInCache(key, wSPrefix, firstNonWS);
|
|
1130 |
150
| return wSPrefix;
|
|
1131 |
| } |
|
1132 |
0
| catch(BadLocationException e) { throw new UnexpectedException(e); }
|
|
1133 |
| |
|
1134 |
| } |
|
1135 |
| |
|
1136 |
| |
|
1137 |
| |
|
1138 |
| |
|
1139 |
| |
|
1140 |
| |
|
1141 |
| |
|
1142 |
| |
|
1143 |
| |
|
1144 |
| |
|
1145 |
| |
|
1146 |
| |
|
1147 |
| |
|
1148 |
| |
|
1149 |
| |
|
1150 |
| |
|
1151 |
| |
|
1152 |
| |
|
1153 |
| |
|
1154 |
| |
|
1155 |
| |
|
1156 |
| |
|
1157 |
| |
|
1158 |
| |
|
1159 |
| |
|
1160 |
129
| public int findCharOnLine(final int pos, final char findChar) {
|
|
1161 |
| |
|
1162 |
129
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1163 |
| |
|
1164 |
| |
|
1165 |
129
| final Query key = new Query.CharOnLine(pos, findChar);
|
|
1166 |
129
| final Integer cached = (Integer) _checkCache(key);
|
|
1167 |
5
| if (cached != null) return cached.intValue();
|
|
1168 |
| |
|
1169 |
124
| int i;
|
|
1170 |
124
| int matchIndex;
|
|
1171 |
| |
|
1172 |
124
| try {
|
|
1173 |
124
| final int oldPos = _currentLocation;
|
|
1174 |
124
| int lineStart = _getLineStartPos(pos);
|
|
1175 |
124
| int lineEnd = _getLineEndPos(pos);
|
|
1176 |
124
| String lineText = getText(lineStart, lineEnd - lineStart);
|
|
1177 |
124
| i = lineText.indexOf(findChar, 0);
|
|
1178 |
124
| matchIndex = i + lineStart;
|
|
1179 |
| |
|
1180 |
124
| while (i != -1) {
|
|
1181 |
| |
|
1182 |
| |
|
1183 |
| |
|
1184 |
| |
|
1185 |
26
| setCurrentLocation(matchIndex);
|
|
1186 |
| |
|
1187 |
| |
|
1188 |
22
| if (_reduced.getStateAtCurrent().equals(FREE)) break;
|
|
1189 |
| |
|
1190 |
| |
|
1191 |
4
| i = lineText.indexOf(findChar, i+1);
|
|
1192 |
| } |
|
1193 |
124
| setCurrentLocation(oldPos);
|
|
1194 |
| |
|
1195 |
102
| if (i == -1) matchIndex = -1;
|
|
1196 |
124
| _storeInCache(key, matchIndex, Math.max(pos - 1, matchIndex));
|
|
1197 |
| } |
|
1198 |
0
| catch (BadLocationException e) { throw new UnexpectedException(e); }
|
|
1199 |
| |
|
1200 |
124
| return matchIndex;
|
|
1201 |
| } |
|
1202 |
| |
|
1203 |
| |
|
1204 |
| |
|
1205 |
| |
|
1206 |
| |
|
1207 |
| |
|
1208 |
17527
| public int _getLineStartPos(final int pos) {
|
|
1209 |
| |
|
1210 |
17527
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1211 |
| |
|
1212 |
0
| if (pos < 0 || pos > getLength()) return -1;
|
|
1213 |
| |
|
1214 |
17527
| final Query key = new Query.LineStartPos(pos);
|
|
1215 |
17527
| final Integer cached = (Integer) _checkCache(key);
|
|
1216 |
2390
| if (cached != null) return cached.intValue();
|
|
1217 |
| |
|
1218 |
15137
| int dist;
|
|
1219 |
| |
|
1220 |
15137
| final int oldPos = _currentLocation;
|
|
1221 |
15137
| setCurrentLocation(pos);
|
|
1222 |
15137
| dist = _reduced.getDistToStart(0);
|
|
1223 |
15137
| setCurrentLocation(oldPos);
|
|
1224 |
| |
|
1225 |
15137
| int newPos = 0;
|
|
1226 |
12322
| if (dist >= 0) newPos = pos - dist;
|
|
1227 |
15137
| _storeInCache(key, newPos, pos - 1);
|
|
1228 |
15137
| return newPos;
|
|
1229 |
| } |
|
1230 |
| |
|
1231 |
| |
|
1232 |
| |
|
1233 |
| |
|
1234 |
| |
|
1235 |
25168
| public int _getLineEndPos(final int pos) {
|
|
1236 |
| |
|
1237 |
25168
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1238 |
| |
|
1239 |
0
| if (pos < 0 || pos > getLength()) return -1;
|
|
1240 |
| |
|
1241 |
| |
|
1242 |
25168
| final Query key = new Query.LineEndPos(pos);
|
|
1243 |
25168
| final Integer cached = (Integer) _checkCache(key);
|
|
1244 |
626
| if (cached != null) return cached.intValue();
|
|
1245 |
| |
|
1246 |
24542
| int dist, newPos;
|
|
1247 |
| |
|
1248 |
24542
| final int oldPos = _currentLocation;
|
|
1249 |
24542
| setCurrentLocation(pos);
|
|
1250 |
24542
| dist = _reduced.getDistToNextNewline();
|
|
1251 |
24542
| setCurrentLocation(oldPos);
|
|
1252 |
| |
|
1253 |
24542
| newPos = pos + dist;
|
|
1254 |
24542
| assert newPos == getLength() || _getText(newPos, 1).charAt(0) == newline;
|
|
1255 |
24542
| _storeInCache(key, newPos, newPos);
|
|
1256 |
24542
| return newPos;
|
|
1257 |
| } |
|
1258 |
| |
|
1259 |
| |
|
1260 |
| |
|
1261 |
| |
|
1262 |
| |
|
1263 |
| |
|
1264 |
| |
|
1265 |
| |
|
1266 |
930
| public int _getLineFirstCharPos(final int pos) {
|
|
1267 |
| |
|
1268 |
930
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1269 |
| |
|
1270 |
| |
|
1271 |
930
| final Query key = new Query.LineFirstCharPos(pos);
|
|
1272 |
930
| final Integer cached = (Integer) _checkCache(key);
|
|
1273 |
250
| if (cached != null) return cached.intValue();
|
|
1274 |
| |
|
1275 |
680
| final int startLinePos = _getLineStartPos(pos);
|
|
1276 |
680
| final int endLinePos = _getLineEndPos(pos);
|
|
1277 |
680
| int nonWSPos = endLinePos;
|
|
1278 |
| |
|
1279 |
| |
|
1280 |
680
| String text = _getText(startLinePos, endLinePos - startLinePos);
|
|
1281 |
680
| int walker = 0;
|
|
1282 |
680
| while (walker < text.length()) {
|
|
1283 |
1086
| if (text.charAt(walker) == ' ' || text.charAt(walker) == '\t') walker++;
|
|
1284 |
| else { |
|
1285 |
604
| nonWSPos = startLinePos + walker;
|
|
1286 |
604
| break;
|
|
1287 |
| } |
|
1288 |
| } |
|
1289 |
680
| _storeInCache(key, nonWSPos, Math.max(pos - 1, nonWSPos));
|
|
1290 |
680
| return nonWSPos;
|
|
1291 |
| } |
|
1292 |
| |
|
1293 |
| |
|
1294 |
| |
|
1295 |
| |
|
1296 |
| |
|
1297 |
| |
|
1298 |
23418
| public int getFirstNonWSCharPos(int pos) throws BadLocationException {
|
|
1299 |
23418
| char[] whitespace = {' ', '\t', '\n'};
|
|
1300 |
23418
| return getFirstNonWSCharPos(pos, whitespace, false);
|
|
1301 |
| } |
|
1302 |
| |
|
1303 |
| |
|
1304 |
| |
|
1305 |
| |
|
1306 |
| |
|
1307 |
| |
|
1308 |
| |
|
1309 |
23
| public int getFirstNonWSCharPos(int pos, boolean acceptComments) throws BadLocationException {
|
|
1310 |
23
| char[] whitespace = {' ', '\t', '\n'};
|
|
1311 |
23
| return getFirstNonWSCharPos(pos, whitespace, acceptComments);
|
|
1312 |
| } |
|
1313 |
| |
|
1314 |
| |
|
1315 |
| |
|
1316 |
| |
|
1317 |
| |
|
1318 |
| |
|
1319 |
| |
|
1320 |
| |
|
1321 |
23965
| public int getFirstNonWSCharPos(final int pos, final char[] whitespace, final boolean acceptComments) throws
|
|
1322 |
| BadLocationException { |
|
1323 |
| |
|
1324 |
23965
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1325 |
| |
|
1326 |
| |
|
1327 |
23965
| final Query key = new Query.FirstNonWSCharPos(pos, whitespace, acceptComments);
|
|
1328 |
23965
| final Integer cached = (Integer) _checkCache(key);
|
|
1329 |
88
| if (cached != null) return cached.intValue();
|
|
1330 |
| |
|
1331 |
23877
| final int docLen = getLength();
|
|
1332 |
23877
| final int origPos = _currentLocation;
|
|
1333 |
23877
| final int endPos = _getLineEndPos(pos);
|
|
1334 |
| |
|
1335 |
23877
| String line = getText(pos, endPos - pos);
|
|
1336 |
23877
| setCurrentLocation(pos);
|
|
1337 |
23877
| try {
|
|
1338 |
23877
| int i = pos;
|
|
1339 |
23877
| int reducedPos = pos;
|
|
1340 |
| |
|
1341 |
23877
| while (i < endPos) {
|
|
1342 |
| |
|
1343 |
| |
|
1344 |
43784
| if (match(line.charAt(i-pos), whitespace)) {
|
|
1345 |
15208
| i++;
|
|
1346 |
15208
| continue;
|
|
1347 |
| } |
|
1348 |
| |
|
1349 |
| |
|
1350 |
28576
| setCurrentLocation(i);
|
|
1351 |
28576
| reducedPos = i;
|
|
1352 |
| |
|
1353 |
| |
|
1354 |
28576
| if (! acceptComments &&
|
|
1355 |
| ((_reduced.getStateAtCurrent().equals(INSIDE_LINE_COMMENT)) || |
|
1356 |
| (_reduced.getStateAtCurrent().equals(INSIDE_BLOCK_COMMENT)))) { |
|
1357 |
5075
| i++;
|
|
1358 |
5075
| continue;
|
|
1359 |
| } |
|
1360 |
| |
|
1361 |
| |
|
1362 |
23501
| if (! acceptComments && _isStartOfComment(line, i - pos)) {
|
|
1363 |
| |
|
1364 |
95
| i = i + 2;
|
|
1365 |
95
| continue;
|
|
1366 |
| } |
|
1367 |
| |
|
1368 |
| |
|
1369 |
23406
| _storeInCache(key, reducedPos, reducedPos);
|
|
1370 |
| |
|
1371 |
23406
| return reducedPos;
|
|
1372 |
| } |
|
1373 |
| |
|
1374 |
| |
|
1375 |
471
| if (endPos + 1 >= docLen) {
|
|
1376 |
24
| _storeInCache(key, -1, Integer.MAX_VALUE);
|
|
1377 |
| |
|
1378 |
24
| return -1;
|
|
1379 |
| } |
|
1380 |
| } |
|
1381 |
23877
| finally { setCurrentLocation(origPos); }
|
|
1382 |
| |
|
1383 |
| |
|
1384 |
447
| return getFirstNonWSCharPos(endPos + 1, whitespace, acceptComments);
|
|
1385 |
| } |
|
1386 |
| |
|
1387 |
6305
| public int _findPrevNonWSCharPos(int pos) throws BadLocationException {
|
|
1388 |
6305
| char[] whitespace = {' ', '\t', '\n'};
|
|
1389 |
6305
| return _findPrevCharPos(pos, whitespace);
|
|
1390 |
| } |
|
1391 |
| |
|
1392 |
| |
|
1393 |
| |
|
1394 |
| |
|
1395 |
63422
| protected static boolean _isStartOfComment(String text, int pos) {
|
|
1396 |
63422
| char currChar = text.charAt(pos);
|
|
1397 |
63422
| if (currChar == '/') {
|
|
1398 |
453
| try {
|
|
1399 |
453
| char afterCurrChar = text.charAt(pos + 1);
|
|
1400 |
107
| if ((afterCurrChar == '/') || (afterCurrChar == '*')) return true;
|
|
1401 |
| } catch (StringIndexOutOfBoundsException e) { } |
|
1402 |
| } |
|
1403 |
63315
| return false;
|
|
1404 |
| } |
|
1405 |
| |
|
1406 |
| |
|
1407 |
| |
|
1408 |
| |
|
1409 |
| |
|
1410 |
| |
|
1411 |
| |
|
1412 |
| |
|
1413 |
| |
|
1414 |
| |
|
1415 |
| |
|
1416 |
| |
|
1417 |
| |
|
1418 |
| |
|
1419 |
| |
|
1420 |
| |
|
1421 |
| |
|
1422 |
| |
|
1423 |
| |
|
1424 |
| |
|
1425 |
| |
|
1426 |
| |
|
1427 |
| |
|
1428 |
| |
|
1429 |
| |
|
1430 |
2
| public boolean _inParenPhrase(final int pos) {
|
|
1431 |
| |
|
1432 |
2
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1433 |
| |
|
1434 |
| |
|
1435 |
2
| final Query key = new Query.PosInParenPhrase(pos);
|
|
1436 |
2
| Boolean cached = (Boolean) _checkCache(key);
|
|
1437 |
0
| if (cached != null) return cached.booleanValue();
|
|
1438 |
| |
|
1439 |
2
| boolean _inParenPhrase;
|
|
1440 |
| |
|
1441 |
2
| final int oldPos = _currentLocation;
|
|
1442 |
| |
|
1443 |
2
| setCurrentLocation(pos);
|
|
1444 |
2
| _inParenPhrase = _inParenPhrase();
|
|
1445 |
2
| setCurrentLocation(oldPos);
|
|
1446 |
2
| _storeInCache(key, _inParenPhrase, pos - 1);
|
|
1447 |
| |
|
1448 |
2
| return _inParenPhrase;
|
|
1449 |
| } |
|
1450 |
| |
|
1451 |
| |
|
1452 |
1210
| public BraceInfo _getLineEnclosingBrace() {
|
|
1453 |
| |
|
1454 |
1210
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1455 |
| |
|
1456 |
| |
|
1457 |
1210
| final int lineStart = _getLineStartPos(_currentLocation);
|
|
1458 |
| |
|
1459 |
0
| if (lineStart < 0) return BraceInfo.NULL;
|
|
1460 |
1210
| final int keyPos = lineStart;
|
|
1461 |
1210
| final Query key = new Query.LineEnclosingBrace(keyPos);
|
|
1462 |
1210
| final BraceInfo cached = (BraceInfo) _checkCache(key);
|
|
1463 |
389
| if (cached != null) return cached;
|
|
1464 |
| |
|
1465 |
| |
|
1466 |
821
| BraceInfo b = _reduced._getLineEnclosingBrace();
|
|
1467 |
| |
|
1468 |
821
| _storeInCache(key, b, keyPos - 1);
|
|
1469 |
821
| return b;
|
|
1470 |
| } |
|
1471 |
| |
|
1472 |
| |
|
1473 |
28
| public BraceInfo _getEnclosingBrace() {
|
|
1474 |
28
| int pos = _currentLocation;
|
|
1475 |
| |
|
1476 |
28
| final Query key = new Query.EnclosingBrace(pos);
|
|
1477 |
28
| final BraceInfo cached = (BraceInfo) _checkCache(key);
|
|
1478 |
0
| if (cached != null) return cached;
|
|
1479 |
28
| BraceInfo b = _reduced._getEnclosingBrace();
|
|
1480 |
28
| _storeInCache(key, b, pos - 1);
|
|
1481 |
28
| return b;
|
|
1482 |
| } |
|
1483 |
| |
|
1484 |
| |
|
1485 |
| |
|
1486 |
| |
|
1487 |
2
| private boolean _inParenPhrase() {
|
|
1488 |
| |
|
1489 |
2
| BraceInfo info = _reduced._getEnclosingBrace();
|
|
1490 |
2
| return info.braceType().equals(BraceInfo.OPEN_PAREN);
|
|
1491 |
| |
|
1492 |
| } |
|
1493 |
| |
|
1494 |
| |
|
1495 |
| |
|
1496 |
| |
|
1497 |
| |
|
1498 |
| |
|
1499 |
| |
|
1500 |
| |
|
1501 |
| |
|
1502 |
| |
|
1503 |
| |
|
1504 |
| |
|
1505 |
| |
|
1506 |
| |
|
1507 |
| |
|
1508 |
| |
|
1509 |
| |
|
1510 |
| |
|
1511 |
| |
|
1512 |
| |
|
1513 |
| |
|
1514 |
| |
|
1515 |
| |
|
1516 |
| |
|
1517 |
| |
|
1518 |
| |
|
1519 |
| |
|
1520 |
| |
|
1521 |
| |
|
1522 |
| |
|
1523 |
| |
|
1524 |
| |
|
1525 |
| |
|
1526 |
| |
|
1527 |
| |
|
1528 |
| |
|
1529 |
| |
|
1530 |
249
| public boolean _inBlockComment(final int pos) {
|
|
1531 |
249
| final int here = _currentLocation;
|
|
1532 |
249
| final int distToStart = here - _getLineStartPos(here);
|
|
1533 |
249
| _reduced.resetLocation();
|
|
1534 |
249
| ReducedModelState state = stateAtRelLocation(-distToStart);
|
|
1535 |
| |
|
1536 |
249
| return (state.equals(INSIDE_BLOCK_COMMENT));
|
|
1537 |
| } |
|
1538 |
| |
|
1539 |
| |
|
1540 |
| |
|
1541 |
| |
|
1542 |
| |
|
1543 |
| |
|
1544 |
| |
|
1545 |
| |
|
1546 |
| |
|
1547 |
| |
|
1548 |
| |
|
1549 |
| |
|
1550 |
| |
|
1551 |
| |
|
1552 |
| |
|
1553 |
| |
|
1554 |
| |
|
1555 |
| |
|
1556 |
| |
|
1557 |
| |
|
1558 |
| |
|
1559 |
| |
|
1560 |
| |
|
1561 |
| |
|
1562 |
333
| protected boolean notInBlock(final int pos) {
|
|
1563 |
| |
|
1564 |
333
| final Query key = new Query.PosNotInBlock(pos);
|
|
1565 |
333
| final Boolean cached = (Boolean) _checkCache(key);
|
|
1566 |
0
| if (cached != null) return cached.booleanValue();
|
|
1567 |
| |
|
1568 |
333
| final int oldPos = _currentLocation;
|
|
1569 |
333
| setCurrentLocation(pos);
|
|
1570 |
333
| final BraceInfo info = _reduced._getEnclosingBrace();
|
|
1571 |
333
| final boolean notInParenPhrase = info.braceType().equals(BraceInfo.NONE);
|
|
1572 |
333
| setCurrentLocation(oldPos);
|
|
1573 |
333
| _storeInCache(key, notInParenPhrase, pos - 1);
|
|
1574 |
333
| return notInParenPhrase;
|
|
1575 |
| } |
|
1576 |
| |
|
1577 |
| |
|
1578 |
| |
|
1579 |
| |
|
1580 |
| |
|
1581 |
| |
|
1582 |
45
| private boolean onlyWhiteSpaceBeforeCurrent() throws BadLocationException{
|
|
1583 |
| |
|
1584 |
45
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1585 |
| |
|
1586 |
45
| int lineStart = _getLineStartPos(_currentLocation);
|
|
1587 |
0
| if (lineStart < 0) lineStart = 0;
|
|
1588 |
45
| int prefixSize = _currentLocation - lineStart;
|
|
1589 |
| |
|
1590 |
| |
|
1591 |
45
| String prefix = getText(lineStart, prefixSize);
|
|
1592 |
| |
|
1593 |
| |
|
1594 |
45
| int pos = prefixSize - 1;
|
|
1595 |
48
| while (pos >= 0 && prefix.charAt(pos) == ' ') pos--;
|
|
1596 |
45
| return (pos < 0);
|
|
1597 |
| } |
|
1598 |
| |
|
1599 |
| |
|
1600 |
| |
|
1601 |
| |
|
1602 |
| |
|
1603 |
| |
|
1604 |
20
| private int _getWhiteSpace() throws BadLocationException {
|
|
1605 |
| |
|
1606 |
20
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1607 |
| |
|
1608 |
20
| int lineEnd = _getLineEndPos(_currentLocation);
|
|
1609 |
20
| int lineLen = lineEnd - _currentLocation;
|
|
1610 |
20
| String line = getText(_currentLocation, lineLen);
|
|
1611 |
20
| int i;
|
|
1612 |
20
| for (i = 0; i < lineLen && line.charAt(i) == ' '; i++) ;
|
|
1613 |
20
| return i;
|
|
1614 |
| } |
|
1615 |
| |
|
1616 |
| |
|
1617 |
| |
|
1618 |
| |
|
1619 |
| |
|
1620 |
| |
|
1621 |
0
| private int _getWhiteSpacePrefix() throws BadLocationException {
|
|
1622 |
| |
|
1623 |
| |
|
1624 |
| |
|
1625 |
0
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1626 |
| |
|
1627 |
0
| int lineStart = _getLineStartPos(_currentLocation);
|
|
1628 |
0
| if (lineStart < 0) lineStart = 0;
|
|
1629 |
0
| int prefixSize = _currentLocation - lineStart;
|
|
1630 |
| |
|
1631 |
| |
|
1632 |
0
| String prefix = getText(lineStart, prefixSize);
|
|
1633 |
| |
|
1634 |
| |
|
1635 |
0
| int pos = prefixSize - 1;
|
|
1636 |
0
| while (pos >= 0 && prefix.charAt(pos) == ' ') pos--;
|
|
1637 |
0
| return (pos < 0) ? prefixSize : 0;
|
|
1638 |
| } |
|
1639 |
| |
|
1640 |
| |
|
1641 |
| |
|
1642 |
| |
|
1643 |
| |
|
1644 |
| |
|
1645 |
270
| public void setTab(int tab, int pos) {
|
|
1646 |
| |
|
1647 |
270
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1648 |
| |
|
1649 |
270
| try {
|
|
1650 |
270
| int startPos = _getLineStartPos(pos);
|
|
1651 |
270
| int firstNonWSPos = _getLineFirstCharPos(pos);
|
|
1652 |
270
| int len = firstNonWSPos - startPos;
|
|
1653 |
| |
|
1654 |
| |
|
1655 |
270
| if (len != tab) {
|
|
1656 |
| |
|
1657 |
207
| int diff = tab - len;
|
|
1658 |
179
| if (diff > 0) insertString(firstNonWSPos, StringOps.getBlankString(diff), null);
|
|
1659 |
28
| else remove(firstNonWSPos + diff, -diff);
|
|
1660 |
| } |
|
1661 |
| |
|
1662 |
| } |
|
1663 |
| catch (BadLocationException e) { |
|
1664 |
| |
|
1665 |
0
| throw new UnexpectedException(e);
|
|
1666 |
| } |
|
1667 |
| } |
|
1668 |
| |
|
1669 |
| |
|
1670 |
| |
|
1671 |
| |
|
1672 |
| |
|
1673 |
27
| public void setTab(String tab, int pos) {
|
|
1674 |
| |
|
1675 |
27
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1676 |
| |
|
1677 |
27
| try {
|
|
1678 |
27
| int startPos = _getLineStartPos(pos);
|
|
1679 |
27
| int firstNonWSPos = _getLineFirstCharPos(pos);
|
|
1680 |
27
| int len = firstNonWSPos - startPos;
|
|
1681 |
| |
|
1682 |
| |
|
1683 |
27
| remove(startPos, len);
|
|
1684 |
27
| insertString(startPos, tab, null);
|
|
1685 |
| } |
|
1686 |
| catch (BadLocationException e) { |
|
1687 |
| |
|
1688 |
0
| throw new UnexpectedException(e);
|
|
1689 |
| } |
|
1690 |
| } |
|
1691 |
| |
|
1692 |
| |
|
1693 |
| |
|
1694 |
| |
|
1695 |
| |
|
1696 |
| |
|
1697 |
| |
|
1698 |
| |
|
1699 |
| |
|
1700 |
| |
|
1701 |
1565
| protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr) {
|
|
1702 |
| |
|
1703 |
1565
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1704 |
| |
|
1705 |
1565
| super.insertUpdate(chng, attr);
|
|
1706 |
| |
|
1707 |
1565
| try {
|
|
1708 |
1565
| final int offset = chng.getOffset();
|
|
1709 |
1565
| final int length = chng.getLength();
|
|
1710 |
1565
| final String str = getText(offset, length);
|
|
1711 |
| |
|
1712 |
1565
| if (length > 0) _clearCache(offset);
|
|
1713 |
| |
|
1714 |
1565
| Runnable doCommand =
|
|
1715 |
1565
| (length == 1) ? new CharInsertCommand(offset, str.charAt(0)) : new InsertCommand(offset, str);
|
|
1716 |
1565
| RemoveCommand undoCommand = new UninsertCommand(offset, length, str);
|
|
1717 |
| |
|
1718 |
| |
|
1719 |
1565
| addUndoRedo(chng, undoCommand, doCommand);
|
|
1720 |
| |
|
1721 |
| |
|
1722 |
1565
| doCommand.run();
|
|
1723 |
| } |
|
1724 |
0
| catch (BadLocationException ble) { throw new UnexpectedException(ble); }
|
|
1725 |
| } |
|
1726 |
| |
|
1727 |
| |
|
1728 |
| |
|
1729 |
| |
|
1730 |
| |
|
1731 |
| |
|
1732 |
| |
|
1733 |
| |
|
1734 |
| |
|
1735 |
274
| protected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng) {
|
|
1736 |
| |
|
1737 |
274
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1738 |
| |
|
1739 |
274
| try {
|
|
1740 |
274
| final int offset = chng.getOffset();
|
|
1741 |
274
| final int length = chng.getLength();
|
|
1742 |
| |
|
1743 |
274
| final String removedText = getText(offset, length);
|
|
1744 |
274
| super.removeUpdate(chng);
|
|
1745 |
| |
|
1746 |
274
| if (length > 0) _clearCache(offset);
|
|
1747 |
| |
|
1748 |
274
| Runnable doCommand = new RemoveCommand(offset, length, removedText);
|
|
1749 |
274
| Runnable undoCommand = new UnremoveCommand(offset, removedText);
|
|
1750 |
| |
|
1751 |
| |
|
1752 |
274
| addUndoRedo(chng, undoCommand, doCommand);
|
|
1753 |
| |
|
1754 |
274
| doCommand.run();
|
|
1755 |
| } |
|
1756 |
0
| catch (BadLocationException e) { throw new UnexpectedException(e); }
|
|
1757 |
| } |
|
1758 |
| |
|
1759 |
| |
|
1760 |
0
| public byte[] getBytes() { return getText().getBytes(); }
|
|
1761 |
| |
|
1762 |
348
| public void clear() {
|
|
1763 |
348
| try { remove(0, getLength()); }
|
|
1764 |
0
| catch(BadLocationException e) { throw new UnexpectedException(e); }
|
|
1765 |
| } |
|
1766 |
| |
|
1767 |
| |
|
1768 |
93176
| private static boolean isCommentOpen(String text, int pos) {
|
|
1769 |
93176
| int len = text.length();
|
|
1770 |
112
| if (len < 2) return false;
|
|
1771 |
10463
| if (pos == len - 1) return isCommentStart(text, pos - 1);
|
|
1772 |
4139
| if (pos == 0) return isCommentStart(text, 0);
|
|
1773 |
78462
| return isCommentStart(text, pos - 1) || isCommentStart(text, pos);
|
|
1774 |
| } |
|
1775 |
| |
|
1776 |
| |
|
1777 |
171461
| private static boolean isCommentStart(String text, int pos) {
|
|
1778 |
171461
| char ch1 = text.charAt(pos);
|
|
1779 |
171461
| char ch2 = text.charAt(pos + 1);
|
|
1780 |
171461
| return ch1 == '/' && (ch2 == '/' || ch2 == '*');
|
|
1781 |
| } |
|
1782 |
| |
|
1783 |
| |
|
1784 |
| protected abstract int startCompoundEdit(); |
|
1785 |
| protected abstract void endCompoundEdit(int i); |
|
1786 |
| protected abstract void endLastCompoundEdit(); |
|
1787 |
| protected abstract void addUndoRedo(AbstractDocument.DefaultDocumentEvent chng, Runnable undoCommand, |
|
1788 |
| Runnable doCommand); |
|
1789 |
| |
|
1790 |
| |
|
1791 |
| |
|
1792 |
| |
|
1793 |
| |
|
1794 |
| |
|
1795 |
| |
|
1796 |
| |
|
1797 |
| private volatile int _numLinesChangedAfter = -1; |
|
1798 |
| |
|
1799 |
|