IDEA-27091 When finishing method lookup item with '(', put caret after this '(' even...
authorpeter <peter.gromov@jetbrains.com>
Tue, 9 Mar 2010 12:17:01 +0000 (12:17 +0000)
committerpeter <peter.gromov@jetbrains.com>
Tue, 9 Mar 2010 12:54:15 +0000 (12:54 +0000)
IDEA-51789 When "Insert pair bracket" on Editor->Smart keys page is off, IDEA still inserts both parentheses on autocomplete lookup, but overtype doesn't work

java/java-impl/src/com/intellij/codeInsight/completion/simple/PsiMethodInsertHandler.java
java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after2.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailType_after.java
java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.java
platform/lang-api/src/com/intellij/codeInsight/completion/util/ParenthesesInsertHandler.java
plugins/groovy/testdata/groovy/completion/FinishMethodWithLParen_after.groovy

index 0f9f5f2bdf3934df8e4385c443db056728595e00..928ddc97fe211381b2318cb93cc21f30c34b1f08 100644 (file)
@@ -53,24 +53,26 @@ public class PsiMethodInsertHandler implements InsertHandler<LookupItem<PsiMetho
     final TailType tailType = getTailType(item, context);
     final Document document = editor.getDocument();
     final PsiFile file = context.getFile();
+    final int offset = editor.getCaretModel().getOffset();
 
     context.setAddCompletionChar(false);
 
     final LookupElement[] allItems = context.getElements();
-    boolean signatureSelected = allItems.length > 1 || item.getUserData(LookupItem.FORCE_SHOW_SIGNATURE_ATTR) != null;
+    final boolean overloadsMatter = allItems.length == 1 && item.getUserData(LookupItem.FORCE_SHOW_SIGNATURE_ATTR) == null;
 
-    int offset = editor.getCaretModel().getOffset();
+    final boolean hasParams = MethodParenthesesHandler.hasParams(item, allItems, overloadsMatter, myMethod);
     final boolean needLeftParenth = isToInsertParenth(file.findElementAt(context.getStartOffset()));
-    final boolean hasParams = MethodParenthesesHandler.hasParams(item, allItems, !signatureSelected, myMethod);
+    final boolean needRightParenth = shouldInsertRParenth(completionChar, tailType, hasParams);
+
     if (needLeftParenth) {
       final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(context.getProject());
-      new MethodParenthesesHandler(myMethod, !signatureSelected,
+      new MethodParenthesesHandler(myMethod, overloadsMatter,
                                            styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES,
                                            styleSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES && hasParams,
-                                           shouldInsertRightParenthesis(tailType)
+                                           needRightParenth
       ).handleInsert(context, item);
     }
-    
+
     insertExplicitTypeParams(item, document, offset, file);
 
     final PsiType type = myMethod.getReturnType();
@@ -86,14 +88,25 @@ public class PsiMethodInsertHandler implements InsertHandler<LookupItem<PsiMetho
 
     if (needLeftParenth && hasParams) {
       // Invoke parameters popup
-      AutoPopupController.getInstance(myMethod.getProject()).autoPopupParameterInfo(editor, signatureSelected ? myMethod : null);
+      AutoPopupController.getInstance(myMethod.getProject()).autoPopupParameterInfo(editor, overloadsMatter ? null : myMethod);
+    }
+    if (tailType == TailType.SMART_COMPLETION || needLeftParenth && needRightParenth) {
+      tailType.processTail(editor, context.getTailOffset());
     }
-    tailType.processTail(editor, context.getTailOffset());
     editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
   }
 
-  protected static boolean shouldInsertRightParenthesis(TailType tailType) {
-    return tailType != TailType.SMART_COMPLETION;
+  private boolean shouldInsertRParenth(char completionChar, TailType tailType, boolean hasParams) {
+    if (tailType == TailType.SMART_COMPLETION) {
+      return false;
+    }
+
+    if (completionChar == '(' && !hasParams) {
+      //it's highly probable that the user will type ')' next and it may not be overwritten if the flag is off
+      return CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET;
+    }
+
+    return true;
   }
 
   @NotNull
diff --git a/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace.java b/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace.java
new file mode 100644 (file)
index 0000000..0403bf2
--- /dev/null
@@ -0,0 +1,8 @@
+class MyClass {
+
+void foo() {}
+
+{
+  fo<caret>
+}
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after.java b/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after.java
new file mode 100644 (file)
index 0000000..c976d92
--- /dev/null
@@ -0,0 +1,8 @@
+class MyClass {
+
+void foo() {}
+
+{
+  foo(<caret>
+}
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after2.java b/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after2.java
new file mode 100644 (file)
index 0000000..b7841df
--- /dev/null
@@ -0,0 +1,8 @@
+class MyClass {
+
+void foo() {}
+
+{
+  foo();<caret>
+}
+}
\ No newline at end of file
index b7841df3ff15746dcf4544d9e80001a321fc673e..7680812e2f7c088c86bb840c658c6e1c642be7aa 100644 (file)
@@ -3,6 +3,6 @@ class MyClass {
 void foo() {}
 
 {
-  foo();<caret>
+  foo(<caret>);
 }
 }
\ No newline at end of file
index c0103a0adde72c4ca78ce63d729bde680c708979..f6a2431c26d6e06ebb86f46549fe2bcbcc118ddf 100644 (file)
@@ -308,6 +308,25 @@ public class NormalCompletionTest extends LightCompletionTestCase {
     checkResultByFile("/codeInsight/completion/normal/MethodWithLeftParTailType2_after.java");
   }
 
+  public void testMethodWithLeftParTailTypeNoPairBrace() throws Exception {
+    final boolean old = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET;
+    CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET = false;
+
+    try {
+      configureByFile("/codeInsight/completion/normal/" + getTestName(false) + ".java");
+      selectItem(myItems[0], '(');
+      checkResultByFile("/codeInsight/completion/normal/" + getTestName(false) + "_after.java");
+
+      //no tail type should work the normal way
+      configureByFile("/codeInsight/completion/normal/" + getTestName(false) + ".java");
+      selectItem(myItems[0]);
+      checkResultByFile("/codeInsight/completion/normal/" + getTestName(false) + "_after2.java");
+    }
+    finally {
+      CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET = old;
+    }
+  }
+
   public void testExcessSpaceInTypeCast() throws Throwable {
    configureByFile("/codeInsight/completion/normal/" + getTestName(false) + ".java");
    selectItem(myItems[0]);
index 939c63455152e9746d58a16eb2671c5b0e560142..c958c6e5b525796b4e5172554a9f183dc19ca8a2 100644 (file)
@@ -72,9 +72,9 @@ public abstract class ParenthesesInsertHandler<T extends LookupElement> implemen
     final Document document = editor.getDocument();
     PsiElement element = findNextToken(context);
 
-    final boolean hasParams = placeCaretInsideParentheses(context, item);
-
     final char completionChar = context.getCompletionChar();
+    final boolean putCaretInside = completionChar == '(' || placeCaretInsideParentheses(context, item);
+
     if (completionChar == '(') {
       context.setAddCompletionChar(false);
     }
@@ -99,7 +99,7 @@ public abstract class ParenthesesInsertHandler<T extends LookupElement> implemen
       if (isToken(last, ")")) {
         int rparenthOffset = last.getTextRange().getStartOffset();
         context.setTailOffset(rparenthOffset + 1);
-        if (!hasParams) {
+        if (!putCaretInside) {
           for (int i = lparenthOffset + 1; i < rparenthOffset; i++) {
             if (!Character.isWhitespace(document.getCharsSequence().charAt(i))) {
               return;
@@ -132,7 +132,7 @@ public abstract class ParenthesesInsertHandler<T extends LookupElement> implemen
       tailOffset = TailType.insertChar(editor, tailOffset, ' ');
     }
     document.insertString(tailOffset, ")");
-    editor.getCaretModel().moveToOffset(hasParams ? caret : context.getTailOffset());
+    editor.getCaretModel().moveToOffset(putCaretInside ? caret : context.getTailOffset());
   }
 
   @Nullable
index 0ace8f68f796611f9fa91b45c126d4b6ee9dadf6..529a0ccf047b0f755ac2172af147f6416a760d88 100644 (file)
@@ -3,4 +3,4 @@ class Foo {
   def bar
 }
 
-new Foo().getBar()<caret>
+new Foo().getBar(<caret>)