From 20c97637a4a5dda781dc1d8e5342604055f242fa Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Fri, 13 May 2022 12:55:15 -0700 Subject: [PATCH] minor tweaks to selections collection api --- crates/editor/src/editor.rs | 78 +++++++--------------- crates/editor/src/selections_collection.rs | 24 +++++-- crates/vim/src/normal.rs | 4 +- 3 files changed, 46 insertions(+), 60 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 8c6cf33e09..3a9934e5c6 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -1392,20 +1392,6 @@ impl Editor { result } - pub fn display_selections( - &mut self, - cx: &mut ViewContext, - ) -> (DisplaySnapshot, Vec>) { - let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let selections = self - .selections - .interleaved::(cx) - .into_iter() - .map(|selection| selection.map(|point| point.to_display_point(&display_map))) - .collect(); - (display_map, selections) - } - pub fn edit(&mut self, edits: I, cx: &mut ViewContext) where I: IntoIterator, T)>, @@ -1464,25 +1450,27 @@ impl Editor { let position = position.to_offset(&display_map, Bias::Left); let tail_anchor = display_map.buffer_snapshot.anchor_before(tail); + let mut pending_selection = self + .selections + .pending_anchor() + .expect("extend_selection not called with pending selection"); + if position >= tail { + pending_selection.start = tail_anchor.clone(); + } else { + pending_selection.end = tail_anchor.clone(); + pending_selection.reversed = true; + } + + let mut pending_mode = self.selections.pending_mode().unwrap(); + match &mut pending_mode { + SelectMode::Word(range) | SelectMode::Line(range) => { + *range = tail_anchor.clone()..tail_anchor + } + _ => {} + } + self.change_selections(Some(Autoscroll::Fit), cx, |s| { - let mut pending = s - .pending_mut() - .as_mut() - .expect("extend_selection not called with pending selection"); - - if position >= tail { - pending.selection.start = tail_anchor.clone(); - } else { - pending.selection.end = tail_anchor.clone(); - pending.selection.reversed = true; - } - - match &mut pending.mode { - SelectMode::Word(range) | SelectMode::Line(range) => { - *range = tail_anchor.clone()..tail_anchor - } - _ => {} - } + s.set_pending(pending_selection, pending_mode) }); } @@ -1591,7 +1579,8 @@ impl Editor { let buffer = self.buffer.read(cx).snapshot(cx); let head; let tail; - match &self.selections.pending_mode().unwrap() { + let mode = self.selections.pending_mode().unwrap(); + match &mode { SelectMode::Character => { head = position.to_point(&display_map); tail = pending.tail().to_point(&buffer); @@ -1660,7 +1649,7 @@ impl Editor { } self.change_selections(None, cx, |s| { - s.pending_mut().as_mut().unwrap().selection = pending; + s.set_pending(pending, mode); }); } else { log::error!("update_selection dispatched with no pending selection"); @@ -1745,8 +1734,7 @@ impl Editor { return; } - if self.change_selections(None, cx, |s| s.try_cancel()) { - self.request_autoscroll(Autoscroll::Fit, cx); + if self.change_selections(Some(Autoscroll::Fit), cx, |s| s.try_cancel()) { return; } } @@ -1851,8 +1839,6 @@ impl Editor { .collect(); this.change_selections(Some(Autoscroll::Fit), cx, |s| s.select(new_selections)); - - this.request_autoscroll(Autoscroll::Fit, cx); }); } @@ -1972,13 +1958,7 @@ impl Editor { let mut snapshot = buffer.snapshot(cx); let left_biased_selections = selections .iter() - .map(|selection| Selection { - id: selection.id, - start: snapshot.anchor_before(selection.start), - end: snapshot.anchor_before(selection.end), - reversed: selection.reversed, - goal: selection.goal, - }) + .map(|selection| selection.map(|p| snapshot.anchor_before(p))) .collect::>(); let autoclose_pair = snapshot.language().and_then(|language| { @@ -2730,13 +2710,6 @@ impl Editor { } } if let Some(current_ranges) = snippet.ranges.get(snippet.active_index) { - let snapshot = self.buffer.read(cx).snapshot(cx); - let current_ranges_resolved = current_ranges - .iter() - .map(|range| range.start.to_point(&snapshot)..range.end.to_point(&snapshot)) - .collect::>(); - dbg!(¤t_ranges, current_ranges_resolved); - self.change_selections(Some(Autoscroll::Fit), cx, |s| { s.select_anchor_ranges(current_ranges.into_iter().cloned()) }); @@ -4024,7 +3997,6 @@ impl Editor { } fn add_selection(&mut self, above: bool, cx: &mut ViewContext) { - self.push_to_selection_history(); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let mut selections = self.selections.interleaved::(cx); let mut state = self.add_selections_state.take().unwrap_or_else(|| { diff --git a/crates/editor/src/selections_collection.rs b/crates/editor/src/selections_collection.rs index 5965ad58f3..d889d14c14 100644 --- a/crates/editor/src/selections_collection.rs +++ b/crates/editor/src/selections_collection.rs @@ -169,6 +169,19 @@ impl SelectionsCollection { .collect() } + pub fn display_interleaved( + &mut self, + cx: &mut MutableAppContext, + ) -> (DisplaySnapshot, Vec>) { + let display_map = self.display_map(cx); + let selections = self + .interleaved::(cx) + .into_iter() + .map(|selection| selection.map(|point| point.to_display_point(&display_map))) + .collect(); + (display_map, selections) + } + pub fn newest_anchor(&self) -> &Selection { self.pending .as_ref() @@ -308,12 +321,12 @@ impl<'a> MutableSelectionsCollection<'a> { mode, }) } - pub fn pending_mut(&mut self) -> &mut Option { - &mut self.collection.pending + + pub fn set_pending(&mut self, selection: Selection, mode: SelectMode) { + self.collection.pending = Some(PendingSelection { selection, mode }); } pub fn try_cancel(&mut self) -> bool { - let buffer = self.buffer.read(self.cx).snapshot(self.cx); if let Some(pending) = self.collection.pending.take() { if self.disjoint.is_empty() { self.collection.disjoint = Arc::from([pending.selection]); @@ -327,7 +340,7 @@ impl<'a> MutableSelectionsCollection<'a> { return true; } - if !oldest.start.cmp(&oldest.end, &buffer).is_eq() { + if !oldest.start.cmp(&oldest.end, &self.buffer()).is_eq() { let head = oldest.head(); oldest.start = head.clone(); oldest.end = head; @@ -584,7 +597,8 @@ impl<'a> MutableSelectionsCollection<'a> { pub fn refresh(&mut self) -> HashMap { // TODO: Pull disjoint constraint out of update_selections so we don't have to // store the pending_selection here. - let buffer = self.buffer.read(self.cx).snapshot(self.cx); + let buffer = self.buffer(); + let mut pending = self.collection.pending.take(); let mut selections_with_lost_position = HashMap::default(); diff --git a/crates/vim/src/normal.rs b/crates/vim/src/normal.rs index be218592cc..36fec6fbfc 100644 --- a/crates/vim/src/normal.rs +++ b/crates/vim/src/normal.rs @@ -130,7 +130,7 @@ fn insert_line_above(_: &mut Workspace, _: &InsertLineAbove, cx: &mut ViewContex vim.switch_mode(Mode::Insert, cx); vim.update_active_editor(cx, |editor, cx| { editor.transact(cx, |editor, cx| { - let (map, old_selections) = editor.display_selections(cx); + let (map, old_selections) = editor.selections.display_interleaved(cx); let selection_start_rows: HashSet = old_selections .into_iter() .map(|selection| selection.start.row()) @@ -162,7 +162,7 @@ fn insert_line_below(_: &mut Workspace, _: &InsertLineBelow, cx: &mut ViewContex vim.switch_mode(Mode::Insert, cx); vim.update_active_editor(cx, |editor, cx| { editor.transact(cx, |editor, cx| { - let (map, old_selections) = editor.display_selections(cx); + let (map, old_selections) = editor.selections.display_interleaved(cx); let selection_end_rows: HashSet = old_selections .into_iter() .map(|selection| selection.end.row())