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
use std::collections::HashSet;
use crate::completions::{Completer, CompletionOptions};
use nu_protocol::{
Category, Span, SuggestionKind,
engine::{CommandType, Stack, StateWorkingSet},
};
use reedline::Suggestion;
use super::{SemanticSuggestion, completion_options::NuMatcher};
// TODO: Add a toggle for quoting multi word commands. Useful for: `which` and `attr complete`
pub struct CommandCompletion {
/// Whether to include internal commands
pub internals: bool,
/// Whether to include external commands
pub externals: bool,
}
impl CommandCompletion {
fn external_command_completion(
&self,
working_set: &StateWorkingSet,
sugg_span: reedline::Span,
internal_suggs: HashSet<String>,
mut matcher: NuMatcher<SemanticSuggestion>,
) -> Vec<SemanticSuggestion> {
let mut external_commands = HashSet::new();
let paths_val = working_set.permanent_state.get_env_var("path");
if let Some(paths_val) = paths_val
&& let Ok(paths) = paths_val.as_list()
{
for path in paths {
let path = path.coerce_str().unwrap_or_default();
if let Ok(mut contents) = std::fs::read_dir(path.as_ref()) {
while let Some(Ok(item)) = contents.next() {
if working_set
.permanent_state
.config
.completions
.external
.max_results
<= external_commands.len() as i64
{
break;
}
let Ok(name) = item.file_name().into_string() else {
continue;
};
// If there's an internal command with the same name, adds ^cmd to the
// matcher so that both the internal and external command are included
let value = if internal_suggs.contains(&name) {
format!("^{name}")
} else {
name.clone()
};
if external_commands.contains(&value) {
continue;
}
// TODO: check name matching before a relative heavy IO involved
// `is_executable` for performance consideration, should avoid
// duplicated `match_aux` call for matched items in the future
if matcher.check_match(&name).is_some()
&& Self::is_executable_command(item.path())
{
external_commands.insert(value.clone());
matcher.add(
name,
SemanticSuggestion {
suggestion: Suggestion {
value,
span: sugg_span,
append_whitespace: true,
..Default::default()
},
kind: Some(SuggestionKind::Command(
CommandType::External,
None,
)),
},
);
}
}
}
}
}
matcher.suggestion_results()
}
fn is_executable_command(path: impl AsRef<std::path::Path>) -> bool {
let path = path.as_ref();
if is_executable::is_executable(path) {
return true;
}
if cfg!(windows)
&& let Some(ext) = path.extension()
{
return ext.eq_ignore_ascii_case("ps1") && path.is_file();
}
false
}
}
impl Completer for CommandCompletion {
fn fetch(
&mut self,
working_set: &StateWorkingSet,
_stack: &Stack,
prefix: impl AsRef<str>,
span: Span,
offset: usize,
options: &CompletionOptions,
) -> Vec<SemanticSuggestion> {
let mut res = Vec::new();
let sugg_span = reedline::Span::new(span.start - offset, span.end - offset);
let mut internal_suggs = HashSet::new();
if self.internals {
let mut matcher = NuMatcher::new(prefix.as_ref(), options, true);
working_set.traverse_commands(|name, decl_id| {
let name = String::from_utf8_lossy(name);
let command = working_set.get_decl(decl_id);
if command.signature().category == Category::Removed {
return;
}
let matched = matcher.add_semantic_suggestion(SemanticSuggestion {
suggestion: Suggestion {
value: name.to_string(),
description: Some(command.description().to_string()),
span: sugg_span,
append_whitespace: true,
..Suggestion::default()
},
kind: Some(SuggestionKind::Command(
command.command_type(),
Some(decl_id),
)),
});
if matched {
internal_suggs.insert(name.to_string());
}
});
res.extend(matcher.suggestion_results());
}
if self.externals {
let external_suggs = self.external_command_completion(
working_set,
sugg_span,
internal_suggs,
NuMatcher::new(prefix, options, true),
);
res.extend(external_suggs);
}
res
}
}