diff --git a/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java b/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java index 97eb03afbea..5a29535986c 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java +++ b/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java @@ -96,7 +96,9 @@ public class JetFunctionInsertHandler implements InsertHandler { String documentText = document.getText(); - boolean braces = bracketType == BracketType.BRACES && context.getCompletionChar() != '('; + char completionChar = context.getCompletionChar(); + + boolean braces = bracketType == BracketType.BRACES && completionChar != '('; char openingBracket = braces ? '{' : '('; char closingBracket = braces ? '}' : ')'; @@ -106,7 +108,7 @@ public class JetFunctionInsertHandler implements InsertHandler { if (openingBracketIndex == -1) { // Insert ()/{} if it's not already exist if (braces) { - if (context.getCompletionChar() == ' ') { + if (completionChar == ' ') { context.setAddCompletionChar(false); } @@ -133,7 +135,11 @@ public class JetFunctionInsertHandler implements InsertHandler { int closeBracketIndex = indexOfSkippingSpace(documentText, closingBracket, openingBracketIndex + 1); Editor editor = context.getEditor(); - if (caretPosition == CaretPosition.IN_BRACKETS || closeBracketIndex == -1) { + + // Satisfy TypedHandler.handleRParen() algorithm for preventing doubling ')' char if user typed "()" manually. + boolean forcePlaceCaretIntoParentheses = completionChar == '('; + + if (caretPosition == CaretPosition.IN_BRACKETS || forcePlaceCaretIntoParentheses || closeBracketIndex == -1) { editor.getCaretModel().moveToOffset(openingBracketIndex + 1 + inBracketsShift); AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(editor, offsetElement); } diff --git a/idea/testData/completion/handlers/InsertFunctionWithBothParentheses.kt b/idea/testData/completion/handlers/InsertFunctionWithBothParentheses.kt new file mode 100644 index 00000000000..38be6ae5eef --- /dev/null +++ b/idea/testData/completion/handlers/InsertFunctionWithBothParentheses.kt @@ -0,0 +1,7 @@ +// KT-1968 Double closing parentheses entered when completing unit function +package some + +fun test() = 12 +fun test1() + +val a = \ No newline at end of file diff --git a/idea/testData/completion/handlers/InsertFunctionWithBothParentheses.kt.after b/idea/testData/completion/handlers/InsertFunctionWithBothParentheses.kt.after new file mode 100644 index 00000000000..b1ab688f280 --- /dev/null +++ b/idea/testData/completion/handlers/InsertFunctionWithBothParentheses.kt.after @@ -0,0 +1,7 @@ +// KT-1968 Double closing parentheses entered when completing unit function +package some + +fun test() = 12 +fun test1() + +val a = test() \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java b/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java index 2ada08618d4..904cb855779 100644 --- a/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java +++ b/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java @@ -92,6 +92,12 @@ public class CompletionHandlerTest extends LightCompletionTestCase { doTest(CompletionType.BASIC, 2, null, null, ' '); } + public void testInsertFunctionWithBothParentheses() { + configureByFile(getBeforeFileName()); + type("test()"); + checkResultByFile(getAfterFileName()); + } + public void testInsertImportOnTab() { doTest(CompletionType.BASIC, 2, "ArrayList", null, '\t'); }