KT-1231 Don't insert () after completion function in imports

This commit is contained in:
Nikolay Krasko
2012-02-13 16:23:40 +04:00
parent 936144a193
commit 4dff90734e
5 changed files with 34 additions and 6 deletions
@@ -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<LookupElement> {
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) {
@@ -0,0 +1,3 @@
package testing
import testing.some.someF<caret>
@@ -0,0 +1,4 @@
package testing.some
fun someFun() {
}
@@ -0,0 +1,3 @@
package testing
import testing.some.someFun
@@ -18,6 +18,10 @@ public class CompletionMultifileHandlerTest extends CompletionTestCase {
doTest();
}
public void testNoParenthesisInImports() {
doTest();
}
public void doTest() {
String fileName = getTestName(false);
try {