Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/document.rs')
| -rw-r--r-- | helix-view/src/document.rs | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 04b7703c..c0672001 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -1320,15 +1320,27 @@ impl Document { Ok(()) } - /// Select text within the [`Document`]. - pub fn set_selection(&mut self, view_id: ViewId, selection: Selection) { + /// Select text within the [`Document`], optionally clearing the previous selection state. + pub fn set_selection_clear(&mut self, view_id: ViewId, selection: Selection, clear_prev: bool) { // TODO: use a transaction? self.selections .insert(view_id, selection.ensure_invariants(self.text().slice(..))); + helix_event::dispatch(SelectionDidChange { doc: self, view: view_id, - }) + }); + + if clear_prev { + self.view_data + .entry(view_id) + .and_modify(|view_data| view_data.object_selections.clear()); + } + } + + /// Select text within the [`Document`]. + pub fn set_selection(&mut self, view_id: ViewId, selection: Selection) { + self.set_selection_clear(view_id, selection, true); } /// Find the origin selection of the text in a document, i.e. where @@ -1520,6 +1532,12 @@ impl Document { apply_inlay_hint_changes(padding_after_inlay_hints); } + // clear out all associated view object selections, as they are no + // longer valid + self.view_data + .values_mut() + .for_each(|view_data| view_data.object_selections.clear()); + helix_event::dispatch(DocumentDidChange { doc: self, view: view_id, @@ -1962,13 +1980,13 @@ impl Document { &self.selections } - fn view_data(&self, view_id: ViewId) -> &ViewData { + pub fn view_data(&self, view_id: ViewId) -> &ViewData { self.view_data .get(&view_id) .expect("This should only be called after ensure_view_init") } - fn view_data_mut(&mut self, view_id: ViewId) -> &mut ViewData { + pub fn view_data_mut(&mut self, view_id: ViewId) -> &mut ViewData { self.view_data.entry(view_id).or_default() } @@ -2286,9 +2304,13 @@ impl Document { } } +/// Stores data needed for views that are tied to this specific Document. #[derive(Debug, Default)] pub struct ViewData { view_position: ViewPosition, + + /// used to store previous selections of tree-sitter objects + pub object_selections: HashMap<&'static str, Vec<Selection>>, } #[derive(Clone, Debug)] @@ -2339,6 +2361,7 @@ mod test { Arc::new(ArcSwap::from_pointee(syntax::Loader::default())), ); let view = ViewId::default(); + doc.ensure_view_init(view); doc.set_selection(view, Selection::single(0, 0)); let transaction = @@ -2377,7 +2400,9 @@ mod test { Arc::new(ArcSwap::new(Arc::new(Config::default()))), Arc::new(ArcSwap::from_pointee(syntax::Loader::default())), ); + let view = ViewId::default(); + doc.ensure_view_init(view); doc.set_selection(view, Selection::single(5, 5)); // insert |