From dfc110ff54ae671dc162cf8595b132ba2367cdb4 Mon Sep 17 00:00:00 2001 From: Alexander Kirillin Date: Mon, 18 Mar 2013 16:23:02 +0400 Subject: [PATCH] OC-6893 ("Run inspection by name" is not working) --- .../GlobalInspectionContextTest.java | 4 ++- .../actions/GotoInspectionModel.java | 6 +++++ .../actions/RunInspectionAction.java | 25 ++++++++++++++++--- .../actions/RunInspectionIntention.java | 24 ++++++++++++------ .../ex/InspectionProfileImpl.java | 2 +- .../impl/CodeInsightTestFixtureImpl.java | 2 +- 6 files changed, 49 insertions(+), 14 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java index 872ae1277119..8299e5f51c3d 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java @@ -24,6 +24,7 @@ import com.intellij.codeInspection.visibility.VisibilityInspection; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; /** * @author Dmitry Avdeev @@ -59,7 +60,8 @@ public class GlobalInspectionContextTest extends CodeInsightTestCase { InspectionProfileEntry[] tools = profile.getInspectionTools(null); for (InspectionProfileEntry tool : tools) { if (!tool.isEnabledByDefault()) { - GlobalInspectionContextImpl context = RunInspectionIntention.createContext(tool, (InspectionManagerEx)InspectionManager.getInstance(myProject), null); + GlobalInspectionContextImpl context = RunInspectionIntention.createContext(tool.getDisplayName(), Collections.singletonList(tool), + (InspectionManagerEx)InspectionManager.getInstance(myProject), null); context.initializeTools(new ArrayList(), new ArrayList(), new ArrayList()); assertEquals(1, context.getTools().size()); return; diff --git a/platform/lang-impl/src/com/intellij/codeInspection/actions/GotoInspectionModel.java b/platform/lang-impl/src/com/intellij/codeInspection/actions/GotoInspectionModel.java index d073bf3f8eb0..38d9906ffba7 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/actions/GotoInspectionModel.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/GotoInspectionModel.java @@ -24,6 +24,7 @@ import com.intellij.ide.util.gotoByName.SimpleChooseByNameModel; import com.intellij.openapi.project.Project; import com.intellij.profile.codeInspection.InspectionProfileManager; import com.intellij.util.ArrayUtil; +import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.util.*; @@ -89,6 +90,11 @@ public class GotoInspectionModel extends SimpleChooseByNameModel { return result.toArray(new InspectionProfileEntry[result.size()]); } + @Nullable + public InspectionProfileEntry getProfileEntryByName(String shortName) { + return myToolShortNames.get(shortName); + } + public String getElementName(final Object element) { if (element instanceof InspectionProfileEntry) { final InspectionProfileEntry entry = (InspectionProfileEntry)element; diff --git a/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionAction.java b/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionAction.java index 63505f288aac..ca3c2ace43f6 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionAction.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionAction.java @@ -43,7 +43,10 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.util.ArrayList; import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.List; /** * @author Konstantin Bulenkov @@ -78,15 +81,31 @@ public class RunInspectionAction extends GotoActionBase { public void elementChosen(ChooseByNamePopup popup, final Object element) { ApplicationManager.getApplication().invokeLater(new Runnable() { public void run() { - runInspection(project, (InspectionProfileEntry)element, virtualFile, psiElement, psiFile); + InspectionProfileEntry profileEntry = (InspectionProfileEntry)element; + LinkedHashSet dependentEntries = new LinkedHashSet(); + collectDependentInspections(profileEntry, dependentEntries, model); + runInspection(project, profileEntry.getDisplayName(), new ArrayList(dependentEntries), virtualFile, psiElement, psiFile); } }); } }); } + private static void collectDependentInspections(@NotNull InspectionProfileEntry profileEntry, + @NotNull LinkedHashSet dependentEntries, + @NotNull GotoInspectionModel model) { + dependentEntries.add(profileEntry); + String mainToolId = profileEntry.getMainToolId(); + InspectionProfileEntry dependentEntry = mainToolId != null ? model.getProfileEntryByName(mainToolId) : null; + + if (dependentEntry != null && !dependentEntries.contains(dependentEntry)) { + collectDependentInspections(dependentEntry, dependentEntries, model); + } + } + private static void runInspection(@NotNull Project project, - @NotNull InspectionProfileEntry profileEntry, + @NotNull String profileName, + @NotNull List profileEntries, @Nullable VirtualFile virtualFile, PsiElement psiElement, PsiFile psiFile) { final InspectionManagerEx managerEx = (InspectionManagerEx)InspectionManager.getInstance(project); @@ -139,6 +158,6 @@ public class RunInspectionAction extends GotoActionBase { if (!dlg.isOK()) return; final AnalysisUIOptions uiOptions = AnalysisUIOptions.getInstance(project); scope = dlg.getScope(uiOptions, scope, project, module); - RunInspectionIntention.rerunInspection(profileEntry, managerEx, scope, psiFile); + RunInspectionIntention.rerunInspection(profileName, profileEntries, managerEx, scope, psiFile); } } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionIntention.java b/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionIntention.java index 000693fe68e8..5af851667705 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionIntention.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionIntention.java @@ -42,6 +42,9 @@ import com.intellij.util.IncorrectOperationException; import org.jdom.Element; import org.jetbrains.annotations.NotNull; +import java.util.Collections; +import java.util.List; + /** * User: anna * Date: 21-Feb-2006 @@ -96,26 +99,31 @@ public class RunInspectionIntention implements IntentionAction, HighPriorityActi analysisScope = dlg.getScope(uiOptions, analysisScope, project, module); final InspectionProfileEntry baseTool = InspectionProjectProfileManager.getInstance(project).getInspectionProfile().getInspectionTool(myShortName, file); - rerunInspection(baseTool, managerEx, analysisScope, file); + rerunInspection(baseTool.getDisplayName(), Collections.singletonList(baseTool), managerEx, analysisScope, file); } - public static void rerunInspection(final InspectionProfileEntry baseTool, final InspectionManagerEx managerEx, final AnalysisScope scope, + public static void rerunInspection(final String profileName, final List baseTools, + final InspectionManagerEx managerEx, final AnalysisScope scope, PsiElement psiElement) { - GlobalInspectionContextImpl inspectionContext = createContext(baseTool, managerEx, psiElement); + GlobalInspectionContextImpl inspectionContext = createContext(profileName, baseTools, managerEx, psiElement); inspectionContext.doInspections(scope, managerEx); } - public static GlobalInspectionContextImpl createContext(final InspectionProfileEntry baseTool, InspectionManagerEx managerEx, PsiElement psiElement) { - final InspectionProfileImpl model = InspectionProfileImpl.createSimple(baseTool.getDisplayName(), baseTool); + public static GlobalInspectionContextImpl createContext(final String profileName, final List baseTools, + InspectionManagerEx managerEx, PsiElement psiElement) { + final InspectionProfileImpl model = InspectionProfileImpl.createSimple(profileName, baseTools); try { Element element = new Element("toCopy"); - baseTool.writeSettings(element); - model.getInspectionTool(baseTool.getShortName(), psiElement).readSettings(element); + + for (InspectionProfileEntry baseTool : baseTools) { + baseTool.writeSettings(element); + model.getInspectionTool(baseTool.getShortName(), psiElement).readSettings(element); + } } catch (Exception e) { //skip } - model.setEditable(baseTool.getDisplayName()); + model.setEditable(profileName); final GlobalInspectionContextImpl inspectionContext = managerEx.createNewGlobalContext(false); inspectionContext.setExternalProfile(model); return inspectionContext; diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java index 4a08748b83ce..a25451d8f08d 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java @@ -130,7 +130,7 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, setProfileManager(InspectionProfileManager.getInstance()); } - public static InspectionProfileImpl createSimple(String name, final InspectionProfileEntry... tools) { + public static InspectionProfileImpl createSimple(String name, final List tools) { InspectionProfileImpl profile = new InspectionProfileImpl(name, new InspectionToolRegistrar(null) { @Override public List createTools() { diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java index 5454bcab2f14..56514d876f9f 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -463,7 +463,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig final InspectionManagerEx inspectionManager, final InspectionTool... tools) { - final InspectionProfileImpl profile = InspectionProfileImpl.createSimple("test", tools); + final InspectionProfileImpl profile = InspectionProfileImpl.createSimple("test", Arrays.asList(tools)); GlobalInspectionContextImpl globalContext = new GlobalInspectionContextImpl(project, inspectionManager.getContentManager()) { @Override protected List getUsedTools() {