Skip to content

Commit aed5c4b

Browse files
committed
feat(completion): add support for 'vpr' command completion in bash, zsh, fish, and PowerShell
1 parent c0a7ef1 commit aed5c4b

2 files changed

Lines changed: 45 additions & 0 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/commands/env/setup.rs

Lines changed: 35 additions & 0 deletions
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)

0 commit comments

Comments
 (0)