Repository navigation
Expand file tree
/
Copy pathcompletions.rs
More file actions
148 lines (145 loc) · 5.93 KB
/
Copy pathcompletions.rs
File metadata and controls
148 lines (145 loc) · 5.93 KB
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
//! Completion candidates come from the public help, keeping the private runner
//! independent of a second CLI framework. Scripts never build the application.
use crate::{Result, args::HELP};
pub fn script(shell: &str) -> Result<String> {
let commands = HELP
.split("Commands:\n")
.nth(1)
.unwrap()
.split("Options:\n")
.next()
.unwrap()
.lines()
.filter_map(|line| line.split_whitespace().next())
.collect::<Vec<_>>();
let options = HELP
.lines()
.filter(|line| line.starts_with(" --"))
.filter_map(|line| line.split_whitespace().next())
.chain(["--version", "-h", "-V", "-p", "-F"])
.collect::<Vec<_>>();
let words = commands
.iter()
.chain(&options)
.copied()
.collect::<Vec<_>>()
.join(" ");
Ok(match shell {
"bash" => format!(
r#"_glory_complete() {{
local current="${{COMP_WORDS[COMP_CWORD]}}" previous="${{COMP_WORDS[COMP_CWORD-1]}}"
local words='{words}'
local word
for word in "${{COMP_WORDS[@]:1:COMP_CWORD-1}}"; do
if [[ "$word" == -- ]]; then COMPREPLY=(); return; fi
done
case "$previous" in
--platform) words='web desktop server' ;;
completions) words='bash zsh fish powershell' ;;
--manifest-path|--cli-path|--build-root|--bindgen|--wasm-opt)
COMPREPLY=()
while IFS= read -r word; do COMPREPLY+=("$word"); done < <(compgen -f -- "$current")
return ;;
--target|--package|-p|--bin|--features|-F|--wasm-opt-version|--port) COMPREPLY=(); return ;;
esac
COMPREPLY=( $(compgen -W "$words" -- "$current") )
}}
complete -F _glory_complete glory bin/glory ./bin/glory
"#
),
"zsh" => format!(
r#"#compdef glory bin/glory ./bin/glory
_glory_complete() {{
local -a candidates
local word
for word in "${{words[@]:1:CURRENT-2}}"; do [[ "$word" == -- ]] && return; done
case "${{words[CURRENT-1]}}" in
--platform) candidates=(web desktop server) ;;
completions) candidates=(bash zsh fish powershell) ;;
--manifest-path|--cli-path|--build-root|--bindgen|--wasm-opt) _files; return ;;
--target|--package|-p|--bin|--features|-F|--wasm-opt-version|--port) return ;;
*) candidates=({words}) ;;
esac
compadd -- "${{candidates[@]}}"
}}
compdef _glory_complete glory bin/glory ./bin/glory
"#
),
"fish" => {
let mut output = String::new();
for executable in ["glory", "bin/glory", "./bin/glory"] {
output.push_str(&format!(
"complete -c {executable} -n 'not contains -- -- (commandline -opc)' -f -a '{}'\n",
commands.join(" ")
));
for option in &options {
if let Some(long) = option.strip_prefix("--") {
let extra = match long {
"platform" => " -r -f -a 'web desktop server'",
"manifest-path" | "cli-path" | "build-root" | "bindgen"
| "wasm-opt" => " -r -F",
"target" | "package" | "bin" | "features" | "wasm-opt-version"
| "port" => " -r -f",
_ => "",
};
output.push_str(&format!("complete -c {executable} -n 'not contains -- -- (commandline -opc)' -l {long}{extra}\n"));
}
}
}
output
}
"powershell" => format!(
r#"Register-ArgumentCompleter -Native -CommandName glory,glory.ps1,./bin/glory.ps1 -ScriptBlock {{
param($wordToComplete, $commandAst, $cursorPosition)
$elements = @($commandAst.CommandElements | Where-Object {{ $_.Extent.StartOffset -lt $cursorPosition }})
if ($elements | Where-Object {{ $_.Extent.Text -eq '--' }}) {{ return }}
$previous = if ($wordToComplete -eq '' -and $elements.Count -gt 0) {{ $elements[-1].Extent.Text }} elseif ($elements.Count -gt 1) {{ $elements[-2].Extent.Text }} else {{ '' }}
$candidates = switch ($previous) {{
'--platform' {{ 'web','desktop','server' }}
'completions' {{ 'bash','zsh','fish','powershell' }}
{{ $_ -in @('--target','--package','-p','--bin','--features','-F','--wasm-opt-version','--port','--manifest-path','--cli-path','--build-root','--bindgen','--wasm-opt') }} {{ return }}
default {{ '{words}'.Split(' ') }}
}}
$candidates | Where-Object {{ $_.StartsWith($wordToComplete, [System.StringComparison]::OrdinalIgnoreCase) }} | ForEach-Object {{
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}}
}}
"#
),
_ => return Err("unsupported completion shell".into()),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scripts_offer_current_commands_and_overrides() {
for shell in ["bash", "zsh", "fish", "powershell"] {
let script = script(shell).unwrap();
for word in [
"doctor",
"completions",
"wasm-opt-version",
"no-default-features",
] {
assert!(script.contains(word), "{shell}: {word}");
}
}
assert!(script("unknown").is_err());
}
#[cfg(unix)]
#[test]
fn bash_completes_platforms_and_stops_at_application_arguments() {
let input = format!(
"{}\nCOMP_WORDS=(./bin/glory serve --platform d); COMP_CWORD=3; _glory_complete; [[ ${{COMPREPLY[*]}} == desktop ]] || exit 1\nCOMP_WORDS=(./bin/glory run -- --port); COMP_CWORD=3; _glory_complete; [[ ${{#COMPREPLY[@]}} == 0 ]]",
script("bash").unwrap()
);
assert!(
std::process::Command::new("bash")
.args(["-c", &input])
.status()
.unwrap()
.success()
);
}
}