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 37a6c19cf39..c3840eb2675 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java +++ b/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java @@ -1,10 +1,13 @@ package org.jetbrains.jet.plugin.completion.handlers; +import com.intellij.codeInsight.AutoPopupController; 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.psi.PsiDocumentManager; +import com.intellij.psi.PsiElement; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.DescriptorsUtils; import org.jetbrains.jet.lang.descriptors.NamedFunctionDescriptor; @@ -34,14 +37,26 @@ public class JetFunctionInsertHandler implements InsertHandler { } int startOffset = context.getStartOffset(); + PsiElement element = context.getFile().findElementAt(startOffset); + if (element == null) return; int lookupStringLength = item.getLookupString().length(); int endOffset = startOffset + lookupStringLength; + Document document = context.getDocument(); - context.getDocument().insertString(endOffset, "()"); + boolean bothParentheses = false; + String documentText = document.getText(); + if (documentText.charAt(endOffset) != '(') { + //do not insert () if it already exists. + document.insertString(endOffset, "()"); + bothParentheses = true; + } else if (documentText.charAt(endOffset + 1) == ')') { + bothParentheses = true; + } Editor editor = context.getEditor(); - if (caretPosition == CaretPosition.IN_BRACKETS) { + if (caretPosition == CaretPosition.IN_BRACKETS || !bothParentheses) { editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + 1); + AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(editor, element); } else { editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + 2); } diff --git a/idea/testData/completion/handlers/ExistingNoParamsFunction.kt b/idea/testData/completion/handlers/ExistingNoParamsFunction.kt new file mode 100644 index 00000000000..b6abcee0369 --- /dev/null +++ b/idea/testData/completion/handlers/ExistingNoParamsFunction.kt @@ -0,0 +1,10 @@ +package testing.handlers + +fun test() { +} + +fun other() { + te() +} + +// INSERT: test \ No newline at end of file diff --git a/idea/testData/completion/handlers/ExistingNoParamsFunction.kt.after b/idea/testData/completion/handlers/ExistingNoParamsFunction.kt.after new file mode 100644 index 00000000000..92da48be6da --- /dev/null +++ b/idea/testData/completion/handlers/ExistingNoParamsFunction.kt.after @@ -0,0 +1,10 @@ +package testing.handlers + +fun test() { +} + +fun other() { + test() +} + +// INSERT: test \ No newline at end of file diff --git a/idea/testData/completion/handlers/ExistingSingleBrackets.kt b/idea/testData/completion/handlers/ExistingSingleBrackets.kt new file mode 100644 index 00000000000..36a6633ffa9 --- /dev/null +++ b/idea/testData/completion/handlers/ExistingSingleBrackets.kt @@ -0,0 +1,13 @@ +package Test.MyTest + +class A { + class object { + public fun testOther(a: Int) { + + } + } +} + +fun testMy() { + A.testOthe() +} \ No newline at end of file diff --git a/idea/testData/completion/handlers/ExistingSingleBrackets.kt.after b/idea/testData/completion/handlers/ExistingSingleBrackets.kt.after new file mode 100644 index 00000000000..9c8dcb84ac9 --- /dev/null +++ b/idea/testData/completion/handlers/ExistingSingleBrackets.kt.after @@ -0,0 +1,13 @@ +package Test.MyTest + +class A { + class object { + public fun testOther(a: Int) { + + } + } +} + +fun testMy() { + A.testOther() +} \ 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 e5d0258176b..c381d64a3d6 100644 --- a/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java +++ b/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java @@ -11,6 +11,10 @@ import java.io.File; */ public class CompletionHandlerTest extends LightCompletionTestCase { + public void testExistingNoParamsFunction() { + doTest(); + } + public void testNoParamsFunction() { doTest(); } @@ -33,6 +37,10 @@ public class CompletionHandlerTest extends LightCompletionTestCase { checkResultByFile("SingleBrackets.kt.after"); } + public void testExistingSingleBrackets() { + doTest(); + } + public void doTest() { String fileName = getTestName(false); try {