From b070dc07a4cd75c1f2ddbb70816bebc30aa23769 Mon Sep 17 00:00:00 2001 From: Anatolii Sieryi Date: Fri, 26 Feb 2021 16:57:43 +0200 Subject: [PATCH 1/2] feat: allow to access output of default completion in custom completion --- lib/completion.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/completion.ts b/lib/completion.ts index 207028831..e6e8634e3 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -193,7 +193,8 @@ export class Completion implements CompletionInstance { return (this.customCompletionFunction as FallbackCompletionFunction)( current, argv, - () => this.defaultCompletion(args, argv, current, done), + (onCompleted = done) => + this.defaultCompletion(args, argv, current, onCompleted), completions => { done(null, completions); } @@ -278,7 +279,7 @@ interface FallbackCompletionFunction { ( current: string, argv: Arguments, - defaultCompletion: () => any, + defaultCompletion: (onCompleted?: CompletionCallback) => any, done: (completions: string[]) => any ): any; } From 3b4f9cdd1d3fec33fb564e43d873701539c6544d Mon Sep 17 00:00:00 2001 From: Anatolii Sieryi Date: Sun, 28 Feb 2021 22:25:30 +0200 Subject: [PATCH 2/2] chore: rename defaultCompletion -> completionFilter, add test --- lib/completion.ts | 2 +- test/completion.cjs | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/completion.ts b/lib/completion.ts index e6e8634e3..1a9075696 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -279,7 +279,7 @@ interface FallbackCompletionFunction { ( current: string, argv: Arguments, - defaultCompletion: (onCompleted?: CompletionCallback) => any, + completionFilter: (onCompleted?: CompletionCallback) => any, done: (completions: string[]) => any ): any; } diff --git a/test/completion.cjs b/test/completion.cjs index 9fc774bd3..10b90661c 100644 --- a/test/completion.cjs +++ b/test/completion.cjs @@ -341,7 +341,7 @@ describe('Completion', () => { r.logs.should.include('success!'); }); - it('allows the custom completion function to use the standard one', done => { + it('allows the custom completion function to use the default completion w/o filter', done => { checkUsage( () => { yargs(['./completion', '--get-yargs-completions']) @@ -349,8 +349,8 @@ describe('Completion', () => { .command('apple', 'banana') .completion( 'completion', - (current, argv, defaultCompletion, done) => { - defaultCompletion(); + (current, argv, completionFilter, done) => { + completionFilter(); } ) .parse(); @@ -365,6 +365,35 @@ describe('Completion', () => { ); }); + it('allows custom completion to be combined with default completion, using filter', done => { + checkUsage( + () => { + yargs(['./completion', '--get-yargs-completions']) + .command('foo', 'bar') + .command('apple', 'banana') + .completion( + 'completion', + (current, argv, completionFilter, done) => { + completionFilter((err, completions) => { + const filteredCompletions = completions.filter( + completion => completion === 'foo' + ); + done(filteredCompletions); + }); + } + ) + .parse(); + }, + null, + (err, r) => { + if (err) throw err; + r.logs.should.include('foo'); + r.logs.should.not.include('apple'); + return done(); + } + ); + }); + it('allows calling callback instead of default completion function', done => { checkUsage( () => { @@ -373,7 +402,7 @@ describe('Completion', () => { .command('apple', 'banana') .completion( 'completion', - (current, argv, defaultCompletion, done) => { + (current, argv, completionFilter, done) => { done(['orange']); } )