Search in sources :

Example 16 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class XmlHighlightVisitor method doCheckRefs.

private void doCheckRefs(final PsiElement value, final PsiReference[] references, int start) {
    for (int i = start; i < references.length; ++i) {
        PsiReference reference = references[i];
        ProgressManager.checkCanceled();
        if (isUrlReference(reference))
            continue;
        if (!hasBadResolve(reference, false)) {
            continue;
        }
        String description = getErrorDescription(reference);
        final int startOffset = reference.getElement().getTextRange().getStartOffset();
        final TextRange referenceRange = reference.getRangeInElement();
        // logging for IDEADEV-29655
        if (referenceRange.getStartOffset() > referenceRange.getEndOffset()) {
            LOG.error("Reference range start offset > end offset:  " + reference + ", start offset: " + referenceRange.getStartOffset() + ", end offset: " + referenceRange.getEndOffset());
        }
        HighlightInfoType type = getTagProblemInfoType(PsiTreeUtil.getParentOfType(value, XmlTag.class));
        if (value instanceof XmlAttributeValue) {
            PsiElement parent = value.getParent();
            if (parent instanceof XmlAttribute) {
                String name = ((XmlAttribute) parent).getName().toLowerCase();
                if (type.getSeverity(null).compareTo(HighlightInfoType.WARNING.getSeverity(null)) > 0 && name.endsWith("stylename")) {
                    type = HighlightInfoType.WARNING;
                }
            }
        }
        HighlightInfo info = HighlightInfo.newHighlightInfo(type).range(startOffset + referenceRange.getStartOffset(), startOffset + referenceRange.getEndOffset()).descriptionAndTooltip(description).create();
        addToResults(info);
        if (reference instanceof LocalQuickFixProvider) {
            LocalQuickFix[] fixes = ((LocalQuickFixProvider) reference).getQuickFixes();
            if (fixes != null) {
                InspectionManager manager = InspectionManager.getInstance(reference.getElement().getProject());
                for (LocalQuickFix fix : fixes) {
                    ProblemDescriptor descriptor = manager.createProblemDescriptor(value, description, fix, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, true);
                    QuickFixAction.registerQuickFixAction(info, new LocalQuickFixAsIntentionAdapter(fix, descriptor));
                }
            }
        }
        UnresolvedReferenceQuickFixProvider.registerReferenceFixes(reference, new QuickFixActionRegistrarImpl(info));
    }
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) QuickFixActionRegistrarImpl(com.intellij.codeInsight.daemon.impl.quickfix.QuickFixActionRegistrarImpl)

Example 17 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class XmlHighlightVisitor method bindMessageToAstNode.

private void bindMessageToAstNode(final PsiElement childByRole, final HighlightInfoType warning, int length, @NotNull String localizedMessage, IntentionAction... quickFixActions) {
    if (childByRole != null) {
        final TextRange textRange = childByRole.getTextRange();
        if (length == -1)
            length = textRange.getLength();
        final int startOffset = textRange.getStartOffset();
        HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(warning).range(childByRole, startOffset, startOffset + length).descriptionAndTooltip(localizedMessage).create();
        if (highlightInfo == null) {
            highlightInfo = HighlightInfo.newHighlightInfo(warning).range(new TextRange(startOffset, startOffset + length)).textAttributes(NONEMPTY_TEXT_ATTRIBUTES).descriptionAndTooltip(localizedMessage).create();
        }
        for (final IntentionAction quickFixAction : quickFixActions) {
            if (quickFixAction == null)
                continue;
            QuickFixAction.registerQuickFixAction(highlightInfo, textRange, quickFixAction);
        }
        addToResults(highlightInfo);
    }
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange)

Example 18 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class XmlHighlightVisitor method checkAttribute.

private void checkAttribute(XmlAttribute attribute) {
    XmlTag tag = attribute.getParent();
    if (tag == null)
        return;
    final String name = attribute.getName();
    PsiElement prevLeaf = PsiTreeUtil.prevLeaf(attribute);
    if (!(prevLeaf instanceof PsiWhiteSpace)) {
        TextRange textRange = attribute.getTextRange();
        HighlightInfoType type = tag instanceof HtmlTag ? HighlightInfoType.WARNING : HighlightInfoType.ERROR;
        String description = XmlErrorMessages.message("attribute.should.be.preceded.with.space");
        HighlightInfo info = HighlightInfo.newHighlightInfo(type).range(textRange.getStartOffset(), textRange.getStartOffset()).descriptionAndTooltip(description).create();
        addToResults(info);
    }
    if (attribute.isNamespaceDeclaration() || XmlUtil.XML_SCHEMA_INSTANCE_URI.equals(attribute.getNamespace())) {
        //checkReferences(attribute.getValueElement());
        return;
    }
    XmlElementDescriptor elementDescriptor = tag.getDescriptor();
    if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor || ourDoJaxpTesting) {
        return;
    }
    XmlAttributeDescriptor attributeDescriptor = elementDescriptor.getAttributeDescriptor(attribute);
    if (attributeDescriptor == null) {
        if (!XmlUtil.attributeFromTemplateFramework(name, tag)) {
            final String localizedMessage = XmlErrorMessages.message("attribute.is.not.allowed.here", name);
            final HighlightInfo highlightInfo = reportAttributeProblem(tag, name, attribute, localizedMessage);
            if (highlightInfo != null) {
                PsiFile file = tag.getContainingFile();
                if (file != null) {
                    for (XmlUndefinedElementFixProvider fixProvider : Extensions.getExtensions(XmlUndefinedElementFixProvider.EP_NAME)) {
                        IntentionAction[] fixes = fixProvider.createFixes(attribute);
                        if (fixes != null) {
                            for (IntentionAction action : fixes) {
                                QuickFixAction.registerQuickFixAction(highlightInfo, action);
                            }
                            break;
                        }
                    }
                }
            }
        }
    } else {
        checkDuplicateAttribute(tag, attribute);
        // we skip resolve of attribute references since there is separate check when taking attribute descriptors
        PsiReference[] attrRefs = attribute.getReferences();
        doCheckRefs(attribute, attrRefs, !attribute.getNamespacePrefix().isEmpty() ? 2 : 1);
    }
}
Also used : AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange) HtmlTag(com.intellij.psi.html.HtmlTag) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor)

Example 19 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class XmlHighlightVisitorBasedInspection method checkFile.

@Override
public void checkFile(@NotNull final PsiFile file, @NotNull final InspectionManager manager, @NotNull ProblemsHolder problemsHolder, @NotNull final GlobalInspectionContext globalContext, @NotNull final ProblemDescriptionsProcessor problemDescriptionsProcessor) {
    HighlightInfoHolder myHolder = new HighlightInfoHolder(file) {

        @Override
        public boolean add(@Nullable HighlightInfo info) {
            if (info != null) {
                GlobalInspectionUtil.createProblem(file, info, new TextRange(info.startOffset, info.endOffset), null, manager, problemDescriptionsProcessor, globalContext);
            }
            return true;
        }
    };
    final XmlHighlightVisitor highlightVisitor = new XmlHighlightVisitor();
    highlightVisitor.analyze(file, true, myHolder, new Runnable() {

        @Override
        public void run() {
            file.accept(new XmlRecursiveElementVisitor() {

                @Override
                public void visitElement(PsiElement element) {
                    highlightVisitor.visit(element);
                    super.visitElement(element);
                }
            });
        }
    });
}
Also used : XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement)

Example 20 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class XmlNamespaceAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        String namespace = tag.getNamespace();
        for (XmlNSColorProvider provider : PROVIDERS) {
            TextAttributesKey key = provider.getKeyForNamespace(namespace, tag);
            if (key != null) {
                TextRange range = XmlTagUtil.getStartTagRange(tag);
                if (range != null) {
                    holder.createInfoAnnotation(range, null).setTextAttributes(key);
                }
                TextRange endTagRange = XmlTagUtil.getEndTagRange(tag);
                if (endTagRange != null) {
                    holder.createInfoAnnotation(endTagRange, null).setTextAttributes(key);
                }
                return;
            }
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

TextRange (com.intellij.openapi.util.TextRange)1314 PsiElement (com.intellij.psi.PsiElement)261 NotNull (org.jetbrains.annotations.NotNull)237 Nullable (org.jetbrains.annotations.Nullable)167 Document (com.intellij.openapi.editor.Document)132 ArrayList (java.util.ArrayList)117 Project (com.intellij.openapi.project.Project)96 PsiFile (com.intellij.psi.PsiFile)96 ASTNode (com.intellij.lang.ASTNode)95 Editor (com.intellij.openapi.editor.Editor)75 PsiReference (com.intellij.psi.PsiReference)54 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 IElementType (com.intellij.psi.tree.IElementType)48 IncorrectOperationException (com.intellij.util.IncorrectOperationException)48 Pair (com.intellij.openapi.util.Pair)47 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)33 ProperTextRange (com.intellij.openapi.util.ProperTextRange)32 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)31 XmlTag (com.intellij.psi.xml.XmlTag)31 List (java.util.List)31