Skip to content

Commit a16f800

Browse files
authored
Merge pull request #400 from jimkyndemeyer/caret-listener-exception-fix
Fixes caret listener causing an exception (#397)
2 parents 8011e9d + f08cbff commit a16f800

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<projectService serviceImplementation="com.intellij.lang.jsgraphql.ide.editor.GraphQLIntrospectionService" />
6565
<projectService serviceInterface="com.intellij.lang.jsgraphql.ide.project.graphqlconfig.GraphQLConfigGlobMatcher" serviceImplementation="com.intellij.lang.jsgraphql.ide.project.graphqlconfig.GraphQLConfigGlobMatcherImpl" />
6666
<projectService serviceImplementation="com.intellij.lang.jsgraphql.ide.GraphQLRelayModernAnnotationFilter" />
67+
<projectService serviceImplementation="com.intellij.lang.jsgraphql.v1.ide.editor.JSGraphQLQueryContextCaretListener" />
6768

6869
<!-- Indexing -->
6970
<fileBasedIndex implementation="com.intellij.lang.jsgraphql.ide.project.indexing.GraphQLIdentifierIndex" />
@@ -73,7 +74,6 @@
7374
<postStartupActivity implementation="com.intellij.lang.jsgraphql.endpoint.ide.startup.GraphQLStartupActivity" />
7475
<postStartupActivity implementation="com.intellij.lang.jsgraphql.ide.project.graphqlconfig.GraphQLConfigProjectStartupActivity" />
7576
<postStartupActivity implementation="com.intellij.lang.jsgraphql.ide.project.relay.GraphQLRelayModernEnableStartupActivity" />
76-
<postStartupActivity implementation="com.intellij.lang.jsgraphql.v1.ide.editor.JSGraphQLQueryContextCaretListener" />
7777

7878
<!-- Syntax and error highlighting -->
7979
<lang.syntaxHighlighterFactory language="GraphQL" implementationClass="com.intellij.lang.jsgraphql.ide.GraphQLSyntaxHighlighterFactory"/>

src/main/com/intellij/lang/jsgraphql/v1/ide/editor/JSGraphQLQueryContextCaretListener.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.intellij.lang.jsgraphql.v1.ide.editor;
99

1010
import com.intellij.lang.jsgraphql.psi.GraphQLFile;
11+
import com.intellij.openapi.Disposable;
1112
import com.intellij.openapi.application.ApplicationManager;
1213
import com.intellij.openapi.editor.EditorFactory;
1314
import com.intellij.openapi.editor.event.CaretEvent;
@@ -24,25 +25,38 @@
2425
/**
2526
* Updates the current caret position in GraphQL files to enable contextual queries and highlighting of included fragments
2627
*/
27-
public class JSGraphQLQueryContextCaretListener implements StartupActivity, DumbAware {
28+
public class JSGraphQLQueryContextCaretListener implements Disposable {
2829

2930
static final Key<Integer> CARET_OFFSET = Key.create("JSGraphQL.QueryContext.CaretOffset");
3031

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;
4639
}
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() {
4761
}
4862
}

0 commit comments

Comments
 (0)