Lines 20-32
Link Here
|
20 |
import org.eclipse.swt.graphics.Rectangle; |
20 |
import org.eclipse.swt.graphics.Rectangle; |
21 |
|
21 |
|
22 |
import org.eclipse.jface.text.BadLocationException; |
22 |
import org.eclipse.jface.text.BadLocationException; |
|
|
23 |
import org.eclipse.jface.text.DocumentEvent; |
23 |
import org.eclipse.jface.text.IDocument; |
24 |
import org.eclipse.jface.text.IDocument; |
24 |
import org.eclipse.jface.text.IPaintPositionManager; |
25 |
import org.eclipse.jface.text.IPaintPositionManager; |
25 |
import org.eclipse.jface.text.IPainter; |
26 |
import org.eclipse.jface.text.IPainter; |
26 |
import org.eclipse.jface.text.IRegion; |
27 |
import org.eclipse.jface.text.IRegion; |
|
|
28 |
import org.eclipse.jface.text.ITextInputListener; |
29 |
import org.eclipse.jface.text.ITextListener; |
27 |
import org.eclipse.jface.text.ITextViewerExtension5; |
30 |
import org.eclipse.jface.text.ITextViewerExtension5; |
28 |
import org.eclipse.jface.text.Position; |
31 |
import org.eclipse.jface.text.Position; |
29 |
import org.eclipse.jface.text.Region; |
32 |
import org.eclipse.jface.text.Region; |
|
|
33 |
import org.eclipse.jface.text.TextEvent; |
34 |
import org.eclipse.jface.text.TextUtilities; |
30 |
|
35 |
|
31 |
/** |
36 |
/** |
32 |
* Highlights the peer character matching the character near the caret position. |
37 |
* Highlights the peer character matching the character near the caret position. |
Lines 37-43
Link Here
|
37 |
* |
42 |
* |
38 |
* @since 2.1 |
43 |
* @since 2.1 |
39 |
*/ |
44 |
*/ |
40 |
public final class MatchingCharacterPainter implements IPainter, PaintListener { |
45 |
public final class MatchingCharacterPainter implements IPainter, PaintListener, ITextListener, ITextInputListener { |
41 |
|
46 |
|
42 |
/** Indicates whether this painter is active */ |
47 |
/** Indicates whether this painter is active */ |
43 |
private boolean fIsActive= false; |
48 |
private boolean fIsActive= false; |
Lines 62-67
Link Here
|
62 |
/** Whether a character is present at caret location or not. */ |
67 |
/** Whether a character is present at caret location or not. */ |
63 |
private boolean fCharacterPresentAtCaretLocation; |
68 |
private boolean fCharacterPresentAtCaretLocation; |
64 |
|
69 |
|
|
|
70 |
private IRegion fPreviousSelection; |
71 |
|
72 |
// private int count= 0; |
73 |
|
74 |
private IDocument fDocument; |
75 |
|
76 |
private boolean fDocumentChanged; |
65 |
|
77 |
|
66 |
/** |
78 |
/** |
67 |
* Creates a new MatchingCharacterPainter for the given source viewer using the given character |
79 |
* Creates a new MatchingCharacterPainter for the given source viewer using the given character |
Lines 75-80
Link Here
|
75 |
fSourceViewer= sourceViewer; |
87 |
fSourceViewer= sourceViewer; |
76 |
fMatcher= matcher; |
88 |
fMatcher= matcher; |
77 |
fTextWidget= sourceViewer.getTextWidget(); |
89 |
fTextWidget= sourceViewer.getTextWidget(); |
|
|
90 |
|
91 |
fDocument= fSourceViewer.getDocument(); |
92 |
|
93 |
if (fMatcher instanceof ICharacterPairMatcherExtension) { |
94 |
fSourceViewer.addTextInputListener(this); |
95 |
fSourceViewer.addTextListener(this); |
96 |
} |
78 |
} |
97 |
} |
79 |
|
98 |
|
80 |
/** |
99 |
/** |
Lines 138-143
Link Here
|
138 |
* @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent) |
157 |
* @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent) |
139 |
*/ |
158 |
*/ |
140 |
public void paintControl(PaintEvent event) { |
159 |
public void paintControl(PaintEvent event) { |
|
|
160 |
// System.out.println("paint event - " + "fPairPosition.offset: " + fPairPosition.offset + " | fPairPosition.length: " + fPairPosition.length); |
141 |
if (fTextWidget != null) |
161 |
if (fTextWidget != null) |
142 |
handleDrawRequest(event.gc); |
162 |
handleDrawRequest(event.gc); |
143 |
} |
163 |
} |
Lines 148-154
Link Here
|
148 |
* @param gc the GC to draw into. |
168 |
* @param gc the GC to draw into. |
149 |
*/ |
169 |
*/ |
150 |
private void handleDrawRequest(GC gc) { |
170 |
private void handleDrawRequest(GC gc) { |
151 |
|
171 |
// System.out.println("drawing"); |
152 |
if (fPairPosition.isDeleted) |
172 |
if (fPairPosition.isDeleted) |
153 |
return; |
173 |
return; |
154 |
|
174 |
|
Lines 262-268
Link Here
|
262 |
*/ |
282 |
*/ |
263 |
public void paint(int reason) { |
283 |
public void paint(int reason) { |
264 |
|
284 |
|
265 |
IDocument document= fSourceViewer.getDocument(); |
285 |
// printDebugStatements(reason, "entering the method"); |
|
|
286 |
IDocument document= fDocument; |
266 |
if (document == null) { |
287 |
if (document == null) { |
267 |
deactivate(false); |
288 |
deactivate(false); |
268 |
return; |
289 |
return; |
Lines 272-283
Link Here
|
272 |
IRegion pair; |
293 |
IRegion pair; |
273 |
boolean characterPresentAtCaretLocation; |
294 |
boolean characterPresentAtCaretLocation; |
274 |
|
295 |
|
|
|
296 |
if (reason != IPainter.CONFIGURATION && !fDocumentChanged && selection.equals(fPreviousSelection)) { |
297 |
// if (reason == IPainter.INTERNAL) |
298 |
// System.out.println("same as previous selection"); |
299 |
return; |
300 |
} |
301 |
|
302 |
if (reason == IPainter.TEXT_CHANGE) { |
303 |
fPreviousSelection= selection; |
304 |
fPairPosition.isDeleted= true; |
305 |
return; |
306 |
} |
307 |
|
308 |
fDocumentChanged= false; |
309 |
|
275 |
if (fMatcher instanceof ICharacterPairMatcherExtension) { |
310 |
if (fMatcher instanceof ICharacterPairMatcherExtension) { |
276 |
ICharacterPairMatcherExtension matcher= (ICharacterPairMatcherExtension)fMatcher; |
311 |
ICharacterPairMatcherExtension matcher= (ICharacterPairMatcherExtension)fMatcher; |
|
|
312 |
|
313 |
if (fHighlightEnclosingPeerCharcters && fPreviousSelection != null && reason != IPainter.INTERNAL && reason != IPainter.CONFIGURATION) { |
314 |
int previousCaretOffset= fPreviousSelection.getOffset() + fPreviousSelection.getLength(); |
315 |
int currentCaretOffset= selection.getOffset() + selection.getLength(); |
316 |
|
317 |
try { |
318 |
//check partitions as well |
319 |
String prevContentType= TextUtilities.getContentType(document, matcher.getPartitioning(), previousCaretOffset, false); |
320 |
String currContentType= TextUtilities.getContentType(document, matcher.getPartitioning(), currentCaretOffset, false); |
321 |
|
322 |
if (prevContentType.equals(currContentType)) { |
323 |
if (Math.abs(currentCaretOffset - previousCaretOffset) == 1) { |
324 |
char c= currentCaretOffset > previousCaretOffset ? document.getChar(previousCaretOffset) : document.getChar(currentCaretOffset); |
325 |
if (!matcher.isMatchedChar(c)) { |
326 |
fPreviousSelection= selection; |
327 |
return; |
328 |
} |
329 |
} |
330 |
|
331 |
int start; |
332 |
int end; |
333 |
if (currentCaretOffset > previousCaretOffset) { |
334 |
start= previousCaretOffset; |
335 |
end= currentCaretOffset; |
336 |
} else { |
337 |
start= currentCaretOffset; |
338 |
end= previousCaretOffset; |
339 |
} |
340 |
boolean found= false; |
341 |
for (int i= start; i < end; i++) { |
342 |
if (matcher.isMatchedChar(document.getChar(i))) { |
343 |
found= true; |
344 |
break; |
345 |
} |
346 |
} |
347 |
if (!found) { |
348 |
fPreviousSelection= selection; |
349 |
return; |
350 |
} |
351 |
} |
352 |
} catch (BadLocationException e) { |
353 |
//do nothing |
354 |
} |
355 |
} |
356 |
|
357 |
// printDebugStatements(reason, "about to compute"); |
358 |
// count++; |
359 |
|
277 |
pair= matcher.match(document, selection.getOffset(), selection.getLength()); |
360 |
pair= matcher.match(document, selection.getOffset(), selection.getLength()); |
278 |
characterPresentAtCaretLocation= (pair != null); |
361 |
characterPresentAtCaretLocation= (pair != null); |
279 |
if (pair == null && fHighlightEnclosingPeerCharcters) { |
362 |
if (pair == null && fHighlightEnclosingPeerCharcters) { |
|
|
363 |
// long start= System.currentTimeMillis(); |
280 |
pair= matcher.findEnclosingPeerCharacters(document, selection.getOffset(), selection.getLength()); |
364 |
pair= matcher.findEnclosingPeerCharacters(document, selection.getOffset(), selection.getLength()); |
|
|
365 |
// long end= System.currentTimeMillis(); |
366 |
// System.out.println("MCP: " + (end - start)); |
281 |
} |
367 |
} |
282 |
} else { |
368 |
} else { |
283 |
if (Math.abs(selection.getLength()) > 0) { |
369 |
if (Math.abs(selection.getLength()) > 0) { |
Lines 288-293
Link Here
|
288 |
characterPresentAtCaretLocation= (pair != null); |
374 |
characterPresentAtCaretLocation= (pair != null); |
289 |
} |
375 |
} |
290 |
|
376 |
|
|
|
377 |
fPreviousSelection= selection; |
291 |
if (pair == null) { |
378 |
if (pair == null) { |
292 |
deactivate(true); |
379 |
deactivate(true); |
293 |
return; |
380 |
return; |
Lines 312-317
Link Here
|
312 |
fPairPosition.isDeleted= false; |
399 |
fPairPosition.isDeleted= false; |
313 |
fPairPosition.offset= pair.getOffset(); |
400 |
fPairPosition.offset= pair.getOffset(); |
314 |
fPairPosition.length= pair.getLength(); |
401 |
fPairPosition.length= pair.getLength(); |
|
|
402 |
// System.out.println("fPairPosition.offset: " + fPairPosition.offset + " | fPairPosition.length: " + fPairPosition.length); |
315 |
fAnchor= fMatcher.getAnchor(); |
403 |
fAnchor= fMatcher.getAnchor(); |
316 |
fCharacterPresentAtCaretLocation= characterPresentAtCaretLocation; |
404 |
fCharacterPresentAtCaretLocation= characterPresentAtCaretLocation; |
317 |
// apply new highlighting |
405 |
// apply new highlighting |
Lines 334-343
Link Here
|
334 |
} |
422 |
} |
335 |
} |
423 |
} |
336 |
|
424 |
|
|
|
425 |
// private void printDebugStatements(int reason, String loc) { |
426 |
//// count++; |
427 |
// String s; |
428 |
// switch (reason) { |
429 |
// case IPainter.SELECTION: |
430 |
// s= "SELECTION"; //$NON-NLS-1$ |
431 |
// break; |
432 |
// case IPainter.TEXT_CHANGE: |
433 |
// s= "TEXT_CHANGE"; //$NON-NLS-1$ |
434 |
// break; |
435 |
// case IPainter.KEY_STROKE: |
436 |
// s= "KEY_STROKE"; //$NON-NLS-1$ |
437 |
// break; |
438 |
// case IPainter.MOUSE_BUTTON: |
439 |
// s= "MOUSE_BUTTON"; //$NON-NLS-1$ |
440 |
// break; |
441 |
// case IPainter.INTERNAL: |
442 |
// s= "INTERNAL"; //$NON-NLS-1$ |
443 |
// break; |
444 |
// case IPainter.CONFIGURATION: |
445 |
// s= "CONFIGURATION"; //$NON-NLS-1$ |
446 |
// break; |
447 |
// default: |
448 |
// s= "You have got to be kidding me"; //$NON-NLS-1$ |
449 |
// break; |
450 |
// } |
451 |
// System.out.println(loc + " Reason: " + s + " count: " + count); //$NON-NLS-1$ //$NON-NLS-2$ |
452 |
// } |
453 |
|
337 |
/* |
454 |
/* |
338 |
* @see org.eclipse.jface.text.IPainter#setPositionManager(org.eclipse.jface.text.IPaintPositionManager) |
455 |
* @see org.eclipse.jface.text.IPainter#setPositionManager(org.eclipse.jface.text.IPaintPositionManager) |
339 |
*/ |
456 |
*/ |
340 |
public void setPositionManager(IPaintPositionManager manager) { |
457 |
public void setPositionManager(IPaintPositionManager manager) { |
341 |
fPaintPositionManager= manager; |
458 |
fPaintPositionManager= manager; |
342 |
} |
459 |
} |
|
|
460 |
|
461 |
/** |
462 |
* @param oldInput the old input |
463 |
* @param newInput the new input |
464 |
* @since 3.8 |
465 |
*/ |
466 |
public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { |
467 |
//do nothing |
468 |
} |
469 |
|
470 |
/** |
471 |
* @param oldInput the old input |
472 |
* @param newInput the new input |
473 |
* @since 3.8 |
474 |
*/ |
475 |
public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { |
476 |
fDocument= newInput; |
477 |
fDocumentChanged= true; |
478 |
// System.out.println("doc changed"); |
479 |
} |
480 |
|
481 |
/** |
482 |
* @param event the text event |
483 |
* @since 3.8 |
484 |
*/ |
485 |
public void textChanged(TextEvent event) { |
486 |
if (!(fMatcher instanceof ICharacterPairMatcherExtension)) |
487 |
return; |
488 |
|
489 |
String text= event.getText(); |
490 |
String replacedText= event.getReplacedText(); |
491 |
// System.out.println("TextEvent: " + "|" + text + "|" + replacedText); |
492 |
|
493 |
boolean viewerRedrawState= event.getViewerRedrawState(); |
494 |
DocumentEvent documentEvent= event.getDocumentEvent(); |
495 |
if (documentEvent == null && !viewerRedrawState) |
496 |
return; |
497 |
|
498 |
ICharacterPairMatcherExtension matcher= (ICharacterPairMatcherExtension)fMatcher; |
499 |
boolean found= searchForCharacter(text, matcher) || searchForCharacter(replacedText, matcher); |
500 |
|
501 |
if (found || (documentEvent == null && viewerRedrawState)) { |
502 |
// System.out.println("smart painting"); |
503 |
paint(IPainter.INTERNAL); |
504 |
} |
505 |
} |
506 |
|
507 |
private boolean searchForCharacter(String text, ICharacterPairMatcherExtension matcher) { |
508 |
if (text == null) |
509 |
return false; |
510 |
for (int i= 0; i < text.length(); i++) { |
511 |
if (matcher.isMatchedChar(text.charAt(i))) { |
512 |
return true; |
513 |
} |
514 |
} |
515 |
return false; |
516 |
} |
343 |
} |
517 |
} |