Search in sources :

Example 1 with TreeElement

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);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 2 with TreeElement

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));
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) XmlText(com.intellij.psi.xml.XmlText) CharTable(com.intellij.util.CharTable) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 3 with TreeElement

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();
}
Also used : ASTNode(com.intellij.lang.ASTNode) TreeElement(com.intellij.psi.impl.source.tree.TreeElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with TreeElement

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);
                }
            }
        }
    }
}
Also used : TreeChange(com.intellij.pom.tree.events.TreeChange) ASTNode(com.intellij.lang.ASTNode) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 5 with TreeElement

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;
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Aggregations

TreeElement (com.intellij.psi.impl.source.tree.TreeElement)43 ASTNode (com.intellij.lang.ASTNode)12 IncorrectOperationException (com.intellij.util.IncorrectOperationException)5 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)4 PsiElement (com.intellij.psi.PsiElement)3 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)3 Nullable (org.jetbrains.annotations.Nullable)3 Document (com.intellij.openapi.editor.Document)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PomModel (com.intellij.pom.PomModel)2 PomTransactionBase (com.intellij.pom.impl.PomTransactionBase)2 XmlAspect (com.intellij.pom.xml.XmlAspect)2 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)2 FileElement (com.intellij.psi.impl.source.tree.FileElement)2 TokenSet (com.intellij.psi.tree.TokenSet)2 CharTable (com.intellij.util.CharTable)2 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 Project (com.intellij.openapi.project.Project)1