From 7c418313a70ba3dc557392e4c8e83a54d9630acf Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 31 May 2021 16:33:26 +0200 Subject: [PATCH 1/2] Merge selections correctly when selecting larger syntax node When running this command with multiple cursors, if one of them was at a later position in the buffer but lying on a shallower node, it could happen that its start could move prior to cursors that were before it but lying on a deeper node. This could cause the selection merging algorithm to mistakenly keep some selections even if they overlapped. With this commit we now sort selections prior to merging them in `Editor::select_larger_syntax_node`. --- zed/src/editor.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/zed/src/editor.rs b/zed/src/editor.rs index 1ace262d60..8b15d71563 100644 --- a/zed/src/editor.rs +++ b/zed/src/editor.rs @@ -1829,7 +1829,7 @@ impl Editor { let mut stack = mem::take(&mut self.select_larger_syntax_node_stack); let mut selected_larger_node = false; let old_selections = self.selections(app).to_vec(); - let mut new_selections = Vec::new(); + let mut new_selection_ranges = Vec::new(); for selection in &old_selections { let old_range = selection.start.to_offset(buffer)..selection.end.to_offset(buffer); let mut new_range = old_range.clone(); @@ -1843,17 +1843,22 @@ impl Editor { } selected_larger_node |= new_range != old_range; - new_selections.push(Selection { - id: selection.id, - start: buffer.anchor_before(new_range.start), - end: buffer.anchor_before(new_range.end), - reversed: selection.reversed, - goal: SelectionGoal::None, - }); + new_selection_ranges.push((selection.id, new_range, selection.reversed)); } if selected_larger_node { stack.push(old_selections); + new_selection_ranges.sort_unstable_by_key(|(_, range, _)| range.start.clone()); + let new_selections = new_selection_ranges + .into_iter() + .map(|(id, range, reversed)| Selection { + id, + start: buffer.anchor_before(range.start), + end: buffer.anchor_before(range.end), + reversed, + goal: SelectionGoal::None, + }) + .collect(); self.update_selections(new_selections, true, cx); } self.select_larger_syntax_node_stack = stack; From 7f265230ea1fd394752130cc62a08605d4d6815f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 31 May 2021 16:51:33 +0200 Subject: [PATCH 2/2] Fix tests --- zed/src/editor.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zed/src/editor.rs b/zed/src/editor.rs index 8b15d71563..4919da9103 100644 --- a/zed/src/editor.rs +++ b/zed/src/editor.rs @@ -4044,7 +4044,7 @@ mod tests { }); assert_eq!( view.read_with(&cx, |view, cx| view.selection_ranges(cx)), - &[DisplayPoint::new(0, 0)..DisplayPoint::new(5, 0)] + &[DisplayPoint::new(5, 0)..DisplayPoint::new(0, 0)] ); // Trying to expand the selected syntax node one more time has no effect. @@ -4053,7 +4053,7 @@ mod tests { }); assert_eq!( view.read_with(&cx, |view, cx| view.selection_ranges(cx)), - &[DisplayPoint::new(0, 0)..DisplayPoint::new(5, 0)] + &[DisplayPoint::new(5, 0)..DisplayPoint::new(0, 0)] ); view.update(&mut cx, |view, cx| {