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
use tokenizer::Token;
use util::longest_common_prefix;
pub struct CompletionOption {
pub option_string: String,
pub complete: bool,
}
impl CompletionOption {
pub fn new(option_string: String, complete: bool) -> CompletionOption {
CompletionOption {
option_string: option_string,
complete: complete,
}
}
}
pub struct Completion<'text> {
pub help_symbol: String,
pub help_text: String,
pub token: Option<Token<'text>>,
pub exhaustive: bool,
pub options: Vec<CompletionOption>,
}
impl<'text> Completion<'text> {
pub fn new(help_symbol: String,
help_text: String,
token: Option<Token<'text>>,
exhaustive: bool,
complete_options: Vec<&str>,
other_options: Vec<&str>)
-> Completion<'text> {
let mut all_options = complete_options.clone();
all_options.extend(other_options.iter().cloned());
let mut complete_options =
complete_options.iter().map(|o| o.to_string()).collect::<Vec<_>>();
let mut other_options = other_options.iter().map(|o| o.to_string()).collect::<Vec<_>>();
if let Some(t) = token {
let token_text = t.text.to_string();
complete_options.retain(|o| o.starts_with(t.text));
other_options.retain(|o| o.starts_with(t.text));
if !exhaustive && !complete_options.contains(&token_text) &&
!other_options.contains(&token_text) {
other_options.push(token_text);
}
}
let lcp = longest_common_prefix(all_options).to_string();
if !complete_options.contains(&lcp) && !other_options.contains(&lcp) {
match token {
Some(t) => {
if lcp != t.text {
other_options.push(lcp)
}
}
None => other_options.push(lcp),
}
}
let mut options = complete_options.iter()
.map(|o| CompletionOption::new(o.clone(), true))
.collect::<Vec<_>>();
options.extend(other_options.iter().map(|o| CompletionOption::new(o.clone(), false)));
Completion {
help_symbol: help_symbol,
help_text: help_text,
token: token,
exhaustive: exhaustive,
options: options,
}
}
}