use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class TagNameReference method getRangeInElement.
@Override
public TextRange getRangeInElement() {
final ASTNode nameElement = getNameElement();
if (nameElement == null) {
return TextRange.EMPTY_RANGE;
}
int colon = nameElement.getText().indexOf(':') + 1;
if (myStartTagFlag) {
final int parentOffset = ((TreeElement) nameElement).getStartOffsetInParent();
return new TextRange(parentOffset + colon, parentOffset + nameElement.getTextLength());
} else {
final PsiElement element = getElement();
if (element == myNameElement)
return new TextRange(colon, myNameElement.getTextLength());
final int elementLength = element.getTextLength();
int diffFromEnd = 0;
for (ASTNode node = element.getNode().getLastChildNode(); node != nameElement && node != null; node = node.getTreePrev()) {
diffFromEnd += node.getTextLength();
}
final int nameEnd = elementLength - diffFromEnd;
return new TextRange(nameEnd - nameElement.getTextLength() + colon, nameEnd);
}
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class XmlWhiteSpaceFormattingStrategy method addWhitespaceToTagBody.
private static void addWhitespaceToTagBody(final ASTNode treePrev, final LeafElement whiteSpaceElement) {
final CharTable charTable = SharedImplUtil.findCharTableByTree(treePrev);
final ASTNode treeParent = treePrev.getTreeParent();
final boolean before;
final XmlText xmlText;
if (treePrev.getElementType() == XmlElementType.XML_TEXT) {
xmlText = (XmlText) treePrev.getPsi();
before = true;
} else if (treePrev.getTreePrev().getElementType() == XmlElementType.XML_TEXT) {
xmlText = (XmlText) treePrev.getTreePrev().getPsi();
before = false;
} else {
xmlText = (XmlText) Factory.createCompositeElement(XmlElementType.XML_TEXT, charTable, treeParent.getPsi().getManager());
CodeEditUtil.setNodeGenerated(xmlText.getNode(), true);
treeParent.addChild(xmlText.getNode(), treePrev);
before = true;
}
final ASTNode node = xmlText.getNode();
assert node != null;
final TreeElement anchorInText = (TreeElement) (before ? node.getFirstChildNode() : node.getLastChildNode());
if (anchorInText == null)
node.addChild(whiteSpaceElement);
else if (anchorInText.getElementType() != XmlTokenType.XML_WHITE_SPACE)
node.addChild(whiteSpaceElement, before ? anchorInText : null);
else {
final String text = before ? whiteSpaceElement.getText() + anchorInText.getText() : anchorInText.getText() + whiteSpaceElement.getText();
node.replaceChild(anchorInText, ASTFactory.whitespace(text));
}
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class PropertiesFileImpl method addPropertyAfter.
@Override
@NotNull
public PsiElement addPropertyAfter(@NotNull final IProperty property, @Nullable final IProperty anchor) throws IncorrectOperationException {
final TreeElement copy = ChangeUtil.copyToElement(property.getPsiElement());
List<IProperty> properties = getProperties();
ASTNode anchorBefore = anchor == null ? properties.isEmpty() ? null : properties.get(0).getPsiElement().getNode() : anchor.getPsiElement().getNode().getTreeNext();
if (anchorBefore != null) {
if (anchorBefore.getElementType() == TokenType.WHITE_SPACE) {
anchorBefore = anchorBefore.getTreeNext();
}
}
if (anchorBefore == null && haveToAddNewLine()) {
insertLineBreakBefore(null);
}
getPropertiesList().addChild(copy, anchorBefore);
if (anchorBefore != null) {
insertLineBreakBefore(anchorBefore);
}
return copy.getPsi();
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class TreeChangeEventImpl method compactChanges.
private void compactChanges(ASTNode parent, int depth) {
int currentDepth = myOfEqualDepth.size();
while (--currentDepth > depth) {
final Set<ASTNode> treeElements = myOfEqualDepth.get(currentDepth);
if (treeElements == null)
continue;
final Iterator<ASTNode> iterator = treeElements.iterator();
while (iterator.hasNext()) {
boolean isUnderCompacted = false;
final TreeElement treeElement = (TreeElement) iterator.next();
ASTNode currentParent = treeElement;
while (currentParent != null) {
if (currentParent == parent) {
isUnderCompacted = true;
break;
}
currentParent = currentParent.getTreeParent();
}
if (isUnderCompacted) {
final ChangeInfoImpl compactedChange = ChangeInfoImpl.create(ChangeInfo.CONTENTS_CHANGED, treeElement);
compactedChange.compactChange(getChangesByElement(treeElement));
iterator.remove();
removeAssociatedChanges(treeElement, currentDepth);
final CompositeElement treeParent = treeElement.getTreeParent();
final TreeChange changesByElement = getChangesByElement(treeParent);
if (changesByElement != null) {
final ChangeInfoImpl changeByChild = (ChangeInfoImpl) changesByElement.getChangeByChild(treeElement);
if (changeByChild != null) {
changeByChild.setOldLength(compactedChange.getOldLength());
} else {
changesByElement.addChange(treeElement, compactedChange);
}
} else {
processElementaryChange(treeParent, treeElement, compactedChange, currentDepth - 1);
}
}
}
}
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class GroovyChangeUtilSupport method decodeInformation.
@Override
public TreeElement decodeInformation(TreeElement element, final Map<Object, Object> decodingState) {
if (element instanceof CompositeElement) {
if (element.getElementType() == GroovyElementTypes.REFERENCE_ELEMENT || element.getElementType() == GroovyElementTypes.REFERENCE_EXPRESSION) {
GrReferenceElement ref = (GrReferenceElement) SourceTreeToPsiMap.treeElementToPsi(element);
final PsiMember refMember = element.getCopyableUserData(REFERENCED_MEMBER_KEY);
if (refMember != null) {
element.putCopyableUserData(REFERENCED_MEMBER_KEY, null);
PsiElement refElement1 = ref.resolve();
if (!refMember.getManager().areElementsEquivalent(refMember, refElement1)) {
try {
if (!(refMember instanceof PsiClass) || ref.getQualifier() == null) {
// can restore only if short (otherwise qualifier should be already restored)
ref = (GrReferenceElement) ref.bindToElement(refMember);
}
} catch (IncorrectOperationException ignored) {
}
return (TreeElement) SourceTreeToPsiMap.psiElementToTree(ref);
}
}
return element;
}
}
return null;
}
Aggregations