Skip to content

Commit 131fabb

Browse files
committed
fix: fallback to file completion
1 parent 476b1c0 commit 131fabb

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/t.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export class RootCommand extends Command {
140140
commands = new Map<string, Command>();
141141
completions: Completion[] = [];
142142
directive = ShellCompDirective.ShellCompDirectiveDefault;
143+
private isCompletingFlags = false;
143144

144145
constructor() {
145146
super('', '');
@@ -385,7 +386,13 @@ export class RootCommand extends Command {
385386
}
386387

387388
private complete(toComplete: string) {
388-
this.directive = ShellCompDirective.ShellCompDirectiveNoFileComp;
389+
// only disable file completion when completing flags
390+
// allow file fallback for commands/positional args
391+
if (this.isCompletingFlags) {
392+
this.directive = ShellCompDirective.ShellCompDirectiveNoFileComp;
393+
} else {
394+
this.directive = ShellCompDirective.ShellCompDirectiveDefault;
395+
}
389396

390397
const seen = new Set<string>();
391398
this.completions
@@ -431,13 +438,15 @@ export class RootCommand extends Command {
431438
const lastPrevArg = previousArgs[previousArgs.length - 1];
432439

433440
if (this.shouldCompleteFlags(lastPrevArg, toComplete)) {
441+
this.isCompletingFlags = true;
434442
this.handleFlagCompletion(
435443
matchedCommand,
436444
previousArgs,
437445
toComplete,
438446
lastPrevArg
439447
);
440448
} else {
449+
this.isCompletingFlags = false;
441450
if (lastPrevArg?.startsWith('-') && toComplete === '' && endsWithSpace) {
442451
let option = this.findOption(this, lastPrevArg);
443452
if (!option) {
@@ -467,9 +476,9 @@ export class RootCommand extends Command {
467476
setup(name: string, executable: string, shell: string) {
468477
assert(
469478
shell === 'zsh' ||
470-
shell === 'bash' ||
471-
shell === 'fish' ||
472-
shell === 'powershell',
479+
shell === 'bash' ||
480+
shell === 'fish' ||
481+
shell === 'powershell',
473482
'Unsupported shell'
474483
);
475484

0 commit comments

Comments
 (0)