Unnamed repository; edit this file 'description' to name the repository.
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use std::{collections::HashMap, mem, ops::Range, sync::Arc};

#[derive(Debug)]
pub(super) struct PickerQuery {
    /// The column names of the picker.
    column_names: Box<[Arc<str>]>,
    /// The index of the primary column in `column_names`.
    /// The primary column is selected by default unless another
    /// field is specified explicitly with `%fieldname`.
    primary_column: usize,
    /// The mapping between column names and input in the query
    /// for those columns.
    inner: HashMap<Arc<str>, Arc<str>>,
    /// The byte ranges of the input text which are used as input for each column.
    /// This is calculated at parsing time for use in [Self::active_column].
    /// This Vec is naturally sorted in ascending order and ranges do not overlap.
    column_ranges: Vec<(Range<usize>, Option<Arc<str>>)>,
}

impl PartialEq<HashMap<Arc<str>, Arc<str>>> for PickerQuery {
    fn eq(&self, other: &HashMap<Arc<str>, Arc<str>>) -> bool {
        self.inner.eq(other)
    }
}

impl PickerQuery {
    pub(super) fn new<I: Iterator<Item = Arc<str>>>(
        column_names: I,
        primary_column: usize,
    ) -> Self {
        let column_names: Box<[_]> = column_names.collect();
        let inner = HashMap::with_capacity(column_names.len());
        let column_ranges = vec![(0..usize::MAX, Some(column_names[primary_column].clone()))];
        Self {
            column_names,
            primary_column,
            inner,
            column_ranges,
        }
    }

    pub(super) fn get(&self, column: &str) -> Option<&Arc<str>> {
        self.inner.get(column)
    }

    pub(super) fn parse(&mut self, input: &str) -> HashMap<Arc<str>, Arc<str>> {
        let mut fields: HashMap<Arc<str>, String> = HashMap::new();
        let primary_field = &self.column_names[self.primary_column];
        let mut escaped = false;
        let mut in_field = false;
        let mut field = None;
        let mut text = String::new();
        self.column_ranges.clear();
        self.column_ranges
            .push((0..usize::MAX, Some(primary_field.clone())));

        macro_rules! finish_field {
            () => {
                let key = field.take().unwrap_or(primary_field);

                // Trims one space from the end, enabling leading and trailing
                // spaces in search patterns, while also retaining spaces as separators
                // between column filters.
                let pat = text.strip_suffix(' ').unwrap_or(&text);

                if let Some(pattern) = fields.get_mut(key) {
                    pattern.push(' ');
                    pattern.push_str(pat);
                } else {
                    fields.insert(key.clone(), pat.to_string());
                }
                text.clear();
            };
        }

        for (idx, ch) in input.char_indices() {
            match ch {
                // Backslash escaping
                _ if escaped => {
                    // '%' is the only character that is special cased.
                    // You can escape it to prevent parsing the text that
                    // follows it as a field name.
                    if ch != '%' {
                        text.push('\\');
                    }
                    text.push(ch);
                    escaped = false;
                }
                '\\' => escaped = !escaped,
                '%' => {
                    if !text.is_empty() {
                        finish_field!();
                    }
                    let (range, _field) = self
                        .column_ranges
                        .last_mut()
                        .expect("column_ranges is non-empty");
                    range.end = idx;
                    in_field = true;
                }
                ' ' if in_field => {
                    text.clear();
                    in_field = false;
                }
                _ if in_field => {
                    text.push(ch);
                    // Go over all columns and their indices, find all that starts with field key,
                    // select a column that fits key the most.
                    field = self
                        .column_names
                        .iter()
                        .filter(|col| col.starts_with(&text))
                        // select "fittest" column
                        .min_by_key(|col| col.len());

                    // Update the column range for this column.
                    if let Some((_range, current_field)) = self
                        .column_ranges
                        .last_mut()
                        .filter(|(range, _)| range.end == usize::MAX)
                    {
                        *current_field = field.cloned();
                    } else {
                        self.column_ranges.push((idx..usize::MAX, field.cloned()));
                    }
                }
                _ => text.push(ch),
            }
        }

        if !in_field && !text.is_empty() {
            finish_field!();
        }

        let new_inner: HashMap<_, _> = fields
            .into_iter()
            .map(|(field, query)| (field, query.as_str().into()))
            .collect();

        mem::replace(&mut self.inner, new_inner)
    }

    /// Finds the column which the cursor is 'within' in the last parse.
    ///
    /// The cursor is considered to be within a column when it is placed within any
    /// of a column's text. See the `active_column_test` unit test below for examples.
    ///
    /// `cursor` is a byte index that represents the location of the prompt's cursor.
    pub fn active_column(&self, cursor: usize) -> Option<&Arc<str>> {
        let point = self
            .column_ranges
            .partition_point(|(range, _field)| cursor > range.end);

        self.column_ranges
            .get(point)
            .filter(|(range, _field)| cursor >= range.start && cursor <= range.end)
            .and_then(|(_range, field)| field.as_ref())
    }
}

#[cfg(test)]
mod test {
    use helix_core::hashmap;

    use super::*;

    #[test]
    fn parse_query_test() {
        let mut query = PickerQuery::new(
            [
                "primary".into(),
                "field1".into(),
                "field2".into(),
                "another".into(),
                "anode".into(),
            ]
            .into_iter(),
            0,
        );

        // Basic field splitting
        query.parse("hello world");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello world".into(),
            )
        );
        query.parse("hello %field1 world %field2 !");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello".into(),
                "field1".into() => "world".into(),
                "field2".into() => "!".into(),
            )
        );
        query.parse("%field1 abc %field2 def xyz");
        assert_eq!(
            query,
            hashmap!(
                "field1".into() => "abc".into(),
                "field2".into() => "def xyz".into(),
            )
        );

        // Trailing space is trimmed
        query.parse("hello ");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello".into(),
            )
        );

        // Unknown fields are trimmed.
        query.parse("hello %foo");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello".into(),
            )
        );

        // Multiple words in a field
        query.parse("hello %field1 a b c");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello".into(),
                "field1".into() => "a b c".into(),
            )
        );

        // Escaping
        query.parse(r#"hello\ world"#);
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => r#"hello\ world"#.into(),
            )
        );
        query.parse(r#"hello \%field1 world"#);
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello %field1 world".into(),
            )
        );
        query.parse(r#"%field1 hello\ world"#);
        assert_eq!(
            query,
            hashmap!(
                "field1".into() => r#"hello\ world"#.into(),
            )
        );
        query.parse(r#"hello %field1 a\"b"#);
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello".into(),
                "field1".into() => r#"a\"b"#.into(),
            )
        );
        query.parse(r#"%field1 hello\ world"#);
        assert_eq!(
            query,
            hashmap!(
                "field1".into() => r#"hello\ world"#.into(),
            )
        );
        query.parse(r#"\bfoo\b"#);
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => r#"\bfoo\b"#.into(),
            )
        );
        query.parse(r#"\\n"#);
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => r#"\\n"#.into(),
            )
        );

        // Only the prefix of a field is required.
        query.parse("hello %anot abc");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello".into(),
                "another".into() => "abc".into(),
            )
        );
        // The shortest matching the prefix is selected.
        query.parse("hello %ano abc");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello".into(),
                "anode".into() => "abc".into()
            )
        );
        // Multiple uses of a column are concatenated with space separators.
        query.parse("hello %field1 xyz %fie abc");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello".into(),
                "field1".into() => "xyz abc".into()
            )
        );
        query.parse("hello %fie abc");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello".into(),
                "field1".into() => "abc".into()
            )
        );
        // The primary column can be explicitly qualified.
        query.parse("hello %fie abc %prim world");
        assert_eq!(
            query,
            hashmap!(
                "primary".into() => "hello world".into(),
                "field1".into() => "abc".into()
            )
        );
    }

    #[test]
    fn active_column_test() {
        fn active_column<'a>(query: &'a mut PickerQuery, input: &str) -> Option<&'a str> {
            let cursor = input.find('|').expect("cursor must be indicated with '|'");
            let input = input.replace('|', "");
            query.parse(&input);
            query.active_column(cursor).map(AsRef::as_ref)
        }

        let mut query = PickerQuery::new(
            ["primary".into(), "foo".into(), "bar".into()].into_iter(),
            0,
        );

        assert_eq!(active_column(&mut query, "|"), Some("primary"));
        assert_eq!(active_column(&mut query, "hello| world"), Some("primary"));
        assert_eq!(active_column(&mut query, "|%foo hello"), Some("primary"));
        assert_eq!(active_column(&mut query, "%foo|"), Some("foo"));
        assert_eq!(active_column(&mut query, "%|"), None);
        assert_eq!(active_column(&mut query, "%baz|"), None);
        assert_eq!(active_column(&mut query, "%quiz%|"), None);
        assert_eq!(active_column(&mut query, "%foo hello| world"), Some("foo"));
        assert_eq!(active_column(&mut query, "%foo hello world|"), Some("foo"));
        assert_eq!(active_column(&mut query, "%foo| hello world"), Some("foo"));
        assert_eq!(active_column(&mut query, "%|foo hello world"), Some("foo"));
        assert_eq!(active_column(&mut query, "%f|oo hello world"), Some("foo"));
        assert_eq!(active_column(&mut query, "hello %f|oo world"), Some("foo"));
        assert_eq!(
            active_column(&mut query, "hello %f|oo world %bar !"),
            Some("foo")
        );
        assert_eq!(
            active_column(&mut query, "hello %foo wo|rld %bar !"),
            Some("foo")
        );
        assert_eq!(
            active_column(&mut query, "hello %foo world %bar !|"),
            Some("bar")
        );
    }
}