Skip to content

Commit

Permalink
fix: two tree move (inside the same parent) issues (#450)
Browse files Browse the repository at this point in the history
* fix: two tree move (inside the same parent) issues

* chore: add release note
  • Loading branch information
zxch3n authored Sep 7, 2024
1 parent 622c881 commit 46e21fc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/lemon-maps-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"loro-crdt": patch
"loro-wasm": patch
---

Fix tree move issues
2 changes: 1 addition & 1 deletion crates/loro-internal/src/handler/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl TreeHandler {
.ok_or(LoroTreeError::TreeNodeNotExist(other))?;
let mut index = self.get_index_by_tree_id(&other).unwrap();
if self.is_parent(target, parent)
&& index > 1
&& index >= 1
&& self.get_index_by_tree_id(&target).unwrap() < index
{
index -= 1;
Expand Down
4 changes: 1 addition & 3 deletions crates/loro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,9 +1322,7 @@ impl LoroTree {
/// tree.mov(root2, root).unwrap();
/// ```
pub fn mov<T: Into<Option<TreeID>>>(&self, target: TreeID, parent: T) -> LoroResult<()> {
let parent = parent.into();
let index = self.children_num(parent).unwrap_or(0);
self.handler.move_to(target, parent, index)
self.handler.mov(target, parent)
}

/// Move the `target` node to be a child of the `parent` node at the given index.
Expand Down
19 changes: 19 additions & 0 deletions crates/loro/tests/loro_rust_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,22 @@ fn len_and_is_empty_inconsistency() {
assert_eq!(map.len(), 0);
assert!(map.is_empty());
}

#[test]
fn test_tree_move() {
let doc = LoroDoc::new();
let tree = doc.get_tree("tree");
let root1 = tree.create(None).unwrap();
let node1 = tree.create(root1).unwrap();
let node2 = tree.create(root1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node1, node2]);
tree.mov_before(node2, node1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node2, node1]);
tree.mov_before(node2, node1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node2, node1]);

tree.mov_after(node2, node1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node1, node2]);
tree.mov_after(node2, node1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node1, node2]);
}
12 changes: 11 additions & 1 deletion loro-js/tests/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ describe("loro tree node", ()=>{
});
});

describe("LoroTree", () => {
it ("move", () => {
const loro = new Loro();
const tree = loro.getTree("root");
const root = tree.createNode();
const child = tree.createNode(root.id);
tree.move(child.id, root.id);
})
})

function one_ms(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 1));
}
}

0 comments on commit 46e21fc

Please sign in to comment.