on master help sign in
Refactor and add tests for surround
commit86271bac18ac4503187986f1c2f5bcebc657acdf authorGokul Soumya <gokulps15@gmail.com> dateTue 22 Jun 2021, 00:03 (5 years ago) parent4754b2e
1 file changed+102−10
helix-core/src/surround.rs+102−10
helix-core/src/surround.rs+102−10
@@ -13,6 +13,14 @@ pub const PAIRS: &[(char, char)] = &[
1313
1414/// Given any char in [PAIRS], return the open and closing chars. If not found in
1515/// [PAIRS] return (ch, ch).
16+///
17+/// ```
18+/// use helix_core::surround::get_pair;
19+///
20+/// assert_eq!(get_pair('['), ('[', ']'));
21+/// assert_eq!(get_pair('}'), ('{', '}'));
22+/// assert_eq!(get_pair('"'), ('"', '"'));
23+/// ```
1624pub fn get_pair(ch: char) -> (char, char) {
1725 PAIRS
1826 .iter()
@@ -50,17 +58,101 @@ pub fn get_surround_pos(
5058 let mut change_pos = Vec::new();
5159
5260 for range in selection {
53− let head = range.head;
54−
55− match find_nth_pairs_pos(text, ch, head, skip) {
56− Some((open_pos, close_pos)) => {
57− if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) {
58− return None;
59− }
60− change_pos.extend_from_slice(&[open_pos, close_pos]);
61− }
62− None => return None,
61+ let (open_pos, close_pos) = find_nth_pairs_pos(text, ch, range.head, skip)?;
62+ if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) {
63+ return None;
6364 }
65+ change_pos.extend_from_slice(&[open_pos, close_pos]);
6466 }
6567 Some(change_pos)
6668}
69+
70+#[cfg(test)]
71+mod test {
72+ use super::*;
73+ use crate::Range;
74+
75+ use ropey::Rope;
76+ use smallvec::SmallVec;
77+
78+ #[test]
79+ fn test_find_nth_pairs_pos() {
80+ let doc = Rope::from("some (text) here");
81+ let slice = doc.slice(..);
82+
83+ // cursor on [t]ext
84+ assert_eq!(find_nth_pairs_pos(slice, '(', 6, 1), Some((5, 10)));
85+ assert_eq!(find_nth_pairs_pos(slice, ')', 6, 1), Some((5, 10)));
86+ // cursor on so[m]e
87+ assert_eq!(find_nth_pairs_pos(slice, '(', 2, 1), None);
88+ // cursor on bracket itself
89+ // assert_eq!(find_nth_pairs_pos(slice, '(', 5, 1), Some((5, 10)));
90+ }
91+
92+ #[test]
93+ fn test_find_nth_pairs_pos_skip() {
94+ let doc = Rope::from("(so (many (good) text) here)");
95+ let slice = doc.slice(..);
96+
97+ // cursor on go[o]d
98+ assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((10, 15)));
99+ assert_eq!(find_nth_pairs_pos(slice, '(', 13, 2), Some((4, 21)));
100+ assert_eq!(find_nth_pairs_pos(slice, '(', 13, 3), Some((0, 27)));
101+ }
102+
103+ #[test]
104+ fn test_find_nth_pairs_pos_mixed() {
105+ let doc = Rope::from("(so [many {good} text] here)");
106+ let slice = doc.slice(..);
107+
108+ // cursor on go[o]d
109+ assert_eq!(find_nth_pairs_pos(slice, '{', 13, 1), Some((10, 15)));
110+ assert_eq!(find_nth_pairs_pos(slice, '[', 13, 1), Some((4, 21)));
111+ assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((0, 27)));
112+ }
113+
114+ #[test]
115+ fn test_get_surround_pos() {
116+ let doc = Rope::from("(some) (chars)\n(newline)");
117+ let slice = doc.slice(..);
118+ let selection = Selection::new(
119+ SmallVec::from_slice(&[Range::point(2), Range::point(9), Range::point(20)]),
120+ 0,
121+ );
122+
123+ // cursor on s[o]me, c[h]ars, newl[i]ne
124+ assert_eq!(
125+ get_surround_pos(slice, &selection, '(', 1)
126+ .unwrap()
127+ .as_slice(),
128+ &[0, 5, 7, 13, 15, 23]
129+ );
130+ }
131+
132+ #[test]
133+ fn test_get_surround_pos_bail() {
134+ let doc = Rope::from("[some]\n(chars)xx\n(newline)");
135+ let slice = doc.slice(..);
136+
137+ let selection = Selection::new(
138+ SmallVec::from_slice(&[Range::point(2), Range::point(9)]),
139+ 0,
140+ );
141+
142+ // cursor on s[o]me, c[h]ars
143+ assert_eq!(
144+ get_surround_pos(slice, &selection, '(', 1),
145+ None // different surround chars
146+ );
147+
148+ let selection = Selection::new(
149+ SmallVec::from_slice(&[Range::point(14), Range::point(24)]),
150+ 0,
151+ );
152+ // cursor on [x]x, newli[n]e
153+ assert_eq!(
154+ get_surround_pos(slice, &selection, '(', 1),
155+ None // overlapping surround chars
156+ );
157+ }
158+}