don't insert pair ) when choosing a lookup item by ( before an identifier
authorpeter <peter@jetbrains.com>
Mon, 30 Jan 2012 16:01:57 +0000 (17:01 +0100)
committerpeter <peter@jetbrains.com>
Mon, 30 Jan 2012 16:07:31 +0000 (17:07 +0100)
java/java-tests/testData/codeInsight/completion/normal/NoClosingWhenChoosingWithParenBeforeIdentifier.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/completion/normal/NoClosingWhenChoosingWithParenBeforeIdentifier_after.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy
platform/lang-api/src/com/intellij/codeInsight/completion/util/ParenthesesInsertHandler.java

diff --git a/java/java-tests/testData/codeInsight/completion/normal/NoClosingWhenChoosingWithParenBeforeIdentifier.java b/java/java-tests/testData/codeInsight/completion/normal/NoClosingWhenChoosingWithParenBeforeIdentifier.java
new file mode 100644 (file)
index 0000000..e9682a8
--- /dev/null
@@ -0,0 +1,5 @@
+class Foo {
+  {
+    toS<caret>xxx
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/completion/normal/NoClosingWhenChoosingWithParenBeforeIdentifier_after.java b/java/java-tests/testData/codeInsight/completion/normal/NoClosingWhenChoosingWithParenBeforeIdentifier_after.java
new file mode 100644 (file)
index 0000000..489d121
--- /dev/null
@@ -0,0 +1,5 @@
+class Foo {
+  {
+    toString(<caret>xxx
+  }
+}
\ No newline at end of file
index 34f9775862df7b63d4805a394d410134f1315f9e..9e74675a1ec28e54155a5e44c4d16e7287914833 100644 (file)
@@ -1264,4 +1264,10 @@ public class ListUtils {
     checkResult()
   }
 
+  public void testNoClosingWhenChoosingWithParenBeforeIdentifier() {
+    configure()
+    myFixture.type '('
+    checkResult()
+  }
+
 }
index e5e2ffd8acf55e0100199db5f485435cabf0289a..8ce543e7f688c3bdabbcc4947eafa1b587f5c8b5 100644 (file)
  */
 package com.intellij.codeInsight.completion.util;
 
-import com.intellij.codeInsight.TailType;
 import com.intellij.codeInsight.completion.InsertHandler;
 import com.intellij.codeInsight.completion.InsertionContext;
 import com.intellij.codeInsight.lookup.LookupElement;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiWhiteSpace;
@@ -58,22 +58,22 @@ public abstract class ParenthesesInsertHandler<T extends LookupElement> implemen
 
   private final boolean mySpaceBeforeParentheses;
   private final boolean mySpaceBetweenParentheses;
-  private final boolean myInsertRightParenthesis;
+  private final boolean myMayInsertRightParenthesis;
   private final boolean myAllowParametersOnNextLine;
 
   protected ParenthesesInsertHandler(final boolean spaceBeforeParentheses,
                                      final boolean spaceBetweenParentheses,
-                                     final boolean insertRightParenthesis) {
-    this(spaceBeforeParentheses, spaceBetweenParentheses, insertRightParenthesis, false);
+                                     final boolean mayInsertRightParenthesis) {
+    this(spaceBeforeParentheses, spaceBetweenParentheses, mayInsertRightParenthesis, false);
   }
 
   protected ParenthesesInsertHandler(boolean spaceBeforeParentheses,
                                      boolean spaceBetweenParentheses,
-                                     boolean insertRightParenthesis,
+                                     boolean mayInsertRightParenthesis,
                                      boolean allowParametersOnNextLine) {
     mySpaceBeforeParentheses = spaceBeforeParentheses;
     mySpaceBetweenParentheses = spaceBetweenParentheses;
-    myInsertRightParenthesis = insertRightParenthesis;
+    myMayInsertRightParenthesis = mayInsertRightParenthesis;
     myAllowParametersOnNextLine = allowParametersOnNextLine;
   }
 
@@ -138,7 +138,15 @@ public abstract class ParenthesesInsertHandler<T extends LookupElement> implemen
       editor.getCaretModel().moveToOffset(context.getTailOffset());
     }
 
-    if (!myInsertRightParenthesis) return;
+    if (!myMayInsertRightParenthesis) return;
+
+    if (context.getCompletionChar() == '(') {
+      //todo use BraceMatchingUtil.isPairedBracesAllowedBeforeTypeInFileType
+      int tail = context.getTailOffset();
+      if (tail < document.getTextLength() && StringUtil.isJavaIdentifierPart(document.getCharsSequence().charAt(tail))) {
+        return;
+      }
+    }
 
     document.insertString(context.getTailOffset(), getSpace(mySpaceBetweenParentheses) + ")");
     if (!putCaretInside) {