@@ -105,7 +105,7 @@ public void visit(@NotNull PsiElement element) {
105105 @ Override
106106 public boolean analyze (final @ NotNull PsiFile file , boolean updateWholeFile , @ NotNull HighlightInfoHolder holder , @ NotNull Runnable action ) {
107107
108- final PsiElement operationAtCursor = getOperationAtCursor (file );
108+ final PsiElement operationAtCursor = getOperationAtCursor (file , null );
109109 if (operationAtCursor != null && hasMultipleVisibleTopLevelElement (file )) {
110110
111111 // store the range of the current operation for use in the caret listener
@@ -172,7 +172,7 @@ public void caretPositionChanged(CaretEvent e) {
172172
173173 if (!sameOperation ) {
174174 // moved to somewhere outside the previous operation
175- if (hadOperation || getOperationAtCursor (psiFile ) != null ) {
175+ if (hadOperation || getOperationAtCursor (psiFile , e ) != null ) {
176176 // perform a new highlighting pass
177177 DaemonCodeAnalyzer .getInstance (project ).restart (psiFile );
178178 }
@@ -328,7 +328,7 @@ public static JSGraphQLQueryContext getQueryContextBufferAndHighlightUnused(fina
328328
329329 // no selection -- see if the caret is inside an operation
330330
331- final PsiElement operationAtCursor = getOperationAtCursor (psiFile );
331+ final PsiElement operationAtCursor = getOperationAtCursor (psiFile , null );
332332 if (operationAtCursor != null ) {
333333 final HashSet <JSGraphQLFragmentDefinitionPsiElement > foundFragments = Sets .newHashSet ();
334334 findFragmentsInsideOperation (operationAtCursor , foundFragments , null );
@@ -439,11 +439,22 @@ private static void showQueryContextHint(Editor editor, String hintText) {
439439 * Gets the operation that wraps the current caret position, or <code>null</code> if none is found,
440440 * e.g. when outside any operation or inside a fragment definition
441441 */
442- private static PsiElement getOperationAtCursor (PsiFile psiFile ) {
442+ private static PsiElement getOperationAtCursor (PsiFile psiFile , CaretEvent caretEvent ) {
443443 final FileEditor fileEditor = FileEditorManager .getInstance (psiFile .getProject ()).getSelectedEditor (psiFile .getVirtualFile ());
444444 if (fileEditor instanceof TextEditor ) {
445445 final Editor editor = ((TextEditor ) fileEditor ).getEditor ();
446- final int currentOffset = editor .getCaretModel ().getOffset ();
446+ final int currentOffset ;
447+ if (caretEvent != null ) {
448+ currentOffset = editor .logicalPositionToOffset (caretEvent .getNewPosition ());
449+ } else {
450+ try {
451+ currentOffset = editor .getCaretModel ().getOffset ();
452+ } catch (Throwable e ) {
453+ // a caret update is in progress, so can't determine the operation at this time,
454+ // but a new caretPositionChanged event will follow
455+ return null ;
456+ }
457+ }
447458 PsiElement currentElement = psiFile .findElementAt (currentOffset );
448459 while (currentElement != null && !(currentElement .getParent () instanceof PsiFile )) {
449460 currentElement = currentElement .getParent ();
0 commit comments