Skip to content

Commit 2a1e908

Browse files
committed
fix root cmd completions
1 parent 933535a commit 2a1e908

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,21 +337,39 @@ export class Completion {
337337
previousArgs: string[],
338338
toComplete: string
339339
) {
340-
const commandParts = [...previousArgs];
340+
const commandParts = [...previousArgs].filter(arg => arg !== '');
341+
342+
// When we have no real command parts (just empty or showing root commands)
343+
const isRootCompletion = commandParts.length === 0;
341344

342345
for (const [k, command] of this.commands) {
343346
if (k === '') continue;
347+
344348
const parts = k.split(' ');
345-
let match = true;
346349

350+
// For root completion, just match the first part of each command
351+
if (isRootCompletion) {
352+
if (parts[0].startsWith(toComplete)) {
353+
this.completions.push({
354+
value: parts[0],
355+
description: command.description,
356+
});
357+
}
358+
continue;
359+
}
360+
361+
// For subcommand completion, match the parts against commandParts
362+
let match = true;
347363
let i = 0;
364+
348365
while (i < commandParts.length) {
349366
if (parts[i] !== commandParts[i]) {
350367
match = false;
351368
break;
352369
}
353370
i++;
354371
}
372+
355373
if (match && parts[i]?.startsWith(toComplete)) {
356374
this.completions.push({
357375
value: parts[i],

0 commit comments

Comments
 (0)