|
32 | 32 | import com.intellij.openapi.editor.ScrollType; |
33 | 33 | import com.intellij.openapi.editor.colors.EditorColors; |
34 | 34 | import com.intellij.openapi.editor.colors.EditorColorsManager; |
35 | | -import com.intellij.openapi.editor.event.CaretAdapter; |
36 | 35 | import com.intellij.openapi.editor.event.CaretEvent; |
| 36 | +import com.intellij.openapi.editor.event.CaretListener; |
37 | 37 | import com.intellij.openapi.editor.markup.EffectType; |
38 | 38 | import com.intellij.openapi.editor.markup.RangeHighlighter; |
39 | 39 | import com.intellij.openapi.editor.markup.TextAttributes; |
@@ -106,7 +106,7 @@ public boolean analyze(final @NotNull PsiFile file, boolean updateWholeFile, @No |
106 | 106 | // run the default pass first (DefaultHighlightVisitor) which calls annotators etc. |
107 | 107 | action.run(); |
108 | 108 |
|
109 | | - final PsiElement operationAtCursor = getOperationAtCursor(file, null); |
| 109 | + final PsiElement operationAtCursor = getOperationAtCursor(file); |
110 | 110 | if (operationAtCursor != null && hasMultipleVisibleTopLevelElement(file)) { |
111 | 111 |
|
112 | 112 | // store the range of the current operation for use in the caret listener |
@@ -146,7 +146,7 @@ public boolean analyze(final @NotNull PsiFile file, boolean updateWholeFile, @No |
146 | 146 | if (fileEditor instanceof TextEditor) { |
147 | 147 | final Editor editor = ((TextEditor) fileEditor).getEditor(); |
148 | 148 | if (!Boolean.TRUE.equals(editor.getUserData(QUERY_HIGHLIGHT_LISTENER_ADDED))) { |
149 | | - editor.getCaretModel().addCaretListener(new CaretAdapter() { |
| 149 | + editor.getCaretModel().addCaretListener(new CaretListener() { |
150 | 150 | @Override |
151 | 151 | public void caretPositionChanged(CaretEvent e) { |
152 | 152 | // re-highlight when the operation changes |
@@ -178,7 +178,7 @@ public void caretPositionChanged(CaretEvent e) { |
178 | 178 |
|
179 | 179 | if (!sameOperation) { |
180 | 180 | // moved to somewhere outside the previous operation |
181 | | - if (hadOperation || getOperationAtCursor(psiFile, e) != null) { |
| 181 | + if (hadOperation || getOperationAtCursor(psiFile) != null) { |
182 | 182 | // perform a new highlighting pass |
183 | 183 | DaemonCodeAnalyzer.getInstance(project).restart(psiFile); |
184 | 184 | } |
@@ -341,7 +341,7 @@ public static JSGraphQLQueryContext getQueryContextBufferAndHighlightUnused(fina |
341 | 341 |
|
342 | 342 | // no selection -- see if the caret is inside an operation |
343 | 343 |
|
344 | | - final GraphQLOperationDefinition operationAtCursor = getOperationAtCursor(psiFile, null); |
| 344 | + final GraphQLOperationDefinition operationAtCursor = getOperationAtCursor(psiFile); |
345 | 345 | if (operationAtCursor != null) { |
346 | 346 | final Map<String, GraphQLFragmentDefinition> foundFragments = Maps.newHashMap(); |
347 | 347 | findFragmentsInsideOperation(operationAtCursor, foundFragments, null); |
@@ -471,27 +471,18 @@ private static void showQueryContextHint(Editor editor, String hintText) { |
471 | 471 | * Gets the operation that wraps the current caret position, or <code>null</code> if none is found, |
472 | 472 | * e.g. when outside any operation or inside a fragment definition |
473 | 473 | */ |
474 | | - private static GraphQLOperationDefinition getOperationAtCursor(PsiFile psiFile, CaretEvent caretEvent) { |
475 | | - final FileEditor fileEditor = FileEditorManager.getInstance(psiFile.getProject()).getSelectedEditor(psiFile.getVirtualFile()); |
476 | | - if (fileEditor instanceof TextEditor) { |
477 | | - final Editor editor = ((TextEditor) fileEditor).getEditor(); |
478 | | - final int currentOffset; |
479 | | - if (caretEvent != null) { |
480 | | - currentOffset = editor.logicalPositionToOffset(caretEvent.getNewPosition()); |
481 | | - } else { |
482 | | - try { |
483 | | - currentOffset = editor.getCaretModel().getOffset(); |
484 | | - } catch (Throwable e) { |
485 | | - // a caret update is in progress, so can't determine the operation at this time, |
486 | | - // but a new caretPositionChanged event will follow |
487 | | - return null; |
488 | | - } |
489 | | - } |
490 | | - PsiElement currentElement = psiFile.findElementAt(currentOffset); |
| 474 | + private static GraphQLOperationDefinition getOperationAtCursor(PsiFile psiFile) { |
| 475 | + |
| 476 | + final Integer caretOffset = psiFile.getUserData(JSGraphQLQueryContextCaretListener.CARET_OFFSET); |
| 477 | + |
| 478 | + if (caretOffset != null) { |
| 479 | + PsiElement currentElement = psiFile.findElementAt(caretOffset); |
491 | 480 | while (currentElement != null && !(currentElement.getParent() instanceof PsiFile)) { |
492 | 481 | currentElement = currentElement.getParent(); |
493 | 482 | } |
494 | | - return asOperationOrNull(currentElement); |
| 483 | + if (currentElement != null) { |
| 484 | + return asOperationOrNull(currentElement); |
| 485 | + } |
495 | 486 | } |
496 | 487 | return null; |
497 | 488 | } |
|
0 commit comments