13
13
import com .intellij .psi .PsiFile ;
14
14
import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
15
15
import fr .adrienbrault .idea .symfony2plugin .codeInsight .caret .overlay .component .CaretOverlayComponent ;
16
- import fr .adrienbrault .idea .symfony2plugin .codeInsight .caret .overlay .provider .XmlServiceContainerCaretTextOverlay ;
17
16
import fr .adrienbrault .idea .symfony2plugin .codeInsight .caret .overlay .util .CaretTextOverlayUtil ;
18
17
import org .jetbrains .annotations .NotNull ;
19
18
20
19
import javax .swing .*;
21
20
import java .awt .*;
22
- import java .util .Timer ;
23
- import java .util .TimerTask ;
21
+ import java .util .concurrent .Executors ;
22
+ import java .util .concurrent .ScheduledExecutorService ;
23
+ import java .util .concurrent .ScheduledFuture ;
24
+ import java .util .concurrent .TimeUnit ;
24
25
25
26
/**
26
27
* @author Daniel Espendiller <daniel@espendiller.net>
27
28
*/
28
29
public class CaretTextOverlayListener implements CaretListener {
29
30
30
- private Timer timer = null ;
31
31
private int startDelayMs = 250 ;
32
32
33
33
private final Object lock = new Object ();
34
34
35
+ private ScheduledExecutorService executor = Executors .newSingleThreadScheduledExecutor ();
36
+ private ScheduledFuture <?> schedule ;
37
+
35
38
@ Override
36
39
public void caretPositionChanged (final CaretEvent caretEvent ) {
40
+ synchronized (lock ) {
41
+ if (schedule != null ) {
42
+ schedule .cancel (true );
43
+ schedule = null ;
44
+ }
45
+ }
37
46
38
- this .clear ();
39
47
final Editor editor = caretEvent .getEditor ();
40
48
removeOverlays (editor );
41
49
@@ -49,14 +57,9 @@ public void caretPositionChanged(final CaretEvent caretEvent) {
49
57
}
50
58
51
59
synchronized (lock ) {
52
- this .timer = new Timer ();
53
- this .timer .schedule (new TimerTask () {
54
- @ Override
55
- public void run () {
56
- ApplicationManager .getApplication ().runReadAction (new MyPsiElementRunnable (project , caretEvent , editor ));
57
- timer = null ;
58
- }
59
- }, startDelayMs );
60
+ schedule = executor .schedule (() -> {
61
+ ApplicationManager .getApplication ().runReadAction (new MyPsiElementRunnable (project , caretEvent , editor ));
62
+ }, startDelayMs , TimeUnit .MILLISECONDS );
60
63
}
61
64
}
62
65
@@ -88,12 +91,8 @@ private void removeOverlays(@NotNull Editor editor) {
88
91
89
92
synchronized public void clear () {
90
93
synchronized (lock ) {
91
- if (timer == null ) {
92
- return ;
93
- }
94
-
95
- timer .cancel ();
96
- timer = null ;
94
+ executor .shutdownNow ();
95
+ schedule = null ;
97
96
}
98
97
}
99
98
0 commit comments