|
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.definitions; |
|
38 |
| |
|
39 |
| import java.awt.EventQueue; |
|
40 |
| import javax.swing.text.*; |
|
41 |
| import javax.swing.undo.*; |
|
42 |
| import javax.swing.event.DocumentEvent; |
|
43 |
| import java.util.LinkedList; |
|
44 |
| import java.util.List; |
|
45 |
| import java.util.Map; |
|
46 |
| import java.util.WeakHashMap; |
|
47 |
| import java.lang.ref.WeakReference; |
|
48 |
| |
|
49 |
| import java.io.Reader; |
|
50 |
| import java.io.StringReader; |
|
51 |
| import java.io.IOException; |
|
52 |
| |
|
53 |
| import koala.dynamicjava.parser.impl.Parser; |
|
54 |
| import koala.dynamicjava.parser.impl.ParseException; |
|
55 |
| import koala.dynamicjava.parser.impl.TokenMgrError; |
|
56 |
| |
|
57 |
| import edu.rice.cs.drjava.DrJava; |
|
58 |
| import edu.rice.cs.drjava.model.definitions.reducedmodel.*; |
|
59 |
| import edu.rice.cs.util.Log; |
|
60 |
| import edu.rice.cs.util.UnexpectedException; |
|
61 |
| import edu.rice.cs.util.swing.Utilities; |
|
62 |
| import edu.rice.cs.drjava.model.definitions.indent.Indenter; |
|
63 |
| import edu.rice.cs.drjava.model.OpenDefinitionsDocument; |
|
64 |
| import edu.rice.cs.drjava.model.*; |
|
65 |
| |
|
66 |
| import static edu.rice.cs.drjava.model.definitions.reducedmodel.ReducedModelStates.*; |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| public class DefinitionsDocument extends AbstractDJDocument implements Finalizable<DefinitionsDocument> { |
|
72 |
| |
|
73 |
| public static final Log _log = new Log("GlobalModel.txt", false); |
|
74 |
| private static final int NO_COMMENT_OFFSET = 0; |
|
75 |
| private static final int WING_COMMENT_OFFSET = 2; |
|
76 |
| |
|
77 |
| private final List<DocumentClosedListener> _closedListeners = new LinkedList<DocumentClosedListener>(); |
|
78 |
| |
|
79 |
146
| public void addDocumentClosedListener(DocumentClosedListener l) {
|
|
80 |
146
| synchronized(_closedListeners) { _closedListeners.add(l); }
|
|
81 |
| } |
|
82 |
| |
|
83 |
0
| public void removeDocumentClosedListener(DocumentClosedListener l) {
|
|
84 |
0
| synchronized(_closedListeners) { _closedListeners.remove(l); }
|
|
85 |
| } |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
117
| public void close() {
|
|
92 |
117
| _removeIndenter();
|
|
93 |
117
| synchronized(_closedListeners) {
|
|
94 |
58
| for (DocumentClosedListener l: _closedListeners) { l.close(); }
|
|
95 |
117
| _closedListeners.clear();
|
|
96 |
| } |
|
97 |
| } |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| private static final int UNDO_LIMIT = 1000; |
|
102 |
| |
|
103 |
| private static boolean _tabsRemoved = true; |
|
104 |
| |
|
105 |
| |
|
106 |
| private volatile boolean _isModifiedSinceSave = false; |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| private volatile OpenDefinitionsDocument _odd; |
|
111 |
| |
|
112 |
| private volatile CompoundUndoManager _undoManager; |
|
113 |
| |
|
114 |
| |
|
115 |
| private final GlobalEventNotifier _notifier; |
|
116 |
| |
|
117 |
| |
|
118 |
| private final DefinitionsEditorKit _editor; |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| private static final Object _wrappedPosListLock = new Object(); |
|
129 |
| |
|
130 |
| |
|
131 |
| private volatile LinkedList<WeakReference<WrappedPosition>> _wrappedPosList; |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
448
| private DefinitionsDocument(Indenter indenter, GlobalEventNotifier notifier, CompoundUndoManager undoManager) {
|
|
138 |
448
| super(indenter);
|
|
139 |
448
| _notifier = notifier;
|
|
140 |
448
| _editor = new DefinitionsEditorKit(notifier);
|
|
141 |
448
| _undoManager = undoManager;
|
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
448
| public DefinitionsDocument(Indenter indenter, GlobalEventNotifier notifier) {
|
|
146 |
| |
|
147 |
448
| this(indenter, notifier, new CompoundUndoManager(notifier));
|
|
148 |
| |
|
149 |
448
| _undoManager.setLimit(UNDO_LIMIT);
|
|
150 |
| } |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
448
| public DefinitionsDocument(GlobalEventNotifier notifier) {
|
|
156 |
448
| this(new Indenter(DrJava.getConfig().getSetting(INDENT_LEVEL).intValue()), notifier);
|
|
157 |
| } |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
0
| public DefinitionsDocument(GlobalEventNotifier notifier, CompoundUndoManager undoManager) {
|
|
163 |
0
| this(new Indenter(DrJava.getConfig().getSetting(INDENT_LEVEL).intValue()), notifier, undoManager);
|
|
164 |
| } |
|
165 |
| |
|
166 |
| |
|
167 |
0
| public DefinitionsEditorKit getEditor() { return _editor; }
|
|
168 |
| |
|
169 |
| |
|
170 |
0
| protected Indenter makeNewIndenter(int indentLevel) { return new Indenter(indentLevel); }
|
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
343
| public void setOpenDefDoc(OpenDefinitionsDocument odd) { if (_odd == null) _odd = odd; }
|
|
176 |
| |
|
177 |
| |
|
178 |
84
| public OpenDefinitionsDocument getOpenDefDoc() {
|
|
179 |
84
| if (_odd == null)
|
|
180 |
0
| throw new IllegalStateException("The OpenDefinitionsDocument for this DefinitionsDocument has never been set");
|
|
181 |
84
| else return _odd;
|
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
752
| protected void _styleChanged() {
|
|
186 |
| |
|
187 |
752
| int length = getLength() - _currentLocation;
|
|
188 |
| |
|
189 |
| |
|
190 |
752
| DocumentEvent evt = new DefaultDocumentEvent(_currentLocation, length, DocumentEvent.EventType.CHANGE);
|
|
191 |
752
| fireChangedUpdate(evt);
|
|
192 |
| } |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
115
| public String getQualifiedClassName() throws ClassNameNotFoundException {
|
|
252 |
115
| return getPackageQualifier() + getMainClassName();
|
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
0
| public String getQualifiedClassName(int pos) throws ClassNameNotFoundException {
|
|
257 |
0
| return getPackageQualifier() + getEnclosingTopLevelClassName(pos);
|
|
258 |
| } |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
115
| private String getPackageQualifier() {
|
|
264 |
115
| String packageName = getPackageName();
|
|
265 |
11
| if ((packageName != null) && (! packageName.equals(""))) { packageName = packageName + "."; }
|
|
266 |
115
| return packageName;
|
|
267 |
| } |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
584
| public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
|
|
275 |
584
| if (_tabsRemoved) str = _removeTabs(str);
|
|
276 |
584
| _setModifiedSinceSave();
|
|
277 |
584
| super.insertString(offset, str, a);
|
|
278 |
| } |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
239
| public void remove(int offset, int len) throws BadLocationException {
|
|
284 |
| |
|
285 |
111
| if (len == 0) return;
|
|
286 |
128
| _setModifiedSinceSave();
|
|
287 |
128
| super.remove(offset, len);
|
|
288 |
| } |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
584
| static String _removeTabs(final String source) { return source.replace('\t', ' '); }
|
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
1
| public void updateModifiedSinceSave() {
|
|
300 |
1
| _isModifiedSinceSave = _undoManager.isModified();
|
|
301 |
1
| if (_odd != null) _odd.documentReset();
|
|
302 |
| } |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
712
| private void _setModifiedSinceSave() {
|
|
308 |
712
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
309 |
712
| if (! _isModifiedSinceSave) {
|
|
310 |
322
| _isModifiedSinceSave = true;
|
|
311 |
218
| if (_odd != null) _odd.documentModified();
|
|
312 |
| } |
|
313 |
| } |
|
314 |
| |
|
315 |
| |
|
316 |
430
| public void resetModification() {
|
|
317 |
430
| _isModifiedSinceSave = false;
|
|
318 |
430
| _undoManager.documentSaved();
|
|
319 |
430
| if (_odd != null) _odd.documentReset();
|
|
320 |
| } |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
2198
| public boolean isModifiedSinceSave() { return _isModifiedSinceSave; }
|
|
326 |
| |
|
327 |
| |
|
328 |
95
| public int getCurrentCol() {
|
|
329 |
95
| Element root = getDefaultRootElement();
|
|
330 |
95
| int line = root.getElementIndex(_currentLocation);
|
|
331 |
95
| return _currentLocation - root.getElement(line).getStartOffset();
|
|
332 |
| } |
|
333 |
| |
|
334 |
| |
|
335 |
141
| public int getCurrentLine() { return getLineOfOffset(_currentLocation); }
|
|
336 |
| |
|
337 |
| |
|
338 |
141
| public int getLineOfOffset(int offset) { return getDefaultRootElement().getElementIndex(offset) + 1; }
|
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
0
| public int _getOffset(int lineNum) {
|
|
346 |
0
| if (lineNum <= 0) return -1;
|
|
347 |
0
| if (lineNum == 1) return 0;
|
|
348 |
| |
|
349 |
| |
|
350 |
0
| final int origPos = getCurrentLocation();
|
|
351 |
0
| try {
|
|
352 |
0
| final int docLen = getLength();
|
|
353 |
| |
|
354 |
0
| setCurrentLocation(0);
|
|
355 |
0
| int i;
|
|
356 |
0
| for (i = 1; (i < lineNum) && (_currentLocation < docLen); i++) {
|
|
357 |
0
| int dist = _reduced.getDistToNextNewline();
|
|
358 |
0
| if (_currentLocation + dist < docLen) dist++;
|
|
359 |
0
| move(dist);
|
|
360 |
| } |
|
361 |
0
| if (i == lineNum) return _currentLocation;
|
|
362 |
0
| else return -1;
|
|
363 |
| } |
|
364 |
0
| finally { setCurrentLocation(origPos); }
|
|
365 |
| |
|
366 |
| } |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
0
| public boolean tabsRemoved() { return _tabsRemoved; }
|
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
3
| public int commentLines(int selStart, int selEnd) {
|
|
377 |
| |
|
378 |
| |
|
379 |
3
| int toReturn = selEnd;
|
|
380 |
3
| if (selStart == selEnd) {
|
|
381 |
0
| setCurrentLocation(_getLineStartPos(selStart));
|
|
382 |
| |
|
383 |
0
| _commentLine();
|
|
384 |
0
| toReturn += WING_COMMENT_OFFSET;
|
|
385 |
| } |
|
386 |
3
| else toReturn = commentBlock(selStart, selEnd);
|
|
387 |
3
| _undoManager.endLastCompoundEdit();
|
|
388 |
3
| return toReturn;
|
|
389 |
| } |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
3
| private int commentBlock(final int start, final int end) {
|
|
397 |
3
| int afterCommentEnd = end;
|
|
398 |
3
| try {
|
|
399 |
| |
|
400 |
| |
|
401 |
3
| final Position endPos = this.createUnwrappedPosition(end);
|
|
402 |
| |
|
403 |
3
| int walker = _getLineStartPos(start);
|
|
404 |
3
| while (walker < endPos.getOffset()) {
|
|
405 |
11
| setCurrentLocation(walker);
|
|
406 |
| |
|
407 |
11
| _commentLine();
|
|
408 |
11
| afterCommentEnd += WING_COMMENT_OFFSET;
|
|
409 |
| |
|
410 |
11
| walker = walker + 2;
|
|
411 |
11
| setCurrentLocation(walker);
|
|
412 |
| |
|
413 |
| |
|
414 |
11
| walker += _reduced.getDistToNextNewline() + 1;
|
|
415 |
| } |
|
416 |
| } |
|
417 |
0
| catch (BadLocationException e) { throw new UnexpectedException(e); }
|
|
418 |
3
| return afterCommentEnd;
|
|
419 |
| } |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
11
| private void _commentLine() {
|
|
424 |
| |
|
425 |
| |
|
426 |
11
| try { insertString(_currentLocation, "//", null); }
|
|
427 |
0
| catch (BadLocationException e) { throw new UnexpectedException(e); }
|
|
428 |
| } |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
8
| public int uncommentLines(int selStart, int selEnd) {
|
|
435 |
| |
|
436 |
| |
|
437 |
8
| int toReturn = selEnd;
|
|
438 |
8
| if (selStart == selEnd) {
|
|
439 |
3
| try {
|
|
440 |
3
| setCurrentLocation(_getLineStartPos(selStart));
|
|
441 |
3
| _uncommentLine();
|
|
442 |
3
| toReturn -= WING_COMMENT_OFFSET;
|
|
443 |
| } |
|
444 |
0
| catch (BadLocationException e) { throw new UnexpectedException(e); }
|
|
445 |
| } |
|
446 |
5
| else toReturn = uncommentBlock(selStart, selEnd);
|
|
447 |
| |
|
448 |
8
| _undoManager.endLastCompoundEdit();
|
|
449 |
8
| return toReturn;
|
|
450 |
| } |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
5
| private int uncommentBlock(final int start, final int end) {
|
|
457 |
5
| int afterUncommentEnd = end;
|
|
458 |
5
| try {
|
|
459 |
| |
|
460 |
| |
|
461 |
5
| final Position endPos = this.createUnwrappedPosition(end);
|
|
462 |
| |
|
463 |
| |
|
464 |
5
| int walker = _getLineStartPos(start);
|
|
465 |
| |
|
466 |
5
| while (walker < endPos.getOffset()) {
|
|
467 |
18
| setCurrentLocation(walker);
|
|
468 |
18
| int diff = _uncommentLine();
|
|
469 |
18
| afterUncommentEnd -= diff;
|
|
470 |
18
| walker = _getLineEndPos(walker) + 1;
|
|
471 |
| |
|
472 |
| } |
|
473 |
| } |
|
474 |
0
| catch (BadLocationException e) { throw new UnexpectedException(e); }
|
|
475 |
5
| return afterUncommentEnd;
|
|
476 |
| } |
|
477 |
| |
|
478 |
| |
|
479 |
| |
|
480 |
| |
|
481 |
21
| private int _uncommentLine() throws BadLocationException {
|
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
21
| int pos1 = getText().indexOf("//", _currentLocation);
|
|
487 |
2
| if (pos1 < 0) return NO_COMMENT_OFFSET;
|
|
488 |
19
| int pos2 = getFirstNonWSCharPos(_currentLocation, true);
|
|
489 |
| |
|
490 |
6
| if (pos1 != pos2) return NO_COMMENT_OFFSET;
|
|
491 |
| |
|
492 |
13
| remove(pos1, 2);
|
|
493 |
13
| return WING_COMMENT_OFFSET;
|
|
494 |
| } |
|
495 |
| |
|
496 |
| |
|
497 |
2
| public void gotoLine(int line) {
|
|
498 |
| |
|
499 |
2
| int dist;
|
|
500 |
0
| if (line < 0) return;
|
|
501 |
2
| int actualLine = 1;
|
|
502 |
| |
|
503 |
2
| int len = getLength();
|
|
504 |
2
| setCurrentLocation(0);
|
|
505 |
2
| for (int i = 1; (i < line) && (_currentLocation < len); i++) {
|
|
506 |
3
| dist = _reduced.getDistToNextNewline();
|
|
507 |
3
| if (_currentLocation + dist < len) dist++;
|
|
508 |
3
| actualLine++;
|
|
509 |
3
| move(dist);
|
|
510 |
| } |
|
511 |
| } |
|
512 |
| |
|
513 |
| |
|
514 |
2406
| private int _findNextOpenCurly(String text, int pos) throws BadLocationException {
|
|
515 |
| |
|
516 |
2406
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
517 |
2406
| int i;
|
|
518 |
2406
| int reducedPos = pos;
|
|
519 |
| |
|
520 |
| |
|
521 |
2406
| final int origLocation = _currentLocation;
|
|
522 |
| |
|
523 |
2406
| _reduced.move(pos - origLocation);
|
|
524 |
| |
|
525 |
| |
|
526 |
2406
| i = text.indexOf('{', reducedPos);
|
|
527 |
2406
| while (i >- 1) {
|
|
528 |
| |
|
529 |
1591
| _reduced.move(i - reducedPos);
|
|
530 |
1591
| reducedPos = i;
|
|
531 |
| |
|
532 |
| |
|
533 |
1591
| ReducedModelState state = _reduced.getStateAtCurrent();
|
|
534 |
1591
| if (!state.equals(FREE) || _isStartOfComment(text, i)
|
|
535 |
| || ((i > 0) && _isStartOfComment(text, i - 1))) { |
|
536 |
0
| i = text.indexOf('{', reducedPos+1);
|
|
537 |
0
| continue;
|
|
538 |
| } |
|
539 |
| else { |
|
540 |
1591
| break;
|
|
541 |
| } |
|
542 |
| } |
|
543 |
2406
| _reduced.move(origLocation - reducedPos);
|
|
544 |
| |
|
545 |
| |
|
546 |
815
| if (i == -1) reducedPos = -1;
|
|
547 |
2406
| return reducedPos;
|
|
548 |
| } |
|
549 |
| |
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
18573
| public int _findPrevKeyword(String text, String kw, int pos) throws BadLocationException {
|
|
554 |
| |
|
555 |
18573
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
556 |
| |
|
557 |
18573
| int i;
|
|
558 |
18573
| int reducedPos = pos;
|
|
559 |
| |
|
560 |
| |
|
561 |
18573
| final int origLocation = _currentLocation;
|
|
562 |
| |
|
563 |
18573
| _reduced.move(pos - origLocation);
|
|
564 |
| |
|
565 |
| |
|
566 |
18573
| i = text.lastIndexOf(kw, reducedPos);
|
|
567 |
18573
| while (i >- 1) {
|
|
568 |
| |
|
569 |
15208
| if (i > 0) {
|
|
570 |
15208
| if (Character.isJavaIdentifierPart(text.charAt(i-1))) {
|
|
571 |
| |
|
572 |
0
| i = text.lastIndexOf(kw, i - 1);
|
|
573 |
0
| continue;
|
|
574 |
| } |
|
575 |
| } |
|
576 |
| |
|
577 |
15208
| if (i + kw.length() < text.length()) {
|
|
578 |
15208
| if (Character.isJavaIdentifierPart(text.charAt(i+kw.length()))) {
|
|
579 |
| |
|
580 |
0
| i = text.lastIndexOf(kw, i-1);
|
|
581 |
0
| continue;
|
|
582 |
| } |
|
583 |
| } |
|
584 |
| |
|
585 |
| |
|
586 |
15208
| _reduced.move(i - reducedPos);
|
|
587 |
15208
| reducedPos = i;
|
|
588 |
| |
|
589 |
| |
|
590 |
15208
| ReducedModelState state = _reduced.getStateAtCurrent();
|
|
591 |
15208
| if (!state.equals(FREE) || _isStartOfComment(text, i) || ((i > 0) && _isStartOfComment(text, i - 1))) {
|
|
592 |
0
| i = text.lastIndexOf(kw, reducedPos-1);
|
|
593 |
0
| continue;
|
|
594 |
| } |
|
595 |
15208
| else break;
|
|
596 |
| } |
|
597 |
| |
|
598 |
18573
| _reduced.move(origLocation - reducedPos);
|
|
599 |
| |
|
600 |
| |
|
601 |
3365
| if (i == -1) reducedPos = -1;
|
|
602 |
18573
| return reducedPos;
|
|
603 |
| } |
|
604 |
| |
|
605 |
| |
|
606 |
| |
|
607 |
| |
|
608 |
| |
|
609 |
| |
|
610 |
| |
|
611 |
| |
|
612 |
| |
|
613 |
168
| public String getEnclosingClassName(int pos, boolean qual) throws BadLocationException, ClassNameNotFoundException {
|
|
614 |
168
| return _getEnclosingClassName(pos, qual);
|
|
615 |
| } |
|
616 |
| |
|
617 |
| |
|
618 |
| |
|
619 |
| |
|
620 |
| |
|
621 |
| |
|
622 |
| |
|
623 |
| |
|
624 |
2171
| public String _getEnclosingClassName(final int pos, final boolean qual) throws BadLocationException,
|
|
625 |
| ClassNameNotFoundException { |
|
626 |
| |
|
627 |
2171
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
628 |
| |
|
629 |
| |
|
630 |
2171
| final Query key = new Query.EnclosingClassName(pos, qual);
|
|
631 |
2171
| final String cached = (String) _checkCache(key);
|
|
632 |
0
| if (cached != null) return cached;
|
|
633 |
| |
|
634 |
2171
| final char[] delims = {'{','}','(',')','[',']','+','-','/','*',';',':','=','!','@','#','$','%','^','~','\\','"','`','|'};
|
|
635 |
2171
| String name = "";
|
|
636 |
| |
|
637 |
2171
| final String text = getText(0, pos);
|
|
638 |
| |
|
639 |
2171
| int curPos = pos;
|
|
640 |
| |
|
641 |
2171
| do {
|
|
642 |
| |
|
643 |
| |
|
644 |
| |
|
645 |
| |
|
646 |
| |
|
647 |
5694
| curPos = findPrevEnclosingBrace(curPos, '{', '}');
|
|
648 |
2091
| if (curPos == -1) { break; }
|
|
649 |
3603
| int classPos = _findPrevKeyword(text, "class", curPos);
|
|
650 |
3603
| int interPos = _findPrevKeyword(text, "interface", curPos);
|
|
651 |
3603
| int otherPos = findPrevDelimiter(curPos, delims);
|
|
652 |
3603
| int newPos = -1;
|
|
653 |
| |
|
654 |
3603
| int closeParenPos = _findPrevNonWSCharPos(curPos);
|
|
655 |
3603
| if (closeParenPos != -1 && text.charAt(closeParenPos) == ')') {
|
|
656 |
| |
|
657 |
2653
| int openParenPos = findPrevEnclosingBrace(closeParenPos, '(', ')');
|
|
658 |
2653
| if (openParenPos != -1 && text.charAt(openParenPos) == '(') {
|
|
659 |
| |
|
660 |
2653
| newPos = _findPrevKeyword(text, "new", openParenPos);
|
|
661 |
| |
|
662 |
2653
| if (! _isAnonymousInnerClass(newPos, curPos)) {
|
|
663 |
| |
|
664 |
2608
| newPos = -1;
|
|
665 |
| } |
|
666 |
| } |
|
667 |
| } |
|
668 |
| |
|
669 |
6223
| while (classPos != -1 || interPos != -1 || newPos != -1) {
|
|
670 |
6223
| if (newPos != -1) {
|
|
671 |
584
| classPos = -1;
|
|
672 |
584
| interPos = -1;
|
|
673 |
584
| break;
|
|
674 |
| } |
|
675 |
5639
| else if (otherPos > classPos && otherPos > interPos) {
|
|
676 |
2620
| if (text.charAt(otherPos) != '{' || text.charAt(otherPos) != '}') ++otherPos;
|
|
677 |
2620
| curPos = findPrevEnclosingBrace(otherPos, '{', '}');
|
|
678 |
2620
| classPos = _findPrevKeyword(text, "class", curPos);
|
|
679 |
2620
| interPos = _findPrevKeyword(text, "interface", curPos);
|
|
680 |
2620
| otherPos = findPrevDelimiter(curPos, delims);
|
|
681 |
2620
| newPos = -1;
|
|
682 |
| |
|
683 |
2620
| closeParenPos = _findPrevNonWSCharPos(curPos);
|
|
684 |
2620
| if (closeParenPos != -1 && text.charAt(closeParenPos) == ')') {
|
|
685 |
| |
|
686 |
539
| int openParenPos = findPrevEnclosingBrace(closeParenPos, '(', ')');
|
|
687 |
539
| if (openParenPos != -1 && text.charAt(openParenPos) == '(') {
|
|
688 |
| |
|
689 |
539
| newPos = _findPrevKeyword(text, "new", openParenPos);
|
|
690 |
| |
|
691 |
0
| if (! _isAnonymousInnerClass(newPos, curPos)) newPos = -1;
|
|
692 |
| } |
|
693 |
| } |
|
694 |
| } |
|
695 |
| else { |
|
696 |
| |
|
697 |
3019
| curPos = Math.max(classPos, Math.max(interPos, newPos));
|
|
698 |
3019
| break;
|
|
699 |
| } |
|
700 |
| } |
|
701 |
| |
|
702 |
3603
| if (classPos != -1 || interPos != -1) {
|
|
703 |
3015
| if (classPos > interPos) curPos += "class".length();
|
|
704 |
4
| else curPos += "interface".length();
|
|
705 |
| |
|
706 |
3019
| int nameStart = getFirstNonWSCharPos(curPos);
|
|
707 |
0
| if (nameStart==-1) { throw new ClassNameNotFoundException("Cannot determine enclosing class name"); }
|
|
708 |
3019
| int nameEnd = nameStart + 1;
|
|
709 |
18365
| while (nameEnd < text.length()) {
|
|
710 |
3019
| if (! Character.isJavaIdentifierPart(text.charAt(nameEnd)) && text.charAt(nameEnd) != '.') break;
|
|
711 |
15346
| ++nameEnd;
|
|
712 |
| } |
|
713 |
3019
| name = text.substring(nameStart,nameEnd) + '$' + name;
|
|
714 |
| } |
|
715 |
584
| else if (newPos != -1) {
|
|
716 |
584
| name = String.valueOf(_getAnonymousInnerClassIndex(curPos)) + "$" + name;
|
|
717 |
584
| curPos = newPos;
|
|
718 |
| } |
|
719 |
0
| else break;
|
|
720 |
3603
| } while(qual);
|
|
721 |
| |
|
722 |
| |
|
723 |
2163
| if (name.length() > 0) name = name.substring(0, name.length() - 1);
|
|
724 |
| |
|
725 |
2171
| if (qual) {
|
|
726 |
2087
| String pn = getPackageName();
|
|
727 |
2087
| if ((pn.length() > 0) && (name.length() > 0)) {
|
|
728 |
2083
| name = getPackageName() + "." + name;
|
|
729 |
| } |
|
730 |
| } |
|
731 |
| |
|
732 |
2171
| _storeInCache(key, name, pos);
|
|
733 |
2171
| return name;
|
|
734 |
| } |
|
735 |
| |
|
736 |
| |
|
737 |
| |
|
738 |
| |
|
739 |
| |
|
740 |
| |
|
741 |
| |
|
742 |
4783
| public boolean _isAnonymousInnerClass(final int pos, final int openCurlyPos) throws BadLocationException {
|
|
743 |
| |
|
744 |
| |
|
745 |
| |
|
746 |
| |
|
747 |
| |
|
748 |
| |
|
749 |
4783
| final Query key = new Query.AnonymousInnerClass(pos, openCurlyPos);
|
|
750 |
4783
| Boolean cached = (Boolean) _checkCache(key);
|
|
751 |
4783
| if (cached != null) {
|
|
752 |
| |
|
753 |
0
| return cached;
|
|
754 |
| } |
|
755 |
4783
| int newPos = pos;
|
|
756 |
| |
|
757 |
4783
| cached = false;
|
|
758 |
| |
|
759 |
4783
| String text = getText(0, openCurlyPos + 1);
|
|
760 |
4783
| newPos += "new".length();
|
|
761 |
4783
| int classStart = getFirstNonWSCharPos(newPos);
|
|
762 |
4783
| if (classStart != -1) {
|
|
763 |
4783
| int classEnd = classStart + 1;
|
|
764 |
33545
| while (classEnd < text.length()) {
|
|
765 |
33545
| if (! Character.isJavaIdentifierPart(text.charAt(classEnd)) && text.charAt(classEnd) != '.') {
|
|
766 |
| |
|
767 |
4783
| break;
|
|
768 |
| } |
|
769 |
28762
| ++classEnd;
|
|
770 |
| } |
|
771 |
| |
|
772 |
| |
|
773 |
| |
|
774 |
4783
| int parenStart = getFirstNonWSCharPos(classEnd);
|
|
775 |
4783
| if (parenStart != -1) {
|
|
776 |
4783
| int origParenStart = parenStart;
|
|
777 |
| |
|
778 |
| |
|
779 |
4783
| if (text.charAt(origParenStart) == '<') {
|
|
780 |
1111
| parenStart = -1;
|
|
781 |
| |
|
782 |
1111
| int closePointyBracket = findNextEnclosingBrace(origParenStart, '<', '>');
|
|
783 |
1111
| if (closePointyBracket != -1) {
|
|
784 |
1111
| if (text.charAt(closePointyBracket) == '>') {
|
|
785 |
1111
| parenStart = getFirstNonWSCharPos(closePointyBracket+1);
|
|
786 |
| } |
|
787 |
| } |
|
788 |
| } |
|
789 |
| } |
|
790 |
| |
|
791 |
4783
| if (parenStart != -1) {
|
|
792 |
4783
| if (text.charAt(parenStart) == '(') {
|
|
793 |
3970
| setCurrentLocation(parenStart + 1);
|
|
794 |
3970
| int parenEnd = balanceForward();
|
|
795 |
3970
| if (parenEnd > -1) {
|
|
796 |
3970
| parenEnd = parenEnd + parenStart + 1;
|
|
797 |
| |
|
798 |
3970
| int afterParen = getFirstNonWSCharPos(parenEnd);
|
|
799 |
| |
|
800 |
3970
| cached = (afterParen == openCurlyPos);
|
|
801 |
| } |
|
802 |
| } |
|
803 |
| } |
|
804 |
| } |
|
805 |
4783
| _storeInCache(key, cached, openCurlyPos);
|
|
806 |
| |
|
807 |
4783
| return cached;
|
|
808 |
| |
|
809 |
| } |
|
810 |
| |
|
811 |
| |
|
812 |
| |
|
813 |
| |
|
814 |
| |
|
815 |
| |
|
816 |
4798
| public String getPackageName() {
|
|
817 |
| |
|
818 |
4798
| Reader r;
|
|
819 |
4798
| r = new StringReader(getText());
|
|
820 |
4798
| try { return new Parser(r).packageDeclaration(Parser.DeclType.TOP).getName(); }
|
|
821 |
599
| catch (ParseException e) { return ""; }
|
|
822 |
| |
|
823 |
| |
|
824 |
0
| catch (TokenMgrError e) { return ""; }
|
|
825 |
| catch (Error e) { |
|
826 |
| |
|
827 |
0
| String msg = e.getMessage();
|
|
828 |
0
| if (msg != null && msg.startsWith("Invalid escape character")) {
|
|
829 |
0
| return "";
|
|
830 |
| } |
|
831 |
0
| else { throw e; }
|
|
832 |
| } |
|
833 |
| finally { |
|
834 |
4798
| try { r.close(); }
|
|
835 |
| catch (IOException e) { } |
|
836 |
| } |
|
837 |
| } |
|
838 |
| |
|
839 |
| |
|
840 |
| |
|
841 |
| |
|
842 |
| |
|
843 |
| |
|
844 |
589
| int _getAnonymousInnerClassIndex(final int pos) throws BadLocationException, ClassNameNotFoundException {
|
|
845 |
| |
|
846 |
| |
|
847 |
589
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
848 |
| |
|
849 |
| |
|
850 |
589
| final Query key = new Query.AnonymousInnerClassIndex(pos);
|
|
851 |
589
| final Integer cached = (Integer) _checkCache(key);
|
|
852 |
589
| if (cached != null) {
|
|
853 |
| |
|
854 |
0
| return cached.intValue();
|
|
855 |
| } |
|
856 |
| |
|
857 |
589
| int newPos = pos;
|
|
858 |
| |
|
859 |
| |
|
860 |
| |
|
861 |
589
| final String className = _getEnclosingClassName(newPos - 2 , true);
|
|
862 |
589
| final String text = getText(0, newPos - 2);
|
|
863 |
589
| int index = 1;
|
|
864 |
| |
|
865 |
| |
|
866 |
?
| while ((newPos = _findPrevKeyword(text, "new", newPos - 4)) != -1) {
|
|
867 |
| |
|
868 |
2406
| int afterNewPos = newPos + "new".length();
|
|
869 |
2406
| int classStart = getFirstNonWSCharPos(afterNewPos);
|
|
870 |
0
| if (classStart == -1) { continue; }
|
|
871 |
2406
| int classEnd = classStart + 1;
|
|
872 |
17842
| while (classEnd < text.length()) {
|
|
873 |
17842
| if (! Character.isJavaIdentifierPart(text.charAt(classEnd)) && text.charAt(classEnd) != '.') {
|
|
874 |
| |
|
875 |
2406
| break;
|
|
876 |
| } |
|
877 |
15436
| ++classEnd;
|
|
878 |
| } |
|
879 |
| |
|
880 |
2406
| int parenStart = getFirstNonWSCharPos(classEnd);
|
|
881 |
0
| if (parenStart == -1) { continue; }
|
|
882 |
2406
| int origParenStart = parenStart;
|
|
883 |
| |
|
884 |
| |
|
885 |
2406
| if (text.charAt(origParenStart) == '<') {
|
|
886 |
441
| parenStart = -1;
|
|
887 |
| |
|
888 |
441
| int closePointyBracket = findNextEnclosingBrace(origParenStart, '<', '>');
|
|
889 |
441
| if (closePointyBracket != -1) {
|
|
890 |
441
| if (text.charAt(closePointyBracket) == '>') {
|
|
891 |
441
| parenStart = getFirstNonWSCharPos(closePointyBracket + 1);
|
|
892 |
| } |
|
893 |
| } |
|
894 |
| } |
|
895 |
0
| if (parenStart == -1) { continue; }
|
|
896 |
0
| if (text.charAt(parenStart) != '(') { continue; }
|
|
897 |
2406
| int parenEnd = findNextEnclosingBrace(parenStart, '(', ')');
|
|
898 |
| |
|
899 |
2406
| int nextOpenCurly = _findNextOpenCurly(text, parenEnd);
|
|
900 |
815
| if (nextOpenCurly == -1) { continue; }
|
|
901 |
| |
|
902 |
| |
|
903 |
| |
|
904 |
1591
| if (_isAnonymousInnerClass(newPos, nextOpenCurly)) {
|
|
905 |
| |
|
906 |
1414
| String cn = _getEnclosingClassName(newPos, true);
|
|
907 |
| |
|
908 |
60
| if (! cn.startsWith(className)) { break; }
|
|
909 |
1354
| else if (! cn.equals(className)) {
|
|
910 |
704
| newPos = findPrevEnclosingBrace(newPos, '{', '}');
|
|
911 |
704
| continue;
|
|
912 |
| } |
|
913 |
650
| else ++index;
|
|
914 |
| } |
|
915 |
| } |
|
916 |
589
| _storeInCache(key, index, pos);
|
|
917 |
| |
|
918 |
589
| return index;
|
|
919 |
| } |
|
920 |
| |
|
921 |
| |
|
922 |
| |
|
923 |
| |
|
924 |
| |
|
925 |
0
| public String getEnclosingTopLevelClassName(int pos) throws ClassNameNotFoundException {
|
|
926 |
0
| int oldPos = _currentLocation;
|
|
927 |
0
| try {
|
|
928 |
0
| setCurrentLocation(pos);
|
|
929 |
0
| BraceInfo info = _getEnclosingBrace();
|
|
930 |
| |
|
931 |
| |
|
932 |
0
| int topLevelBracePos = -1;
|
|
933 |
0
| String braceType = info.braceType();
|
|
934 |
0
| while (! braceType.equals(BraceInfo.NONE)) {
|
|
935 |
0
| if (braceType.equals(BraceInfo.OPEN_CURLY)) {
|
|
936 |
0
| topLevelBracePos = _currentLocation - info.distance();
|
|
937 |
| } |
|
938 |
0
| move(-info.distance());
|
|
939 |
0
| info = _getEnclosingBrace();
|
|
940 |
0
| braceType = info.braceType();
|
|
941 |
| } |
|
942 |
0
| if (topLevelBracePos == -1) {
|
|
943 |
| |
|
944 |
0
| setCurrentLocation(oldPos);
|
|
945 |
0
| throw new ClassNameNotFoundException("no top level brace found");
|
|
946 |
| } |
|
947 |
| |
|
948 |
0
| char[] delims = {'{', '}', ';'};
|
|
949 |
0
| int prevDelimPos = findPrevDelimiter(topLevelBracePos, delims);
|
|
950 |
0
| if (prevDelimPos == -1) {
|
|
951 |
| |
|
952 |
0
| prevDelimPos = 0;
|
|
953 |
| } |
|
954 |
0
| else prevDelimPos++;
|
|
955 |
0
| setCurrentLocation(oldPos);
|
|
956 |
| |
|
957 |
| |
|
958 |
0
| return getNextTopLevelClassName(prevDelimPos, topLevelBracePos);
|
|
959 |
| } |
|
960 |
0
| catch (BadLocationException ble) { throw new UnexpectedException(ble); }
|
|
961 |
0
| finally { setCurrentLocation(oldPos); }
|
|
962 |
| } |
|
963 |
| |
|
964 |
| |
|
965 |
| |
|
966 |
| |
|
967 |
| |
|
968 |
| |
|
969 |
133
| private String getFirstClassName(int indexOfClass, int indexOfInterface,
|
|
970 |
| int indexOfEnum) throws ClassNameNotFoundException { |
|
971 |
133
| try {
|
|
972 |
2
| if ((indexOfClass == -1) && (indexOfInterface == -1) && (indexOfEnum == -1)) throw ClassNameNotFoundException.DEFAULT;
|
|
973 |
| |
|
974 |
| |
|
975 |
| |
|
976 |
131
| if ((indexOfEnum == -1) ||
|
|
977 |
| ((indexOfClass != -1) && (indexOfClass < indexOfEnum)) || |
|
978 |
| ((indexOfInterface != -1) && (indexOfInterface < indexOfEnum))) { |
|
979 |
| |
|
980 |
| |
|
981 |
| |
|
982 |
125
| if ((indexOfInterface == -1) ||
|
|
983 |
| ((indexOfClass != -1) && (indexOfClass < indexOfInterface))) { |
|
984 |
| |
|
985 |
116
| return getNextIdentifier(indexOfClass + "class".length());
|
|
986 |
| } |
|
987 |
| else { |
|
988 |
| |
|
989 |
9
| return getNextIdentifier(indexOfInterface + "interface".length());
|
|
990 |
| } |
|
991 |
| } |
|
992 |
| else { |
|
993 |
| |
|
994 |
6
| return getNextIdentifier(indexOfEnum + "enum".length());
|
|
995 |
| } |
|
996 |
| } |
|
997 |
0
| catch(IllegalStateException ise) { throw ClassNameNotFoundException.DEFAULT; }
|
|
998 |
| } |
|
999 |
| |
|
1000 |
| |
|
1001 |
| |
|
1002 |
133
| public String getMainClassName() throws ClassNameNotFoundException {
|
|
1003 |
133
| final int oldPos = _currentLocation;
|
|
1004 |
| |
|
1005 |
133
| try {
|
|
1006 |
133
| setCurrentLocation(0);
|
|
1007 |
133
| final String text = getText();
|
|
1008 |
| |
|
1009 |
133
| final int indexOfClass = _findKeywordAtToplevel("class", text, 0);
|
|
1010 |
133
| final int indexOfInterface = _findKeywordAtToplevel("interface", text, 0);
|
|
1011 |
133
| final int indexOfEnum = _findKeywordAtToplevel("enum", text, 0);
|
|
1012 |
133
| final int indexOfPublic = _findKeywordAtToplevel("public", text, 0);
|
|
1013 |
| |
|
1014 |
75
| if (indexOfPublic == -1) return getFirstClassName(indexOfClass, indexOfInterface, indexOfEnum);
|
|
1015 |
| |
|
1016 |
| |
|
1017 |
| |
|
1018 |
| |
|
1019 |
| |
|
1020 |
58
| final int afterPublic = indexOfPublic + "public".length();
|
|
1021 |
58
| final String subText = text.substring(afterPublic);
|
|
1022 |
58
| setCurrentLocation(afterPublic);
|
|
1023 |
| |
|
1024 |
58
| int indexOfPublicClass = _findKeywordAtToplevel("class", subText, afterPublic);
|
|
1025 |
54
| if (indexOfPublicClass != -1) indexOfPublicClass += afterPublic;
|
|
1026 |
58
| int indexOfPublicInterface = _findKeywordAtToplevel("interface", subText, afterPublic);
|
|
1027 |
11
| if (indexOfPublicInterface != -1) indexOfPublicInterface += afterPublic;
|
|
1028 |
58
| int indexOfPublicEnum = _findKeywordAtToplevel("enum", subText, afterPublic);
|
|
1029 |
4
| if (indexOfPublicEnum != -1) indexOfPublicEnum += afterPublic;
|
|
1030 |
| |
|
1031 |
| |
|
1032 |
58
| return getFirstClassName(indexOfPublicClass, indexOfPublicInterface, indexOfPublicEnum);
|
|
1033 |
| |
|
1034 |
| } |
|
1035 |
133
| finally { setCurrentLocation(oldPos); }
|
|
1036 |
| } |
|
1037 |
| |
|
1038 |
| |
|
1039 |
| |
|
1040 |
| |
|
1041 |
| |
|
1042 |
0
| public String getFirstTopLevelClassName() throws ClassNameNotFoundException {
|
|
1043 |
0
| return getNextTopLevelClassName(0, getLength());
|
|
1044 |
| } |
|
1045 |
| |
|
1046 |
| |
|
1047 |
0
| public String getNextTopLevelClassName(int startPos, int endPos) throws ClassNameNotFoundException {
|
|
1048 |
0
| int oldPos = _currentLocation;
|
|
1049 |
| |
|
1050 |
0
| try {
|
|
1051 |
0
| setCurrentLocation(startPos);
|
|
1052 |
0
| final int textLength = endPos - startPos;
|
|
1053 |
0
| final String text = getText(startPos, textLength);
|
|
1054 |
| |
|
1055 |
0
| int index;
|
|
1056 |
| |
|
1057 |
0
| int indexOfClass = _findKeywordAtToplevel("class", text, startPos);
|
|
1058 |
0
| int indexOfInterface = _findKeywordAtToplevel("interface", text, startPos);
|
|
1059 |
0
| int indexOfEnum = _findKeywordAtToplevel("enum",text,startPos);
|
|
1060 |
| |
|
1061 |
| |
|
1062 |
| |
|
1063 |
0
| if (indexOfClass > -1 && (indexOfInterface <= -1 || indexOfClass < indexOfInterface)
|
|
1064 |
| && (indexOfEnum <= -1 || indexOfClass < indexOfEnum)) { |
|
1065 |
0
| index = indexOfClass + "class".length();
|
|
1066 |
| } |
|
1067 |
0
| else if (indexOfInterface > -1 && (indexOfClass <= -1 || indexOfInterface < indexOfClass)
|
|
1068 |
| && (indexOfEnum <= -1 || indexOfInterface < indexOfEnum)) { |
|
1069 |
0
| index = indexOfInterface + "interface".length();
|
|
1070 |
| } |
|
1071 |
0
| else if (indexOfEnum > -1 && (indexOfClass <= -1 || indexOfEnum < indexOfClass)
|
|
1072 |
| && (indexOfInterface <= -1 || indexOfEnum < indexOfInterface)) { |
|
1073 |
0
| index = indexOfEnum + "enum".length();
|
|
1074 |
| } |
|
1075 |
| else { |
|
1076 |
| |
|
1077 |
0
| throw ClassNameNotFoundException.DEFAULT;
|
|
1078 |
| } |
|
1079 |
| |
|
1080 |
| |
|
1081 |
0
| return getNextIdentifier(startPos + index);
|
|
1082 |
| } |
|
1083 |
0
| catch (BadLocationException ble) { throw new UnexpectedException(ble); }
|
|
1084 |
0
| catch (IllegalStateException e) { throw new ClassNameNotFoundException("No top level class name found"); }
|
|
1085 |
0
| finally { setCurrentLocation(oldPos); }
|
|
1086 |
| } |
|
1087 |
| |
|
1088 |
| |
|
1089 |
| |
|
1090 |
131
| private String getNextIdentifier(final int startPos) throws ClassNameNotFoundException {
|
|
1091 |
| |
|
1092 |
| |
|
1093 |
| |
|
1094 |
| |
|
1095 |
| |
|
1096 |
| |
|
1097 |
| |
|
1098 |
| |
|
1099 |
131
| try {
|
|
1100 |
| |
|
1101 |
131
| int index = getFirstNonWSCharPos(startPos);
|
|
1102 |
0
| if (index == -1) throw new IllegalStateException("No identifier found");
|
|
1103 |
| |
|
1104 |
131
| String text = getText();
|
|
1105 |
131
| int length = text.length();
|
|
1106 |
131
| int endIndex = length;
|
|
1107 |
| |
|
1108 |
| |
|
1109 |
| |
|
1110 |
| |
|
1111 |
| |
|
1112 |
131
| char c;
|
|
1113 |
1468
| for (int i = index; i < length; i++) {
|
|
1114 |
1468
| c = text.charAt(i);
|
|
1115 |
1468
| if (! Character.isJavaIdentifierPart(c)) {
|
|
1116 |
131
| endIndex = i;
|
|
1117 |
131
| break;
|
|
1118 |
| } |
|
1119 |
| } |
|
1120 |
| |
|
1121 |
131
| return text.substring(index, endIndex);
|
|
1122 |
| } |
|
1123 |
| catch(BadLocationException e) { |
|
1124 |
| |
|
1125 |
| |
|
1126 |
| |
|
1127 |
0
| throw new UnexpectedException(e);
|
|
1128 |
| } |
|
1129 |
| } |
|
1130 |
| |
|
1131 |
| |
|
1132 |
| |
|
1133 |
| |
|
1134 |
| |
|
1135 |
| |
|
1136 |
| |
|
1137 |
| |
|
1138 |
706
| private int _findKeywordAtToplevel(String keyword, String text, int textOffset) {
|
|
1139 |
| |
|
1140 |
706
| int oldPos = _currentLocation;
|
|
1141 |
706
| int index = 0;
|
|
1142 |
706
| while (true) {
|
|
1143 |
707
| index = text.indexOf(keyword, index);
|
|
1144 |
382
| if (index == -1) break;
|
|
1145 |
| else { |
|
1146 |
| |
|
1147 |
325
| setCurrentLocation(textOffset + index);
|
|
1148 |
| |
|
1149 |
| |
|
1150 |
325
| int indexPastKeyword = index + keyword.length();
|
|
1151 |
325
| if (indexPastKeyword < text.length()) {
|
|
1152 |
325
| if (! isShadowed() && Character.isWhitespace(text.charAt(indexPastKeyword))) {
|
|
1153 |
| |
|
1154 |
60
| if (! notInBlock(index)) index = -1;
|
|
1155 |
324
| break;
|
|
1156 |
| } |
|
1157 |
1
| else index++;
|
|
1158 |
| } |
|
1159 |
| else { |
|
1160 |
0
| index = -1;
|
|
1161 |
0
| break;
|
|
1162 |
| } |
|
1163 |
| } |
|
1164 |
| } |
|
1165 |
706
| setCurrentLocation(oldPos);
|
|
1166 |
| |
|
1167 |
706
| return index;
|
|
1168 |
| } |
|
1169 |
| |
|
1170 |
| |
|
1171 |
| public static class WrappedPosition implements Position { |
|
1172 |
| private Position _wrapped; |
|
1173 |
| |
|
1174 |
5871
| WrappedPosition(Position w) { setWrapped(w); }
|
|
1175 |
5871
| public void setWrapped(Position w) { _wrapped = w; }
|
|
1176 |
42265
| public int getOffset() { return _wrapped.getOffset(); }
|
|
1177 |
| } |
|
1178 |
| |
|
1179 |
| |
|
1180 |
| |
|
1181 |
| |
|
1182 |
5871
| public Position createPosition(final int offset) throws BadLocationException {
|
|
1183 |
| |
|
1184 |
| |
|
1185 |
| |
|
1186 |
| |
|
1187 |
| |
|
1188 |
| |
|
1189 |
| |
|
1190 |
| |
|
1191 |
5871
| WrappedPosition wp = new WrappedPosition(createUnwrappedPosition(offset));
|
|
1192 |
5871
| synchronized(_wrappedPosListLock) {
|
|
1193 |
448
| if (_wrappedPosList == null) _wrappedPosList = new LinkedList<WeakReference<WrappedPosition>>();
|
|
1194 |
5871
| _wrappedPosList.add(new WeakReference<WrappedPosition>(wp));
|
|
1195 |
| } |
|
1196 |
5871
| return wp;
|
|
1197 |
| } |
|
1198 |
| |
|
1199 |
| |
|
1200 |
| |
|
1201 |
| |
|
1202 |
| |
|
1203 |
29
| public WeakHashMap<WrappedPosition, Integer> getWrappedPositionOffsets() {
|
|
1204 |
29
| LinkedList<WeakReference<WrappedPosition>> newList = new LinkedList<WeakReference<WrappedPosition>>();
|
|
1205 |
29
| synchronized(_wrappedPosListLock) {
|
|
1206 |
0
| if (_wrappedPosList == null) { _wrappedPosList = new LinkedList<WeakReference<WrappedPosition>>(); }
|
|
1207 |
29
| WeakHashMap<WrappedPosition, Integer> ret = new WeakHashMap<WrappedPosition, Integer>(_wrappedPosList.size());
|
|
1208 |
| |
|
1209 |
29
| for (WeakReference<WrappedPosition> wr: _wrappedPosList) {
|
|
1210 |
0
| if (wr.get() != null) {
|
|
1211 |
| |
|
1212 |
0
| newList.add(wr);
|
|
1213 |
0
| ret.put(wr.get(), wr.get().getOffset());
|
|
1214 |
| } |
|
1215 |
| } |
|
1216 |
29
| _wrappedPosList.clear();
|
|
1217 |
29
| _wrappedPosList = newList;
|
|
1218 |
29
| return ret;
|
|
1219 |
| } |
|
1220 |
| } |
|
1221 |
| |
|
1222 |
| |
|
1223 |
| |
|
1224 |
| |
|
1225 |
343
| public void setWrappedPositionOffsets(WeakHashMap<WrappedPosition, Integer> whm) throws BadLocationException {
|
|
1226 |
343
| synchronized(_wrappedPosListLock) {
|
|
1227 |
0
| if (_wrappedPosList == null) { _wrappedPosList = new LinkedList<WeakReference<WrappedPosition>>(); }
|
|
1228 |
343
| _wrappedPosList.clear();
|
|
1229 |
| |
|
1230 |
343
| for(Map.Entry<WrappedPosition, Integer> entry: whm.entrySet()) {
|
|
1231 |
0
| if (entry.getKey() != null) {
|
|
1232 |
| |
|
1233 |
0
| WrappedPosition wp = entry.getKey();
|
|
1234 |
0
| wp.setWrapped(createUnwrappedPosition(entry.getValue()));
|
|
1235 |
0
| _wrappedPosList.add(new WeakReference<WrappedPosition>(wp));
|
|
1236 |
| } |
|
1237 |
| } |
|
1238 |
| } |
|
1239 |
| } |
|
1240 |
| |
|
1241 |
| |
|
1242 |
| private static class CommandUndoableEdit extends AbstractUndoableEdit { |
|
1243 |
| private final Runnable _undoCommand; |
|
1244 |
| private final Runnable _redoCommand; |
|
1245 |
| |
|
1246 |
710
| public CommandUndoableEdit(final Runnable undoCommand, final Runnable redoCommand) {
|
|
1247 |
710
| _undoCommand = undoCommand;
|
|
1248 |
710
| _redoCommand = redoCommand;
|
|
1249 |
| } |
|
1250 |
| |
|
1251 |
23
| public void undo() throws CannotUndoException {
|
|
1252 |
23
| super.undo();
|
|
1253 |
23
| _undoCommand.run();
|
|
1254 |
| } |
|
1255 |
| |
|
1256 |
19
| public void redo() throws CannotRedoException {
|
|
1257 |
19
| super.redo();
|
|
1258 |
19
| _redoCommand.run();
|
|
1259 |
| } |
|
1260 |
| |
|
1261 |
0
| public boolean isSignificant() { return false; }
|
|
1262 |
| } |
|
1263 |
| |
|
1264 |
| |
|
1265 |
| |
|
1266 |
| |
|
1267 |
438
| public CompoundUndoManager getUndoManager() { return _undoManager; }
|
|
1268 |
| |
|
1269 |
| |
|
1270 |
118
| public void resetUndoManager() {
|
|
1271 |
118
| _undoManager = new CompoundUndoManager(_notifier);
|
|
1272 |
118
| _undoManager.setLimit(UNDO_LIMIT);
|
|
1273 |
| } |
|
1274 |
| |
|
1275 |
| |
|
1276 |
0
| public UndoableEdit getNextUndo() { return _undoManager.getNextUndo(); }
|
|
1277 |
| |
|
1278 |
| |
|
1279 |
0
| public UndoableEdit getNextRedo() { return _undoManager.getNextRedo(); }
|
|
1280 |
| |
|
1281 |
| |
|
1282 |
0
| public void documentSaved() { _undoManager.documentSaved(); }
|
|
1283 |
| |
|
1284 |
0
| protected int startCompoundEdit() { return _undoManager.startCompoundEdit(); }
|
|
1285 |
| |
|
1286 |
0
| protected void endCompoundEdit(int key) { _undoManager.endCompoundEdit(key); }
|
|
1287 |
| |
|
1288 |
| |
|
1289 |
66
| protected void endLastCompoundEdit() { _undoManager.endLastCompoundEdit(); }
|
|
1290 |
| |
|
1291 |
710
| protected void addUndoRedo(AbstractDocument.DefaultDocumentEvent chng, Runnable undoCommand, Runnable doCommand) {
|
|
1292 |
710
| chng.addEdit(new CommandUndoableEdit(undoCommand, doCommand));
|
|
1293 |
| } |
|
1294 |
| |
|
1295 |
| |
|
1296 |
| |
|
1297 |
| |
|
1298 |
| |
|
1299 |
| |
|
1300 |
| |
|
1301 |
| |
|
1302 |
| |
|
1303 |
| |
|
1304 |
| |
|
1305 |
| |
|
1306 |
| |
|
1307 |
| |
|
1308 |
| |
|
1309 |
| |
|
1310 |
| |
|
1311 |
| |
|
1312 |
| |
|
1313 |
| |
|
1314 |
| |
|
1315 |
| |
|
1316 |
| |
|
1317 |
| |
|
1318 |
| |
|
1319 |
| |
|
1320 |
| |
|
1321 |
| |
|
1322 |
| |
|
1323 |
| |
|
1324 |
| |
|
1325 |
| |
|
1326 |
| |
|
1327 |
| |
|
1328 |
| |
|
1329 |
| |
|
1330 |
| |
|
1331 |
| |
|
1332 |
| |
|
1333 |
| |
|
1334 |
| |
|
1335 |
| |
|
1336 |
| |
|
1337 |
| |
|
1338 |
| |
|
1339 |
| |
|
1340 |
| |
|
1341 |
| |
|
1342 |
| |
|
1343 |
| |
|
1344 |
| |
|
1345 |
| |
|
1346 |
| |
|
1347 |
| |
|
1348 |
| |
|
1349 |
| |
|
1350 |
| |
|
1351 |
| |
|
1352 |
| |
|
1353 |
| |
|
1354 |
| |
|
1355 |
| |
|
1356 |
| |
|
1357 |
| |
|
1358 |
| |
|
1359 |
| |
|
1360 |
| |
|
1361 |
| |
|
1362 |
| |
|
1363 |
| |
|
1364 |
| |
|
1365 |
| |
|
1366 |
| |
|
1367 |
| |
|
1368 |
| private List<FinalizationListener<DefinitionsDocument>> _finalizationListeners = |
|
1369 |
| new LinkedList<FinalizationListener<DefinitionsDocument>>(); |
|
1370 |
| |
|
1371 |
| |
|
1372 |
| |
|
1373 |
| |
|
1374 |
| |
|
1375 |
| |
|
1376 |
| |
|
1377 |
16
| public void addFinalizationListener(FinalizationListener<DefinitionsDocument> fl) {
|
|
1378 |
16
| synchronized(_finalizationListeners) { _finalizationListeners.add(fl); }
|
|
1379 |
| } |
|
1380 |
| |
|
1381 |
30
| public List<FinalizationListener<DefinitionsDocument>> getFinalizationListeners() {
|
|
1382 |
30
| return _finalizationListeners;
|
|
1383 |
| } |
|
1384 |
| |
|
1385 |
| |
|
1386 |
| |
|
1387 |
| |
|
1388 |
83
| protected void finalize() {
|
|
1389 |
83
| FinalizationEvent<DefinitionsDocument> fe = new FinalizationEvent<DefinitionsDocument>(this);
|
|
1390 |
83
| synchronized(_finalizationListeners) {
|
|
1391 |
83
| for (FinalizationListener<DefinitionsDocument> fl: _finalizationListeners) {
|
|
1392 |
12
| fl.finalized(fe);
|
|
1393 |
| } |
|
1394 |
| } |
|
1395 |
| } |
|
1396 |
| |
|
1397 |
0
| public String toString() { return "ddoc for " + _odd; }
|
|
1398 |
| |
|
1399 |
| |
|
1400 |
| |
|
1401 |
28
| public boolean containsClassOrInterfaceOrEnum() throws BadLocationException {
|
|
1402 |
| |
|
1403 |
28
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
1404 |
28
| int i, j;
|
|
1405 |
28
| int reducedPos = 0;
|
|
1406 |
| |
|
1407 |
28
| final String text = getText();
|
|
1408 |
28
| final int origLocation = _currentLocation;
|
|
1409 |
28
| try {
|
|
1410 |
| |
|
1411 |
28
| _reduced.move(-origLocation);
|
|
1412 |
| |
|
1413 |
| |
|
1414 |
28
| i = text.indexOf("class", reducedPos);
|
|
1415 |
28
| j = text.indexOf("interface", reducedPos);
|
|
1416 |
0
| if (i==-1) i = j; else if (j >= 0) i = Math.min(i,j);
|
|
1417 |
28
| j = text.indexOf("enum", reducedPos);
|
|
1418 |
0
| if (i==-1) i = j; else if (j >= 0) i = Math.min(i,j);
|
|
1419 |
28
| while (i > - 1) {
|
|
1420 |
| |
|
1421 |
28
| _reduced.move(i - reducedPos);
|
|
1422 |
28
| reducedPos = i;
|
|
1423 |
| |
|
1424 |
| |
|
1425 |
28
| ReducedModelState state = _reduced.getStateAtCurrent();
|
|
1426 |
28
| if (!state.equals(FREE) || _isStartOfComment(text, i) || ((i > 0) && _isStartOfComment(text, i - 1))) {
|
|
1427 |
15
| i = text.indexOf("class", reducedPos+1);
|
|
1428 |
15
| j = text.indexOf("interface", reducedPos+1);
|
|
1429 |
0
| if (i==-1) i = j; else if (j >= 0) i = Math.min(i,j);
|
|
1430 |
15
| j = text.indexOf("enum", reducedPos+1);
|
|
1431 |
0
| if (i==-1) i = j; else if (j >= 0) i = Math.min(i,j);
|
|
1432 |
15
| continue;
|
|
1433 |
| } |
|
1434 |
| else { |
|
1435 |
13
| return true;
|
|
1436 |
| } |
|
1437 |
| } |
|
1438 |
| |
|
1439 |
15
| return false;
|
|
1440 |
| } |
|
1441 |
| finally { |
|
1442 |
28
| _reduced.move(origLocation - reducedPos);
|
|
1443 |
| } |
|
1444 |
| } |
|
1445 |
| } |