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 c5fcaf89d59..e2cd5ff0bb5 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java +++ b/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java @@ -14,6 +14,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.NamedFunctionDescriptor; import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.lang.psi.JetImportDirective; import org.jetbrains.jet.lang.psi.JetQualifiedExpression; import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.plugin.completion.JetLookupObject; @@ -48,31 +49,44 @@ public class JetFunctionInsertHandler implements InsertHandler { return; } + if (shouldAddParenthesis(element)) { + addParenthesis(context, item, element); + } + + addImport(context, item); + } + + private static boolean shouldAddParenthesis(PsiElement element) { + return PsiTreeUtil.getParentOfType(element, JetImportDirective.class) == null; + } + + private void addParenthesis(InsertionContext context, LookupElement item, PsiElement offsetElement) { + int startOffset = context.getStartOffset(); + int lookupStringLength = item.getLookupString().length(); int endOffset = startOffset + lookupStringLength; Document document = context.getDocument(); boolean bothParentheses = false; String documentText = document.getText(); - if (documentText.charAt(endOffset) != '(') { - //do not insert () if it already exists. + + if (!(endOffset < documentText.length() && documentText.charAt(endOffset) == '(')) { + // Insert () if it's not already exist document.insertString(endOffset, "()"); bothParentheses = true; - } else if (documentText.charAt(endOffset + 1) == ')') { + } else if (endOffset + 1 < documentText.length() && documentText.charAt(endOffset) == ')') { bothParentheses = true; } Editor editor = context.getEditor(); if (caretPosition == CaretPosition.IN_BRACKETS || !bothParentheses) { editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + 1); - AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(editor, element); + AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(editor, offsetElement); } else { editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + 2); } PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument()); - - addImport(context, item); } private static void addImport(final InsertionContext context, final @NotNull LookupElement item) { diff --git a/idea/testData/completion/handlers/multifile/NoParenthesisInImports-1.kt b/idea/testData/completion/handlers/multifile/NoParenthesisInImports-1.kt new file mode 100644 index 00000000000..0f4e7eb9580 --- /dev/null +++ b/idea/testData/completion/handlers/multifile/NoParenthesisInImports-1.kt @@ -0,0 +1,3 @@ +package testing + +import testing.some.someF \ No newline at end of file diff --git a/idea/testData/completion/handlers/multifile/NoParenthesisInImports-2.kt b/idea/testData/completion/handlers/multifile/NoParenthesisInImports-2.kt new file mode 100644 index 00000000000..a2b42a9b375 --- /dev/null +++ b/idea/testData/completion/handlers/multifile/NoParenthesisInImports-2.kt @@ -0,0 +1,4 @@ +package testing.some + +fun someFun() { +} \ No newline at end of file diff --git a/idea/testData/completion/handlers/multifile/NoParenthesisInImports.kt.after b/idea/testData/completion/handlers/multifile/NoParenthesisInImports.kt.after new file mode 100644 index 00000000000..ad1b9dc1f72 --- /dev/null +++ b/idea/testData/completion/handlers/multifile/NoParenthesisInImports.kt.after @@ -0,0 +1,3 @@ +package testing + +import testing.some.someFun \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/completion/handlers/CompletionMultifileHandlerTest.java b/idea/tests/org/jetbrains/jet/completion/handlers/CompletionMultifileHandlerTest.java index 2257967233d..ba8ddc3c132 100644 --- a/idea/tests/org/jetbrains/jet/completion/handlers/CompletionMultifileHandlerTest.java +++ b/idea/tests/org/jetbrains/jet/completion/handlers/CompletionMultifileHandlerTest.java @@ -18,6 +18,10 @@ public class CompletionMultifileHandlerTest extends CompletionTestCase { doTest(); } + public void testNoParenthesisInImports() { + doTest(); + } + public void doTest() { String fileName = getTestName(false); try {