Skip to content

Commit bc59418

Browse files
committed
update
1 parent 6c62f08 commit bc59418

6 files changed

Lines changed: 452 additions & 411 deletions

File tree

.github/workflows/shell-argv-contract.yml

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Shell Completions
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
shell-argv-protocol:
13+
name: Shell argv protocol
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Install shell dependencies
21+
shell: bash
22+
run: |
23+
sudo apt-get update
24+
sudo apt-get install -y zsh fish wget apt-transport-https software-properties-common
25+
26+
- name: Install PowerShell
27+
shell: bash
28+
run: |
29+
source /etc/os-release
30+
wget -q https://packages.microsoft.com/config/ubuntu/$VERSION_ID/packages-microsoft-prod.deb
31+
sudo dpkg -i packages-microsoft-prod.deb
32+
rm packages-microsoft-prod.deb
33+
sudo apt-get update
34+
sudo apt-get install -y powershell
35+
36+
- name: Install pnpm
37+
uses: pnpm/action-setup@v4.0.0
38+
39+
- name: Set node version to 20
40+
uses: actions/setup-node@v4
41+
with:
42+
node-version: 20
43+
registry-url: https://registry.npmjs.org/
44+
cache: pnpm
45+
46+
- name: Install deps
47+
run: pnpm install
48+
49+
- name: Print shell versions
50+
shell: bash
51+
run: |
52+
bash --version
53+
zsh --version
54+
fish --version
55+
pwsh --version
56+
57+
- name: Run shell argv protocol tests
58+
run: pnpm test tests/shell-empty-argv.test.ts

examples/demo.t.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,27 @@ t.command('lint', 'Lint project').argument(
120120
true
121121
); // Variadic argument for multiple files
122122

123+
const supportedShells = ['zsh', 'bash', 'fish', 'powershell'];
124+
const completeUsage =
125+
'ERROR: Usage: vite complete <shell> | vite complete -- <argv>';
126+
127+
function printCompleteUsageAndExit() {
128+
console.error(completeUsage);
129+
process.exit(1);
130+
}
131+
123132
// Handle completion command
124133
if (process.argv[2] === 'complete') {
125-
const shell = process.argv[3];
126-
if (shell && ['zsh', 'bash', 'fish', 'powershell'].includes(shell)) {
127-
t.setup('vite', 'pnpm tsx examples/demo.t.ts', shell);
134+
const mode = process.argv[3];
135+
136+
if (mode === '--') {
137+
// Runtime completion request from the generated shell script.
138+
t.parse(process.argv.slice(4));
139+
} else if (mode && supportedShells.includes(mode)) {
140+
// Shell script generation.
141+
t.setup('vite', 'pnpm tsx examples/demo.t.ts', mode);
128142
} else {
129-
// Parse completion arguments (everything after --)
130-
const separatorIndex = process.argv.indexOf('--');
131-
const completionArgs =
132-
separatorIndex !== -1 ? process.argv.slice(separatorIndex + 1) : [];
133-
t.parse(completionArgs);
143+
printCompleteUsageAndExit();
134144
}
135145
} else {
136146
// Regular CLI usage (just show help for demo)

src/zsh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ _${name}() {
4848
4949
# Prepare the command to obtain completions, ensuring arguments are quoted for eval
5050
local -a args_to_quote=("\${(@)words[2,-1]}")
51-
if [ "\${lastChar}" = "" ]; then
51+
if [ "\${lastChar}" = "" ] && [ "\${args_to_quote[-1]}" != "" ]; then
5252
# If the last parameter is complete (there is a space following it)
5353
# We add an extra empty parameter so we can indicate this to the go completion code.
5454
__${name}_debug "Adding extra empty parameter"

0 commit comments

Comments
 (0)