diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java index 8299e5f51c3d..872ae1277119 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java @@ -24,7 +24,6 @@ import com.intellij.codeInspection.visibility.VisibilityInspection; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; /** * @author Dmitry Avdeev @@ -60,8 +59,7 @@ public class GlobalInspectionContextTest extends CodeInsightTestCase { InspectionProfileEntry[] tools = profile.getInspectionTools(null); for (InspectionProfileEntry tool : tools) { if (!tool.isEnabledByDefault()) { - GlobalInspectionContextImpl context = RunInspectionIntention.createContext(tool.getDisplayName(), Collections.singletonList(tool), - (InspectionManagerEx)InspectionManager.getInstance(myProject), null); + GlobalInspectionContextImpl context = RunInspectionIntention.createContext(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 38d9906ffba7..d073bf3f8eb0 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/actions/GotoInspectionModel.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/GotoInspectionModel.java @@ -24,7 +24,6 @@ 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.*; @@ -90,11 +89,6 @@ 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 ca3c2ace43f6..63505f288aac 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionAction.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionAction.java @@ -43,10 +43,7 @@ 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 @@ -81,31 +78,15 @@ public class RunInspectionAction extends GotoActionBase { public void elementChosen(ChooseByNamePopup popup, final Object element) { ApplicationManager.getApplication().invokeLater(new Runnable() { public void run() { - InspectionProfileEntry profileEntry = (InspectionProfileEntry)element; - LinkedHashSet dependentEntries = new LinkedHashSet(); - collectDependentInspections(profileEntry, dependentEntries, model); - runInspection(project, profileEntry.getDisplayName(), new ArrayList(dependentEntries), virtualFile, psiElement, psiFile); + runInspection(project, (InspectionProfileEntry)element, 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 String profileName, - @NotNull List profileEntries, + @NotNull InspectionProfileEntry profileEntry, @Nullable VirtualFile virtualFile, PsiElement psiElement, PsiFile psiFile) { final InspectionManagerEx managerEx = (InspectionManagerEx)InspectionManager.getInstance(project); @@ -158,6 +139,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(profileName, profileEntries, managerEx, scope, psiFile); + RunInspectionIntention.rerunInspection(profileEntry, 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 5af851667705..b19db1330bd4 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionIntention.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionIntention.java @@ -35,6 +35,7 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.profile.codeInspection.InspectionProfileManager; import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; @@ -42,8 +43,8 @@ import com.intellij.util.IncorrectOperationException; import org.jdom.Element; import org.jetbrains.annotations.NotNull; -import java.util.Collections; -import java.util.List; +import java.util.LinkedHashSet; +import java.util.Set; /** * User: anna @@ -72,7 +73,7 @@ public class RunInspectionIntention implements IntentionAction, HighPriorityActi public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { final InspectionProfileEntry tool = - InspectionProjectProfileManager.getInstance(project).getInspectionProfile().getInspectionTool(myShortName, file); + InspectionProjectProfileManager.getInstance(project).getInspectionProfile().getInspectionTool(myShortName, file); if (tool instanceof LocalInspectionToolWrapper && ((LocalInspectionToolWrapper)tool).isUnfair()) { return false; } @@ -98,32 +99,34 @@ public class RunInspectionIntention implements IntentionAction, HighPriorityActi final AnalysisUIOptions uiOptions = AnalysisUIOptions.getInstance(project); analysisScope = dlg.getScope(uiOptions, analysisScope, project, module); final InspectionProfileEntry baseTool = - InspectionProjectProfileManager.getInstance(project).getInspectionProfile().getInspectionTool(myShortName, file); - rerunInspection(baseTool.getDisplayName(), Collections.singletonList(baseTool), managerEx, analysisScope, file); + InspectionProjectProfileManager.getInstance(project).getInspectionProfile().getInspectionTool(myShortName, file); + rerunInspection(baseTool, managerEx, analysisScope, file); } - public static void rerunInspection(final String profileName, final List baseTools, - final InspectionManagerEx managerEx, final AnalysisScope scope, - PsiElement psiElement) { - GlobalInspectionContextImpl inspectionContext = createContext(profileName, baseTools, managerEx, psiElement); + public static void rerunInspection(final InspectionProfileEntry baseTool, final InspectionManagerEx managerEx, final AnalysisScope scope, + PsiElement psiElement) { + GlobalInspectionContextImpl inspectionContext = createContext(baseTool, managerEx, psiElement); inspectionContext.doInspections(scope, managerEx); } - public static GlobalInspectionContextImpl createContext(final String profileName, final List baseTools, - InspectionManagerEx managerEx, PsiElement psiElement) { - final InspectionProfileImpl model = InspectionProfileImpl.createSimple(profileName, baseTools); + public static GlobalInspectionContextImpl createContext(final InspectionProfileEntry baseTool, InspectionManagerEx managerEx, PsiElement psiElement) { + final InspectionProfileImpl rootProfile = (InspectionProfileImpl)InspectionProfileManager.getInstance().getRootProfile(); + LinkedHashSet dependentEntries = new LinkedHashSet(); + GlobalInspectionContextImpl.collectDependentInspections(baseTool, dependentEntries, rootProfile); + InspectionProfileEntry[] toolsArray = dependentEntries.toArray(new InspectionProfileEntry[dependentEntries.size()]); + final InspectionProfileImpl model = InspectionProfileImpl.createSimple(baseTool.getDisplayName(), toolsArray); try { Element element = new Element("toCopy"); - for (InspectionProfileEntry baseTool : baseTools) { - baseTool.writeSettings(element); - model.getInspectionTool(baseTool.getShortName(), psiElement).readSettings(element); + for (InspectionProfileEntry tool : dependentEntries) { + tool.writeSettings(element); + model.getInspectionTool(tool.getShortName(), psiElement).readSettings(element); } } catch (Exception e) { //skip } - model.setEditable(profileName); + model.setEditable(baseTool.getDisplayName()); final GlobalInspectionContextImpl inspectionContext = managerEx.createNewGlobalContext(false); inspectionContext.setExternalProfile(model); return inspectionContext; diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java index cc7c779571fb..e6fd49b9ce3b 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java @@ -54,8 +54,10 @@ import com.intellij.psi.search.LocalSearchScope; import com.intellij.psi.search.scope.packageSet.NamedScope; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.ui.content.*; +import com.intellij.util.Function; import com.intellij.util.Processor; import com.intellij.util.TripleFunction; +import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.HashMap; import com.intellij.util.ui.UIUtil; import gnu.trove.THashMap; @@ -70,10 +72,7 @@ import org.jetbrains.annotations.TestOnly; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; public class GlobalInspectionContextImpl extends UserDataHolderBase implements GlobalInspectionContext { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.ex.GlobalInspectionContextImpl"); @@ -743,22 +742,47 @@ public class GlobalInspectionContextImpl extends UserDataHolderBase implements G } } + public static void collectDependentInspections(@NotNull InspectionProfileEntry profileEntry, + @NotNull Set dependentEntries, + @NotNull InspectionProfileImpl rootProfile) { + dependentEntries.add(profileEntry); + String mainToolId = profileEntry.getMainToolId(); + + if (mainToolId != null) { + InspectionProfileEntry dependentEntry = rootProfile.getInspectionTool(mainToolId); + + if (dependentEntry != null) { + if (!dependentEntries.contains(dependentEntry)) { + collectDependentInspections(dependentEntry, dependentEntries, rootProfile); + } + } + else { + LOG.error("Can't find main tool: " + mainToolId); + } + } + } + protected List getUsedTools() { InspectionProfileImpl profile = new InspectionProfileImpl((InspectionProfileImpl)getCurrentProfile()); List tools = profile.getAllEnabledInspectionTools(myProject); - THashSet set = null; + Set dependentTools = new LinkedHashSet(); for (ToolsImpl tool : tools) { - String id = tool.getTool().getMainToolId(); - if (id != null) { - InspectionProfileEntry mainTool = profile.getInspectionTool(id); - LOG.assertTrue(mainTool != null, "Can't find main tool: " + id); - if (set == null) { - set = new THashSet(tools, TOOLS_HASHING_STRATEGY); + collectDependentInspections(tool.getTool(), dependentTools, profile); + } + + if (!dependentTools.isEmpty()) { + THashSet set = new THashSet(tools, TOOLS_HASHING_STRATEGY); + set.addAll(ContainerUtil.map(dependentTools, new Function() { + @Override + public ToolsImpl fun(InspectionProfileEntry entry) { + return new ToolsImpl(entry, entry.getDefaultLevel(), true); } - set.add(new ToolsImpl(mainTool, mainTool.getDefaultLevel(), true)); - } + })); + return new ArrayList(set); + } + else { + return tools; } - return set == null ? tools : new ArrayList(set); } private void classifyTool(List outGlobalTools, 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 a25451d8f08d..4a08748b83ce 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 List tools) { + public static InspectionProfileImpl createSimple(String name, final InspectionProfileEntry... 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 56514d876f9f..5454bcab2f14 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", Arrays.asList(tools)); + final InspectionProfileImpl profile = InspectionProfileImpl.createSimple("test", tools); GlobalInspectionContextImpl globalContext = new GlobalInspectionContextImpl(project, inspectionManager.getContentManager()) { @Override protected List getUsedTools() {