Search in sources :

Example 1 with AnnotationSession

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

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

the class JsonBySchemaObjectAnnotator method checkIfAlreadyProcessed.

private static boolean checkIfAlreadyProcessed(@NotNull AnnotationHolder holder, PsiElement property) {
    final AnnotationSession session = holder.getCurrentAnnotationSession();
    Set<PsiElement> data = session.getUserData(ANNOTATED_PROPERTIES);
    if (data == null) {
        data = new HashSet<>();
        session.putUserData(ANNOTATED_PROPERTIES, data);
    }
    if (data.contains(property))
        return true;
    data.add(property);
    return false;
}
Also used : AnnotationSession(com.intellij.lang.annotation.AnnotationSession) PsiElement(com.intellij.psi.PsiElement)

Example 3 with AnnotationSession

use of com.intellij.lang.annotation.AnnotationSession in project intellij-plugins by JetBrains.

the class DartAnnotator method annotate.

@Override
public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {
    if (holder.isBatchMode())
        return;
    final AnnotationSession session = holder.getCurrentAnnotationSession();
    if (session.getUserData(DART_SERVER_DATA_HANDLED) != Boolean.TRUE) {
        session.putUserData(DART_SERVER_DATA_HANDLED, Boolean.TRUE);
        final VirtualFile vFile = element.getContainingFile().getVirtualFile();
        final DartAnalysisServerService service = DartAnalysisServerService.getInstance(element.getProject());
        if (canBeAnalyzedByServer(element.getProject(), vFile) && service.serverReadyForRequest(element.getProject())) {
            service.updateFilesContent();
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                service.waitForAnalysisToComplete_TESTS_ONLY(vFile);
            }
            applyServerHighlighting(vFile, holder);
        }
    }
    if (DartTokenTypes.COLON == element.getNode().getElementType() && element.getParent() instanceof DartTernaryExpression) {
        holder.createInfoAnnotation(element, null).setTextAttributes(DartSyntaxHighlighterColors.OPERATION_SIGN);
        return;
    }
    if (DartTokenTypesSets.BUILT_IN_IDENTIFIERS.contains(element.getNode().getElementType())) {
        if (element.getNode().getTreeParent().getElementType() != DartTokenTypes.ID) {
            holder.createInfoAnnotation(element, null).setTextAttributes(DartSyntaxHighlighterColors.KEYWORD);
            return;
        }
    }
    // sync*, async* and yield*
    if (DartTokenTypes.MUL == element.getNode().getElementType()) {
        final ASTNode previous = element.getNode().getTreePrev();
        if (previous != null && (previous.getElementType() == DartTokenTypes.SYNC || previous.getElementType() == DartTokenTypes.ASYNC || previous.getElementType() == DartTokenTypes.YIELD)) {
            holder.createInfoAnnotation(element, null).setTextAttributes(DartSyntaxHighlighterColors.KEYWORD);
        }
    }
    if (element.getNode().getElementType() == DartTokenTypes.REGULAR_STRING_PART) {
        highlightEscapeSequences(element, holder);
        return;
    }
    if (element instanceof DartSymbolLiteralExpression) {
        holder.createInfoAnnotation(element, null).setTextAttributes(DartSyntaxHighlighterColors.SYMBOL_LITERAL);
        //noinspection UnnecessaryReturnStatement
        return;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AnnotationSession(com.intellij.lang.annotation.AnnotationSession) DartSymbolLiteralExpression(com.jetbrains.lang.dart.psi.DartSymbolLiteralExpression) ASTNode(com.intellij.lang.ASTNode) DartTernaryExpression(com.jetbrains.lang.dart.psi.DartTernaryExpression) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)

Example 4 with AnnotationSession

use of com.intellij.lang.annotation.AnnotationSession 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 5 with AnnotationSession

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

the class GroovyPostHighlightingPass method doApplyInformationToEditor.

@Override
public void doApplyInformationToEditor() {
    if (myUnusedDeclarations == null || myUnusedImports == null) {
        return;
    }
    AnnotationHolder annotationHolder = new AnnotationHolderImpl(new AnnotationSession(myFile));
    List<HighlightInfo> infos = new ArrayList<>(myUnusedDeclarations);
    for (GrImportStatement unusedImport : myUnusedImports) {
        Annotation annotation = annotationHolder.createWarningAnnotation(calculateRangeToUse(unusedImport), GroovyInspectionBundle.message("unused.import"));
        annotation.setHighlightType(ProblemHighlightType.LIKE_UNUSED_SYMBOL);
        annotation.registerFix(GroovyQuickFixFactory.getInstance().createOptimizeImportsFix(false));
        infos.add(HighlightInfo.fromAnnotation(annotation));
    }
    UpdateHighlightersUtil.setHighlightersToEditor(myProject, myDocument, 0, myFile.getTextLength(), infos, getColorsScheme(), getId());
    if (myUnusedImports != null && !myUnusedImports.isEmpty()) {
        IntentionAction fix = GroovyQuickFixFactory.getInstance().createOptimizeImportsFix(true);
        if (fix.isAvailable(myProject, myEditor, myFile) && myFile.isWritable()) {
            fix.invoke(myProject, myEditor, myFile);
        }
    }
}
Also used : AnnotationSession(com.intellij.lang.annotation.AnnotationSession) AnnotationHolder(com.intellij.lang.annotation.AnnotationHolder) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) Annotation(com.intellij.lang.annotation.Annotation)

Aggregations

AnnotationSession (com.intellij.lang.annotation.AnnotationSession)6 AnnotationHolderImpl (com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl)3 ASTNode (com.intellij.lang.ASTNode)2 Annotation (com.intellij.lang.annotation.Annotation)2 PsiElement (com.intellij.psi.PsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)1 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 AnnotationHolder (com.intellij.lang.annotation.AnnotationHolder)1 Annotator (com.intellij.lang.annotation.Annotator)1 ExternalAnnotator (com.intellij.lang.annotation.ExternalAnnotator)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LinkedHashMap (com.intellij.util.containers.hash.LinkedHashMap)1 DartAnalysisServerService (com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)1 DartSymbolLiteralExpression (com.jetbrains.lang.dart.psi.DartSymbolLiteralExpression)1 DartTernaryExpression (com.jetbrains.lang.dart.psi.DartTernaryExpression)1 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)1