aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src/test.rs
blob: 964a770a7e283a65a216d637d6349ccdfe0982d6 (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
159
160
161
//! Test helpers.
use crate::{Range, Selection};
use smallvec::SmallVec;
use std::cmp::Reverse;

/// Convert annotated test string to test string and selection.
///
/// `#[|` for primary selection with head before anchor followed by `]#`.
/// `#(|` for secondary selection with head before anchor followed by `)#`.
/// `#[` for primary selection with head after anchor followed by `|]#`.
/// `#(` for secondary selection with head after anchor followed by `|)#`.
///
/// # Examples
///
/// ```
/// use helix_core::{Range, Selection, test::print};
/// use smallvec::smallvec;
///
/// assert_eq!(
///     print("#[a|]#b#(|c)#"),
///     ("abc".to_owned(), Selection::new(smallvec![Range::new(0, 1), Range::new(3, 2)], 0))
/// );
/// ```
///
/// # Panics
///
/// Panics when missing primary or appeared more than once.
/// Panics when missing head or anchor.
/// Panics when head come after head or anchor come after anchor.
pub fn print(s: &str) -> (String, Selection) {
    let mut primary = None;
    let mut ranges = SmallVec::new();
    let mut iter = s.chars().peekable();
    let mut left = String::with_capacity(s.len());
    'outer: while let Some(c) = iter.next() {
        let start = left.len();
        if c == '#' {
            if iter.next_if_eq(&'[').is_some() {
                if primary.is_some() {
                    panic!("primary `#[` already appeared {left:?} {s:?}");
                }
                if iter.next_if_eq(&'|').is_some() {
                    while let Some(c) = iter.next() {
                        if c == ']' && iter.next_if_eq(&'#').is_some() {
                            primary = Some(ranges.len());
                            ranges.push(Range::new(left.len(), start));
                            continue 'outer;
                        } else {
                            left.push(c);
                        }
                    }
                    panic!("missing primary end `]#` {left:?} {s:?}");
                } else {
                    while let Some(c) = iter.next() {
                        if c == '|' {
                            if let Some(cc) = iter.next_if_eq(&']') {
                                if iter.next_if_eq(&'#').is_some() {
                                    primary = Some(ranges.len());
                                    ranges.push(Range::new(start, left.len()));
                                    continue 'outer;
                                } else {
                                    left.push(c);
                                    left.push(cc);
                                }
                            } else {
                                left.push(c);
                            }
                        } else {
                            left.push(c);
                        }
                    }
                    panic!("missing primary end `|]#` {left:?} {s:?}");
                }
            } else if iter.next_if_eq(&'(').is_some() {
                if iter.next_if_eq(&'|').is_some() {
                    while let Some(c) = iter.next() {
                        if c == ')' && iter.next_if_eq(&'#').is_some() {
                            ranges.push(Range::new(left.len(), start));
                            continue 'outer;
                        } else {
                            left.push(c);
                        }
                    }
                    panic!("missing end `)#` {left:?} {s:?}");
                } else {
                    while let Some(c) = iter.next() {
                        if c == '|' {
                            if let Some(cc) = iter.next_if_eq(&')') {
                                if iter.next_if_eq(&'#').is_some() {
                                    ranges.push(Range::new(start, left.len()));
                                    continue 'outer;
                                } else {
                                    left.push(c);
                                    left.push(cc);
                                }
                            } else {
                                left.push(c);
                            }
                        } else {
                            left.push(c);
                        }
                    }
                    panic!("missing end `|)#` {left:?} {s:?}");
                }
            } else {
                left.push(c);
            }
        } else {
            left.push(c);
        }
    }
    let primary = match primary {
        Some(i) => i,
        None => panic!("missing primary `#[|]#` {s:?}"),
    };
    let selection = Selection::new(ranges, primary);
    (left, selection)
}

/// Convert test string and selection to annotated test string.
///
/// `#[|` for primary selection with head before anchor followed by `]#`.
/// `#(|` for secondary selection with head before anchor followed by `)#`.
/// `#[` for primary selection with head after anchor followed by `|]#`.
/// `#(` for secondary selection with head after anchor followed by `|)#`.
///
/// # Examples
///
/// ```
/// use helix_core::{Range, Selection, test::plain};
/// use smallvec::smallvec;
///
/// assert_eq!(
///     plain("abc", Selection::new(smallvec![Range::new(0, 1), Range::new(3, 2)], 0)),
///     "#[a|]#b#(|c)#".to_owned()
/// );
/// ```
pub fn plain(s: &str, selection: Selection) -> String {
    let primary = selection.primary_index();
    let mut out = String::with_capacity(s.len() + 5 * selection.len());
    out.push_str(s);
    let mut insertion: Vec<_> = selection
        .iter()
        .enumerate()
        .flat_map(|(i, range)| {
            // sort like this before reversed so anchor < head later
            match (range.anchor < range.head, i == primary) {
                (true, true) => [(range.anchor, "#["), (range.head, "|]#")],
                (true, false) => [(range.anchor, "#("), (range.head, "|)#")],
                (false, true) => [(range.anchor, "]#"), (range.head, "#[|")],
                (false, false) => [(range.anchor, ")#"), (range.head, "#(|")],
            }
        })
        .collect();
    // insert in reverse order
    insertion.sort_unstable_by_key(|k| Reverse(k.0));
    for (i, s) in insertion {
        out.insert_str(i, s);
    }
    out
}