Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-core/src/snippets/active.rs')
| -rw-r--r-- | helix-core/src/snippets/active.rs | 65 |
1 files changed, 24 insertions, 41 deletions
diff --git a/helix-core/src/snippets/active.rs b/helix-core/src/snippets/active.rs index 1c10b76d..65ac1b7b 100644 --- a/helix-core/src/snippets/active.rs +++ b/helix-core/src/snippets/active.rs @@ -1,6 +1,6 @@ use std::ops::{Index, IndexMut}; -use foldhash::HashSet; +use hashbrown::HashSet; use helix_stdx::range::{is_exact_subset, is_subset}; use helix_stdx::Range; use ropey::Rope; @@ -13,7 +13,7 @@ use crate::{Assoc, ChangeSet, Selection, Transaction}; pub struct ActiveSnippet { ranges: Vec<Range>, active_tabstops: HashSet<TabstopIdx>, - current_tabstop: TabstopIdx, + active_tabstop: TabstopIdx, tabstops: Vec<Tabstop>, } @@ -35,8 +35,8 @@ impl ActiveSnippet { let snippet = Self { ranges: snippet.ranges, tabstops: snippet.tabstops, - active_tabstops: HashSet::default(), - current_tabstop: TabstopIdx(0), + active_tabstops: HashSet::new(), + active_tabstop: TabstopIdx(0), }; (snippet.tabstops.len() != 1).then_some(snippet) } @@ -52,14 +52,14 @@ impl ActiveSnippet { pub fn delete_placeholder(&self, doc: &Rope) -> Transaction { Transaction::delete( doc, - self[self.current_tabstop] + self[self.active_tabstop] .ranges .iter() .map(|range| (range.start, range.end)), ) } - /// maps the active snippets through a `ChangeSet` updating all tabstop ranges + /// maps the active snippets trough a `ChangeSet` updating all tabstop ranges pub fn map(&mut self, changes: &ChangeSet) -> bool { let positions_to_map = self.ranges.iter_mut().flat_map(|range| { [ @@ -112,13 +112,13 @@ impl ActiveSnippet { if retain { range.start = range.start.max(snippet_range.start); range.end = range.end.max(range.start).min(snippet_range.end); - // guaranteed by assoc + // garunteed by assoc debug_assert!(prev.start <= range.start); debug_assert!(range.start <= range.end); if prev.end > range.start { // not really sure what to do in this case. It shouldn't - // really occur in practice, the below just ensures - // our invariants hold + // really occur in practice% the below just ensures + // our invriants hold range.start = prev.end; range.end = range.end.max(range.start) } @@ -132,11 +132,11 @@ impl ActiveSnippet { pub fn next_tabstop(&mut self, current_selection: &Selection) -> (Selection, bool) { let primary_idx = self.primary_idx(current_selection); - while self.current_tabstop.0 + 1 < self.tabstops.len() { - self.current_tabstop.0 += 1; + while self.active_tabstop.0 + 1 < self.tabstops.len() { + self.active_tabstop.0 += 1; if self.activate_tabstop() { let selection = self.tabstop_selection(primary_idx, Direction::Forward); - return (selection, self.current_tabstop.0 + 1 == self.tabstops.len()); + return (selection, self.active_tabstop.0 + 1 == self.tabstops.len()); } } @@ -148,15 +148,15 @@ impl ActiveSnippet { pub fn prev_tabstop(&mut self, current_selection: &Selection) -> Option<Selection> { let primary_idx = self.primary_idx(current_selection); - while self.current_tabstop.0 != 0 { - self.current_tabstop.0 -= 1; + while self.active_tabstop.0 != 0 { + self.active_tabstop.0 -= 1; if self.activate_tabstop() { return Some(self.tabstop_selection(primary_idx, Direction::Forward)); } } None } - // computes the primary idx adjusted for the number of cursors in the current tabstop + // computes the primary idx adjust for the number of cursors in the current tabstop fn primary_idx(&self, current_selection: &Selection) -> usize { let primary: Range = current_selection.primary().into(); let res = self @@ -172,19 +172,19 @@ impl ActiveSnippet { } fn activate_tabstop(&mut self) -> bool { - let tabstop = &self[self.current_tabstop]; + let tabstop = &self[self.active_tabstop]; if tabstop.has_placeholder() && tabstop.ranges.iter().all(|range| range.is_empty()) { return false; } self.active_tabstops.clear(); - self.active_tabstops.insert(self.current_tabstop); - let mut parent = self[self.current_tabstop].parent; + self.active_tabstops.insert(self.active_tabstop); + let mut parent = self[self.active_tabstop].parent; while let Some(tabstop) = parent { self.active_tabstops.insert(tabstop); parent = self[tabstop].parent; } true - // TODO: if the user removes the selection(s) in one snippet (but + // TODO: if the user removes the seleciton(s) in one snippet (but // there are still other cursors in other snippets) and jumps to the // next tabstop the selection in that tabstop is restored (at the // next tabstop). This could be annoying since its not possible to @@ -192,11 +192,11 @@ impl ActiveSnippet { // hand it may be useful since the user may just have meant to edit // a subselection (like with s) of the tabstops and so the selection // removal was just temporary. Potentially this could have some sort of - // separate keymap + // seperate keymap } pub fn tabstop_selection(&self, primary_idx: usize, direction: Direction) -> Selection { - let tabstop = &self[self.current_tabstop]; + let tabstop = &self[self.active_tabstop]; tabstop.selection(direction, primary_idx, self.ranges.len()) } @@ -208,18 +208,18 @@ impl ActiveSnippet { return ActiveSnippet::new(snippet); } let mut cnt = 0; - let parent = self[self.current_tabstop].parent; + let parent = self[self.active_tabstop].parent; let tabstops = snippet.tabstops.into_iter().map(|mut tabstop| { cnt += 1; if let Some(parent) = &mut tabstop.parent { - parent.0 += self.current_tabstop.0; + parent.0 += self.active_tabstop.0; } else { tabstop.parent = parent; } tabstop }); self.tabstops - .splice(self.current_tabstop.0..=self.current_tabstop.0, tabstops); + .splice(self.active_tabstop.0..=self.active_tabstop.0, tabstops); self.activate_tabstop(); Some(self) } @@ -252,21 +252,4 @@ mod tests { snippet.map(edit.changes()); assert!(!snippet.is_valid(&Selection::point(4))) } - - #[test] - fn tabstop_zero_with_placeholder() { - // The `$0` tabstop should not have placeholder text. When we receive a snippet like this - // (from older versions of clangd for example) we should discard the placeholder text. - let snippet = Snippet::parse("sizeof(${0:expression-or-type})").unwrap(); - let mut doc = Rope::from("\n"); - let (transaction, _, snippet) = snippet.render( - &doc, - &Selection::point(0), - |_| (0, 0), - &mut SnippetRenderCtx::test_ctx(), - ); - assert!(transaction.apply(&mut doc)); - assert_eq!(doc, "sizeof()\n"); - assert!(ActiveSnippet::new(snippet).is_none()); - } } |