|
8 | 8 | package com.intellij.lang.jsgraphql.v1.ide.editor;
|
9 | 9 |
|
10 | 10 | import com.intellij.lang.jsgraphql.psi.GraphQLFile;
|
| 11 | +import com.intellij.openapi.Disposable; |
11 | 12 | import com.intellij.openapi.application.ApplicationManager;
|
12 | 13 | import com.intellij.openapi.editor.EditorFactory;
|
13 | 14 | import com.intellij.openapi.editor.event.CaretEvent;
|
|
24 | 25 | /**
|
25 | 26 | * Updates the current caret position in GraphQL files to enable contextual queries and highlighting of included fragments
|
26 | 27 | */
|
27 |
| -public class JSGraphQLQueryContextCaretListener implements StartupActivity, DumbAware { |
| 28 | +public class JSGraphQLQueryContextCaretListener implements Disposable { |
28 | 29 |
|
29 | 30 | static final Key<Integer> CARET_OFFSET = Key.create("JSGraphQL.QueryContext.CaretOffset");
|
30 | 31 |
|
31 |
| - @Override |
32 |
| - public void runActivity(@NotNull Project project) { |
33 |
| - if (!ApplicationManager.getApplication().isHeadlessEnvironment()) { |
34 |
| - final EditorEventMulticaster eventMulticaster = EditorFactory.getInstance().getEventMulticaster(); |
35 |
| - final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); |
36 |
| - eventMulticaster.addCaretListener(new CaretListener() { |
37 |
| - @Override |
38 |
| - public void caretPositionChanged(CaretEvent e) { |
39 |
| - final PsiFile psiFile = psiDocumentManager.getPsiFile(e.getEditor().getDocument()); |
40 |
| - if (psiFile instanceof GraphQLFile) { |
41 |
| - int offset = e.getEditor().logicalPositionToOffset(e.getNewPosition()); |
42 |
| - psiFile.putUserData(CARET_OFFSET, offset); |
43 |
| - } |
44 |
| - } |
45 |
| - }, project); |
| 32 | + public JSGraphQLQueryContextCaretListener(@NotNull Project project) { |
| 33 | + listen(project); |
| 34 | + } |
| 35 | + |
| 36 | + private void listen(@NotNull Project project) { |
| 37 | + if (ApplicationManager.getApplication().isHeadlessEnvironment()) { |
| 38 | + return; |
46 | 39 | }
|
| 40 | + |
| 41 | + final EditorEventMulticaster eventMulticaster = EditorFactory.getInstance().getEventMulticaster(); |
| 42 | + final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); |
| 43 | + eventMulticaster.addCaretListener(new CaretListener() { |
| 44 | + @Override |
| 45 | + public void caretPositionChanged(@NotNull CaretEvent e) { |
| 46 | + if (project.isDisposed() || project != e.getEditor().getProject()) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + final PsiFile psiFile = psiDocumentManager.getPsiFile(e.getEditor().getDocument()); |
| 51 | + if (psiFile instanceof GraphQLFile) { |
| 52 | + int offset = e.getEditor().logicalPositionToOffset(e.getNewPosition()); |
| 53 | + psiFile.putUserData(CARET_OFFSET, offset); |
| 54 | + } |
| 55 | + } |
| 56 | + }, this); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void dispose() { |
47 | 61 | }
|
48 | 62 | }
|
0 commit comments