Search in sources :

Example 1 with ExternalAnnotator

use of com.intellij.lang.annotation.ExternalAnnotator 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 ExternalAnnotator

use of com.intellij.lang.annotation.ExternalAnnotator in project intellij-community by JetBrains.

the class ExternalAnnotatorBatchInspection method checkFile.

/**
   * To be invoked during batch run
   */
@NotNull
default default ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull GlobalInspectionContext context, @NotNull InspectionManager manager) {
    final String shortName = getShortName();
    final FileViewProvider viewProvider = file.getViewProvider();
    final Set<Language> relevantLanguages = viewProvider.getLanguages();
    for (Language language : relevantLanguages) {
        PsiFile psiRoot = viewProvider.getPsi(language);
        final List<ExternalAnnotator> externalAnnotators = ExternalLanguageAnnotators.allForFile(language, psiRoot);
        for (ExternalAnnotator annotator : externalAnnotators) {
            if (shortName.equals(annotator.getPairedBatchInspectionShortName())) {
                return ExternalAnnotatorInspectionVisitor.checkFileWithExternalAnnotator(file, manager, false, annotator);
            }
        }
    }
    return ProblemDescriptor.EMPTY_ARRAY;
}
Also used : ExternalAnnotator(com.intellij.lang.annotation.ExternalAnnotator) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ExternalAnnotator

use of com.intellij.lang.annotation.ExternalAnnotator in project intellij-community by JetBrains.

the class DaemonRespondToChangesTest method testHighlightingDoesWaitForEmbarrassinglySlowExternalAnnotatorsToFinish.

public void testHighlightingDoesWaitForEmbarrassinglySlowExternalAnnotatorsToFinish() throws Exception {
    configureByText(JavaFileType.INSTANCE, "class X { int f() { int gg<caret> = 11; return 0;} }");
    final AtomicBoolean run = new AtomicBoolean();
    final int SLEEP = 20000;
    ExternalAnnotator<Integer, Integer> annotator = new ExternalAnnotator<Integer, Integer>() {

        @Nullable
        @Override
        public Integer collectInformation(@NotNull PsiFile file) {
            return 0;
        }

        @Nullable
        @Override
        public Integer doAnnotate(final Integer collectedInfo) {
            TimeoutUtil.sleep(SLEEP);
            return 0;
        }

        @Override
        public void apply(@NotNull final PsiFile file, final Integer annotationResult, @NotNull final AnnotationHolder holder) {
            run.set(true);
        }
    };
    ExternalLanguageAnnotators.INSTANCE.addExplicitExtension(JavaLanguage.INSTANCE, annotator);
    try {
        long start = System.currentTimeMillis();
        List<HighlightInfo> errors = filter(CodeInsightTestFixtureImpl.instantiateAndRun(getFile(), getEditor(), new int[0], false), HighlightSeverity.ERROR);
        long elapsed = System.currentTimeMillis() - start;
        assertEquals(0, errors.size());
        if (!run.get()) {
            fail(ThreadDumper.dumpThreadsToString());
        }
        assertTrue("Elapsed: " + elapsed, elapsed >= SLEEP);
    } finally {
        ExternalLanguageAnnotators.INSTANCE.removeExplicitExtension(JavaLanguage.INSTANCE, annotator);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExternalAnnotator(com.intellij.lang.annotation.ExternalAnnotator) AnnotationHolder(com.intellij.lang.annotation.AnnotationHolder) NotNull(org.jetbrains.annotations.NotNull) LightweightHint(com.intellij.ui.LightweightHint)

Example 4 with ExternalAnnotator

use of com.intellij.lang.annotation.ExternalAnnotator in project intellij-community by JetBrains.

the class ExternalToolPass method collectInformationWithProgress.

@Override
protected void collectInformationWithProgress(@NotNull ProgressIndicator progress) {
    final FileViewProvider viewProvider = myFile.getViewProvider();
    final Set<Language> relevantLanguages = viewProvider.getLanguages();
    int externalAnnotatorsInRoots = 0;
    for (Language language : relevantLanguages) {
        PsiFile psiRoot = viewProvider.getPsi(language);
        if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot))
            continue;
        final List<ExternalAnnotator> externalAnnotators = ExternalLanguageAnnotators.allForFile(language, psiRoot);
        externalAnnotatorsInRoots += externalAnnotators.size();
    }
    setProgressLimit(externalAnnotatorsInRoots);
    InspectionProfileImpl profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    for (Language language : relevantLanguages) {
        PsiFile psiRoot = viewProvider.getPsi(language);
        if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot))
            continue;
        final List<ExternalAnnotator> externalAnnotators = ExternalLanguageAnnotators.allForFile(language, psiRoot);
        if (!externalAnnotators.isEmpty()) {
            DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject);
            boolean errorFound = daemonCodeAnalyzer.getFileStatusMap().wasErrorFound(myDocument);
            for (ExternalAnnotator externalAnnotator : externalAnnotators) {
                String shortName = externalAnnotator.getPairedBatchInspectionShortName();
                if (shortName != null) {
                    HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
                    LOG.assertTrue(key != null, "Paired tool '" + shortName + "' not found for external annotator: " + externalAnnotator);
                    if (!profile.isToolEnabled(key, myFile))
                        continue;
                }
                final Object collectedInfo;
                Editor editor = getEditor();
                if (editor != null) {
                    collectedInfo = externalAnnotator.collectInformation(psiRoot, editor, errorFound);
                } else {
                    collectedInfo = externalAnnotator.collectInformation(psiRoot);
                }
                advanceProgress(1);
                if (collectedInfo != null) {
                    myAnnotator2DataMap.put(externalAnnotator, new MyData(psiRoot, collectedInfo));
                }
            }
        }
    }
}
Also used : ExternalAnnotator(com.intellij.lang.annotation.ExternalAnnotator) InspectionProfileImpl(com.intellij.codeInspection.ex.InspectionProfileImpl) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Aggregations

ExternalAnnotator (com.intellij.lang.annotation.ExternalAnnotator)4 Language (com.intellij.lang.Language)2 FileViewProvider (com.intellij.psi.FileViewProvider)2 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)1 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)1 AnnotationHolderImpl (com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl)1 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 InspectionProfileImpl (com.intellij.codeInspection.ex.InspectionProfileImpl)1 Annotation (com.intellij.lang.annotation.Annotation)1 AnnotationHolder (com.intellij.lang.annotation.AnnotationHolder)1 AnnotationSession (com.intellij.lang.annotation.AnnotationSession)1 Editor (com.intellij.openapi.editor.Editor)1 PsiElement (com.intellij.psi.PsiElement)1 LightweightHint (com.intellij.ui.LightweightHint)1 LinkedHashMap (com.intellij.util.containers.hash.LinkedHashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1