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;
}
}
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);
}
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;
}
Aggregations