optimize imports: don't collapse into on-demand when result in ambiguity

This commit is contained in:
Anna.Kozlova
2016-11-25 09:56:52 +01:00
parent 4dc8ffadcd
commit 3e13378341
4 changed files with 53 additions and 2 deletions
@@ -47,6 +47,7 @@ import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
import gnu.trove.TObjectIntHashMap;
@@ -81,13 +82,29 @@ public class ImportHelper{
List<PsiElement> nonImports = new ArrayList<>();
// Note: this array may contain "<packageOrClassName>.*" for unresolved imports!
List<Pair<String, Boolean>> names = new ArrayList<>(collectNamesToImport(file, nonImports));
Collections.sort(names, (o1, o2) -> o1.getFirst().compareTo(o2.getFirst()));
Collections.sort(names, Comparator.comparing(o -> o.getFirst()));
List<Pair<String, Boolean>> resultList = sortItemsAccordingToSettings(names, mySettings);
final Map<String, Boolean> classesOrPackagesToImportOnDemand = new THashMap<>();
collectOnDemandImports(resultList, mySettings, classesOrPackagesToImportOnDemand);
MultiMap<String, String> conflictingMemberNames = new MultiMap<>();
for (Pair<String, Boolean> pair : resultList) {
if (pair.second) {
conflictingMemberNames.putValue(StringUtil.getShortName(pair.first), StringUtil.getPackageName(pair.first));
}
}
for (String methodName : conflictingMemberNames.keySet()) {
Collection<String> collection = conflictingMemberNames.get(methodName);
if (!classesOrPackagesToImportOnDemand.keySet().containsAll(collection)) {
for (String name : collection) {
classesOrPackagesToImportOnDemand.remove(name);
}
}
}
Set<String> classesToUseSingle = findSingleImports(file, resultList, classesOrPackagesToImportOnDemand.keySet());
Set<String> toReimport = new THashSet<>();
calcClassesConflictingViaOnDemandImports(file, classesOrPackagesToImportOnDemand, file.getResolveScope(), toReimport);
@@ -0,0 +1,16 @@
import java.util.ArrayList;
import static java.util.Arrays.asList;
import static java.util.Arrays.deepHashCode;
import static java.util.Arrays.sort;
import static java.util.Collections.sort;
class OnDemand {
{
sort(new Integer[0]);
asList(new Integer[0]);
deepHashCode(new Integer[0]);
sort(new ArrayList<String>());
}
}
@@ -0,0 +1,16 @@
import java.util.ArrayList;
import static java.util.Arrays.asList;
import static java.util.Arrays.deepHashCode;
import static java.util.Arrays.sort;
import static java.util.Collections.sort;
class OnDemand {
{
sort(new Integer[0]);
asList(new Integer[0]);
deepHashCode(new Integer[0]);
sort(new ArrayList<String>());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,6 +38,8 @@ public class OptimizeImportsTest extends OptimizeImportsTestCase {
public void testNewImportListIsEmptyAndCommentPreserved() throws Exception { doTest(); }
public void testNewImportListIsEmptyAndJavaDocWithInvalidCodePreserved() throws Exception { doTest(); }
public void testDontCollapseToOnDemandImport() throws Exception { doTest(); }
private void doTest() throws Exception {
doTest(".java");
}