[java-intentions] IDEA-370336 Optimize Imports is available but does nothing in very broken file

GitOrigin-RevId: 35622adddc1d7c4975b913df7d11b8781376acaa
This commit is contained in:
Tagir Valeev
2025-04-03 14:48:03 +00:00
committed by intellij-monorepo-bot
parent a253da6dcf
commit c6bb83f683
6 changed files with 30 additions and 12 deletions
@@ -3,7 +3,6 @@ package com.intellij.psi.impl.source.codeStyle;
import com.intellij.application.options.CodeStyle;
import com.intellij.codeInsight.ImportFilter;
import com.intellij.codeInsight.JavaProjectCodeInsightSettings;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.jsp.JspSpiUtil;
import com.intellij.lang.ASTNode;
@@ -19,6 +18,7 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
import com.intellij.psi.codeStyle.PackageEntry;
import com.intellij.psi.codeStyle.PackageEntryTable;
import com.intellij.psi.impl.IncompleteModelUtil;
import com.intellij.psi.impl.PsiFileFactoryImpl;
import com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
@@ -77,7 +77,7 @@ public final class ImportHelper {
}
/**
* @param filter pretend some references do not exist so the corresponding imports may be deleted
* @param filter pretend some references do not exist, so the corresponding imports may be deleted
* @return the import list to replace with, or null when there's no need to replace the import list because they are the same
*/
public @Nullable PsiImportList prepareOptimizeImportsResult(@NotNull PsiJavaFile file, @NotNull Predicate<? super Import> filter) {
@@ -103,8 +103,7 @@ public final class ImportHelper {
Map<String, Boolean> classesOrPackagesToImportOnDemand = new HashMap<>();
List<PsiImportModuleStatement> previousModuleStatements = collectModuleImports(file, mySettings);
Map<String, PsiImportModuleStatement> moduleStatementMap = collectNamesImportedByModules(file, previousModuleStatements, resultList);
JavaProjectCodeInsightSettings javaProjectCodeInsightSettings = JavaProjectCodeInsightSettings.getSettings(file.getProject());
collectOnDemandImports(resultList, mySettings, javaProjectCodeInsightSettings, classesOrPackagesToImportOnDemand, moduleStatementMap);
collectOnDemandImports(resultList, mySettings, classesOrPackagesToImportOnDemand, moduleStatementMap);
MultiMap<String, String> conflictingMemberNames = new MultiMap<>();
for (Import anImport : resultList) {
@@ -161,7 +160,7 @@ public final class ImportHelper {
}
/**
* Collects the names of classes that are imported by modules specified implicitly in the given Java file and in import list.
* Collects the names of classes that are imported by modules specified implicitly in the given Java file and in the import list.
*
* @param file the Java file for which imported class names are being collected.
* @param statements a list of import module statements that specify the modules from which classes are imported.
@@ -213,7 +212,6 @@ public final class ImportHelper {
public static void collectOnDemandImports(@NotNull List<Import> resultList,
@NotNull JavaCodeStyleSettings javaCodeStyleSettings,
@NotNull JavaProjectCodeInsightSettings javaProjectCodeInsightSettings,
@NotNull Map<String, Boolean> outClassesOrPackagesToImportOnDemand,
@NotNull Map<String, PsiImportModuleStatement> moduleStatementMap) {
Object2IntMap<String> packageToCountMap = new Object2IntOpenHashMap<>();
@@ -1240,7 +1238,8 @@ public final class ImportHelper {
Import unresolvedImport = unresolvedNames.get(name);
if (reference.multiResolve(false).length == 0) {
hasResolveProblem[0] = true;
if (unresolvedImport != null) {
if (unresolvedImport != null &&
(IncompleteModelUtil.canBeClassReference(reference) || unresolvedImport.isStatic())) {
namesToImport.add(unresolvedImport);
unresolvedNames.remove(name);
if (unresolvedNames.isEmpty()) return;
@@ -220,7 +220,7 @@ public final class IncompleteModelUtil {
public static List<PsiImportStatementBase> getPotentialImports(@NotNull PsiJavaCodeReferenceElement ref) {
PsiElement parent = ref.getParent();
if (parent instanceof PsiImportStatementBase || ref.isQualified()) return Collections.emptyList();
boolean maybeClass = canBeClass(ref);
boolean maybeClass = canBeClassReference(ref);
if (!(ref.getContainingFile() instanceof PsiJavaFile)) return Collections.emptyList();
PsiImportList list = ((PsiJavaFile)ref.getContainingFile()).getImportList();
List<PsiImportStatementBase> imports = new ArrayList<>();
@@ -243,7 +243,11 @@ public final class IncompleteModelUtil {
return imports;
}
private static boolean canBeClass(@NotNull PsiJavaCodeReferenceElement ref) {
/**
* @param ref reference to check
* @return true if this reference could be a reference to a class
*/
public static boolean canBeClassReference(@NotNull PsiJavaCodeReferenceElement ref) {
PsiElement parent = ref.getParent();
if (parent instanceof PsiMethodCallExpression) return false;
if (!(ref instanceof PsiReferenceExpression)) return true;
@@ -0,0 +1,7 @@
import com.foo.Hello;
class Test {
void method {
(Hello
}
}
@@ -0,0 +1,5 @@
class Test {
void method {
(Hello
}
}
@@ -768,6 +768,11 @@ public class OptimizeImportsTest extends OptimizeImportsTestCase {
""");
doTest();
}
public void testUnresolvedReferenceAfterParenthesis() {
doTest();
}
private void doTest() {
doTest(".java");
}
@@ -1,7 +1,6 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.plugins.javaFX.fxml.codeInsight;
import com.intellij.codeInsight.JavaProjectCodeInsightSettings;
import com.intellij.ide.highlighter.XmlFileType;
import com.intellij.injected.editor.VirtualFileWindow;
import com.intellij.lang.ASTNode;
@@ -48,10 +47,9 @@ public final class JavaFxImportsOptimizer implements ImportOptimizer {
collectNamesToImport(names, demandedForNested, (XmlFile)file);
names.sort((o1, o2) -> StringUtil.compare(o1.name(), o2.name(), true));
final JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(file);
JavaProjectCodeInsightSettings javaProjectCodeInsightSettings = JavaProjectCodeInsightSettings.getSettings(file.getProject());
final @NotNull List<ImportHelper.Import> sortedNames = ImportHelper.sortItemsAccordingToSettings(names, settings);
final Map<String, Boolean> onDemand = new HashMap<>();
ImportHelper.collectOnDemandImports(sortedNames, settings, javaProjectCodeInsightSettings, onDemand, new HashMap<>());
ImportHelper.collectOnDemandImports(sortedNames, settings, onDemand, new HashMap<>());
for (String s : demandedForNested) {
onDemand.put(s, false);
}