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
|
use {
super::CommandParts,
crate::{
app::{
AppContext,
SelInfo,
},
path::{self, PathAnchor},
syntactic::SYNTAX_THEMES,
verb::{
ArgDef,
PrefixSearchResult,
},
},
lazy_regex::regex_captures,
std::{
io,
path::Path,
},
};
/// find the longest common start of a and b
fn common_start<'l>(a: &'l str, b: &str) -> &'l str {
for i in 0..a.len().min(b.len()) {
if a.as_bytes()[i] != b.as_bytes()[i] {
return &a[..i];
}
}
a
}
/// how an input can be completed
#[derive(Debug)]
pub enum Completions {
/// no completion found
None,
/// all possible completions have this common root
Common(String),
/// a list of possible completions
List(Vec<String>),
}
impl Completions {
fn from_list(completions: Vec<String>) -> Self {
if completions.is_empty() {
return Self::None;
}
let mut iter = completions.iter();
let mut common: &str = match iter.next() {
Some(s) => s,
_ => { return Self::None; }
};
for c in iter {
common = common_start(common, c);
}
if common.is_empty() {
Self::List(completions)
} else {
Self::Common(common.to_string())
}
}
/// the wholes are assumed to all start with start.
fn for_wholes(
start: &str,
wholes: Vec<&str>,
) -> Self {
let completions = wholes.iter()
.map(|w|
if let Some(stripped) = w.strip_prefix(start) {
stripped
} else {
// this might become a feature but right now it's a bug
warn!("unexpected non completing whole: {:?}", w);
*w
}
)
.map(|c| c.to_string())
.collect();
Self::from_list(completions)
}
fn for_verb(
start: &str,
con: &AppContext,
sel_info: SelInfo<'_>,
) -> Self {
match con.verb_store.search_sel_info(start, sel_info) {
PrefixSearchResult::NoMatch => Self::None,
PrefixSearchResult::Match(name, _) => {
if start.len() >= name.len() {
debug_assert!(name == start);
Self::None
} else {
Self::Common(name[start.len()..].to_string())
}
}
PrefixSearchResult::Matches(completions) => Self::for_wholes(
start,
completions,
),
}
}
fn list_for_path(
verb_name: &str,
arg: &str,
path: &Path,
sel_info: SelInfo<'_>,
con: &AppContext,
) -> io::Result<Vec<String>> {
let anchor = match con.verb_store.search_sel_info(verb_name, sel_info) {
PrefixSearchResult::Match(_, verb) => verb.get_unique_arg_anchor(),
_ => PathAnchor::Unspecified,
};
let (_, parent_part, child_part) = regex_captures!(r"^(.*?)([^/]*)$", arg).unwrap();
let parent = path::path_from(path, anchor, parent_part);
let mut children = Vec::new();
if !parent.exists() {
debug!("no path completion possible because {:?} doesn't exist", &parent);
} else {
for entry in parent.read_dir()? {
let entry = entry?;
let mut name = entry.file_name().to_string_lossy().to_string();
if !child_part.is_empty() {
if !name.starts_with(child_part) {
continue;
}
if name == child_part && entry.file_type()?.is_dir() {
name = "/".to_string();
} else {
name.drain(0..child_part.len());
}
}
children.push(name);
}
}
Ok(children)
}
/// we have a verb, we try to complete one of the args
fn for_arg(
verb_name: &str,
arg: &str,
con: &AppContext,
sel_info: SelInfo<'_>,
) -> Self {
if arg.contains(' ') {
return Self::None;
}
// we try to get the type of argument
let arg_def = con
.verb_store
.search_sel_info_unique(verb_name, sel_info)
.and_then(|verb| verb.invocation_parser.as_ref())
.and_then(|invocation_parser| invocation_parser.get_unique_arg_def());
if matches!(arg_def, Some(ArgDef::Theme)) {
Self::for_theme_arg(arg)
} else {
Self::for_path_arg(verb_name, arg, con, sel_info)
}
}
/// we have a verb and it asks for a theme
fn for_theme_arg(
arg: &str,
) -> Self {
let arg = arg.to_lowercase();
let completions: Vec<String> = SYNTAX_THEMES
.iter()
.map(|st| st.name().to_lowercase())
.filter_map(|name| name.strip_prefix(&arg).map(|s| s.to_string()))
.collect();
Self::from_list(completions)
}
/// we have a verb and it asks for a path
fn for_path_arg(
verb_name: &str,
arg: &str,
con: &AppContext,
sel_info: SelInfo<'_>,
) -> Self {
// in the future we might offer completion of other types
// of arguments, maybe user supplied, but there's no use case
// now so we'll just assume the user wants to complete a path.
if arg.contains(' ') {
return Self::None;
}
match &sel_info {
SelInfo::None => Self::None,
SelInfo::One(sel) => {
match Self::list_for_path(verb_name, arg, sel.path, sel_info, con) {
Ok(list) => Self::from_list(list),
Err(e) => {
warn!("Error while trying to complete path: {:?}", e);
Self::None
}
}
}
SelInfo::More(stage) => {
// We're looking for the possible completions which
// are valid for all elements of the stage
let mut lists = stage.paths()
.iter()
.filter_map(|path| {
Self::list_for_path(
verb_name,
arg,
path,
sel_info,
con
).ok()
});
let mut list = match lists.next() {
Some(list) => list,
None => {
// can happen if there were IO errors on paths in stage, for example
// on removals
return Self::None;
}
};
for ol in lists {
list.retain(|c| ol.contains(c));
if list.is_empty() {
break;
}
}
Self::from_list(list)
}
}
}
pub fn for_input(
parts: &CommandParts,
con: &AppContext,
sel_info: SelInfo<'_>,
) -> Self {
info!("Looking for completions");
match &parts.verb_invocation {
Some(invocation) if !invocation.is_empty() => {
match &invocation.args {
None => {
// looking into verb completion
Self::for_verb(&invocation.name, con, sel_info)
}
Some(args) if !args.is_empty() => {
// looking into arg completion
Self::for_arg(&invocation.name, args, con, sel_info)
}
_ => {
// nothing possible
Self::None
}
}
}
_ => Self::None, // no possible completion if no verb invocation
}
}
}
|