From 5cf708d0560629bd8daf50208937c34d63d6a4eb Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 8 Nov 2012 18:29:29 +0400 Subject: [PATCH] Don't insert fqn even if current element is resolved into constructor --- .../plugin/quickfix/ImportInsertHelper.java | 18 +++++++++++++----- ...nsertImportIfResolvedIntoJavaConstructor.kt | 3 +++ ...mportIfResolvedIntoJavaConstructor.kt.after | 3 +++ .../handlers/CompletionHandlerTest.java | 4 ++++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 idea/testData/completion/handlers/DoNotInsertImportIfResolvedIntoJavaConstructor.kt create mode 100644 idea/testData/completion/handlers/DoNotInsertImportIfResolvedIntoJavaConstructor.kt.after diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java index 25f54c9643d..d5d26ab060c 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java @@ -18,10 +18,7 @@ package org.jetbrains.jet.plugin.quickfix; import com.intellij.openapi.editor.Document; import com.intellij.openapi.util.TextRange; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiReference; +import com.intellij.psi.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.DefaultModuleConfiguration; @@ -92,7 +89,18 @@ public class ImportInsertHelper { PsiElement target = reference.resolve(); if (target != null) { boolean same = file.getManager().areElementsEquivalent(target, targetElement); - same |= target instanceof PsiClass && importFqn.getFqName().equals(((PsiClass)target).getQualifiedName()); + + if (!same) { + same = target instanceof PsiClass && importFqn.getFqName().equals(((PsiClass)target).getQualifiedName()); + } + + if (!same) { + if (target instanceof PsiMethod) { + PsiMethod method = (PsiMethod) target; + same = (method.isConstructor() && file.getManager().areElementsEquivalent(method.getContainingClass(), targetElement)); + } + } + if (!same) { Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file); TextRange refRange = reference.getElement().getTextRange(); diff --git a/idea/testData/completion/handlers/DoNotInsertImportIfResolvedIntoJavaConstructor.kt b/idea/testData/completion/handlers/DoNotInsertImportIfResolvedIntoJavaConstructor.kt new file mode 100644 index 00000000000..a2ec4592038 --- /dev/null +++ b/idea/testData/completion/handlers/DoNotInsertImportIfResolvedIntoJavaConstructor.kt @@ -0,0 +1,3 @@ +fun main(args: Array) { + throw IllegalAccessExceptio() //Press Ctrl+Space and select it +} \ No newline at end of file diff --git a/idea/testData/completion/handlers/DoNotInsertImportIfResolvedIntoJavaConstructor.kt.after b/idea/testData/completion/handlers/DoNotInsertImportIfResolvedIntoJavaConstructor.kt.after new file mode 100644 index 00000000000..13d0a0cebad --- /dev/null +++ b/idea/testData/completion/handlers/DoNotInsertImportIfResolvedIntoJavaConstructor.kt.after @@ -0,0 +1,3 @@ +fun main(args: Array) { + throw IllegalAccessException() //Press Ctrl+Space and select it +} \ 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 a05111e6e56..d606091a370 100644 --- a/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java +++ b/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java @@ -45,6 +45,10 @@ public class CompletionHandlerTest extends LightCompletionTestCase { doTest(); } + public void testDoNotInsertImportIfResolvedIntoJavaConstructor() { + doTest(); + } + public void testNonStandardArray() { doTest(CompletionType.BASIC, 2, "Array", "java.lang.reflect", '\n'); }