Skip to content

Commit 2399064

Browse files
authored
feat(completion): add shell completion for 'vpr' command (#1227)
Add dynamic shell completion support for `vpr` shorthand. This is a follow-up to #1181, applying the same completion logic to `vpr` in #1178.
1 parent 3fc8005 commit 2399064

3 files changed

Lines changed: 56 additions & 6 deletions

File tree

crates/vite_global_cli/completion-register.bash

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,18 @@ _clap_complete_vp() {
5757
fi
5858
_clap_trim_completions
5959
}
60+
61+
_clap_complete_vpr() {
62+
local COMP_WORDS=("vp" "run" "${COMP_WORDS[@]:1}")
63+
local COMP_CWORD=$((COMP_CWORD + 1))
64+
local COMP_LINE="vp run ${COMP_LINE#vpr}"
65+
_clap_complete_vp
66+
}
67+
6068
if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then
6169
complete -o nospace -o bashdefault -o nosort -F _clap_complete_vp vp
70+
complete -o nospace -o bashdefault -o nosort -F _clap_complete_vpr vpr
6271
else
6372
complete -o nospace -o bashdefault -F _clap_complete_vp vp
73+
complete -o nospace -o bashdefault -F _clap_complete_vpr vpr
6474
fi

crates/vite_global_cli/src/cli.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,11 +1557,7 @@ fn should_force_global_delegate(command: &str, args: &[String]) -> bool {
15571557
/// Delegates to the local vite-plus CLI to run `vp run` without arguments,
15581558
/// which returns a list of available tasks in the format "task_name: description".
15591559
fn run_tasks_completions(current: &OsStr) -> Vec<clap_complete::CompletionCandidate> {
1560-
let Some(cwd) = std::env::current_dir()
1561-
.ok()
1562-
.and_then(AbsolutePathBuf::new)
1563-
.filter(|p| commands::has_vite_plus_dependency(p))
1564-
else {
1560+
let Ok(cwd) = vite_path::current_dir() else {
15651561
return vec![];
15661562
};
15671563

crates/vite_global_cli/src/commands/env/setup.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,13 @@ if [ -n "$BASH_VERSION" ] && type complete >/dev/null 2>&1; then
449449
eval "$(VP_COMPLETE=bash command vp)"
450450
elif [ -n "$ZSH_VERSION" ] && type compdef >/dev/null 2>&1; then
451451
eval "$(VP_COMPLETE=zsh command vp)"
452+
_vpr_complete() {
453+
local -a orig=("${words[@]}")
454+
words=("vp" "run" "${orig[@]:1}")
455+
CURRENT=$((CURRENT + 1))
456+
${=_comps[vp]}
457+
}
458+
compdef _vpr_complete vpr
452459
fi
453460
"#
454461
.replace("__VP_BIN__", &bin_path_ref);
@@ -478,6 +485,13 @@ end
478485
479486
# Dynamic shell completion for fish
480487
VP_COMPLETE=fish command vp | source
488+
489+
function __vpr_complete
490+
set -l tokens (commandline --current-process --tokenize --cut-at-cursor)
491+
set -l current (commandline --current-token)
492+
VP_COMPLETE=fish command vp -- vp run $tokens[2..] $current
493+
end
494+
complete -c vpr --keep-order --exclusive --arguments "(__vpr_complete)"
481495
"#
482496
.replace("__VP_BIN__", &bin_path_ref);
483497
let env_fish_file = vite_plus_home.join("env.fish");
@@ -518,6 +532,27 @@ function vp {
518532
$env:VP_COMPLETE = "powershell"
519533
& (Join-Path $__vp_bin "vp.exe") | Out-String | Invoke-Expression
520534
Remove-Item Env:\VP_COMPLETE -ErrorAction SilentlyContinue
535+
536+
$__vpr_comp = {
537+
param($wordToComplete, $commandAst, $cursorPosition)
538+
$prev = $env:VP_COMPLETE
539+
$env:VP_COMPLETE = "powershell"
540+
$commandLine = $commandAst.Extent.Text
541+
$args = $commandLine.Substring(0, [math]::Min($cursorPosition, $commandLine.Length))
542+
$args = $args -replace '^(vpr\.exe|vpr)\b', 'vp run'
543+
if ($wordToComplete -eq "") { $args += " ''" }
544+
$results = Invoke-Expression @"
545+
& (Join-Path $__vp_bin 'vp.exe') -- $args
546+
"@;
547+
if ($prev) { $env:VP_COMPLETE = $prev } else { Remove-Item Env:\VP_COMPLETE }
548+
$results | ForEach-Object {
549+
$split = $_.Split("`t")
550+
$cmd = $split[0];
551+
if ($split.Length -eq 2) { $help = $split[1] } else { $help = $split[0] }
552+
[System.Management.Automation.CompletionResult]::new($cmd, $cmd, 'ParameterValue', $help)
553+
}
554+
}
555+
Register-ArgumentCompleter -Native -CommandName vpr -ScriptBlock $__vpr_comp
521556
"#;
522557

523558
// For PowerShell, use the actual absolute path (not $HOME-relative)
@@ -866,7 +901,6 @@ mod tests {
866901
let fish_content = tokio::fs::read_to_string(home.join("env.fish")).await.unwrap();
867902
let ps1_content = tokio::fs::read_to_string(home.join("env.ps1")).await.unwrap();
868903

869-
// Verify completion env is set
870904
assert!(
871905
env_content.contains("VP_COMPLETE=bash") && env_content.contains("VP_COMPLETE=zsh"),
872906
"env file should contain completion for bash and zsh"
@@ -879,5 +913,15 @@ mod tests {
879913
ps1_content.contains("VP_COMPLETE = \"powershell\""),
880914
"env.ps1 file should contain completion for PowerShell"
881915
);
916+
917+
assert!(
918+
env_content.contains("compdef _vpr_complete vpr"),
919+
"env should have vpr completion for zsh"
920+
);
921+
assert!(fish_content.contains("complete -c vpr"), "env.fish should have vpr completion");
922+
assert!(
923+
ps1_content.contains("Register-ArgumentCompleter -Native -CommandName vpr"),
924+
"env.ps1 should have vpr completion"
925+
);
882926
}
883927
}

0 commit comments

Comments
 (0)