From 0e0dc8192427a07b026d8d2ac0c0c8e1af12f21f Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Fri, 20 Jul 2012 12:41:19 +0400 Subject: [PATCH] avoiding inspection instantiations: simple profile --- .../actions/RunInspectionIntention.java | 12 +++---- .../ex/InspectionProfileImpl.java | 32 ++++++++++++++++--- .../impl/CodeInsightTestFixtureImpl.java | 13 +------- 3 files changed, 34 insertions(+), 23 deletions(-) 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 eea32c8968f1..000693fe68e8 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionIntention.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/RunInspectionIntention.java @@ -26,7 +26,10 @@ import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInspection.InspectionManager; import com.intellij.codeInspection.InspectionProfileEntry; import com.intellij.codeInspection.InspectionsBundle; -import com.intellij.codeInspection.ex.*; +import com.intellij.codeInspection.ex.GlobalInspectionContextImpl; +import com.intellij.codeInspection.ex.InspectionManagerEx; +import com.intellij.codeInspection.ex.InspectionProfileImpl; +import com.intellij.codeInspection.ex.LocalInspectionToolWrapper; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtil; @@ -102,11 +105,8 @@ public class RunInspectionIntention implements IntentionAction, HighPriorityActi inspectionContext.doInspections(scope, managerEx); } - public static GlobalInspectionContextImpl createContext(InspectionProfileEntry baseTool, InspectionManagerEx managerEx, PsiElement psiElement) { - final InspectionProfileImpl profile = new InspectionProfileImpl(baseTool.getDisplayName()); - final InspectionProfileImpl model = (InspectionProfileImpl)profile.getModifiableModel(); - model.disableAllTools(); - model.enableTool(baseTool.getShortName()); + public static GlobalInspectionContextImpl createContext(final InspectionProfileEntry baseTool, InspectionManagerEx managerEx, PsiElement psiElement) { + final InspectionProfileImpl model = InspectionProfileImpl.createSimple(baseTool.getDisplayName(), baseTool); try { Element element = new Element("toCopy"); baseTool.writeSettings(element); 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 ec6ff529c395..11d139235720 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java @@ -40,6 +40,8 @@ import com.intellij.profile.codeInspection.InspectionProfileManager; import com.intellij.profile.codeInspection.SeverityProvider; import com.intellij.psi.PsiElement; import com.intellij.psi.search.scope.packageSet.NamedScope; +import com.intellij.util.Function; +import com.intellij.util.containers.ContainerUtil; import gnu.trove.THashMap; import org.jdom.Document; import org.jdom.Element; @@ -57,6 +59,13 @@ import java.util.*; public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, InspectionProfile, ExternalizableScheme { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.ex.InspectionProfileImpl"); @NonNls private static final String VALID_VERSION = "1.0"; + private static final Function WRAPPER_FUNCTION = + new Function() { + @Override + public InspectionToolWrapper fun(InspectionProfileEntry tool) { + return InspectionToolRegistrar.wrapTool(tool); + } + }; private Map myTools = new THashMap(); @@ -107,21 +116,30 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, copyFrom(inspectionProfile); } - public InspectionProfileImpl(final String inspectionProfile, + public InspectionProfileImpl(final String profileName, final InspectionToolRegistrar registrar, final ProfileManager profileManager) { - super(inspectionProfile); + super(profileName); myRegistrar = registrar; myBaseProfile = getDefaultProfile(); setProfileManager(profileManager); } - public InspectionProfileImpl(@NonNls String name) { - super(name); + public InspectionProfileImpl(@NonNls String profileName) { + super(profileName); myRegistrar = InspectionToolRegistrar.getInstance(); setProfileManager(InspectionProfileManager.getInstance()); } + public static InspectionProfileImpl createSimple(String name, final InspectionProfileEntry... tools) { + return new InspectionProfileImpl(name, new InspectionToolRegistrar(null) { + @Override + public List createTools() { + return ContainerUtil.map(tools, WRAPPER_FUNCTION); + } + }, InspectionProfileManager.getInstance()); + } + @Override public InspectionProfile getParentProfile() { return mySource; @@ -460,7 +478,7 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, final List tools; try { - tools = myRegistrar.createTools(); + tools = createTools(); } catch (ProcessCanceledException e) { return false; @@ -499,6 +517,10 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, return true; } + protected List createTools() { + return myRegistrar.createTools(); + } + private HighlightDisplayLevel getErrorLevel(@NotNull HighlightDisplayKey key) { final ToolsImpl tools = getTools(key.toString()); LOG.assertTrue(tools != null, "profile name: " + myName + " base profile: " + (myBaseProfile != null ? myBaseProfile.getName() : "-") + " key: " + key); 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 488103ef7ef4..ce40343714b6 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -444,18 +444,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig final InspectionManagerEx inspectionManager, final InspectionTool... tools) { - InspectionToolRegistrar registrar = new InspectionToolRegistrar(null) { - @Override - public List createTools() { - return ContainerUtil.map(tools, new Function() { - @Override - public InspectionToolWrapper fun(InspectionTool tool) { - return tool instanceof InspectionToolWrapper ? (InspectionToolWrapper)tool : wrapTool(tool); - } - }); - } - }; - final InspectionProfileImpl profile = new InspectionProfileImpl("test", registrar, InspectionProfileManager.getInstance()); + final InspectionProfileImpl profile = InspectionProfileImpl.createSimple("test", tools); GlobalInspectionContextImpl globalContext = new GlobalInspectionContextImpl(project, inspectionManager.getContentManager()) { @Override protected List getUsedTools() {