diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassAndFunFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassAndFunFix.java index 55b6e81f435..847e54b626b 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassAndFunFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassAndFunFix.java @@ -17,6 +17,7 @@ package org.jetbrains.jet.plugin.quickfix; import com.google.common.base.Function; +import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -46,6 +47,7 @@ import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetSimpleNameExpression; import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.FqName; +import org.jetbrains.jet.lang.resolve.ImportPath; import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.actions.JetAddImportAction; import org.jetbrains.jet.plugin.caches.JetCacheManager; @@ -61,14 +63,14 @@ import java.util.*; public class ImportClassAndFunFix extends JetHintAction implements HighPriorityAction { @NotNull - private final List suggestions; + private final Collection suggestions; public ImportClassAndFunFix(@NotNull JetSimpleNameExpression element) { super(element); suggestions = computeSuggestions(element); } - private static List computeSuggestions(@NotNull JetSimpleNameExpression element) { + private static Collection computeSuggestions(@NotNull JetSimpleNameExpression element) { final PsiFile file = element.getContainingFile(); if (!(file instanceof JetFile)) { return Collections.emptyList(); @@ -82,12 +84,18 @@ public class ImportClassAndFunFix extends JetHintAction assert referenceName != null; - final ArrayList result = Lists.newArrayList(); + List result = Lists.newArrayList(); result.addAll(getClassNames(referenceName, file.getProject())); result.addAll(getJetTopLevelFunctions(referenceName, element, file.getProject())); result.addAll(getJetExtensionFunctions(referenceName, element, file.getProject())); - return result; + return Collections2.filter(result, new Predicate() { + @Override + public boolean apply(@Nullable FqName fqName) { + assert fqName != null; + return ImportInsertHelper.doNeedImport(new ImportPath(fqName, false), null, (JetFile) file); + } + }); } private static Collection getJetTopLevelFunctions(@NotNull String referenceName, JetSimpleNameExpression expression, @NotNull Project project) { @@ -180,7 +188,7 @@ public class ImportClassAndFunFix extends JetHintAction } if (!ApplicationManager.getApplication().isUnitTestMode()) { - String hintText = ShowAutoImportPass.getMessage(suggestions.size() > 1, suggestions.get(0).getFqName()); + String hintText = ShowAutoImportPass.getMessage(suggestions.size() > 1, suggestions.iterator().next().getFqName()); HintManager.getInstance().showQuestionHint( editor, hintText, diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java index 3d476bd6c44..37d9b4ef71e 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java @@ -73,32 +73,14 @@ public class ImportInsertHelper { } public static void addImportDirective(@NotNull ImportPath importPath, @Nullable String aliasName, @NotNull JetFile file) { - - if (QualifiedNamesUtil.getFirstSegment(importPath.fqnPart().getFqName()).equals(JavaDescriptorResolver.JAVA_ROOT)) { - FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart()); - importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder()); - } - - if (isImportedByDefault(importPath, aliasName, JetPsiUtil.getFQName(file))) { + if (!doNeedImport(importPath, aliasName, file)) { return; } JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath, aliasName); - List importDirectives = file.getImportDirectives(); if (!importDirectives.isEmpty()) { - - // Check if import is already present - for (JetImportDirective directive : importDirectives) { - ImportPath existentImportPath = JetPsiUtil.getImportPath(directive); - if (directive.getAliasName() == null && aliasName == null) { - if (existentImportPath != null && QualifiedNamesUtil.isImported(existentImportPath, importPath)) { - return; - } - } - } - JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1); lastDirective.getParent().addAfter(newDirective, lastDirective); } @@ -110,7 +92,7 @@ public class ImportInsertHelper { /** * Check that import is useless. */ - private static boolean isImportedByDefault(@NotNull ImportPath importPath, String aliasName, @NotNull FqName filePackageFqn) { + private static boolean isImportedByDefault(@NotNull ImportPath importPath, @Nullable String aliasName, @NotNull FqName filePackageFqn) { if (importPath.fqnPart().isRoot()) { return true; } @@ -143,4 +125,31 @@ public class ImportInsertHelper { return false; } + + public static boolean doNeedImport(@NotNull ImportPath importPath, @Nullable String aliasName, @NotNull JetFile file) { + if (QualifiedNamesUtil.getFirstSegment(importPath.fqnPart().getFqName()).equals(JavaDescriptorResolver.JAVA_ROOT)) { + FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart()); + importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder()); + } + + if (isImportedByDefault(importPath, null, JetPsiUtil.getFQName(file))) { + return false; + } + + List importDirectives = file.getImportDirectives(); + + if (!importDirectives.isEmpty()) { + // Check if import is already present + for (JetImportDirective directive : importDirectives) { + ImportPath existentImportPath = JetPsiUtil.getImportPath(directive); + if (directive.getAliasName() == null && aliasName == null) { + if (existentImportPath != null && QualifiedNamesUtil.isImported(existentImportPath, importPath)) { + return false; + } + } + } + } + + return true; + } } diff --git a/idea/testData/quickfix/autoImports/afterNoImportAlreadyImported.kt b/idea/testData/quickfix/autoImports/afterNoImportAlreadyImported.kt new file mode 100644 index 00000000000..7a44d5248c4 --- /dev/null +++ b/idea/testData/quickfix/autoImports/afterNoImportAlreadyImported.kt @@ -0,0 +1,9 @@ +// "Import Class" "false" + +package Teting + +import Teting.test.someFun + +fun main(args : Array) { + someFun +} \ No newline at end of file diff --git a/idea/testData/quickfix/autoImports/beforeNoImportForAlreadyImported.kt b/idea/testData/quickfix/autoImports/beforeNoImportForAlreadyImported.kt new file mode 100644 index 00000000000..645e8fe534b --- /dev/null +++ b/idea/testData/quickfix/autoImports/beforeNoImportForAlreadyImported.kt @@ -0,0 +1,9 @@ +// "Import Class" "false" + +package Teting + +import Teting.test.someFun + +fun main(args : Array) { + someFun +} \ No newline at end of file