Skip to content

Commit 3ec4723

Browse files
committed
fix(completion): replace bash completion script to handle colons in task names
1 parent f45c026 commit 3ec4723

4 files changed

Lines changed: 76 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
_clap_reassemble_words() {
2+
if [[ "$COMP_WORDBREAKS" != *:* ]]; then
3+
return
4+
fi
5+
local i j=0 line=$COMP_LINE
6+
words=()
7+
_CLAP_COMPLETE_INDEX=0
8+
for ((i = 0; i < ${#COMP_WORDS[@]}; i++)); do
9+
if ((i > 0 && j > 0)) && [[ "${COMP_WORDS[i]}" == :* || "${words[j-1]}" == *: ]] && [[ "$line" != [[:blank:]]* ]]; then
10+
words[j-1]="${words[j-1]}${COMP_WORDS[i]}"
11+
else
12+
words[j]="${COMP_WORDS[i]}"
13+
((j++))
14+
fi
15+
if ((i == COMP_CWORD)); then
16+
_CLAP_COMPLETE_INDEX=$((j - 1))
17+
fi
18+
line=${line#*"${COMP_WORDS[i]}"}
19+
done
20+
}
21+
22+
_clap_trim_completions() {
23+
local cur="${words[_CLAP_COMPLETE_INDEX]}"
24+
if [[ "$cur" != *:* || "$COMP_WORDBREAKS" != *:* ]]; then
25+
return
26+
fi
27+
local colon_word=${cur%"${cur##*:}"}
28+
local i=${#COMPREPLY[*]}
29+
while [[ $((--i)) -ge 0 ]]; do
30+
COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
31+
done
32+
}
33+
34+
_clap_complete_vp() {
35+
local IFS=$'\013'
36+
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
37+
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
38+
if compopt +o nospace 2> /dev/null; then
39+
local _CLAP_COMPLETE_SPACE=false
40+
else
41+
local _CLAP_COMPLETE_SPACE=true
42+
fi
43+
local words=("${COMP_WORDS[@]}")
44+
_clap_reassemble_words
45+
COMPREPLY=( $( \
46+
_CLAP_IFS="$IFS" \
47+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
48+
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
49+
_CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" \
50+
VP_COMPLETE="bash" \
51+
"vp" -- "${words[@]}" \
52+
) )
53+
if [[ $? != 0 ]]; then
54+
unset COMPREPLY
55+
elif [[ $_CLAP_COMPLETE_SPACE == false ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
56+
compopt -o nospace
57+
fi
58+
_clap_trim_completions
59+
}
60+
if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then
61+
complete -o nospace -o bashdefault -o nosort -F _clap_complete_vp vp
62+
else
63+
complete -o nospace -o bashdefault -F _clap_complete_vp vp
64+
fi

crates/vite_global_cli/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,6 @@ fn should_force_global_delegate(command: &str, args: &[String]) -> bool {
14871487
///
14881488
/// Delegates to the local vite-plus CLI to run `vp run` without arguments,
14891489
/// which returns a list of available tasks in the format "task_name: description".
1490-
14911490
fn run_tasks_completions(current: &OsStr) -> Vec<clap_complete::CompletionCandidate> {
14921491
let Some(cwd) = std::env::current_dir()
14931492
.ok()
@@ -1497,6 +1496,7 @@ fn run_tasks_completions(current: &OsStr) -> Vec<clap_complete::CompletionCandid
14971496
return vec![];
14981497
};
14991498

1499+
// Unescape hashtag and trim quotes for better matching
15001500
let current = current
15011501
.to_string_lossy()
15021502
.replace("\\#", "#")

crates/vite_global_cli/src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod shim;
1717
mod tips;
1818

1919
use std::{
20+
env,
2021
io::{IsTerminal, Write},
2122
process::{ExitCode, ExitStatus},
2223
};
@@ -229,11 +230,18 @@ async fn main() -> ExitCode {
229230
// Initialize tracing
230231
vite_shared::init_tracing();
231232

233+
let mut args: Vec<String> = std::env::args().collect();
234+
235+
// Replace bash completion script to fix completion for items containing ':'
236+
if env::var_os("VP_COMPLETE").is_some_and(|shell| shell == "bash") && args.len() == 1 {
237+
print!("{}", include_str!("../completion-register.bash"));
238+
return ExitCode::SUCCESS;
239+
}
240+
232241
// Handle shell completion
233242
CompleteEnv::with_factory(command_with_help).var("VP_COMPLETE").complete();
234243

235244
// Check for shim mode (invoked as node, npm, or npx)
236-
let mut args: Vec<String> = std::env::args().collect();
237245
let argv0 = args.first().map(|s| s.as_str()).unwrap_or("vp");
238246
tracing::debug!("argv0: {argv0}");
239247

0 commit comments

Comments
 (0)