Search in sources :

Example 11 with UnfairTextRange

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

the class XmlCodeFoldingBuilder method getRangeToFold.

@Nullable
public TextRange getRangeToFold(PsiElement element) {
    if (element instanceof XmlTag) {
        final ASTNode tagNode = element.getNode();
        XmlToken tagNameElement = XmlTagUtil.getStartTagNameElement((XmlTag) element);
        if (tagNameElement == null)
            return null;
        int nameEnd = tagNameElement.getTextRange().getEndOffset();
        // last child node can be another tag in unbalanced tree
        int end = tagNode.getLastChildNode().getTextRange().getEndOffset() - 1;
        ASTNode[] attributes = tagNode.getChildren(XML_ATTRIBUTE_SET);
        if (attributes.length > 0) {
            ASTNode lastAttribute = attributes[attributes.length - 1];
            ASTNode lastAttributeBeforeCR = null;
            for (ASTNode child = tagNode.getFirstChildNode(); child != lastAttribute.getTreeNext(); child = child.getTreeNext()) {
                if (child.getElementType() == XmlElementType.XML_ATTRIBUTE) {
                    lastAttributeBeforeCR = child;
                } else if (child.getPsi() instanceof PsiWhiteSpace) {
                    if (child.textContains('\n'))
                        break;
                }
            }
            if (lastAttributeBeforeCR != null) {
                int attributeEnd = lastAttributeBeforeCR.getTextRange().getEndOffset();
                return new UnfairTextRange(attributeEnd, end);
            }
        }
        return new UnfairTextRange(nameEnd, end);
    } else if (element instanceof XmlComment) {
        final XmlComment xmlComment = (XmlComment) element;
        final TextRange textRange = element.getTextRange();
        int commentStartOffset = getCommentStartOffset(xmlComment);
        int commentEndOffset = getCommentStartEnd(xmlComment);
        if (textRange.getEndOffset() - textRange.getStartOffset() > commentStartOffset + commentEndOffset) {
            return new TextRange(textRange.getStartOffset() + commentStartOffset, textRange.getEndOffset() - commentEndOffset);
        } else {
            return null;
        }
    } else if (element instanceof XmlConditionalSection) {
        final XmlConditionalSection conditionalSection = (XmlConditionalSection) element;
        final TextRange textRange = element.getTextRange();
        final PsiElement bodyStart = conditionalSection.getBodyStart();
        int startOffset = bodyStart != null ? bodyStart.getStartOffsetInParent() : MIN_TEXT_RANGE_LENGTH;
        int endOffset = MIN_TEXT_RANGE_LENGTH;
        if (textRange.getEndOffset() - textRange.getStartOffset() > startOffset + endOffset) {
            return new TextRange(textRange.getStartOffset() + startOffset, textRange.getEndOffset() - endOffset);
        } else {
            return null;
        }
    } else if (element instanceof XmlAttribute) {
        final XmlAttributeValue valueElement = ((XmlAttribute) element).getValueElement();
        return valueElement != null ? valueElement.getValueTextRange() : null;
    } else if (isEntity(element)) {
        return element.getTextRange();
    } else {
        return null;
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with UnfairTextRange

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

the class DomElementResolveProblemDescriptorImpl method computeProblemRange.

@Override
@NotNull
protected Pair<TextRange, PsiElement> computeProblemRange() {
    final PsiReference reference = myReference;
    PsiElement element = reference.getElement();
    if (element instanceof XmlAttributeValue && element.getTextLength() == 0)
        return NO_PROBLEM;
    final TextRange referenceRange = reference.getRangeInElement();
    if (referenceRange.isEmpty()) {
        int startOffset = referenceRange.getStartOffset();
        return element instanceof XmlAttributeValue ? Pair.create((TextRange) new UnfairTextRange(startOffset - 1, startOffset + 1), element) : Pair.create(TextRange.from(startOffset, 1), element);
    }
    return Pair.create(referenceRange, element);
}
Also used : UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with UnfairTextRange

use of com.intellij.openapi.util.UnfairTextRange in project intellij-plugins by JetBrains.

the class DartFoldingBuilder method foldFileHeader.

@Nullable
private static TextRange foldFileHeader(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final DartFile dartFile, @NotNull final Document document) {
    PsiElement firstComment = dartFile.getFirstChild();
    if (firstComment instanceof PsiWhiteSpace)
        firstComment = firstComment.getNextSibling();
    if (!(firstComment instanceof PsiComment))
        return null;
    boolean containsCustomRegionMarker = false;
    PsiElement nextAfterComments = firstComment;
    while (nextAfterComments instanceof PsiComment || nextAfterComments instanceof PsiWhiteSpace) {
        containsCustomRegionMarker |= isCustomRegionElement(nextAfterComments);
        nextAfterComments = nextAfterComments.getNextSibling();
    }
    if (nextAfterComments == null)
        return null;
    if (nextAfterComments instanceof DartLibraryStatement || nextAfterComments instanceof DartPartStatement || nextAfterComments instanceof DartPartOfStatement || nextAfterComments instanceof DartImportOrExportStatement) {
        if (nextAfterComments.getPrevSibling() instanceof PsiWhiteSpace)
            nextAfterComments = nextAfterComments.getPrevSibling();
        if (nextAfterComments.equals(firstComment))
            return null;
        final TextRange fileHeaderCommentsRange = new UnfairTextRange(firstComment.getTextRange().getStartOffset(), nextAfterComments.getTextRange().getStartOffset());
        if (fileHeaderCommentsRange.getLength() > 1 && document.getLineNumber(fileHeaderCommentsRange.getEndOffset()) > document.getLineNumber(fileHeaderCommentsRange.getStartOffset())) {
            if (!containsCustomRegionMarker) {
                descriptors.add(new FoldingDescriptor(dartFile, fileHeaderCommentsRange));
            }
            return fileHeaderCommentsRange;
        }
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)13 TextRange (com.intellij.openapi.util.TextRange)12 PsiElement (com.intellij.psi.PsiElement)4 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 Nullable (org.jetbrains.annotations.Nullable)3 ASTNode (com.intellij.lang.ASTNode)2 NotNull (org.jetbrains.annotations.NotNull)2 IntPair (com.intellij.diff.util.IntPair)1 Language (com.intellij.lang.Language)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 Document (com.intellij.openapi.editor.Document)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 ManualRangeMarker (com.intellij.openapi.editor.impl.ManualRangeMarker)1 PsiComment (com.intellij.psi.PsiComment)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 PsiReference (com.intellij.psi.PsiReference)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 BeforeAfter (com.intellij.util.BeforeAfter)1 ArrayList (java.util.ArrayList)1