mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
reporting xml highlight visitor problems as inspection
This commit is contained in:
@@ -314,6 +314,20 @@ public class HighlightInfo {
|
||||
: severity == HighlightSeverity.INFO ? HighlightInfoType.INFO : HighlightInfoType.INFORMATION;
|
||||
}
|
||||
|
||||
public static ProblemHighlightType convertType(HighlightInfoType infoType) {
|
||||
if (infoType == HighlightInfoType.ERROR || infoType == HighlightInfoType.WRONG_REF) return ProblemHighlightType.ERROR;
|
||||
if (infoType == HighlightInfoType.WARNING) return ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
|
||||
if (infoType == HighlightInfoType.INFORMATION) return ProblemHighlightType.INFORMATION;
|
||||
return ProblemHighlightType.INFO;
|
||||
}
|
||||
|
||||
public static ProblemHighlightType convertSeverityToProblemHighlight(HighlightSeverity severity) {
|
||||
return severity == HighlightSeverity.ERROR? ProblemHighlightType.ERROR :
|
||||
severity == HighlightSeverity.WARNING ? ProblemHighlightType.GENERIC_ERROR_OR_WARNING :
|
||||
severity == HighlightSeverity.INFO ? ProblemHighlightType.INFO : ProblemHighlightType.INFORMATION;
|
||||
}
|
||||
|
||||
|
||||
public boolean hasHint() {
|
||||
return hasHint;
|
||||
}
|
||||
|
||||
@@ -2,25 +2,24 @@ package com.intellij.codeInspection;
|
||||
|
||||
import com.intellij.analysis.AnalysisScope;
|
||||
import com.intellij.codeHighlighting.HighlightDisplayLevel;
|
||||
import com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl;
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.lang.LanguageAnnotators;
|
||||
import com.intellij.lang.annotation.Annotator;
|
||||
import com.intellij.lang.annotation.AnnotationHolder;
|
||||
import com.intellij.lang.annotation.Annotation;
|
||||
import com.intellij.lang.annotation.AnnotationHolder;
|
||||
import com.intellij.lang.annotation.Annotator;
|
||||
import com.intellij.lang.annotation.HighlightSeverity;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiLanguageInjectionHost;
|
||||
import com.intellij.psi.PsiRecursiveElementVisitor;
|
||||
import com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class AnnotatorBasedInspection extends GlobalInspectionTool {
|
||||
@Override
|
||||
public boolean isGraphNeeded() {
|
||||
@@ -62,17 +61,12 @@ public class AnnotatorBasedInspection extends GlobalInspectionTool {
|
||||
}
|
||||
|
||||
private static class MyPsiRecursiveElementVisitor extends PsiRecursiveElementVisitor implements PsiLanguageInjectionHost.InjectedPsiVisitor {
|
||||
private final InspectionManager manager;
|
||||
private final GlobalInspectionContext globalContext;
|
||||
private final ProblemDescriptionsProcessor problemDescriptionsProcessor;
|
||||
private AnnotationHolder myHolder;
|
||||
private List<Annotator> annotators;
|
||||
private PsiFile myFile;
|
||||
|
||||
public MyPsiRecursiveElementVisitor(final InspectionManager manager, final GlobalInspectionContext globalContext,
|
||||
final ProblemDescriptionsProcessor problemDescriptionsProcessor) {
|
||||
this.manager = manager;
|
||||
this.globalContext = globalContext;
|
||||
this.problemDescriptionsProcessor = problemDescriptionsProcessor;
|
||||
myHolder = new AnnotationHolderImpl() {
|
||||
@Override
|
||||
public Annotation createErrorAnnotation(@NotNull PsiElement elt, String message) {
|
||||
@@ -86,7 +80,7 @@ public class AnnotatorBasedInspection extends GlobalInspectionTool {
|
||||
|
||||
@Override
|
||||
public Annotation createInfoAnnotation(PsiElement elt, String message) {
|
||||
return createProblem(elt, message, ProblemHighlightType.INFO, HighlightSeverity.INFO, null);
|
||||
return super.createInfoAnnotation(elt, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -96,27 +90,54 @@ public class AnnotatorBasedInspection extends GlobalInspectionTool {
|
||||
|
||||
private Annotation createProblem(PsiElement elt, String message, ProblemHighlightType problemHighlightType,
|
||||
HighlightSeverity severity, TextRange range) {
|
||||
ProblemDescriptor descriptor = manager.createProblemDescriptor(
|
||||
elt,
|
||||
range,
|
||||
GlobalInspectionUtil.createInspectionMessage(message),
|
||||
problemHighlightType
|
||||
);
|
||||
problemDescriptionsProcessor.addProblemElement(
|
||||
GlobalInspectionUtil.retrieveRefElement(elt, globalContext),
|
||||
descriptor
|
||||
);
|
||||
GlobalInspectionUtil.createProblem(elt, message, problemHighlightType, range, manager, problemDescriptionsProcessor, globalContext);
|
||||
return super.createAnnotation(elt.getTextRange(), severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation createErrorAnnotation(ASTNode node, String message) {
|
||||
return createErrorAnnotation(node.getPsi(), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation createWarningAnnotation(ASTNode node, String message) {
|
||||
return createWarningAnnotation(node.getPsi(), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation createInformationAnnotation(ASTNode node, String message) {
|
||||
return createInformationAnnotation(node.getPsi(), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation createInfoAnnotation(ASTNode node, String message) {
|
||||
return createInfoAnnotation(node.getPsi(), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Annotation createAnnotation(TextRange range, HighlightSeverity severity, String message) {
|
||||
assert false;
|
||||
if (severity != HighlightSeverity.INFORMATION) {
|
||||
GlobalInspectionUtil.createProblem(
|
||||
myFile,
|
||||
message,
|
||||
HighlightInfo.convertSeverityToProblemHighlight(severity),
|
||||
range,
|
||||
manager,
|
||||
problemDescriptionsProcessor,
|
||||
globalContext
|
||||
);
|
||||
}
|
||||
return super.createAnnotation(range, severity, message);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitFile(PsiFile file) {
|
||||
myFile = file;
|
||||
super.visitFile(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitElement(PsiElement element) {
|
||||
super.visitElement(element);
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.intellij.codeInspection;
|
||||
import com.intellij.codeInspection.reference.RefElement;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
|
||||
/**
|
||||
* User: Maxim.Mossienko
|
||||
@@ -25,4 +26,19 @@ public class GlobalInspectionUtil {
|
||||
public static String createInspectionMessage(String message) {
|
||||
return message + LOC_MARKER;
|
||||
}
|
||||
|
||||
public static void createProblem(PsiElement elt, String message, ProblemHighlightType problemHighlightType, TextRange range,
|
||||
InspectionManager manager, ProblemDescriptionsProcessor problemDescriptionsProcessor,
|
||||
GlobalInspectionContext globalContext) {
|
||||
ProblemDescriptor descriptor = manager.createProblemDescriptor(
|
||||
elt,
|
||||
range,
|
||||
createInspectionMessage(message),
|
||||
problemHighlightType
|
||||
);
|
||||
problemDescriptionsProcessor.addProblemElement(
|
||||
retrieveRefElement(elt, globalContext),
|
||||
descriptor
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ package com.intellij.codeInspection.ui;
|
||||
|
||||
import com.intellij.codeInspection.CommonProblemDescriptor;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ProblemHighlightType;
|
||||
import com.intellij.codeInspection.ex.DescriptorProviderInspection;
|
||||
import com.intellij.codeInspection.ex.ProblemDescriptorImpl;
|
||||
import com.intellij.codeInspection.reference.RefElement;
|
||||
import com.intellij.codeInspection.reference.RefEntity;
|
||||
import com.intellij.openapi.util.IconLoader;
|
||||
@@ -18,7 +20,9 @@ import javax.swing.*;
|
||||
* @author max
|
||||
*/
|
||||
public class ProblemDescriptionNode extends InspectionTreeNode {
|
||||
public static final Icon INFO = IconLoader.getIcon("/compiler/information.png");
|
||||
private static final Icon INFO = IconLoader.getIcon("/compiler/information.png");
|
||||
private static final Icon ERROR = IconLoader.getIcon("/compiler/error.png");
|
||||
private static final Icon WARNING = IconLoader.getIcon("/compiler/warning.png");
|
||||
protected RefEntity myElement;
|
||||
private CommonProblemDescriptor myDescriptor;
|
||||
protected DescriptorProviderInspection myTool;
|
||||
@@ -44,6 +48,9 @@ public class ProblemDescriptionNode extends InspectionTreeNode {
|
||||
public CommonProblemDescriptor getDescriptor() { return myDescriptor; }
|
||||
|
||||
public Icon getIcon(boolean expanded) {
|
||||
ProblemHighlightType problemHighlightType = ((ProblemDescriptorImpl)myDescriptor).getHighlightType();
|
||||
if (problemHighlightType == ProblemHighlightType.ERROR) return ERROR;
|
||||
if (problemHighlightType == ProblemHighlightType.GENERIC_ERROR_OR_WARNING) return WARNING;
|
||||
return INFO;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
<font face="verdana" size="-1">Allows to see xml stuff validation problems in the results of Analyze | Inspect Code.
|
||||
</body>
|
||||
</html>
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.intellij.codeInsight.daemon.impl.analysis;
|
||||
|
||||
import com.intellij.analysis.AnalysisScope;
|
||||
import com.intellij.codeHighlighting.HighlightDisplayLevel;
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfoFilter;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.XmlRecursiveElementVisitor;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class XmlHighlightVisitorBasedInspection extends GlobalInspectionTool {
|
||||
@Override
|
||||
public boolean isGraphNeeded() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public HighlightDisplayLevel getDefaultLevel() {
|
||||
return HighlightDisplayLevel.ERROR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runInspection(AnalysisScope scope,
|
||||
final InspectionManager manager,
|
||||
final GlobalInspectionContext globalContext,
|
||||
final ProblemDescriptionsProcessor problemDescriptionsProcessor) {
|
||||
scope.accept(new XmlRecursiveElementVisitor() {
|
||||
final XmlHighlightVisitor highlightVisitor = new XmlHighlightVisitor();
|
||||
|
||||
HighlightInfoHolder myHolder;
|
||||
|
||||
@Override
|
||||
public void visitFile(final PsiFile file) {
|
||||
myHolder = new HighlightInfoHolder(file, HighlightInfoFilter.EMPTY_ARRAY) {
|
||||
@Override
|
||||
public boolean add(@Nullable HighlightInfo info) {
|
||||
if (info != null) {
|
||||
ProblemHighlightType problemHighlightType = HighlightInfo.convertType(info.type);
|
||||
GlobalInspectionUtil.createProblem(
|
||||
file,
|
||||
info.description,
|
||||
problemHighlightType,
|
||||
new TextRange(info.startOffset, info.endOffset),
|
||||
manager,
|
||||
problemDescriptionsProcessor,
|
||||
globalContext
|
||||
);
|
||||
}
|
||||
return super.add(info);
|
||||
}
|
||||
};
|
||||
myHolder.setWritable(true);
|
||||
super.visitFile(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitElement(PsiElement element) {
|
||||
highlightVisitor.visit(element, myHolder);
|
||||
super.visitElement(element);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getGroupDisplayName() {
|
||||
return "General";
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return "Xml Highlighting";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getShortName() {
|
||||
return "XmlHighlighting";
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -3,6 +3,7 @@ package com.intellij.codeInspection.htmlInspections;
|
||||
import com.intellij.codeInspection.InspectionToolProvider;
|
||||
import com.intellij.xml.util.*;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.XmlUnboundNsPrefixInspection;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitorBasedInspection;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
@@ -21,7 +22,8 @@ public class XmlInspectionToolProvider implements InspectionToolProvider {
|
||||
XmlWrongRootElementInspection.class,
|
||||
HtmlUnknownTagInspection.class,
|
||||
HtmlUnknownAttributeInspection.class,
|
||||
XmlUnboundNsPrefixInspection.class
|
||||
XmlUnboundNsPrefixInspection.class,
|
||||
XmlHighlightVisitorBasedInspection.class
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user