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;
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<FqName> usedQualifiedNames = extractUsedQualifiedNames(jetFile);
final List<JetImportDirective> sortedDirectives = 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);
final List<JetImportDirective> 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<JetImportDirective> directivesBeforeCurrent = Lists.newArrayList();
final List<JetImportDirective> 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);
}
}
}
@@ -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<JetImportDirective> 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<JetImportDirective> 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<JetImportDirective> importDirectives = file.getImportDirectives();
if (!importDirectives.isEmpty()) {
// Check if import is already present
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.util
import java.util.Map
import java.util.List
import java.util.Map
import java.util.Map.Entry
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.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() {
@@ -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 {
@@ -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();
}