summaryrefslogtreecommitdiff
path: root/helix-core/src/surround.rs
blob: a27033024744f8db76bcd2bd71491eb523f09729 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::{search, Selection};
use ropey::RopeSlice;

pub const PAIRS: &[(char, char)] = &[
    ('(', ')'),
    ('[', ']'),
    ('{', '}'),
    ('<', '>'),
    ('«', '»'),
    ('「', '」'),
    ('(', ')'),
];

/// Given any char in [PAIRS], return the open and closing chars. If not found in
/// [PAIRS] return (ch, ch).
///
/// ```
/// use helix_core::surround::get_pair;
///
/// assert_eq!(get_pair('['), ('[', ']'));
/// assert_eq!(get_pair('}'), ('{', '}'));
/// assert_eq!(get_pair('"'), ('"', '"'));
/// ```
pub fn get_pair(ch: char) -> (char, char) {
    PAIRS
        .iter()
        .find(|(open, close)| *open == ch || *close == ch)
        .copied()
        .unwrap_or((ch, ch))
}

/// Find the position of surround pairs of `ch` which can be either a closing
/// or opening pair. `n` will skip n - 1 pairs (eg. n=2 will discard (only)
/// the first pair found and keep looking)
pub fn find_nth_pairs_pos(
    text: RopeSlice,
    ch: char,
    pos: usize,
    n: usize,
) -> Option<(usize, usize)> {
    let (open, close) = get_pair(ch);
    let open_pos = search::find_nth_prev(text, open, pos, n, true)?;
    let close_pos = search::find_nth_next(text, close, pos, n, true)?;

    Some((open_pos, close_pos))
}

/// Find position of surround characters around every cursor. Returns None
/// if any positions overlap. Note that the positions are in a flat Vec.
/// Use get_surround_pos().chunks(2) to get matching pairs of surround positions.
/// `ch` can be either closing or opening pair.
pub fn get_surround_pos(
    text: RopeSlice,
    selection: &Selection,
    ch: char,
    skip: usize,
) -> Option<Vec<usize>> {
    let mut change_pos = Vec::new();

    for range in selection {
        let (open_pos, close_pos) = find_nth_pairs_pos(text, ch, range.head, skip)?;
        if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) {
            return None;
        }
        change_pos.extend_from_slice(&[open_pos, close_pos]);
    }
    Some(change_pos)
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::Range;

    use ropey::Rope;
    use smallvec::SmallVec;

    #[test]
    fn test_find_nth_pairs_pos() {
        let doc = Rope::from("some (text) here");
        let slice = doc.slice(..);

        // cursor on [t]ext
        assert_eq!(find_nth_pairs_pos(slice, '(', 6, 1), Some((5, 10)));
        assert_eq!(find_nth_pairs_pos(slice, ')', 6, 1), Some((5, 10)));
        // cursor on so[m]e
        assert_eq!(find_nth_pairs_pos(slice, '(', 2, 1), None);
        // cursor on bracket itself
        // assert_eq!(find_nth_pairs_pos(slice, '(', 5, 1), Some((5, 10)));
    }

    #[test]
    fn test_find_nth_pairs_pos_skip() {
        let doc = Rope::from("(so (many (good) text) here)");
        let slice = doc.slice(..);

        // cursor on go[o]d
        assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((10, 15)));
        assert_eq!(find_nth_pairs_pos(slice, '(', 13, 2), Some((4, 21)));
        assert_eq!(find_nth_pairs_pos(slice, '(', 13, 3), Some((0, 27)));
    }

    #[test]
    fn test_find_nth_pairs_pos_mixed() {
        let doc = Rope::from("(so [many {good} text] here)");
        let slice = doc.slice(..);

        // cursor on go[o]d
        assert_eq!(find_nth_pairs_pos(slice, '{', 13, 1), Some((10, 15)));
        assert_eq!(find_nth_pairs_pos(slice, '[', 13, 1), Some((4, 21)));
        assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((0, 27)));
    }

    #[test]
    fn test_get_surround_pos() {
        let doc = Rope::from("(some) (chars)\n(newline)");
        let slice = doc.slice(..);
        let selection = Selection::new(
            SmallVec::from_slice(&[Range::point(2), Range::point(9), Range::point(20)]),
            0,
        );

        // cursor on s[o]me, c[h]ars, newl[i]ne
        assert_eq!(
            get_surround_pos(slice, &selection, '(', 1)
                .unwrap()
                .as_slice(),
            &[0, 5, 7, 13, 15, 23]
        );
    }

    #[test]
    fn test_get_surround_pos_bail() {
        let doc = Rope::from("[some]\n(chars)xx\n(newline)");
        let slice = doc.slice(..);

        let selection = Selection::new(
            SmallVec::from_slice(&[Range::point(2), Range::point(9)]),
            0,
        );

        // cursor on s[o]me, c[h]ars
        assert_eq!(
            get_surround_pos(slice, &selection, '(', 1),
            None // different surround chars
        );

        let selection = Selection::new(
            SmallVec::from_slice(&[Range::point(14), Range::point(24)]),
            0,
        );
        // cursor on [x]x, newli[n]e
        assert_eq!(
            get_surround_pos(slice, &selection, '(', 1),
            None  // overlapping surround chars
        );
    }
}