-
Notifications
You must be signed in to change notification settings - Fork 137
/
completer.rs
55 lines (48 loc) · 1.47 KB
/
completer.rs
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
use liner::{Completer, FilenameCompleter};
pub struct IonFileCompleter {
inner: FilenameCompleter
}
impl IonFileCompleter {
pub fn new(path: Option<&str>) -> IonFileCompleter {
IonFileCompleter { inner: FilenameCompleter::new(path) }
}
}
impl Completer for IonFileCompleter {
fn completions(&self, start: &str) -> Vec<String> {
self.inner.completions(start).iter().map(|x| escape(x.as_str())).collect()
}
}
fn escape(input: &str) -> String {
let mut output = Vec::with_capacity(input.len());
for character in input.bytes() {
match character {
b'(' | b')' | b'[' | b']' => output.push(b'\\'),
_ => ()
}
output.push(character);
}
unsafe { String::from_utf8_unchecked(output) }
}
/// A completer that combines suggestions from multiple completers.
#[derive(Clone, Eq, PartialEq)]
pub struct MultiCompleter<A, B> where A: Completer, B: Completer {
a: Vec<A>,
b: B
}
impl<A, B> MultiCompleter<A, B> where A: Completer, B: Completer {
pub fn new(a: Vec<A>, b: B) -> MultiCompleter<A, B> {
MultiCompleter {
a: a,
b: b
}
}
}
impl<A, B> Completer for MultiCompleter<A, B> where A: Completer, B: Completer {
fn completions(&self, start: &str) -> Vec<String> {
let mut completions = self.b.completions(start);
for x in &self.a {
completions.extend_from_slice(&x.completions(start));
}
completions
}
}