diff --git a/platform/lang-api/src/com/intellij/codeInspection/InspectionProfile.java b/platform/lang-api/src/com/intellij/codeInspection/InspectionProfile.java index 07766bacc783..5b8528fa65ca 100644 --- a/platform/lang-api/src/com/intellij/codeInspection/InspectionProfile.java +++ b/platform/lang-api/src/com/intellij/codeInspection/InspectionProfile.java @@ -19,6 +19,7 @@ package com.intellij.codeInspection; import com.intellij.codeHighlighting.HighlightDisplayLevel; import com.intellij.codeInsight.daemon.HighlightDisplayKey; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Key; import com.intellij.profile.Profile; import com.intellij.psi.PsiElement; import com.intellij.util.Consumer; @@ -34,7 +35,7 @@ public interface InspectionProfile extends Profile { HighlightDisplayLevel getErrorLevel(@NotNull HighlightDisplayKey inspectionToolKey, PsiElement element); /** - * If you need to modify tool's settings, please use {@link #modifyProfile(com.intellij.util.Consumer)} + * If you need to modify tool's settings, please use {@link #modifyToolSettings} */ InspectionProfileEntry getInspectionTool(@NotNull String shortName, @NotNull PsiElement element); @@ -44,8 +45,24 @@ public interface InspectionProfile extends Profile { /** Returns (unwrapped) inspection */ InspectionProfileEntry getUnwrappedTool(@NotNull String shortName, @NotNull PsiElement element); + /** Returns (unwrapped) inspection */ + + T getUnwrappedTool(@NotNull Key shortNameKey, @NotNull PsiElement element); + void modifyProfile(Consumer modelConsumer); + /** + * Allows a plugin to modify the settings of the inspection tool with the specified ID programmatically, without going through + * the settings dialog. + * + * @param shortNameKey the ID of the tool to change. + * @param psiElement the element for which the settings should be changed. + * @param toolConsumer the callback that receives the tool. + * @since 12.1 + */ + + void modifyToolSettings(Key shortNameKey, @NotNull PsiElement psiElement, Consumer toolConsumer); + /** * @param element context element * @return all (both enabled and disabled) tools 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 f07f264c0a3a..4a08748b83ce 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java @@ -339,6 +339,12 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, return tool instanceof InspectionToolWrapper ? ((InspectionToolWrapper)tool).getTool() : tool; } + @Override + public T getUnwrappedTool(@NotNull Key shortNameKey, @NotNull PsiElement element) { + //noinspection unchecked + return (T) getUnwrappedTool(shortNameKey.toString(), element); + } + @Override public void modifyProfile(Consumer modelConsumer) { ModifiableModel model = getModifiableModel(); @@ -351,6 +357,20 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, } } + @Override + public void modifyToolSettings(final Key shortNameKey, + @NotNull final PsiElement psiElement, + final Consumer toolConsumer) { + modifyProfile(new Consumer() { + @Override + public void consume(ModifiableModel model) { + InspectionProfileEntry tool = model.getUnwrappedTool(shortNameKey.toString(), psiElement); + //noinspection unchecked + toolConsumer.consume((T) tool); + } + }); + } + @Override @Nullable public InspectionProfileEntry getInspectionTool(@NotNull String shortName) { diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProjectProfileManager.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProjectProfileManager.java index 306d83e9114f..1313d61def43 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProjectProfileManager.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProjectProfileManager.java @@ -111,6 +111,9 @@ public class InspectionProjectProfileManager extends DefaultProjectProfileManage return (InspectionProfile)getProjectProfileImpl(); } + /** + * @deprecated use {@link #getInspectionProfile()} instead + */ @SuppressWarnings({"UnusedDeclaration"}) @NotNull public InspectionProfile getInspectionProfile(PsiElement element){ diff --git a/xml/impl/src/com/intellij/codeInspection/htmlInspections/AddCustomTagOrAttributeIntentionAction.java b/xml/impl/src/com/intellij/codeInspection/htmlInspections/AddCustomTagOrAttributeIntentionAction.java index ce0d01ef3cc2..ba84b96d3f62 100644 --- a/xml/impl/src/com/intellij/codeInspection/htmlInspections/AddCustomTagOrAttributeIntentionAction.java +++ b/xml/impl/src/com/intellij/codeInspection/htmlInspections/AddCustomTagOrAttributeIntentionAction.java @@ -16,10 +16,11 @@ package com.intellij.codeInspection.htmlInspections; +import com.intellij.codeInspection.InspectionProfile; import com.intellij.codeInspection.LocalQuickFix; -import com.intellij.codeInspection.ModifiableModel; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Key; import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.PsiElement; import com.intellij.util.Consumer; @@ -32,10 +33,10 @@ import org.jetbrains.annotations.NotNull; public class AddCustomTagOrAttributeIntentionAction implements LocalQuickFix { private final String myName; private final int myType; - private final String myInspectionName; + @NotNull private final Key myInspectionKey; - public AddCustomTagOrAttributeIntentionAction(@NotNull String shortName, String name, int type) { - myInspectionName = shortName; + public AddCustomTagOrAttributeIntentionAction(@NotNull Key inspectionKey, String name, int type) { + myInspectionKey = inspectionKey; myName = name; myType = type; } @@ -65,10 +66,10 @@ public class AddCustomTagOrAttributeIntentionAction implements LocalQuickFix { public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); - InspectionProjectProfileManager.getInstance(project).getInspectionProfile().modifyProfile(new Consumer() { + InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile(); + profile.modifyToolSettings(myInspectionKey, element, new Consumer() { @Override - public void consume(ModifiableModel model) { - HtmlUnknownTagInspection tool = (HtmlUnknownTagInspection)model.getUnwrappedTool(myInspectionName, element); + public void consume(HtmlUnknownTagInspection tool) { tool.addCustomPropertyName(myName); } }); diff --git a/xml/impl/src/com/intellij/codeInspection/htmlInspections/AddHtmlTagOrAttributeToCustomsIntention.java b/xml/impl/src/com/intellij/codeInspection/htmlInspections/AddHtmlTagOrAttributeToCustomsIntention.java index 63be9c5ab72d..78366a2c0ab1 100644 --- a/xml/impl/src/com/intellij/codeInspection/htmlInspections/AddHtmlTagOrAttributeToCustomsIntention.java +++ b/xml/impl/src/com/intellij/codeInspection/htmlInspections/AddHtmlTagOrAttributeToCustomsIntention.java @@ -17,9 +17,11 @@ package com.intellij.codeInspection.htmlInspections; import com.intellij.codeInsight.intention.IntentionAction; -import com.intellij.codeInspection.ModifiableModel; +import com.intellij.codeInspection.InspectionProfile; +import com.intellij.codeInspection.InspectionProfileEntry; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Key; import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.PsiFile; import com.intellij.util.Consumer; @@ -33,10 +35,10 @@ import org.jetbrains.annotations.NotNull; public class AddHtmlTagOrAttributeToCustomsIntention implements IntentionAction { private final String myName; private final int myType; - private final String myInspectionName; + private final Key myInspectionKey; - public AddHtmlTagOrAttributeToCustomsIntention(String shortName, String name, int type) { - myInspectionName = shortName; + public AddHtmlTagOrAttributeToCustomsIntention(Key inspectionKey, String name, int type) { + myInspectionKey = inspectionKey; myName = name; myType = type; } @@ -68,10 +70,11 @@ public class AddHtmlTagOrAttributeToCustomsIntention implements IntentionAction } public void invoke(@NotNull Project project, Editor editor, final PsiFile file) throws IncorrectOperationException { - InspectionProjectProfileManager.getInstance(project).getInspectionProfile().modifyProfile(new Consumer() { + InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile(); + profile.modifyToolSettings(myInspectionKey, file, new Consumer() { @Override - public void consume(ModifiableModel model) { - XmlEntitiesInspection xmlEntitiesInspection = (XmlEntitiesInspection)model.getUnwrappedTool(myInspectionName, file); + public void consume(InspectionProfileEntry entry) { + XmlEntitiesInspection xmlEntitiesInspection = (XmlEntitiesInspection) entry; xmlEntitiesInspection.setAdditionalEntries(myType, appendName(xmlEntitiesInspection.getAdditionalEntries(myType))); } }); diff --git a/xml/impl/src/com/intellij/codeInspection/htmlInspections/HtmlUnknownAttributeInspection.java b/xml/impl/src/com/intellij/codeInspection/htmlInspections/HtmlUnknownAttributeInspection.java index 9dac096fc6db..58617e8d1b29 100644 --- a/xml/impl/src/com/intellij/codeInspection/htmlInspections/HtmlUnknownAttributeInspection.java +++ b/xml/impl/src/com/intellij/codeInspection/htmlInspections/HtmlUnknownAttributeInspection.java @@ -22,6 +22,7 @@ import com.intellij.codeInspection.ProblemHighlightType; import com.intellij.codeInspection.ProblemsHolder; import com.intellij.lang.ASTNode; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.util.Key; import com.intellij.psi.PsiElement; import com.intellij.psi.html.HtmlTag; import com.intellij.psi.xml.XmlAttribute; @@ -44,6 +45,7 @@ public class HtmlUnknownAttributeInspection extends HtmlUnknownTagInspection { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.htmlInspections.HtmlUnknownAttributeInspection"); @NonNls public static final String ATTRIBUTE_SHORT_NAME = "HtmlUnknownAttribute"; + public static final Key ATTRIBUTE_KEY = Key.create(ATTRIBUTE_SHORT_NAME); public HtmlUnknownAttributeInspection() { super(""); @@ -100,7 +102,7 @@ public class HtmlUnknownAttributeInspection extends HtmlUnknownTagInspection { boolean maySwitchToHtml5 = HtmlUtil.isCustomHtml5Attribute(name) && !HtmlUtil.hasNonHtml5Doctype(tag); LocalQuickFix[] quickfixes = new LocalQuickFix[maySwitchToHtml5 ? 3 : 2]; - quickfixes[0] = new AddCustomTagOrAttributeIntentionAction(getShortName(), name, XmlEntitiesInspection.UNKNOWN_ATTRIBUTE); + quickfixes[0] = new AddCustomTagOrAttributeIntentionAction(ATTRIBUTE_KEY, name, XmlEntitiesInspection.UNKNOWN_ATTRIBUTE); quickfixes[1] = new RemoveAttributeIntentionAction(name); if (maySwitchToHtml5) { quickfixes[2] = new SwitchToHtml5WithHighPriorityAction(); diff --git a/xml/impl/src/com/intellij/codeInspection/htmlInspections/HtmlUnknownTagInspection.java b/xml/impl/src/com/intellij/codeInspection/htmlInspections/HtmlUnknownTagInspection.java index 08ab3339463f..38a54cd7d944 100644 --- a/xml/impl/src/com/intellij/codeInspection/htmlInspections/HtmlUnknownTagInspection.java +++ b/xml/impl/src/com/intellij/codeInspection/htmlInspections/HtmlUnknownTagInspection.java @@ -24,6 +24,7 @@ import com.intellij.codeInspection.ProblemHighlightType; import com.intellij.codeInspection.ProblemsHolder; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.JDOMExternalizableStringList; +import com.intellij.openapi.util.Key; import com.intellij.psi.PsiElement; import com.intellij.psi.html.HtmlTag; import com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl; @@ -43,7 +44,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; -import javax.swing.border.EmptyBorder; import javax.swing.event.DocumentEvent; import javax.swing.text.BadLocationException; import javax.swing.text.Document; @@ -63,6 +63,7 @@ public class HtmlUnknownTagInspection extends HtmlLocalInspectionTool { public JDOMExternalizableStringList myValues; public boolean myCustomValuesEnabled = true; @NonNls public static final String TAG_SHORT_NAME = "HtmlUnknownTag"; + public static final Key TAG_KEY = Key.create(TAG_SHORT_NAME); public HtmlUnknownTagInspection() { this("nobr,noembed,comment,noscript,embed,script"); @@ -229,7 +230,7 @@ public class HtmlUnknownTagInspection extends HtmlLocalInspectionTool { if (!isCustomValuesEnabled() || !isCustomValue(name)) { final AddCustomTagOrAttributeIntentionAction action = - new AddCustomTagOrAttributeIntentionAction(getShortName(), name, XmlEntitiesInspection.UNKNOWN_TAG); + new AddCustomTagOrAttributeIntentionAction(TAG_KEY, name, XmlEntitiesInspection.UNKNOWN_TAG); // todo: support "element is not allowed" message for html5 // some tags in html5 cannot be found in xhtml5.xsd if they are located in incorrect context, so they get any-element descriptor (ex. "canvas: tag) diff --git a/xml/impl/src/com/intellij/codeInspection/htmlInspections/RequiredAttributesInspection.java b/xml/impl/src/com/intellij/codeInspection/htmlInspections/RequiredAttributesInspection.java index d4cdffe1d274..bde145d007a8 100644 --- a/xml/impl/src/com/intellij/codeInspection/htmlInspections/RequiredAttributesInspection.java +++ b/xml/impl/src/com/intellij/codeInspection/htmlInspections/RequiredAttributesInspection.java @@ -16,11 +16,13 @@ package com.intellij.codeInspection.htmlInspections; import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.codeInspection.InspectionProfileEntry; import com.intellij.codeInspection.InspectionsBundle; -import com.intellij.codeInspection.XmlSuppressableInspectionTool; import com.intellij.codeInspection.XmlInspectionGroupNames; +import com.intellij.codeInspection.XmlSuppressableInspectionTool; import com.intellij.codeInspection.ex.UnfairLocalInspectionTool; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.util.Key; import com.intellij.ui.DocumentAdapter; import com.intellij.ui.FieldPanel; import org.jetbrains.annotations.NonNls; @@ -43,6 +45,7 @@ public class RequiredAttributesInspection extends XmlSuppressableInspectionTool private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.htmlInspections.RequiredAttributesInspection"); @NonNls public static final String SHORT_NAME = "RequiredAttributes"; + @NonNls public static final Key SHORT_NAME_KEY = Key.create(SHORT_NAME); @NotNull public String getGroupDisplayName() { @@ -87,7 +90,7 @@ public class RequiredAttributesInspection extends XmlSuppressableInspectionTool } public IntentionAction getIntentionAction(String name, int type) { - return new AddHtmlTagOrAttributeToCustomsIntention(getShortName(), name, type); + return new AddHtmlTagOrAttributeToCustomsIntention(SHORT_NAME_KEY, name, type); } public String getAdditionalEntries(int type) {