From eed9138dbd5649d046faf79285e3057ea21510c9 Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 9 Mar 2010 12:17:01 +0000 Subject: [PATCH] IDEA-27091 When finishing method lookup item with '(', put caret after this '(' even if method has no parameters 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 --- .../simple/PsiMethodInsertHandler.java | 33 +++++++++++++------ .../MethodWithLeftParTailTypeNoPairBrace.java | 8 +++++ ...dWithLeftParTailTypeNoPairBrace_after.java | 8 +++++ ...WithLeftParTailTypeNoPairBrace_after2.java | 8 +++++ .../MethodWithLeftParTailType_after.java | 2 +- .../completion/NormalCompletionTest.java | 19 +++++++++++ .../util/ParenthesesInsertHandler.java | 8 ++--- .../FinishMethodWithLParen_after.groovy | 2 +- 8 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after2.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/simple/PsiMethodInsertHandler.java b/java/java-impl/src/com/intellij/codeInsight/completion/simple/PsiMethodInsertHandler.java index 0f9f5f2bdf39..928ddc97fe21 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/simple/PsiMethodInsertHandler.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/simple/PsiMethodInsertHandler.java @@ -53,24 +53,26 @@ public class PsiMethodInsertHandler implements InsertHandler 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 +} +} \ 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 index 000000000000..c976d92117e9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after.java @@ -0,0 +1,8 @@ +class MyClass { + +void foo() {} + +{ + foo( +} +} \ 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 index 000000000000..b7841df3ff15 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailTypeNoPairBrace_after2.java @@ -0,0 +1,8 @@ +class MyClass { + +void foo() {} + +{ + foo(); +} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailType_after.java b/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailType_after.java index b7841df3ff15..7680812e2f7c 100644 --- a/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailType_after.java +++ b/java/java-tests/testData/codeInsight/completion/normal/MethodWithLeftParTailType_after.java @@ -3,6 +3,6 @@ class MyClass { void foo() {} { - foo(); + foo(); } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.java index c0103a0adde7..f6a2431c26d6 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.java @@ -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]); diff --git a/platform/lang-api/src/com/intellij/codeInsight/completion/util/ParenthesesInsertHandler.java b/platform/lang-api/src/com/intellij/codeInsight/completion/util/ParenthesesInsertHandler.java index 939c63455152..c958c6e5b525 100644 --- a/platform/lang-api/src/com/intellij/codeInsight/completion/util/ParenthesesInsertHandler.java +++ b/platform/lang-api/src/com/intellij/codeInsight/completion/util/ParenthesesInsertHandler.java @@ -72,9 +72,9 @@ public abstract class ParenthesesInsertHandler 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 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 implemen tailOffset = TailType.insertChar(editor, tailOffset, ' '); } document.insertString(tailOffset, ")"); - editor.getCaretModel().moveToOffset(hasParams ? caret : context.getTailOffset()); + editor.getCaretModel().moveToOffset(putCaretInside ? caret : context.getTailOffset()); } @Nullable diff --git a/plugins/groovy/testdata/groovy/completion/FinishMethodWithLParen_after.groovy b/plugins/groovy/testdata/groovy/completion/FinishMethodWithLParen_after.groovy index 0ace8f68f796..529a0ccf047b 100644 --- a/plugins/groovy/testdata/groovy/completion/FinishMethodWithLParen_after.groovy +++ b/plugins/groovy/testdata/groovy/completion/FinishMethodWithLParen_after.groovy @@ -3,4 +3,4 @@ class Foo { def bar } -new Foo().getBar() +new Foo().getBar()