language suppressors api to provide suppressions by default as well as for multi-language inspections

This commit is contained in:
Anna Kozlova
2014-06-17 09:51:30 +04:00
parent 65756dc6b9
commit d46b411752
16 changed files with 166 additions and 37 deletions
@@ -29,7 +29,7 @@ import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
public abstract class SuppressManager implements BatchSuppressManager {
public abstract class SuppressManager implements BatchSuppressManager, InspectionSuppressor {
public static SuppressManager getInstance() {
return ServiceManager.getService(SuppressManager.class);
@@ -37,6 +37,11 @@ public class SuppressManagerImpl extends SuppressManager {
return SuppressIntentionActionFromFix.convertBatchToSuppressIntentionActions(batchSuppressActions);
}
@Override
public SuppressQuickFix[] getSuppressActions(@NotNull PsiElement element, String toolShortName) {
return createBatchSuppressActions(HighlightDisplayKey.find(toolShortName));
}
@Override
public boolean isSuppressedFor(@NotNull final PsiElement element, final String toolId) {
return JavaSuppressionUtil.getElementToolSuppressedIn(element, toolId) != null;
@@ -15,23 +15,20 @@
*/
package com.intellij.spellchecker;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInspection.BatchSuppressManager;
import com.intellij.codeInspection.SuppressManager;
import com.intellij.codeInspection.SuppressQuickFix;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.spellchecker.tokenizer.SuppressibleSpellcheckingStrategy;
import com.intellij.spellchecker.tokenizer.SpellcheckingStrategy;
import com.intellij.spellchecker.tokenizer.Tokenizer;
import org.jetbrains.annotations.NotNull;
/**
* @author shkate@jetbrains.com
*/
public class JavaSpellcheckingStrategy extends SuppressibleSpellcheckingStrategy {
public class JavaSpellcheckingStrategy extends SpellcheckingStrategy {
private final MethodNameTokenizerJava myMethodNameTokenizer = new MethodNameTokenizerJava();
private final DocCommentTokenizer myDocCommentTokenizer = new DocCommentTokenizer();
private final LiteralExpressionTokenizer myLiteralExpressionTokenizer = new LiteralExpressionTokenizer();
@@ -58,14 +55,4 @@ public class JavaSpellcheckingStrategy extends SuppressibleSpellcheckingStrategy
return super.getTokenizer(element);
}
@Override
public boolean isSuppressedFor(@NotNull PsiElement element, @NotNull String name) {
return SuppressManager.getInstance().isSuppressedFor(element, name);
}
@Override
public SuppressQuickFix[] getSuppressActions(@NotNull PsiElement element, @NotNull String name) {
return BatchSuppressManager.SERVICE.getInstance().createBatchSuppressActions(HighlightDisplayKey.find(name));
}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
public interface InspectionSuppressor {
/**
* @see com.intellij.codeInspection.CustomSuppressableInspectionTool#isSuppressedFor(com.intellij.psi.PsiElement)
*/
boolean isSuppressedFor(@NotNull PsiElement element, String toolId);
/**
* @see com.intellij.codeInspection.BatchSuppressableTool#getBatchSuppressActions(com.intellij.psi.PsiElement)
*/
SuppressQuickFix[] getSuppressActions(@NotNull PsiElement element, String toolShortName);
}
@@ -0,0 +1,12 @@
package com.intellij.codeInspection;
import com.intellij.lang.LanguageExtension;
public class LanguageInspectionSuppressors extends LanguageExtension<InspectionSuppressor> {
public static final LanguageInspectionSuppressors INSTANCE = new LanguageInspectionSuppressors();
private LanguageInspectionSuppressors() {
super("com.intellij.lang.inspectionSuppressor");
}
}
@@ -830,15 +830,27 @@ public class HighlightInfo implements Segment {
throw new AssertionError("unknown tool: " + toolWrapper+"; key: "+myKey);
}
SuppressQuickFix[] suppressFixes = null;
if (wrappedTool instanceof CustomSuppressableInspectionTool) {
suppressFixes = SuppressQuickFix.EMPTY_ARRAY;
final IntentionAction[] suppressActions = ((CustomSuppressableInspectionTool)wrappedTool).getSuppressActions(element);
if (suppressActions != null) {
ContainerUtil.addAll(newOptions, suppressActions);
}
}
if (wrappedTool instanceof BatchSuppressableTool) {
final SuppressQuickFix[] suppressActions = ((BatchSuppressableTool)wrappedTool).getBatchSuppressActions(element);
ContainerUtil.addAll(newOptions, ContainerUtil.map(suppressActions, new Function<SuppressQuickFix, IntentionAction>() {
suppressFixes = ((BatchSuppressableTool)wrappedTool).getBatchSuppressActions(element);
}
if (suppressFixes == null) {
final InspectionSuppressor suppressor = LanguageInspectionSuppressors.INSTANCE.forLanguage(element.getLanguage());
if (suppressor != null) {
suppressFixes = suppressor.getSuppressActions(element, wrappedTool.getShortName());
}
}
if (suppressFixes != null) {
ContainerUtil.addAll(newOptions, ContainerUtil.map(suppressFixes, new Function<SuppressQuickFix, IntentionAction>() {
@Override
public IntentionAction fun(SuppressQuickFix fix) {
return SuppressIntentionActionFromFix.convertBatchToSuppressIntentionAction(fix);
@@ -200,6 +200,12 @@ public class SuppressionUtil extends SuppressionUtilCore {
if (tool instanceof BatchSuppressableTool) {
return ((BatchSuppressableTool)tool).isSuppressedFor(place);
}
final InspectionSuppressor suppressor = LanguageInspectionSuppressors.INSTANCE.forLanguage(place.getLanguage());
if (suppressor != null) {
return suppressor.isSuppressedFor(place, tool.getID());
}
String alternativeId;
String id;
@@ -147,6 +147,11 @@
<with attribute="implementationClass" implements="com.intellij.lang.Commenter"/>
</extensionPoint>
<extensionPoint name="lang.inspectionSuppressor"
beanClass="com.intellij.lang.LanguageExtensionPoint">
<with attribute="implementationClass" implements="com.intellij.codeInspection.InspectionSuppressor"/>
</extensionPoint>
<extensionPoint name="lang.braceMatcher"
beanClass="com.intellij.lang.LanguageExtensionPoint">
<with attribute="implementationClass" implements="com.intellij.lang.PairedBraceMatcher"/>
@@ -529,6 +529,7 @@
<applicationConfigurable instance="com.intellij.ide.browsers.BrowserSettings" id="reference.settings.ide.settings.web.browsers"
key="browsers.settings" bundle="messages.IdeBundle"/>
<lang.inspectionSuppressor language="XML" implementationClass="com.intellij.codeInspection.XmlInspectionSuppressor"/>
</extensions>
<extensions defaultExtensionNs="org.jetbrains">
<urlOpener implementation="com.intellij.ide.browsers.impl.DefaultUrlOpener" order="last"/>
@@ -283,6 +283,7 @@
<spellchecker.support language="Groovy"
implementationClass="org.jetbrains.plugins.groovy.codeInspection.spellchecker.GroovySpellcheckingStrategy"/>
<lang.inspectionSuppressor language="Groovy" implementationClass="org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionSuppressor"/>
<colorSettingsPage implementation="org.jetbrains.plugins.groovy.highlighter.GroovyColorsAndFontsPage"/>
<framework.type implementation="org.jetbrains.plugins.groovy.config.GroovyFrameworkType"/>
@@ -0,0 +1,34 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.codeInspection;
import com.intellij.codeInspection.InspectionSuppressor;
import com.intellij.codeInspection.SuppressQuickFix;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
public class GroovyInspectionSuppressor implements InspectionSuppressor {
@Override
public boolean isSuppressedFor(@NotNull PsiElement element, @NotNull String name) {
return GroovySuppressableInspectionTool.getElementToolSuppressedIn(element, name) != null;
}
@Override
public SuppressQuickFix[] getSuppressActions(@NotNull PsiElement element, String toolShortName) {
return GroovySuppressableInspectionTool.getSuppressActions(toolShortName);
}
}
@@ -15,16 +15,14 @@
*/
package org.jetbrains.plugins.groovy.codeInspection.spellchecker;
import com.intellij.codeInspection.SuppressQuickFix;
import com.intellij.psi.PsiElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.spellchecker.inspections.PlainTextSplitter;
import com.intellij.spellchecker.tokenizer.EscapeSequenceTokenizer;
import com.intellij.spellchecker.tokenizer.SuppressibleSpellcheckingStrategy;
import com.intellij.spellchecker.tokenizer.SpellcheckingStrategy;
import com.intellij.spellchecker.tokenizer.TokenConsumer;
import com.intellij.spellchecker.tokenizer.Tokenizer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.codeInspection.GroovySuppressableInspectionTool;
import org.jetbrains.plugins.groovy.lang.lexer.TokenSets;
import org.jetbrains.plugins.groovy.lang.psi.GrNamedElement;
import org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil;
@@ -32,7 +30,7 @@ import org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil;
/**
* @author peter
*/
public class GroovySpellcheckingStrategy extends SuppressibleSpellcheckingStrategy {
public class GroovySpellcheckingStrategy extends SpellcheckingStrategy {
private final GrDocCommentTokenizer myDocCommentTokenizer = new GrDocCommentTokenizer();
private final Tokenizer<PsiElement> myStringTokenizer = new Tokenizer<PsiElement>() {
@Override
@@ -66,14 +64,4 @@ public class GroovySpellcheckingStrategy extends SuppressibleSpellcheckingStrate
//if (element instanceof GrLiteralImpl && ((GrLiteralImpl)element).isStringLiteral()) return myStringTokenizer;
return super.getTokenizer(element);
}
@Override
public boolean isSuppressedFor(@NotNull PsiElement element, @NotNull String name) {
return GroovySuppressableInspectionTool.getElementToolSuppressedIn(element, name) != null;
}
@Override
public SuppressQuickFix[] getSuppressActions(@NotNull PsiElement element, @NotNull String name) {
return GroovySuppressableInspectionTool.getSuppressActions(name);
}
}
+1
View File
@@ -1516,6 +1516,7 @@
<codeInsight.linkHandler prefix="#assignment/" handlerClass="com.intellij.codeInsight.intention.impl.config.AssignmentTooltipLinkHandler"/>
<nonProjectFileWritingAccessExtension
implementation="com.intellij.codeInsight.ExternalAnnotationsNonProjectFileWritingAccessExtension"/>
<lang.inspectionSuppressor language="JAVA" implementationClass="com.intellij.codeInspection.SuppressManagerImpl"/>
</extensions>
<actions>
@@ -61,10 +61,15 @@ public class SpellCheckingInspection extends LocalInspectionTool implements Batc
@Override
public SuppressQuickFix[] getBatchSuppressActions(@Nullable PsiElement element) {
if (element != null) {
SpellcheckingStrategy strategy = getSpellcheckingStrategy(element, element.getLanguage());
final Language language = element.getLanguage();
SpellcheckingStrategy strategy = getSpellcheckingStrategy(element, language);
if(strategy instanceof SuppressibleSpellcheckingStrategy) {
return ((SuppressibleSpellcheckingStrategy)strategy).getSuppressActions(element, getShortName());
}
final InspectionSuppressor suppressor = LanguageInspectionSuppressors.INSTANCE.forLanguage(language);
if (suppressor != null) {
return suppressor.getSuppressActions(element, getShortName());
}
}
return SuppressQuickFix.EMPTY_ARRAY;
}
@@ -80,9 +85,13 @@ public class SpellCheckingInspection extends LocalInspectionTool implements Batc
@Override
public boolean isSuppressedFor(@NotNull PsiElement element) {
SpellcheckingStrategy strategy = getSpellcheckingStrategy(element, element.getLanguage());
return strategy instanceof SuppressibleSpellcheckingStrategy &&
((SuppressibleSpellcheckingStrategy)strategy).isSuppressedFor(element, getShortName());
final Language language = element.getLanguage();
SpellcheckingStrategy strategy = getSpellcheckingStrategy(element, language);
if (strategy instanceof SuppressibleSpellcheckingStrategy) {
return ((SuppressibleSpellcheckingStrategy)strategy).isSuppressedFor(element, getShortName());
}
final InspectionSuppressor suppressor = LanguageInspectionSuppressors.INSTANCE.forLanguage(language);
return suppressor != null && suppressor.isSuppressedFor(element, getShortName());
}
@Override
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
public class XmlInspectionSuppressor implements InspectionSuppressor{
@Override
public boolean isSuppressedFor(@NotNull PsiElement element, String toolId) {
return XmlSuppressionProvider.isSuppressed(element, toolId);
}
@Override
public SuppressQuickFix[] getSuppressActions(@NotNull PsiElement element, String toolShortName) {
return XmlSuppressableInspectionTool.getSuppressFixes(toolShortName);
}
}
@@ -16,6 +16,7 @@
package com.intellij.codeInspection;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
@@ -31,7 +32,12 @@ public abstract class XmlSuppressableInspectionTool extends LocalInspectionTool
@NotNull
@Override
public SuppressQuickFix[] getBatchSuppressActions(@Nullable PsiElement element) {
return new SuppressQuickFix[]{new SuppressTag(), new SuppressForFile(getID()), new SuppressAllForFile()};
return getSuppressFixes(getID());
}
public static SuppressQuickFix[] getSuppressFixes(final String shortName) {
final String id = HighlightDisplayKey.find(shortName).getID();
return new SuppressQuickFix[]{new SuppressTagStatic(id), new SuppressForFile(id), new SuppressAllForFile()};
}
@Override