Pull count out of FindType

pull/11695/head
thomasschafer 2025-01-29 10:50:47 +00:00 committed by Thomas Schafer
parent 3bb88d88c4
commit 31d45abf97
No known key found for this signature in database
3 changed files with 25 additions and 19 deletions

View File

@ -155,9 +155,9 @@ fn find_nth_closest_pairs_plain(
} }
pub enum FindType { pub enum FindType {
Surround(usize), Surround,
Next(usize), Next,
Prev(usize), Prev,
} }
/// Find the position of surround pairs of `ch` which can be either a closing /// Find the position of surround pairs of `ch` which can be either a closing
@ -168,6 +168,7 @@ pub fn find_nth_pairs_pos(
ch: char, ch: char,
range: Range, range: Range,
find_type: FindType, find_type: FindType,
n: usize,
) -> Result<(usize, usize)> { ) -> Result<(usize, usize)> {
if text.len_chars() < 2 { if text.len_chars() < 2 {
return Err(Error::PairNotFound); return Err(Error::PairNotFound);
@ -179,12 +180,12 @@ pub fn find_nth_pairs_pos(
let (open, close) = get_pair(ch); let (open, close) = get_pair(ch);
let pos = range.cursor(text); let pos = range.cursor(text);
let (pos, n) = match find_type { let (pos, n) = match find_type {
FindType::Surround(n) => (pos, n), FindType::Surround => (pos, n),
FindType::Next(n) => match search::find_nth_next(text, open, pos, n) { FindType::Next => match search::find_nth_next(text, open, pos, n) {
Some(next_pos) => (next_pos + 1, 1), Some(next_pos) => (next_pos + 1, 1),
None => return Err(Error::PairNotFound), None => return Err(Error::PairNotFound),
}, },
FindType::Prev(n) => match search::find_nth_prev(text, close, pos, n) { FindType::Prev => match search::find_nth_prev(text, close, pos, n) {
Some(next_pos) => (next_pos - 1, 1), Some(next_pos) => (next_pos - 1, 1),
None => return Err(Error::PairNotFound), None => return Err(Error::PairNotFound),
}, },
@ -315,7 +316,7 @@ pub fn get_surround_pos(
for &range in selection { for &range in selection {
let (open_pos, close_pos) = { let (open_pos, close_pos) = {
let range_raw = match ch { let range_raw = match ch {
Some(ch) => find_nth_pairs_pos(text, ch, range, FindType::Surround(skip))?, Some(ch) => find_nth_pairs_pos(text, ch, range, FindType::Surround, skip)?,
None => find_nth_closest_pairs_pos(syntax, text, range, skip)?, None => find_nth_closest_pairs_pos(syntax, text, range, skip)?,
}; };
let range = Range::new(range_raw.0, range_raw.1); let range = Range::new(range_raw.0, range_raw.1);
@ -413,7 +414,8 @@ mod test {
doc.slice(..), doc.slice(..),
'\'', '\'',
selection.primary(), selection.primary(),
FindType::Surround(1) FindType::Surround,
1
) )
.expect("find should succeed"), .expect("find should succeed"),
(expectations[0], expectations[1]) (expectations[0], expectations[1])
@ -435,7 +437,8 @@ mod test {
doc.slice(..), doc.slice(..),
'\'', '\'',
selection.primary(), selection.primary(),
FindType::Surround(2) FindType::Surround,
2
) )
.expect("find should succeed"), .expect("find should succeed"),
(expectations[0], expectations[1]) (expectations[0], expectations[1])
@ -453,7 +456,7 @@ mod test {
assert_eq!(2, expectations.len()); assert_eq!(2, expectations.len());
assert_eq!( assert_eq!(
find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), FindType::Next(3)) find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), FindType::Next, 3)
.expect("find should succeed"), .expect("find should succeed"),
(expectations[0], expectations[1]) (expectations[0], expectations[1])
) )
@ -470,7 +473,7 @@ mod test {
assert_eq!(2, expectations.len()); assert_eq!(2, expectations.len());
assert_eq!( assert_eq!(
find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), FindType::Prev(1)) find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), FindType::Prev, 1)
.expect("find should succeed"), .expect("find should succeed"),
(expectations[0], expectations[1]) (expectations[0], expectations[1])
) )
@ -490,7 +493,8 @@ mod test {
doc.slice(..), doc.slice(..),
'\'', '\'',
selection.primary(), selection.primary(),
FindType::Surround(1) FindType::Surround,
1
), ),
Err(Error::CursorOnAmbiguousPair) Err(Error::CursorOnAmbiguousPair)
) )

View File

@ -206,13 +206,14 @@ pub fn textobject_pair_surround(
textobject: TextObject, textobject: TextObject,
ch: char, ch: char,
find_type: FindType, find_type: FindType,
count: usize,
) -> Range { ) -> Range {
textobject_pair_surround_impl( textobject_pair_surround_impl(
syntax, syntax,
slice, slice,
range, range,
textobject, textobject,
FindVariant::Char((ch, find_type)), FindVariant::Char((ch, find_type, count)),
) )
} }
@ -233,7 +234,7 @@ pub fn textobject_pair_surround_closest(
} }
enum FindVariant { enum FindVariant {
Char((char, FindType)), Char((char, FindType, usize)),
Closest(usize), Closest(usize),
} }
@ -245,8 +246,8 @@ fn textobject_pair_surround_impl(
find_variant: FindVariant, find_variant: FindVariant,
) -> Range { ) -> Range {
let pair_pos = match find_variant { let pair_pos = match find_variant {
FindVariant::Char((ch, find_type)) => { FindVariant::Char((ch, find_type, count)) => {
surround::find_nth_pairs_pos(slice, ch, range, find_type) surround::find_nth_pairs_pos(slice, ch, range, find_type, count)
} }
FindVariant::Closest(count) => { FindVariant::Closest(count) => {
surround::find_nth_closest_pairs_pos(syntax, slice, range, count) surround::find_nth_closest_pairs_pos(syntax, slice, range, count)
@ -603,7 +604,8 @@ mod test {
Range::point(pos), Range::point(pos),
objtype, objtype,
ch, ch,
FindType::Surround(count), FindType::Surround,
count,
); );
assert_eq!( assert_eq!(
result, result,

View File

@ -6148,9 +6148,9 @@ fn textobject_surrounding_pair(
None => FindType::Surround, None => FindType::Surround,
Some(Direction::Forward) => FindType::Next, Some(Direction::Forward) => FindType::Next,
Some(Direction::Backward) => FindType::Prev, Some(Direction::Backward) => FindType::Prev,
}(count); };
let mut range = textobject::textobject_pair_surround( let mut range = textobject::textobject_pair_surround(
syntax, text, range, textobject, pair_char, find_type, syntax, text, range, textobject, pair_char, find_type, count,
); );
if let Some(direction) = direction { if let Some(direction) = direction {
range = range.with_direction(direction); range = range.with_direction(direction);