mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
optimize imports: fix order of on demand imports in presence of single import statements from same package (IDEA-163103)
This commit is contained in:
@@ -381,10 +381,14 @@ public class ImportHelper{
|
||||
String packageOrClassName = getPackageOrClassName(name);
|
||||
final boolean implicitlyImported = JAVA_LANG_PACKAGE.equals(packageOrClassName);
|
||||
boolean useOnDemand = implicitlyImported || packagesOrClassesToImportOnDemand.contains(packageOrClassName);
|
||||
final Pair<String, Boolean> current = Pair.create(packageOrClassName, isStatic);
|
||||
if (namesToUseSingle.remove(name)) {
|
||||
if (useOnDemand && importedPackagesOrClasses.contains(current)) {
|
||||
buffer.insert(buffer.lastIndexOf("import"), "import " + (isStatic ? "static " : "") + name + ";\n");
|
||||
continue;
|
||||
}
|
||||
useOnDemand = false;
|
||||
}
|
||||
final Pair<String, Boolean> current = Pair.create(packageOrClassName, isStatic);
|
||||
if (useOnDemand && (importedPackagesOrClasses.contains(current) || implicitlyImported)) continue;
|
||||
buffer.append("import ");
|
||||
if (isStatic) buffer.append("static ");
|
||||
|
||||
+47
@@ -7,6 +7,7 @@ import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
public class LightOptimizeImportsTest extends LightCodeInsightFixtureTestCase {
|
||||
|
||||
public void testSingleImportConflictingWith2Others() throws Exception {
|
||||
@@ -50,4 +51,50 @@ public class LightOptimizeImportsTest extends LightCodeInsightFixtureTestCase {
|
||||
" };\n" +
|
||||
"}\n");
|
||||
}
|
||||
|
||||
public void testStaticImportsOrder() throws Exception {
|
||||
|
||||
myFixture.addClass("package p; public class C1 {" +
|
||||
" public static String Byte;\n" +
|
||||
" public static String Field2;" +
|
||||
"}");
|
||||
myFixture.addClass("package p; public class C2 { " +
|
||||
" public static String Long;\n" +
|
||||
" public static String Field4;" +
|
||||
"}");
|
||||
|
||||
myFixture.configureByText(StdFileTypes.JAVA, "\n" +
|
||||
"import static p.C1.*;\n" +
|
||||
"import static p.C1.Byte;\n" +
|
||||
"import static p.C2.Long;\n" +
|
||||
"import static p.C2.*;\n" +
|
||||
"\n" +
|
||||
"public class Main {\n" +
|
||||
" public static void main(String[] args) {\n" +
|
||||
" System.out.println(Byte);\n" +
|
||||
" System.out.println(Field2);\n" +
|
||||
" System.out.println(Long);\n" +
|
||||
" System.out.println(Field4);\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
|
||||
JavaCodeStyleSettings javaSettings = JavaCodeStyleSettings.getInstance(getProject());
|
||||
javaSettings.NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND = 1;
|
||||
|
||||
WriteCommandAction.runWriteCommandAction(getProject(), () -> JavaCodeStyleManager.getInstance(getProject()).optimizeImports(getFile()));
|
||||
|
||||
myFixture.checkResult("import static p.C1.Byte;\n" +
|
||||
"import static p.C1.*;\n" +
|
||||
"import static p.C2.Long;\n" +
|
||||
"import static p.C2.*;\n" +
|
||||
"\n" +
|
||||
"public class Main {\n" +
|
||||
" public static void main(String[] args) {\n" +
|
||||
" System.out.println(Byte);\n" +
|
||||
" System.out.println(Field2);\n" +
|
||||
" System.out.println(Long);\n" +
|
||||
" System.out.println(Field4);\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user