nicer API for plugins to modify inspection tool settings

This commit is contained in:
Dmitry Jemerov
2013-02-28 11:43:43 +01:00
parent e3d97bf8ee
commit a553950319
8 changed files with 70 additions and 20 deletions
@@ -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 extends InspectionProfileEntry>
T getUnwrappedTool(@NotNull Key<T> shortNameKey, @NotNull PsiElement element);
void modifyProfile(Consumer<ModifiableModel> 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
*/
<T extends InspectionProfileEntry>
void modifyToolSettings(Key<T> shortNameKey, @NotNull PsiElement psiElement, Consumer<T> toolConsumer);
/**
* @param element context element
* @return all (both enabled and disabled) tools
@@ -339,6 +339,12 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel,
return tool instanceof InspectionToolWrapper ? ((InspectionToolWrapper)tool).getTool() : tool;
}
@Override
public <T extends InspectionProfileEntry> T getUnwrappedTool(@NotNull Key<T> shortNameKey, @NotNull PsiElement element) {
//noinspection unchecked
return (T) getUnwrappedTool(shortNameKey.toString(), element);
}
@Override
public void modifyProfile(Consumer<ModifiableModel> modelConsumer) {
ModifiableModel model = getModifiableModel();
@@ -351,6 +357,20 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel,
}
}
@Override
public <T extends InspectionProfileEntry> void modifyToolSettings(final Key<T> shortNameKey,
@NotNull final PsiElement psiElement,
final Consumer<T> toolConsumer) {
modifyProfile(new Consumer<ModifiableModel>() {
@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) {
@@ -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){
@@ -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<HtmlUnknownTagInspection> myInspectionKey;
public AddCustomTagOrAttributeIntentionAction(@NotNull String shortName, String name, int type) {
myInspectionName = shortName;
public AddCustomTagOrAttributeIntentionAction(@NotNull Key<HtmlUnknownTagInspection> 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<ModifiableModel>() {
InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
profile.modifyToolSettings(myInspectionKey, element, new Consumer<HtmlUnknownTagInspection>() {
@Override
public void consume(ModifiableModel model) {
HtmlUnknownTagInspection tool = (HtmlUnknownTagInspection)model.getUnwrappedTool(myInspectionName, element);
public void consume(HtmlUnknownTagInspection tool) {
tool.addCustomPropertyName(myName);
}
});
@@ -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<InspectionProfileEntry> myInspectionKey;
public AddHtmlTagOrAttributeToCustomsIntention(String shortName, String name, int type) {
myInspectionName = shortName;
public AddHtmlTagOrAttributeToCustomsIntention(Key<InspectionProfileEntry> 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<ModifiableModel>() {
InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
profile.modifyToolSettings(myInspectionKey, file, new Consumer<InspectionProfileEntry>() {
@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)));
}
});
@@ -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<HtmlUnknownTagInspection> 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();
@@ -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<HtmlUnknownTagInspection> 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)
@@ -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<InspectionProfileEntry> 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) {