Search in sources :

Example 1 with AnnotationHolderImpl

use of com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl in project intellij-community by JetBrains.

the class InspectionValidatorWrapper method runXmlFileSchemaValidation.

private Map<ProblemDescriptor, HighlightDisplayLevel> runXmlFileSchemaValidation(@NotNull XmlFile xmlFile) {
    final AnnotationHolderImpl holder = new AnnotationHolderImpl(new AnnotationSession(xmlFile));
    final List<ExternalAnnotator> annotators = ExternalLanguageAnnotators.allForFile(StdLanguages.XML, xmlFile);
    for (ExternalAnnotator<?, ?> annotator : annotators) {
        processAnnotator(xmlFile, holder, annotator);
    }
    if (!holder.hasAnnotations())
        return Collections.emptyMap();
    Map<ProblemDescriptor, HighlightDisplayLevel> problemsMap = new LinkedHashMap<>();
    for (final Annotation annotation : holder) {
        final HighlightInfo info = HighlightInfo.fromAnnotation(annotation);
        if (info.getSeverity() == HighlightSeverity.INFORMATION)
            continue;
        final PsiElement startElement = xmlFile.findElementAt(info.startOffset);
        final PsiElement endElement = info.startOffset == info.endOffset ? startElement : xmlFile.findElementAt(info.endOffset - 1);
        if (startElement == null || endElement == null)
            continue;
        final ProblemDescriptor descriptor = myInspectionManager.createProblemDescriptor(startElement, endElement, info.getDescription(), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false);
        final HighlightDisplayLevel level = info.getSeverity() == HighlightSeverity.ERROR ? HighlightDisplayLevel.ERROR : HighlightDisplayLevel.WARNING;
        problemsMap.put(descriptor, level);
    }
    return problemsMap;
}
Also used : AnnotationSession(com.intellij.lang.annotation.AnnotationSession) ExternalAnnotator(com.intellij.lang.annotation.ExternalAnnotator) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) AnnotationHolderImpl(com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Annotation(com.intellij.lang.annotation.Annotation) PsiElement(com.intellij.psi.PsiElement) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Example 2 with AnnotationHolderImpl

use of com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl in project intellij-community by JetBrains.

the class ExternalAnnotatorInspectionVisitor method checkFileWithExternalAnnotator.

@NotNull
public static <Init, Result> ProblemDescriptor[] checkFileWithExternalAnnotator(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly, @NotNull ExternalAnnotator<Init, Result> annotator) {
    if (isOnTheFly) {
        // ExternalAnnotator does this work
        return ProblemDescriptor.EMPTY_ARRAY;
    }
    Init info = ReadAction.compute(() -> annotator.collectInformation(file));
    if (info != null) {
        Result annotationResult = annotator.doAnnotate(info);
        if (annotationResult == null) {
            return ProblemDescriptor.EMPTY_ARRAY;
        }
        return ReadAction.compute(() -> {
            AnnotationHolderImpl annotationHolder = new AnnotationHolderImpl(new AnnotationSession(file));
            annotator.apply(file, annotationResult, annotationHolder);
            return convertToProblemDescriptors(annotationHolder, manager, file);
        });
    }
    return ProblemDescriptor.EMPTY_ARRAY;
}
Also used : AnnotationSession(com.intellij.lang.annotation.AnnotationSession) AnnotationHolderImpl(com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with AnnotationHolderImpl

use of com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl in project intellij-community by JetBrains.

the class MyPsiUtil method checkFile.

public static String checkFile(final PsiFile file) {
    final String[] error = new String[1];
    file.accept(new PsiRecursiveElementVisitor() {

        public void visitErrorElement(PsiErrorElement element) {
            error[0] = element.getErrorDescription();
        }
    });
    if (error[0] != null)
        return error[0];
    final Annotator annotator = LanguageAnnotators.INSTANCE.forLanguage(file.getLanguage());
    file.accept(new PsiRecursiveElementVisitor() {

        public void visitElement(PsiElement element) {
            annotator.annotate(element, new AnnotationHolderImpl(new AnnotationSession(file)) {

                public Annotation createErrorAnnotation(@NotNull ASTNode astNode, String string) {
                    error[0] = string;
                    return super.createErrorAnnotation(astNode, string);
                }

                public Annotation createErrorAnnotation(@NotNull PsiElement element, String string) {
                    error[0] = string;
                    return super.createErrorAnnotation(element, string);
                }

                public Annotation createErrorAnnotation(@NotNull TextRange textRange, String string) {
                    error[0] = string;
                    return super.createErrorAnnotation(textRange, string);
                }
            });
            super.visitElement(element);
        }
    });
    return error[0];
}
Also used : AnnotationSession(com.intellij.lang.annotation.AnnotationSession) Annotator(com.intellij.lang.annotation.Annotator) AnnotationHolderImpl(com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AnnotationHolderImpl (com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl)3 AnnotationSession (com.intellij.lang.annotation.AnnotationSession)3 NotNull (org.jetbrains.annotations.NotNull)2 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)1 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 ASTNode (com.intellij.lang.ASTNode)1 Annotation (com.intellij.lang.annotation.Annotation)1 Annotator (com.intellij.lang.annotation.Annotator)1 ExternalAnnotator (com.intellij.lang.annotation.ExternalAnnotator)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 LinkedHashMap (com.intellij.util.containers.hash.LinkedHashMap)1