IDEA-91017 If an inspection is off by default, running it by name returns "No suspicios code found"

This commit is contained in:
Dmitry Avdeev
2012-09-07 15:25:27 +04:00
parent 02f10f6629
commit 8c86e59a75
7 changed files with 49 additions and 74 deletions
@@ -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<Tools>(), new ArrayList<Tools>(), new ArrayList<Tools>());
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<Tools>(), new ArrayList<Tools>(), new ArrayList<Tools>());
assertEquals(1, context.getTools().size());
return;
}
}
fail("No disabled tools found");
}
@Override
@@ -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());
}
@@ -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);
@@ -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);
@@ -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) {
@@ -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<InspectionToolWrapper> 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<InspectionTool> result = new ArrayList<InspectionTool>();
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() {
@@ -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());
}