IDEA-161930 Editing method invocations with parameter hints creates too much distraction
authorDmitry Batrak <Dmitry.Batrak@jetbrains.com>
Tue, 4 Oct 2016 12:54:17 +0000 (15:54 +0300)
committerDmitry Batrak <Dmitry.Batrak@jetbrains.com>
Tue, 4 Oct 2016 12:55:05 +0000 (15:55 +0300)
java/java-impl/src/com/intellij/codeInsight/daemon/impl/ParameterHintsPassFactory.java

index a9abb809fa2a6ec9ae18ba54dc5c6e9ffb833e88..49d8c2e34044113d09c0a124bed42b8395676f3b 100644 (file)
@@ -20,8 +20,10 @@ import com.intellij.codeHighlighting.TextEditorHighlightingPass;
 import com.intellij.codeHighlighting.TextEditorHighlightingPassFactory;
 import com.intellij.codeHighlighting.TextEditorHighlightingPassRegistrar;
 import com.intellij.openapi.components.AbstractProjectComponent;
 import com.intellij.codeHighlighting.TextEditorHighlightingPassFactory;
 import com.intellij.codeHighlighting.TextEditorHighlightingPassRegistrar;
 import com.intellij.openapi.components.AbstractProjectComponent;
+import com.intellij.openapi.editor.Caret;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.Inlay;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.Inlay;
+import com.intellij.openapi.editor.VisualPosition;
 import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
 import com.intellij.openapi.progress.ProgressIndicator;
 import com.intellij.openapi.progress.ProgressIndicatorProvider;
 import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
 import com.intellij.openapi.progress.ProgressIndicator;
 import com.intellij.openapi.progress.ProgressIndicatorProvider;
@@ -29,6 +31,7 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Key;
 import com.intellij.psi.*;
 import com.intellij.util.containers.HashSet;
 import com.intellij.openapi.util.Key;
 import com.intellij.psi.*;
 import com.intellij.util.containers.HashSet;
+import gnu.trove.TIntObjectHashMap;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
@@ -141,11 +144,16 @@ public class ParameterHintsPassFactory extends AbstractProjectComponent implemen
       boolean firstTime = myEditor.getUserData(REPEATED_PASS) == null;
       ParameterHintsPresentationManager presentationManager = ParameterHintsPresentationManager.getInstance();
       Set<String> removedHints = new HashSet<>();
       boolean firstTime = myEditor.getUserData(REPEATED_PASS) == null;
       ParameterHintsPresentationManager presentationManager = ParameterHintsPresentationManager.getInstance();
       Set<String> removedHints = new HashSet<>();
+      TIntObjectHashMap<Caret> caretMap = new TIntObjectHashMap<>();
+      for (Caret caret : myEditor.getCaretModel().getAllCarets()) {
+        caretMap.put(caret.getOffset(), caret);
+      }
       for (Inlay inlay : myEditor.getInlayModel().getInlineElementsInRange(0, myDocument.getTextLength())) {
         if (!presentationManager.isParameterHint(inlay)) continue;
         int offset = inlay.getOffset();
       for (Inlay inlay : myEditor.getInlayModel().getInlineElementsInRange(0, myDocument.getTextLength())) {
         if (!presentationManager.isParameterHint(inlay)) continue;
         int offset = inlay.getOffset();
-        String oldText = presentationManager.getHintText(inlay);
         String newText = myAnnotations.remove(offset);
         String newText = myAnnotations.remove(offset);
+        if (delayRemoval(inlay, caretMap)) continue;
+        String oldText = presentationManager.getHintText(inlay);
         if (!Objects.equals(newText, oldText)) {
           if (newText == null) {
             removedHints.add(oldText);
         if (!Objects.equals(newText, oldText)) {
           if (newText == null) {
             removedHints.add(oldText);
@@ -163,5 +171,17 @@ public class ParameterHintsPassFactory extends AbstractProjectComponent implemen
       }
       myEditor.putUserData(REPEATED_PASS, Boolean.TRUE);
     }
       }
       myEditor.putUserData(REPEATED_PASS, Boolean.TRUE);
     }
+
+    private boolean delayRemoval(Inlay inlay, TIntObjectHashMap<Caret> caretMap) {
+      int offset = inlay.getOffset();
+      Caret caret = caretMap.get(offset);
+      if (caret == null) return false;
+      char afterCaret = myEditor.getDocument().getImmutableCharSequence().charAt(offset);
+      if (afterCaret != ',' && afterCaret != ')') return false;
+      VisualPosition afterInlayPosition = myEditor.offsetToVisualPosition(offset, true, false);
+      // check whether caret is to the right of inlay
+      if (!caret.getVisualPosition().equals(afterInlayPosition)) return false;
+      return true;
+    }
   }
 }
   }
 }