From 46e21fccc76e07f38529e87fd5d4647f9c979e12 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sat, 7 Sep 2024 16:49:28 +0800 Subject: [PATCH] fix: two tree move (inside the same parent) issues (#450) * fix: two tree move (inside the same parent) issues * chore: add release note --- .changeset/lemon-maps-smash.md | 6 ++++++ crates/loro-internal/src/handler/tree.rs | 2 +- crates/loro/src/lib.rs | 4 +--- crates/loro/tests/loro_rust_test.rs | 19 +++++++++++++++++++ loro-js/tests/tree.test.ts | 12 +++++++++++- 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 .changeset/lemon-maps-smash.md diff --git a/.changeset/lemon-maps-smash.md b/.changeset/lemon-maps-smash.md new file mode 100644 index 00000000..dec550dc --- /dev/null +++ b/.changeset/lemon-maps-smash.md @@ -0,0 +1,6 @@ +--- +"loro-crdt": patch +"loro-wasm": patch +--- + +Fix tree move issues diff --git a/crates/loro-internal/src/handler/tree.rs b/crates/loro-internal/src/handler/tree.rs index 7b9e2718..539b39ec 100644 --- a/crates/loro-internal/src/handler/tree.rs +++ b/crates/loro-internal/src/handler/tree.rs @@ -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; diff --git a/crates/loro/src/lib.rs b/crates/loro/src/lib.rs index a985426b..a49040c4 100644 --- a/crates/loro/src/lib.rs +++ b/crates/loro/src/lib.rs @@ -1322,9 +1322,7 @@ impl LoroTree { /// tree.mov(root2, root).unwrap(); /// ``` pub fn mov>>(&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. diff --git a/crates/loro/tests/loro_rust_test.rs b/crates/loro/tests/loro_rust_test.rs index a8706b8e..d9b36a46 100644 --- a/crates/loro/tests/loro_rust_test.rs +++ b/crates/loro/tests/loro_rust_test.rs @@ -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]); +} diff --git a/loro-js/tests/tree.test.ts b/loro-js/tests/tree.test.ts index d6a2d07f..b34f482d 100644 --- a/loro-js/tests/tree.test.ts +++ b/loro-js/tests/tree.test.ts @@ -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 { return new Promise((resolve) => setTimeout(resolve, 1)); -} \ No newline at end of file +}