Search in sources :

Example 66 with Annotation

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

the class ExternalAnnotatorInspectionVisitor method convertToProblemDescriptors.

@NotNull
private static ProblemDescriptor[] convertToProblemDescriptors(@NotNull final List<Annotation> annotations, @NotNull final InspectionManager manager, @NotNull final PsiFile file) {
    if (annotations.isEmpty()) {
        return ProblemDescriptor.EMPTY_ARRAY;
    }
    List<ProblemDescriptor> problems = ContainerUtil.newArrayListWithCapacity(annotations.size());
    IdentityHashMap<IntentionAction, LocalQuickFix> quickFixMappingCache = ContainerUtil.newIdentityHashMap();
    for (Annotation annotation : annotations) {
        if (annotation.getSeverity() == HighlightSeverity.INFORMATION || annotation.getStartOffset() == annotation.getEndOffset()) {
            continue;
        }
        final PsiElement startElement = file.findElementAt(annotation.getStartOffset());
        final PsiElement endElement = file.findElementAt(annotation.getEndOffset() - 1);
        if (startElement == null || endElement == null) {
            continue;
        }
        LocalQuickFix[] quickFixes = toLocalQuickFixes(annotation.getQuickFixes(), quickFixMappingCache);
        ProblemDescriptor descriptor = manager.createProblemDescriptor(startElement, endElement, annotation.getMessage(), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false, quickFixes);
        problems.add(descriptor);
    }
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) Annotation(com.intellij.lang.annotation.Annotation) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 67 with Annotation

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

the class XPathAnnotator method visitXPath2TypeElement.

@Override
public void visitXPath2TypeElement(XPath2TypeElement o) {
    final ContextProvider contextProvider = o.getXPathContext();
    checkPrefixReferences(myHolder, o, contextProvider);
    if (o.getDeclaredType() == XPathType.UNKNOWN) {
        final PsiReference[] references = o.getReferences();
        for (PsiReference reference : references) {
            if (reference instanceof XsltReferenceContributor.SchemaTypeReference) {
                if (!reference.isSoft() && reference.resolve() == null) {
                    final String message = ((EmptyResolveMessageProvider) reference).getUnresolvedMessagePattern();
                    final Annotation annotation = myHolder.createErrorAnnotation(reference.getRangeInElement().shiftRight(o.getTextOffset()), message);
                    annotation.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
                }
            }
        }
    }
    super.visitXPath2TypeElement(o);
}
Also used : EmptyResolveMessageProvider(com.intellij.codeInsight.daemon.EmptyResolveMessageProvider) PsiReference(com.intellij.psi.PsiReference) ContextProvider(org.intellij.lang.xpath.context.ContextProvider) Annotation(com.intellij.lang.annotation.Annotation)

Example 68 with Annotation

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

the class XPathAnnotator method visitXPathNumber.

@Override
public void visitXPathNumber(XPathNumber number) {
    if (number.getXPathVersion() == XPathVersion.V2) {
        final PsiElement leaf = PsiTreeUtil.nextLeaf(number);
        if (leaf != null) {
            final IElementType elementType = leaf.getNode().getElementType();
            if (elementType != XPathTokenTypes.STAR && XPath2TokenTypes.KEYWORDS.contains(elementType)) {
                final TextRange range = TextRange.create(number.getTextRange().getStartOffset(), leaf.getTextRange().getEndOffset());
                final Annotation annotation = myHolder.createErrorAnnotation(range, "Number literal must be followed by whitespace in XPath 2");
                final XPathBinaryExpression expression = PsiTreeUtil.getParentOfType(number, XPathBinaryExpression.class, true);
                if (expression != null) {
                    final XPathExpression lOperand = expression.getLOperand();
                    if (number == lOperand) {
                        final XPathExpression rOperand = expression.getROperand();
                        if (rOperand != null) {
                            final String display = number.getText() + " " + expression.getOperationSign();
                            final String replacement = display + " " + rOperand.getText();
                            assert PsiEquivalenceUtil.areElementsEquivalent(expression, XPathChangeUtil.createExpression(expression, replacement));
                            annotation.registerFix(new ExpressionReplacementFix(replacement, display, expression));
                        }
                    } else if (number == expression.getROperand()) {
                        final PsiElement next = PsiTreeUtil.getParentOfType(PsiTreeUtil.nextLeaf(expression), XPathExpression.class, true);
                        if (next instanceof XPathBinaryExpression) {
                            final XPathBinaryExpression left = (XPathBinaryExpression) next;
                            final XPathExpression rOperand = left.getROperand();
                            if (rOperand != null && lOperand != null) {
                                final String display = number.getText() + " " + left.getOperationSign();
                                final String replacement = lOperand.getText() + " " + expression.getOperationSign() + " " + display + " " + rOperand.getText();
                                assert PsiEquivalenceUtil.areElementsEquivalent(next, XPathChangeUtil.createExpression(next, replacement));
                                annotation.registerFix(new ExpressionReplacementFix(replacement, display, (XPathExpression) next));
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (((XPathNumberImpl) number).isScientificNotation()) {
            myHolder.createErrorAnnotation(number, "Number literals in scientific notation are not allowed in XPath 1.0");
        }
    }
    super.visitXPathNumber(number);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) XPathNumberImpl(org.intellij.lang.xpath.psi.impl.XPathNumberImpl) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Annotation(com.intellij.lang.annotation.Annotation)

Example 69 with Annotation

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

the class XPathAnnotator method markUnresolvedVariable.

private static void markUnresolvedVariable(XPathVariableReference reference, AnnotationHolder holder) {
    final String referencedName = reference.getReferencedName();
    // missing name is already flagged by parser
    if (referencedName.length() > 0) {
        final TextRange range = reference.getTextRange().shiftRight(1).grown(-1);
        final Annotation ann = holder.createErrorAnnotation(range, "Unresolved variable '" + referencedName + "'");
        ann.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
        final VariableContext variableContext = ContextProvider.getContextProvider(reference).getVariableContext();
        if (variableContext != null) {
            final IntentionAction[] fixes = variableContext.getUnresolvedVariableFixes(reference);
            for (IntentionAction fix : fixes) {
                ann.registerFix(fix);
            }
        }
    }
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) TextRange(com.intellij.openapi.util.TextRange) VariableContext(org.intellij.lang.xpath.context.VariableContext) Annotation(com.intellij.lang.annotation.Annotation)

Example 70 with Annotation

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

the class XPathAnnotator method checkPrefixReferences.

private static void checkPrefixReferences(AnnotationHolder holder, QNameElement element, ContextProvider myProvider) {
    final PsiReference[] references = element.getReferences();
    for (PsiReference reference : references) {
        if (reference instanceof PrefixReference) {
            final PrefixReference pr = ((PrefixReference) reference);
            if (!pr.isSoft() && pr.isUnresolved()) {
                final TextRange range = pr.getRangeInElement().shiftRight(pr.getElement().getTextRange().getStartOffset());
                final Annotation a = holder.createErrorAnnotation(range, "Unresolved namespace prefix '" + pr.getCanonicalText() + "'");
                a.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
                final NamespaceContext namespaceContext = myProvider.getNamespaceContext();
                final PrefixedName qName = element.getQName();
                if (namespaceContext != null && qName != null) {
                    final IntentionAction[] fixes = namespaceContext.getUnresolvedNamespaceFixes(reference, qName.getLocalName());
                    for (IntentionAction fix : fixes) {
                        a.registerFix(fix);
                    }
                }
            }
        }
    }
}
Also used : NamespaceContext(org.intellij.lang.xpath.context.NamespaceContext) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation)

Aggregations

Annotation (com.intellij.lang.annotation.Annotation)98 PsiElement (com.intellij.psi.PsiElement)24 ASTNode (com.intellij.lang.ASTNode)22 TextRange (com.intellij.openapi.util.TextRange)19 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)15 Project (com.intellij.openapi.project.Project)11 NotNull (org.jetbrains.annotations.NotNull)10 PsiFile (com.intellij.psi.PsiFile)8 GrModifierFix (org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix)6 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)4 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)4 Editor (com.intellij.openapi.editor.Editor)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)4 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)3 Module (com.intellij.openapi.module.Module)3 PsiReference (com.intellij.psi.PsiReference)3 IElementType (com.intellij.psi.tree.IElementType)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 Nullable (org.jetbrains.annotations.Nullable)3