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