Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-lamps-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bomb.sh/tab': patch
---

prevent duplicate empty args in generated completions
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: pnpm install

- name: Run tests
run: pnpm test
run: pnpm test:unit

typecheck:
name: Lint and Type Check
Expand Down
58 changes: 58 additions & 0 deletions .github/workflows/shell-completions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Shell Completions

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
shell-argv-protocol:
name: Shell argv protocol
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install shell dependencies
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y zsh fish wget apt-transport-https software-properties-common

- name: Install PowerShell
shell: bash
run: |
source /etc/os-release
wget -q https://packages.microsoft.com/config/ubuntu/$VERSION_ID/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install -y powershell

- name: Install pnpm
uses: pnpm/action-setup@v4.0.0

- name: Set node version to 20
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
cache: pnpm

- name: Install deps
run: pnpm install

- name: Print shell versions
shell: bash
run: |
bash --version
zsh --version
fish --version
pwsh --version

- name: Run shell argv protocol tests
run: pnpm test tests/shell-empty-argv.test.ts
25 changes: 17 additions & 8 deletions examples/demo.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,26 @@ t.command('lint', 'Lint project').argument(
true
); // Variadic argument for multiple files

const supportedShells = ['zsh', 'bash', 'fish', 'powershell'];
const completeUsage = 'Usage: vite complete <shell> | vite complete -- <argv>';

function printCompleteUsageAndExit() {
console.error(completeUsage);
process.exit(1);
}

// Handle completion command
if (process.argv[2] === 'complete') {
const shell = process.argv[3];
if (shell && ['zsh', 'bash', 'fish', 'powershell'].includes(shell)) {
t.setup('vite', 'pnpm tsx examples/demo.t.ts', shell);
const mode = process.argv[3];

if (mode === '--') {
// Runtime completion request from the generated shell script.
t.parse(process.argv.slice(4));
} else if (mode && supportedShells.includes(mode)) {
// Shell script generation.
t.setup('vite', 'pnpm tsx examples/demo.t.ts', mode);
} else {
// Parse completion arguments (everything after --)
const separatorIndex = process.argv.indexOf('--');
const completionArgs =
separatorIndex !== -1 ? process.argv.slice(separatorIndex + 1) : [];
t.parse(completionArgs);
printCompleteUsageAndExit();
}
} else {
// Regular CLI usage (just show help for demo)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
},
"scripts": {
"test": "vitest run",
"test:unit": "vitest run --exclude tests/shell-empty-argv.test.ts",
"test:shell": "vitest run tests/shell-empty-argv.test.ts",
"type-check": "tsc --noEmit",
"format": "prettier --write .",
"format:check": "prettier --check .",
Expand Down
6 changes: 4 additions & 2 deletions src/powershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ export function generate(name: string, exec: string): string {
# Remove the flag part
$Flag, $WordToComplete = $WordToComplete.Split("=", 2)
}

if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) {
$HasTrailingEmptyArg = $QuotedArgs -match "(^| )''$"
__${name}_debug "HasTrailingEmptyArg: $HasTrailingEmptyArg"

if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag ) -And ( -Not $HasTrailingEmptyArg )) {
# If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method.
__${name}_debug "Adding extra empty parameter"
Expand Down
2 changes: 1 addition & 1 deletion src/zsh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ _${name}() {

# Prepare the command to obtain completions, ensuring arguments are quoted for eval
local -a args_to_quote=("\${(@)words[2,-1]}")
if [ "\${lastChar}" = "" ]; then
if [ "\${lastChar}" = "" ] && [ "\${args_to_quote[-1]}" != "" ]; then
# If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go completion code.
__${name}_debug "Adding extra empty parameter"
Expand Down
Loading
Loading