Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: allow default completion to be referenced and modified, in cust…
…om completion (#1878)
  • Loading branch information
jacobator committed Feb 28, 2021
1 parent 00b74ad commit 01619f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/completion.ts
Expand Up @@ -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);
}
Expand Down Expand Up @@ -278,7 +279,7 @@ interface FallbackCompletionFunction {
(
current: string,
argv: Arguments,
defaultCompletion: () => any,
completionFilter: (onCompleted?: CompletionCallback) => any,
done: (completions: string[]) => any
): any;
}
Expand Down
37 changes: 33 additions & 4 deletions test/completion.cjs
Expand Up @@ -341,16 +341,16 @@ 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'])
.command('foo', 'bar')
.command('apple', 'banana')
.completion(
'completion',
(current, argv, defaultCompletion, done) => {
defaultCompletion();
(current, argv, completionFilter, done) => {
completionFilter();
}
)
.parse();
Expand All @@ -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(
() => {
Expand All @@ -373,7 +402,7 @@ describe('Completion', () => {
.command('apple', 'banana')
.completion(
'completion',
(current, argv, defaultCompletion, done) => {
(current, argv, completionFilter, done) => {
done(['orange']);
}
)
Expand Down

0 comments on commit 01619f6

Please sign in to comment.