Search in sources :

Example 11 with ChangeInfo

use of com.intellij.pom.tree.events.ChangeInfo in project intellij-community by JetBrains.

the class TreeChangeImpl method getOldLength.

@Override
public int getOldLength() {
    int oldLength = ((TreeElement) myParent).getNotCachedLength();
    for (final Map.Entry<ASTNode, ChangeInfo> entry : myChanges.entrySet()) {
        final ASTNode key = entry.getKey();
        final ChangeInfo change = entry.getValue();
        final int length = ((TreeElement) key).getNotCachedLength();
        switch(change.getChangeType()) {
            case ChangeInfo.ADD:
                oldLength -= length;
                break;
            case ChangeInfo.REMOVED:
                oldLength += length;
                break;
            case ChangeInfo.REPLACE:
            case ChangeInfo.CONTENTS_CHANGED:
                oldLength += change.getOldLength() - length;
                break;
        }
    }
    return oldLength;
}
Also used : ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) ASTNode(com.intellij.lang.ASTNode) THashMap(gnu.trove.THashMap) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 12 with ChangeInfo

use of com.intellij.pom.tree.events.ChangeInfo in project intellij-community by JetBrains.

the class TreeChangeImpl method toString.

@SuppressWarnings({ "HardCodedStringLiteral" })
public String toString() {
    final StringBuilder buffer = new StringBuilder();
    final Iterator<Pair<ASTNode, Integer>> iterator = mySortedChanges.iterator();
    while (iterator.hasNext()) {
        final Pair<ASTNode, Integer> pair = iterator.next();
        final ASTNode node = pair.getFirst();
        buffer.append("(");
        buffer.append(node.getElementType().toString());
        buffer.append(" at ").append(pair.getSecond()).append(", ");
        ChangeInfo child = getChangeByChild(node);
        buffer.append(child != null ? child.toString() : "null");
        buffer.append(")");
        if (iterator.hasNext())
            buffer.append(", ");
    }
    return buffer.toString();
}
Also used : ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) ASTNode(com.intellij.lang.ASTNode) Pair(com.intellij.openapi.util.Pair)

Example 13 with ChangeInfo

use of com.intellij.pom.tree.events.ChangeInfo in project intellij-community by JetBrains.

the class PsiEventWrapperAspect method sendAfterEvents.

private static void sendAfterEvents(TreeChangeEvent changeSet) {
    ASTNode rootElement = changeSet.getRootElement();
    final PsiFile file = (PsiFile) SourceTreeToPsiMap.treeElementToPsi(rootElement);
    final PsiManagerImpl manager = (PsiManagerImpl) file.getManager();
    if (manager == null)
        return;
    if (!file.isPhysical()) {
        manager.afterChange(false);
        return;
    }
    final ASTNode[] changedElements = changeSet.getChangedElements();
    for (ASTNode changedElement : changedElements) {
        TreeChange changesByElement = changeSet.getChangesByElement(changedElement);
        PsiElement psiParent = null;
        while (changedElement != null && ((psiParent = changedElement.getPsi()) == null || !checkPsiForChildren(changesByElement.getAffectedChildren()))) {
            final ASTNode parent = changedElement.getTreeParent();
            final ChangeInfoImpl changeInfo = ChangeInfoImpl.create(ChangeInfo.CONTENTS_CHANGED, changedElement);
            changeInfo.compactChange(changesByElement);
            changesByElement = new TreeChangeImpl(parent);
            changesByElement.addChange(changedElement, changeInfo);
            changedElement = parent;
        }
        if (changedElement == null)
            continue;
        final ASTNode[] affectedChildren = changesByElement.getAffectedChildren();
        for (final ASTNode treeElement : affectedChildren) {
            PsiTreeChangeEventImpl psiEvent = new PsiTreeChangeEventImpl(manager);
            psiEvent.setParent(psiParent);
            psiEvent.setFile(file);
            final PsiElement psiChild = treeElement.getPsi();
            psiEvent.setChild(psiChild);
            final ChangeInfo changeByChild = changesByElement.getChangeByChild(treeElement);
            switch(changeByChild.getChangeType()) {
                case ChangeInfo.ADD:
                    psiEvent.setOffset(treeElement.getStartOffset());
                    psiEvent.setOldLength(0);
                    manager.childAdded(psiEvent);
                    break;
                case ChangeInfo.REPLACE:
                    final ReplaceChangeInfo change = (ReplaceChangeInfo) changeByChild;
                    psiEvent.setOffset(treeElement.getStartOffset());
                    final ASTNode replaced = change.getReplaced();
                    psiEvent.setOldChild(replaced.getPsi());
                    psiEvent.setNewChild(psiChild);
                    psiEvent.setOldLength(replaced.getTextLength());
                    manager.childReplaced(psiEvent);
                    break;
                case ChangeInfo.CONTENTS_CHANGED:
                    psiEvent.setOffset(treeElement.getStartOffset());
                    psiEvent.setParent(psiChild);
                    psiEvent.setOldLength(changeByChild.getOldLength());
                    manager.childrenChanged(psiEvent);
                    break;
                case ChangeInfo.REMOVED:
                    psiEvent.setOffset(changesByElement.getChildOffsetInNewTree(treeElement));
                    psiEvent.setOldParent(psiParent);
                    psiEvent.setOldChild(psiChild);
                    psiEvent.setOldLength(changeByChild.getOldLength());
                    manager.childRemoved(psiEvent);
                    break;
            }
        }
    }
}
Also used : ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) PsiManagerImpl(com.intellij.psi.impl.PsiManagerImpl) TreeChange(com.intellij.pom.tree.events.TreeChange) TreeChangeImpl(com.intellij.pom.tree.events.impl.TreeChangeImpl) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ASTNode(com.intellij.lang.ASTNode) ChangeInfoImpl(com.intellij.pom.tree.events.impl.ChangeInfoImpl) PsiFile(com.intellij.psi.PsiFile) PsiTreeChangeEventImpl(com.intellij.psi.impl.PsiTreeChangeEventImpl) PsiElement(com.intellij.psi.PsiElement)

Aggregations

ASTNode (com.intellij.lang.ASTNode)13 ChangeInfo (com.intellij.pom.tree.events.ChangeInfo)13 ReplaceChangeInfo (com.intellij.pom.tree.events.ReplaceChangeInfo)11 TreeChange (com.intellij.pom.tree.events.TreeChange)3 ChangeInfoImpl (com.intellij.pom.tree.events.impl.ChangeInfoImpl)2 TreeChangeImpl (com.intellij.pom.tree.events.impl.TreeChangeImpl)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 Pair (com.intellij.openapi.util.Pair)1 TreeChangeEvent (com.intellij.pom.tree.events.TreeChangeEvent)1 XmlElementVisitor (com.intellij.psi.XmlElementVisitor)1 PsiManagerImpl (com.intellij.psi.impl.PsiManagerImpl)1 PsiTreeChangeEventImpl (com.intellij.psi.impl.PsiTreeChangeEventImpl)1 FileElement (com.intellij.psi.impl.source.tree.FileElement)1 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)1 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)1 IElementType (com.intellij.psi.tree.IElementType)1 CharTable (com.intellij.util.CharTable)1 THashMap (gnu.trove.THashMap)1