diff --git a/zed/src/editor.rs b/zed/src/editor.rs index 1ace262d60..4919da9103 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; @@ -4039,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. @@ -4048,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| {