From 2a1e908b17b92f7ea69149beefc44b629daad31a Mon Sep 17 00:00:00 2001 From: AmirHossein Sakhravi Date: Sat, 12 Apr 2025 11:23:32 +0330 Subject: [PATCH 1/2] fix root cmd completions --- src/index.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 35d9b47..c9137c1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -337,14 +337,31 @@ export class Completion { previousArgs: string[], toComplete: string ) { - const commandParts = [...previousArgs]; + const commandParts = [...previousArgs].filter(arg => arg !== ''); + + // When we have no real command parts (just empty or showing root commands) + const isRootCompletion = commandParts.length === 0; for (const [k, command] of this.commands) { if (k === '') continue; + const parts = k.split(' '); - let match = true; + // For root completion, just match the first part of each command + if (isRootCompletion) { + if (parts[0].startsWith(toComplete)) { + this.completions.push({ + value: parts[0], + description: command.description, + }); + } + continue; + } + + // For subcommand completion, match the parts against commandParts + let match = true; let i = 0; + while (i < commandParts.length) { if (parts[i] !== commandParts[i]) { match = false; @@ -352,6 +369,7 @@ export class Completion { } i++; } + if (match && parts[i]?.startsWith(toComplete)) { this.completions.push({ value: parts[i], From 628b546e2d3108729bc69bbf9922e8d3cbd7ca35 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Mon, 28 Apr 2025 23:48:44 +0330 Subject: [PATCH 2/2] update --- src/index.ts | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index c9137c1..5e0286a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -337,28 +337,13 @@ export class Completion { previousArgs: string[], toComplete: string ) { - const commandParts = [...previousArgs].filter(arg => arg !== ''); - - // When we have no real command parts (just empty or showing root commands) - const isRootCompletion = commandParts.length === 0; + const commandParts = [...previousArgs].filter(Boolean); for (const [k, command] of this.commands) { if (k === '') continue; const parts = k.split(' '); - // For root completion, just match the first part of each command - if (isRootCompletion) { - if (parts[0].startsWith(toComplete)) { - this.completions.push({ - value: parts[0], - description: command.description, - }); - } - continue; - } - - // For subcommand completion, match the parts against commandParts let match = true; let i = 0;