@@ -105,7 +105,7 @@ public void visit(@NotNull PsiElement element) {
105
105
@ Override
106
106
public boolean analyze (final @ NotNull PsiFile file , boolean updateWholeFile , @ NotNull HighlightInfoHolder holder , @ NotNull Runnable action ) {
107
107
108
- final PsiElement operationAtCursor = getOperationAtCursor (file );
108
+ final PsiElement operationAtCursor = getOperationAtCursor (file , null );
109
109
if (operationAtCursor != null && hasMultipleVisibleTopLevelElement (file )) {
110
110
111
111
// store the range of the current operation for use in the caret listener
@@ -172,7 +172,7 @@ public void caretPositionChanged(CaretEvent e) {
172
172
173
173
if (!sameOperation ) {
174
174
// moved to somewhere outside the previous operation
175
- if (hadOperation || getOperationAtCursor (psiFile ) != null ) {
175
+ if (hadOperation || getOperationAtCursor (psiFile , e ) != null ) {
176
176
// perform a new highlighting pass
177
177
DaemonCodeAnalyzer .getInstance (project ).restart (psiFile );
178
178
}
@@ -328,7 +328,7 @@ public static JSGraphQLQueryContext getQueryContextBufferAndHighlightUnused(fina
328
328
329
329
// no selection -- see if the caret is inside an operation
330
330
331
- final PsiElement operationAtCursor = getOperationAtCursor (psiFile );
331
+ final PsiElement operationAtCursor = getOperationAtCursor (psiFile , null );
332
332
if (operationAtCursor != null ) {
333
333
final HashSet <JSGraphQLFragmentDefinitionPsiElement > foundFragments = Sets .newHashSet ();
334
334
findFragmentsInsideOperation (operationAtCursor , foundFragments , null );
@@ -439,11 +439,22 @@ private static void showQueryContextHint(Editor editor, String hintText) {
439
439
* Gets the operation that wraps the current caret position, or <code>null</code> if none is found,
440
440
* e.g. when outside any operation or inside a fragment definition
441
441
*/
442
- private static PsiElement getOperationAtCursor (PsiFile psiFile ) {
442
+ private static PsiElement getOperationAtCursor (PsiFile psiFile , CaretEvent caretEvent ) {
443
443
final FileEditor fileEditor = FileEditorManager .getInstance (psiFile .getProject ()).getSelectedEditor (psiFile .getVirtualFile ());
444
444
if (fileEditor instanceof TextEditor ) {
445
445
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
+ }
447
458
PsiElement currentElement = psiFile .findElementAt (currentOffset );
448
459
while (currentElement != null && !(currentElement .getParent () instanceof PsiFile )) {
449
460
currentElement = currentElement .getParent ();
0 commit comments