From 8c86e59a75af6b4b35536e26f591f88bf1ea535a Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Fri, 7 Sep 2012 15:16:06 +0400 Subject: [PATCH] IDEA-91017 If an inspection is off by default, running it by name returns "No suspicios code found" --- .../GlobalInspectionContextTest.java | 16 ++-- .../daemon/DaemonAnalyzerTestCase.java | 5 -- .../codeInspection/InspectionProfile.java | 4 + .../codeInspection/ModifiableModel.java | 2 + .../actions/RunInspectionAction.java | 4 +- .../ex/InspectionProfileImpl.java | 87 +++++++------------ .../impl/CodeInsightTestFixtureImpl.java | 5 -- 7 files changed, 49 insertions(+), 74 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java index 354ea4562a72..e6bdf0032a5c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/GlobalInspectionContextTest.java @@ -55,11 +55,17 @@ public class GlobalInspectionContextTest extends CodeInsightTestCase { } public void testRunInspectionContext() throws Exception { - InspectionProfileEntry tool = - ((InspectionProfile)InspectionProfileManager.getInstance().getRootProfile()).getInspectionTool("CanBeFinal"); - GlobalInspectionContextImpl context = RunInspectionIntention.createContext(tool, (InspectionManagerEx)InspectionManager.getInstance(myProject), null); - context.initializeTools(new ArrayList(), new ArrayList(), new ArrayList()); - assertEquals(1, context.getTools().size()); + InspectionProfile profile = (InspectionProfile)InspectionProfileManager.getInstance().getRootProfile(); + InspectionProfileEntry[] tools = profile.getInspectionTools(null); + for (InspectionProfileEntry tool : tools) { + if (!tool.isEnabledByDefault()) { + GlobalInspectionContextImpl context = RunInspectionIntention.createContext(tool, (InspectionManagerEx)InspectionManager.getInstance(myProject), null); + context.initializeTools(new ArrayList(), new ArrayList(), new ArrayList()); + assertEquals(1, context.getTools().size()); + return; + } + } + fail("No disabled tools found"); } @Override diff --git a/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java b/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java index 09373f9122b1..dcf69acd525a 100644 --- a/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java @@ -136,11 +136,6 @@ public abstract class DaemonAnalyzerTestCase extends CodeInsightTestCase { @Override public boolean isToolEnabled(HighlightDisplayKey key, PsiElement element) { - return isToolEnabled(key); - } - - @Override - public boolean isToolEnabled(HighlightDisplayKey key) { return key != null && myAvailableTools.containsKey(key.toString()); } diff --git a/platform/lang-api/src/com/intellij/codeInspection/InspectionProfile.java b/platform/lang-api/src/com/intellij/codeInspection/InspectionProfile.java index 0108e6d0912d..8bd4250ebc93 100644 --- a/platform/lang-api/src/com/intellij/codeInspection/InspectionProfile.java +++ b/platform/lang-api/src/com/intellij/codeInspection/InspectionProfile.java @@ -41,6 +41,10 @@ public interface InspectionProfile extends Profile { @Deprecated InspectionProfileEntry getInspectionTool(@NotNull String shortName); + /** + * @param element context element + * @return all (both enabled and disabled) tools + */ @NotNull InspectionProfileEntry[] getInspectionTools(@Nullable PsiElement element); diff --git a/platform/lang-api/src/com/intellij/codeInspection/ModifiableModel.java b/platform/lang-api/src/com/intellij/codeInspection/ModifiableModel.java index d6c712357565..01ee7fb3e68f 100644 --- a/platform/lang-api/src/com/intellij/codeInspection/ModifiableModel.java +++ b/platform/lang-api/src/com/intellij/codeInspection/ModifiableModel.java @@ -21,6 +21,7 @@ import com.intellij.codeInsight.daemon.HighlightDisplayKey; import com.intellij.profile.Profile; import com.intellij.psi.PsiElement; import com.intellij.psi.search.scope.packageSet.NamedScope; +import org.jetbrains.annotations.Nullable; import java.io.IOException; @@ -32,6 +33,7 @@ public interface ModifiableModel extends Profile { InspectionProfile getParentProfile(); + @Nullable String getBaseProfileName(); void setBaseProfile(InspectionProfile profile); 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 e6365f3baed7..63505f288aac 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionAction.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionAction.java @@ -33,7 +33,7 @@ import com.intellij.openapi.actionSystem.LangDataKeys; import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.module.Module; -import com.intellij.openapi.module.ModuleUtil; +import com.intellij.openapi.module.ModuleUtilCore; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; @@ -90,7 +90,7 @@ public class RunInspectionAction extends GotoActionBase { @Nullable VirtualFile virtualFile, PsiElement psiElement, PsiFile psiFile) { final InspectionManagerEx managerEx = (InspectionManagerEx)InspectionManager.getInstance(project); - final Module module = virtualFile != null ? ModuleUtil.findModuleForFile(virtualFile, project) : null; + final Module module = virtualFile != null ? ModuleUtilCore.findModuleForFile(virtualFile, project) : null; AnalysisScope analysisScope = null; if (psiFile != null) { 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 11d139235720..df63f7286297 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java @@ -132,12 +132,16 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, } public static InspectionProfileImpl createSimple(String name, final InspectionProfileEntry... tools) { - return new InspectionProfileImpl(name, new InspectionToolRegistrar(null) { + InspectionProfileImpl profile = new InspectionProfileImpl(name, new InspectionToolRegistrar(null) { @Override public List createTools() { return ContainerUtil.map(tools, WRAPPER_FUNCTION); } }, InspectionProfileManager.getInstance()); + for (InspectionProfileEntry tool : tools) { + profile.enableTool(tool.getShortName()); + } + return profile; } @Override @@ -145,18 +149,6 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, return mySource; } - @Override - public void setProfileManager(@NotNull ProfileManager profileManager) { - super.setProfileManager(profileManager); - /*final NamedScopesHolder scopesHolder = profileManager.getScopesManager(); - if (scopesHolder != null) { - scopesHolder.addScopeListener(new NamedScopesHolder.ScopeListener() {//todo scopes change tracking - public void scopesChanged() { - } - }); - }*/ - } - @Override public String getBaseProfileName() { if (myBaseProfile == null) return null; @@ -357,24 +349,6 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, @Override public void save() throws IOException { - /* - if (isLocal()) { - if (myName.compareTo("Default") == 0 && myElement == null){ - myElement = new Element(ROOT_ELEMENT_TAG); - } - if (myElement != null) { - try { - myElement = new Element(ROOT_ELEMENT_TAG); - myElement.setAttribute(PROFILE_NAME_TAG, myName); - writeExternal(myElement); - myVisibleTreeState.writeExternal(myElement); - } - catch (WriteExternalException e) { - LOG.error(e); - } - } - } - */ InspectionProfileManager.getInstance().fireProfileChanged(this); } @@ -421,7 +395,7 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, @Override @NotNull - public InspectionProfileEntry[] getInspectionTools(PsiElement element) { + public InspectionProfileEntry[] getInspectionTools(@Nullable PsiElement element) { initInspectionTools(element != null ? element.getProject() : null); List result = new ArrayList(); for (Tools toolList : myTools.values()) { @@ -542,7 +516,7 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, myBaseProfile = inspectionProfile.myBaseProfile; } - private void copyToolsConfigurations(InspectionProfileImpl profile, Project project) { + private void copyToolsConfigurations(InspectionProfileImpl profile, @Nullable Project project) { try { for (ToolsImpl toolList : profile.myTools.values()) { final ToolsImpl tools = myTools.get(toolList.getShortName()); @@ -642,8 +616,7 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, @Override public boolean isToolEnabled(HighlightDisplayKey key) { - final Tools toolState = getTools(key.toString()); - return toolState != null && toolState.isEnabled(); + return isToolEnabled(key, (PsiElement)null); } @Override @@ -703,34 +676,34 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, public void convert(Element element) { initInspectionTools(null); final Element scopes = element.getChild(DefaultProjectProfileManager.SCOPES); - if (scopes != null) { - final List children = scopes.getChildren(SCOPE); - if (children != null) { - for (Object s : children) { - Element scopeElement = (Element)s; - final String profile = scopeElement.getAttributeValue(DefaultProjectProfileManager.PROFILE); - if (profile != null) { - final InspectionProfileImpl inspectionProfile = (InspectionProfileImpl)getProfileManager().getProfile(profile); - if (inspectionProfile != null) { - final NamedScope scope = getProfileManager().getScopesManager().getScope(scopeElement.getAttributeValue(NAME)); - if (scope != null) { - for (InspectionProfileEntry entry : inspectionProfile.getInspectionTools(null)) { - final HighlightDisplayKey key = HighlightDisplayKey.find(entry.getShortName()); - try { - getTools(entry.getShortName()) - .addTool(scope, copyToolSettings((InspectionToolWrapper)entry), inspectionProfile.isToolEnabled(key), inspectionProfile.getErrorLevel(key, (NamedScope)null)); - } - catch (Exception e) { - LOG.error(e); - } - } + if (scopes == null) { + return; + } + final List children = scopes.getChildren(SCOPE); + for (Object s : children) { + Element scopeElement = (Element)s; + final String profile = scopeElement.getAttributeValue(DefaultProjectProfileManager.PROFILE); + if (profile != null) { + final InspectionProfileImpl inspectionProfile = (InspectionProfileImpl)getProfileManager().getProfile(profile); + if (inspectionProfile != null) { + final NamedScope scope = getProfileManager().getScopesManager().getScope(scopeElement.getAttributeValue(NAME)); + if (scope != null) { + for (InspectionProfileEntry entry : inspectionProfile.getInspectionTools(null)) { + final HighlightDisplayKey key = HighlightDisplayKey.find(entry.getShortName()); + try { + getTools(entry.getShortName()) + .addTool(scope, copyToolSettings((InspectionToolWrapper)entry), inspectionProfile.isToolEnabled(key), + inspectionProfile.getErrorLevel(key, (NamedScope)null)); + } + catch (Exception e) { + LOG.error(e); } } } } - reduceConvertedScopes(); } } + reduceConvertedScopes(); } private void reduceConvertedScopes() { 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 e0173add5201..b894ed124586 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -1173,12 +1173,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig return result; } - @Override public boolean isToolEnabled(HighlightDisplayKey key, PsiElement element) { - return isToolEnabled(key); - } - - public boolean isToolEnabled(HighlightDisplayKey key) { return key != null && key.toString() != null && myAvailableTools.containsKey(key.toString()); }