Temporary disable imports sorting

This commit is contained in:
Natalia.Ukhorskaya
2013-03-18 14:29:34 +04:00
parent b936b56235
commit abed8b59cb
10 changed files with 61 additions and 34 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.jet.plugin.editor.importOptimizer; package org.jetbrains.jet.plugin.editor.importOptimizer;
import com.google.common.collect.Lists;
import com.intellij.lang.ImportOptimizer; import com.intellij.lang.ImportOptimizer;
import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicatorProvider; import com.intellij.openapi.progress.ProgressIndicatorProvider;
@@ -42,6 +43,8 @@ import org.jetbrains.jet.util.QualifiedNamesUtil;
import java.util.*; import java.util.*;
import static org.jetbrains.jet.plugin.quickfix.ImportInsertHelper.doNeedImport;
public class JetImportOptimizer implements ImportOptimizer { public class JetImportOptimizer implements ImportOptimizer {
@Override @Override
public boolean supports(PsiFile file) { public boolean supports(PsiFile file) {
@@ -58,31 +61,10 @@ public class JetImportOptimizer implements ImportOptimizer {
final JetFile jetFile = (JetFile) file; final JetFile jetFile = (JetFile) file;
final Set<FqName> usedQualifiedNames = extractUsedQualifiedNames(jetFile); final Set<FqName> usedQualifiedNames = extractUsedQualifiedNames(jetFile);
final List<JetImportDirective> sortedDirectives = jetFile.getImportDirectives(); final List<JetImportDirective> directives = jetFile.getImportDirectives();
Collections.sort(sortedDirectives, new Comparator<JetImportDirective>() {
@Override
public int compare(JetImportDirective directive1, JetImportDirective directive2) {
ImportPath firstPath = JetPsiUtil.getImportPath(directive1);
ImportPath secondPath = JetPsiUtil.getImportPath(directive2);
if (firstPath == null || secondPath == null) { final List<JetImportDirective> directivesBeforeCurrent = Lists.newArrayList();
return firstPath == null && secondPath == null ? 0 : final List<JetImportDirective> directivesAfterCurrent = jetFile.getImportDirectives();
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());
}
});
ApplicationManager.getApplication().runWriteAction(new Runnable() { ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override @Override
@@ -104,14 +86,19 @@ public class JetImportOptimizer implements ImportOptimizer {
} }
// Insert back only necessary imports in correct order // Insert back only necessary imports in correct order
for (JetImportDirective anImport : sortedDirectives) { for (JetImportDirective anImport : directives) {
directivesAfterCurrent.remove(anImport);
ImportPath importPath = JetPsiUtil.getImportPath(anImport); ImportPath importPath = JetPsiUtil.getImportPath(anImport);
if (importPath == null) { if (importPath == null) {
continue; continue;
} }
if (isUseful(importPath, usedQualifiedNames)) { if (isUseful(importPath, usedQualifiedNames) &&
ImportInsertHelper.addImportDirective(importPath, jetFile); doNeedImport(importPath, jetFile, directivesBeforeCurrent) &&
doNeedImport(importPath, jetFile, directivesAfterCurrent)) {
ImportInsertHelper.writeImportToFile(importPath, jetFile);
directivesBeforeCurrent.add(anImport);
} }
} }
} }
@@ -20,7 +20,6 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*; import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.DefaultModuleConfiguration; import org.jetbrains.jet.lang.DefaultModuleConfiguration;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
@@ -117,6 +116,10 @@ public class ImportInsertHelper {
return; return;
} }
writeImportToFile(importPath, file);
}
public static void writeImportToFile(ImportPath importPath, JetFile file) {
JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath); JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath);
List<JetImportDirective> importDirectives = file.getImportDirectives(); List<JetImportDirective> importDirectives = file.getImportDirectives();
@@ -179,6 +182,10 @@ public class ImportInsertHelper {
} }
public static boolean doNeedImport(@NotNull ImportPath importPath, @NotNull JetFile file) { 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<JetImportDirective> importDirectives) {
if (importPath.fqnPart().firstSegmentIs(JavaDescriptorResolver.JAVA_ROOT)) { if (importPath.fqnPart().firstSegmentIs(JavaDescriptorResolver.JAVA_ROOT)) {
FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart()); FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart());
importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder(), importPath.getAlias()); importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder(), importPath.getAlias());
@@ -188,8 +195,6 @@ public class ImportInsertHelper {
return false; return false;
} }
List<JetImportDirective> importDirectives = file.getImportDirectives();
if (!importDirectives.isEmpty()) { if (!importDirectives.isEmpty()) {
// Check if import is already present // Check if import is already present
for (JetImportDirective directive : importDirectives) { for (JetImportDirective directive : importDirectives) {
@@ -0,0 +1,8 @@
import java.util.ArrayList
import java.util.ArrayList
class Action {
fun test() {
val test : ArrayList<Int>? = null
}
}
@@ -0,0 +1,7 @@
import java.util.ArrayList
class Action {
fun test() {
val test : ArrayList<Int>? = null
}
}
@@ -1,7 +1,7 @@
import java import java
import java.util import java.util
import java.util.Map
import java.util.List import java.util.List
import java.util.Map
import java.util.Map.Entry import java.util.Map.Entry
var x : Map.Entry<List<String>, String>? = null var x : Map.Entry<List<String>, String>? = null
@@ -0,0 +1,9 @@
import java.util.*
import java.util.ArrayList
import java.util.HashSet
class Action {
fun test(hash: HashSet<Int>) {
val test : ArrayList<Int>? = null
}
}
@@ -0,0 +1,7 @@
import java.util.*
class Action {
fun test(hash: HashSet<Int>) {
val test : ArrayList<Int>? = null
}
}
@@ -3,11 +3,11 @@ import com.sun.org.apache.xpath.internal.operations.And
import java.lang.StringBuilder import java.lang.StringBuilder
import java.net.HttpRetryException import java.net.HttpRetryException
import java.util.ArrayList import java.util.ArrayList
import kotlin.util.measureTimeMillis
import java.util.HashMap import java.util.HashMap
import kotlin.test.asserter import kotlin.test.asserter
import kotlin.test.Asserter import kotlin.test.Asserter
import kotlin.Assertions import kotlin.Assertions
import kotlin.util.measureTimeMillis
class Action { class Action {
fun test() { fun test() {
@@ -1,6 +1,6 @@
import java.io as JavaIO
import java.util.ArrayList import java.util.ArrayList
import java.util.ArrayList as SomeThing import java.util.ArrayList as SomeThing
import java.io as JavaIO
import java.io.* import java.io.*
class Action { class Action {
@@ -37,7 +37,11 @@ public class OptimizeImportsTest extends LightCodeInsightTestCase {
doTest(); doTest();
} }
public void testSortImports() throws Exception { public void testRemoveImportsIfGeneralBefore() throws Exception {
doTest();
}
public void testDuplicatedImports() throws Exception {
doTest(); doTest();
} }