diff --git a/AppKit/CPTreeNode.j b/AppKit/CPTreeNode.j index b6d31066c..0a89f4430 100644 --- a/AppKit/CPTreeNode.j +++ b/AppKit/CPTreeNode.j @@ -233,12 +233,11 @@ [_childNodes removeObjectAtIndex:originalIndex]; /* - * The removal shifts the array elements. - * Adjust the target index to maintain the position relative to the original array. - * This matches the Cocoa move semantics. + * No index adjustment here. anIndex is the target position in + * the final array, per the KVC to-many contract. The array + * above is already one element short from the removal, so + * inserting at anIndex against it lands the node correctly. */ - if (originalIndex < anIndex) - --anIndex; } else { @@ -308,8 +307,12 @@ aTreeNode._parentNode = nil; [_childNodes removeObjectAtIndex:replacementIndex]; - if (replacementIndex < anIndex) - --anIndex; + /* + * No index adjustment here. anIndex is the target position in the + * final array, per the KVC to-many contract. The array above is + * already one element short from the removal, so replacing at + * anIndex against it lands the node correctly. + */ } else if (oldParent) { diff --git a/Tests/AppKit/CPTreeNodeTest.j b/Tests/AppKit/CPTreeNodeTest.j index 0895e6591..6a7128678 100644 --- a/Tests/AppKit/CPTreeNodeTest.j +++ b/Tests/AppKit/CPTreeNodeTest.j @@ -131,7 +131,7 @@ // 9. index paths - (void)testIndexPaths { - [self assert:nil equals:[root indexPath]]; + [self assert:[CPIndexPath indexPathWithIndexes:[]] equals:[root indexPath]]; [root insertObject:child1 inChildNodesAtIndex:0]; [self assert:[CPIndexPath indexPathWithIndex:0] equals:[child1 indexPath]];