Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to access output of default completion in custom completion #1878

Merged
merged 2 commits into from Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a test:

it('allows custom completion to be combined with default completion, using filter').

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