Search in sources :

Example 6 with ChangeInfo

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

the class TreeChangeImpl method addChangeAtOffset.

private void addChangeAtOffset(final ASTNode child, final int nodeOffset) {
    int optimizedIndex = haveNotCalculated;
    Pair<ASTNode, Integer> element = Pair.create(child, Integer.valueOf(nodeOffset));
    if (mySortedChanges.size() > 0) {
        // check adding at end
        Pair<ASTNode, Integer> pair = mySortedChanges.get(mySortedChanges.size() - 1);
        if (pair.getFirst() == child.getTreePrev() && pair.getSecond() <= nodeOffset) {
            optimizedIndex = mySortedChanges.size();
            if (!ourDoChecks) {
                mySortedChanges.add(element);
                return;
            }
        }
    }
    int index = 0;
    for (Pair<ASTNode, Integer> pair : mySortedChanges) {
        if (child == pair.getFirst())
            return;
        if (nodeOffset < pair.getSecond().intValue() || nodeOffset == pair.getSecond().intValue() && isAfter(pair.getFirst(), child)) {
            break;
        }
        index++;
    }
    int insertionIndex = optimizedIndex != haveNotCalculated ? optimizedIndex : index;
    if (insertionIndex == mySortedChanges.size())
        mySortedChanges.add(element);
    else
        mySortedChanges.add(insertionIndex, element);
    if (optimizedIndex != haveNotCalculated && index != optimizedIndex && !ourReportedDifferentAddChangeAtOffsetOptimization) {
        ASTNode prev = child.getTreePrev();
        Pair<ASTNode, Integer> pair = mySortedChanges.get(index);
        ChangeInfo prevChange = myChanges.get(prev);
        ChangeInfo prevChange2 = myChanges.get(pair.getFirst());
        LOG.error("Failed to calculate optimized index for add change at offset: prev node:" + prev + ", prev change:" + prevChange + ",prev change length:" + (prevChange != null ? prevChange.getOldLength() : null) + ", prev text length:" + prev.getTextLength() + ",prev offset:" + mySortedChanges.get(mySortedChanges.size() - 1).getSecond() + ", node:" + child + ", nodeOffset:" + nodeOffset + ", optimizedIndex:" + optimizedIndex + ", real index:" + index + ", same node:" + (pair.getFirst() == child) + ", at place:" + pair.getSecond() + ", node:" + pair.getFirst() + ", change:" + prevChange2 + ", prevChange oldLength:" + (prevChange2 != null ? prevChange2.getOldLength() : null) + ", prevchange length2:" + pair.getFirst().getTextLength() + "," + toString());
        ourReportedDifferentAddChangeAtOffsetOptimization = true;
    }
}
Also used : ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) ASTNode(com.intellij.lang.ASTNode)

Example 7 with ChangeInfo

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

the class ChangeInfoImpl method compactChange.

public void compactChange(TreeChange change) {
    for (final ASTNode treeElement : change.getAffectedChildren()) {
        final ChangeInfo changeByChild = change.getChangeByChild(treeElement);
        processElementaryChange(changeByChild, treeElement);
    }
}
Also used : ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ASTNode(com.intellij.lang.ASTNode)

Example 8 with ChangeInfo

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

the class TreeChangeEventImpl method addElementaryChange.

@Override
public void addElementaryChange(@NotNull ASTNode element, @NotNull ChangeInfo change) {
    LOG.assertTrue(isAncestor(element, myFileElement), element);
    final ASTNode parent = element.getTreeParent();
    if (parent == null)
        return;
    ASTNode currentParent = parent;
    ASTNode prevParent = element;
    int depth = 0;
    while (currentParent != null) {
        if (myChangedElements.containsKey(currentParent)) {
            final TreeChange changesByElement = getChangesByElement(currentParent);
            final boolean currentParentHasChange = changesByElement.getChangeByChild(prevParent) != null;
            if (currentParentHasChange && prevParent != element)
                return;
            if (prevParent != element) {
                final ChangeInfo newChange = ChangeInfoImpl.create(ChangeInfo.CONTENTS_CHANGED, prevParent);
                if (change.getChangeType() != ChangeInfo.REMOVED) {
                    ((ChangeInfoImpl) newChange).processElementaryChange(change, element);
                }
                change = newChange;
            }
            processElementaryChange(currentParent, prevParent, change, -1);
            return;
        }
        depth++;
        prevParent = currentParent;
        currentParent = currentParent.getTreeParent();
    }
    compactChanges(parent, depth - 1);
    processElementaryChange(parent, element, change, depth - 1);
}
Also used : TreeChange(com.intellij.pom.tree.events.TreeChange) ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ASTNode(com.intellij.lang.ASTNode)

Example 9 with ChangeInfo

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

the class TreeChangeImpl method calculateOldOffsetLinearly.

private int calculateOldOffsetLinearly(ASTNode child) {
    int oldOffsetInParent = 0;
    // find last changed element before child
    ASTNode current = myParent.getFirstChildNode();
    // calculate not changed elements
    while (current != child) {
        if (!myChanges.containsKey(current)) {
            oldOffsetInParent += current.getTextLength();
        }
        current = current.getTreeNext();
    }
    for (Pair<ASTNode, Integer> offset : mySortedChanges) {
        if (offset.getSecond() > oldOffsetInParent)
            break;
        final ChangeInfo change = myChanges.get(offset.getFirst());
        oldOffsetInParent += change.getOldLength();
    }
    return oldOffsetInParent;
}
Also used : ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) ASTNode(com.intellij.lang.ASTNode)

Example 10 with ChangeInfo

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

the class TreeChangeImpl method getNewOffset.

private int getNewOffset(ASTNode node) {
    int optimizedResult = haveNotCalculated;
    ASTNode prev = node.getTreePrev();
    if (myLastNode == prev) {
        ChangeInfo prevChangeInfo = myChanges.get(prev);
        ChangeInfo changeInfo = myChanges.get(node);
        // newoffset of removed element is the same of removed previous sibling
        if (prevChangeInfo != null && changeInfo != null && prevChangeInfo.getChangeType() == ChangeInfo.REMOVED && changeInfo.getChangeType() == ChangeInfo.REMOVED) {
            optimizedResult = myLastOffsetInNewTree;
            myLastNode = node;
            if (!ourDoChecks)
                return optimizedResult;
        }
    }
    int currentOffsetInNewTree = 0;
    try {
        ASTNode current = myParent.getFirstChildNode();
        int i = 0;
        Pair<ASTNode, Integer> currentChange = i < mySortedChanges.size() ? mySortedChanges.get(i) : null;
        int currentOldOffset = 0;
        while (current != null) {
            boolean counted = false;
            while (currentChange != null && currentOldOffset == currentChange.getSecond().intValue()) {
                if (currentChange.getFirst() == node) {
                    myLastNode = node;
                    myLastOffsetInNewTree = currentOffsetInNewTree;
                    return currentOffsetInNewTree;
                }
                if (current == currentChange.getFirst()) {
                    final int textLength = current.getTextLength();
                    counted = true;
                    current = current.getTreeNext();
                    currentOffsetInNewTree += textLength;
                }
                final ChangeInfo changeInfo = myChanges.get(currentChange.getFirst());
                currentOldOffset += changeInfo.getOldLength();
                ++i;
                currentChange = i < mySortedChanges.size() ? mySortedChanges.get(i) : null;
            }
            if (current == null)
                break;
            if (!counted) {
                final int textLength = current.getTextLength();
                currentOldOffset += textLength;
                current = current.getTreeNext();
                currentOffsetInNewTree += textLength;
            }
        }
    } finally {
        if (optimizedResult != haveNotCalculated && optimizedResult != currentOffsetInNewTree && !ourReportedDifferentEnableGetNewOffset) {
            LOG.error("Failed to calculate optimized getNewOffset:" + myChanges.get(node) + "," + prev + "," + myChanges.get(prev));
            ourReportedDifferentEnableGetNewOffset = true;
            // always use optimized result
            currentOffsetInNewTree = optimizedResult;
        }
    }
    return currentOffsetInNewTree;
}
Also used : ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) ASTNode(com.intellij.lang.ASTNode)

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