diff --git a/idea/src/org/jetbrains/jet/plugin/editor/importOptimizer/JetImportOptimizer.java b/idea/src/org/jetbrains/jet/plugin/editor/importOptimizer/JetImportOptimizer.java index ed5e6b09fef..d528c38288d 100644 --- a/idea/src/org/jetbrains/jet/plugin/editor/importOptimizer/JetImportOptimizer.java +++ b/idea/src/org/jetbrains/jet/plugin/editor/importOptimizer/JetImportOptimizer.java @@ -16,6 +16,7 @@ package org.jetbrains.jet.plugin.editor.importOptimizer; +import com.google.common.collect.Lists; import com.intellij.lang.ImportOptimizer; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.progress.ProgressIndicatorProvider; @@ -42,6 +43,8 @@ import org.jetbrains.jet.util.QualifiedNamesUtil; import java.util.*; +import static org.jetbrains.jet.plugin.quickfix.ImportInsertHelper.doNeedImport; + public class JetImportOptimizer implements ImportOptimizer { @Override public boolean supports(PsiFile file) { @@ -58,31 +61,10 @@ public class JetImportOptimizer implements ImportOptimizer { final JetFile jetFile = (JetFile) file; final Set usedQualifiedNames = extractUsedQualifiedNames(jetFile); - final List sortedDirectives = jetFile.getImportDirectives(); - Collections.sort(sortedDirectives, new Comparator() { - @Override - public int compare(JetImportDirective directive1, JetImportDirective directive2) { - ImportPath firstPath = JetPsiUtil.getImportPath(directive1); - ImportPath secondPath = JetPsiUtil.getImportPath(directive2); + final List directives = jetFile.getImportDirectives(); - if (firstPath == null || secondPath == null) { - return firstPath == null && secondPath == null ? 0 : - firstPath == null ? -1 : - 1; - } - - // import bla.bla.bla.* should be before import bla.bla.bla.something - if (firstPath.isAllUnder() && !secondPath.isAllUnder() && firstPath.fqnPart().equals(secondPath.fqnPart().parent())) { - return -1; - } - - if (!firstPath.isAllUnder() && secondPath.isAllUnder() && secondPath.fqnPart().equals(firstPath.fqnPart().parent())) { - return 1; - } - - return firstPath.getPathStr().compareTo(secondPath.getPathStr()); - } - }); + final List directivesBeforeCurrent = Lists.newArrayList(); + final List directivesAfterCurrent = jetFile.getImportDirectives(); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override @@ -104,14 +86,19 @@ public class JetImportOptimizer implements ImportOptimizer { } // Insert back only necessary imports in correct order - for (JetImportDirective anImport : sortedDirectives) { + for (JetImportDirective anImport : directives) { + directivesAfterCurrent.remove(anImport); + ImportPath importPath = JetPsiUtil.getImportPath(anImport); if (importPath == null) { continue; } - if (isUseful(importPath, usedQualifiedNames)) { - ImportInsertHelper.addImportDirective(importPath, jetFile); + if (isUseful(importPath, usedQualifiedNames) && + doNeedImport(importPath, jetFile, directivesBeforeCurrent) && + doNeedImport(importPath, jetFile, directivesAfterCurrent)) { + ImportInsertHelper.writeImportToFile(importPath, jetFile); + directivesBeforeCurrent.add(anImport); } } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java index e9f2df7f592..e9355e85fa8 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java @@ -20,7 +20,6 @@ import com.intellij.openapi.editor.Document; import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.DefaultModuleConfiguration; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; @@ -117,6 +116,10 @@ public class ImportInsertHelper { return; } + writeImportToFile(importPath, file); + } + + public static void writeImportToFile(ImportPath importPath, JetFile file) { JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath); List importDirectives = file.getImportDirectives(); @@ -179,6 +182,10 @@ public class ImportInsertHelper { } public static boolean doNeedImport(@NotNull ImportPath importPath, @NotNull JetFile file) { + return doNeedImport(importPath, file, file.getImportDirectives()); + } + + public static boolean doNeedImport(@NotNull ImportPath importPath, @NotNull JetFile file, List importDirectives) { if (importPath.fqnPart().firstSegmentIs(JavaDescriptorResolver.JAVA_ROOT)) { FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart()); importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder(), importPath.getAlias()); @@ -188,8 +195,6 @@ public class ImportInsertHelper { return false; } - List importDirectives = file.getImportDirectives(); - if (!importDirectives.isEmpty()) { // Check if import is already present for (JetImportDirective directive : importDirectives) { diff --git a/idea/testData/editor/optimizeImports/DuplicatedImports.kt b/idea/testData/editor/optimizeImports/DuplicatedImports.kt new file mode 100644 index 00000000000..26a57bd184f --- /dev/null +++ b/idea/testData/editor/optimizeImports/DuplicatedImports.kt @@ -0,0 +1,8 @@ +import java.util.ArrayList +import java.util.ArrayList + +class Action { + fun test() { + val test : ArrayList? = null + } +} \ No newline at end of file diff --git a/idea/testData/editor/optimizeImports/DuplicatedImports_after.kt b/idea/testData/editor/optimizeImports/DuplicatedImports_after.kt new file mode 100644 index 00000000000..a8214bf5e72 --- /dev/null +++ b/idea/testData/editor/optimizeImports/DuplicatedImports_after.kt @@ -0,0 +1,7 @@ +import java.util.ArrayList + +class Action { + fun test() { + val test : ArrayList? = null + } +} \ No newline at end of file diff --git a/idea/testData/editor/optimizeImports/Kt1850InnerClass.kt b/idea/testData/editor/optimizeImports/Kt1850InnerClass.kt index a94ad861478..9f8e28f1fe9 100644 --- a/idea/testData/editor/optimizeImports/Kt1850InnerClass.kt +++ b/idea/testData/editor/optimizeImports/Kt1850InnerClass.kt @@ -1,7 +1,7 @@ import java import java.util -import java.util.Map import java.util.List +import java.util.Map import java.util.Map.Entry var x : Map.Entry, String>? = null diff --git a/idea/testData/editor/optimizeImports/RemoveImportsIfGeneralBefore.kt b/idea/testData/editor/optimizeImports/RemoveImportsIfGeneralBefore.kt new file mode 100644 index 00000000000..73683fae8ab --- /dev/null +++ b/idea/testData/editor/optimizeImports/RemoveImportsIfGeneralBefore.kt @@ -0,0 +1,9 @@ +import java.util.* +import java.util.ArrayList +import java.util.HashSet + +class Action { + fun test(hash: HashSet) { + val test : ArrayList? = null + } +} \ No newline at end of file diff --git a/idea/testData/editor/optimizeImports/RemoveImportsIfGeneralBefore_after.kt b/idea/testData/editor/optimizeImports/RemoveImportsIfGeneralBefore_after.kt new file mode 100644 index 00000000000..02236aad175 --- /dev/null +++ b/idea/testData/editor/optimizeImports/RemoveImportsIfGeneralBefore_after.kt @@ -0,0 +1,7 @@ +import java.util.* + +class Action { + fun test(hash: HashSet) { + val test : ArrayList? = null + } +} \ No newline at end of file diff --git a/idea/testData/editor/optimizeImports/UnusedImports.kt b/idea/testData/editor/optimizeImports/UnusedImports.kt index e1a5b931d66..349e61ee9f2 100644 --- a/idea/testData/editor/optimizeImports/UnusedImports.kt +++ b/idea/testData/editor/optimizeImports/UnusedImports.kt @@ -3,11 +3,11 @@ import com.sun.org.apache.xpath.internal.operations.And import java.lang.StringBuilder import java.net.HttpRetryException import java.util.ArrayList -import kotlin.util.measureTimeMillis import java.util.HashMap import kotlin.test.asserter import kotlin.test.Asserter import kotlin.Assertions +import kotlin.util.measureTimeMillis class Action { fun test() { diff --git a/idea/testData/editor/optimizeImports/WithAliases.kt b/idea/testData/editor/optimizeImports/WithAliases.kt index 5a40a3b86c3..1d408fc05b6 100644 --- a/idea/testData/editor/optimizeImports/WithAliases.kt +++ b/idea/testData/editor/optimizeImports/WithAliases.kt @@ -1,6 +1,6 @@ +import java.io as JavaIO import java.util.ArrayList import java.util.ArrayList as SomeThing -import java.io as JavaIO import java.io.* class Action { diff --git a/idea/tests/org/jetbrains/jet/plugin/importOptimizer/OptimizeImportsTest.java b/idea/tests/org/jetbrains/jet/plugin/importOptimizer/OptimizeImportsTest.java index 0a0a62d0b91..f3622dc07ed 100644 --- a/idea/tests/org/jetbrains/jet/plugin/importOptimizer/OptimizeImportsTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/importOptimizer/OptimizeImportsTest.java @@ -37,7 +37,11 @@ public class OptimizeImportsTest extends LightCodeInsightTestCase { doTest(); } - public void testSortImports() throws Exception { + public void testRemoveImportsIfGeneralBefore() throws Exception { + doTest(); + } + + public void testDuplicatedImports() throws Exception { doTest(); }