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

[draft] add token argument to ICompleteHandler #238

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,10 @@ export class Command<
typeof handler.values !== "undefined")
) {
const completeHandler: ICompleteHandler = (
token: string,
cmd: Command,
parent?: Command,
) => handler.complete?.(cmd, parent) || [];
) => handler.complete?.(token, cmd, parent) || [];
this.complete(name, completeHandler, options);
}

Expand Down
26 changes: 23 additions & 3 deletions command/completions/_fish_completions_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ ${this.generateCompletions(this.cmd).trim()}
required: true,
standalone: option.standalone,
arguments: option.args.length
? this.getCompletionCommand(
? this.getOptionCompletionCommand(
option.args[0].action + " " + getCompletionsPath(command),
shortOption,
longOption,
)
: undefined,
});
Expand Down Expand Up @@ -137,8 +139,26 @@ ${this.generateCompletions(this.cmd).trim()}
return cmd.join(" ");
}

private getCompletionCommand(cmd: string): string {
return `'(${this.cmd.getName()} completions complete ${cmd.trim()})'`;
private getOptionCompletionCommand(
cmd: string,
shortOption: string | undefined,
longOption: string | undefined,
): string {
const makeFilter = (s: string) => `string replace -- ${s} ""`;
const filters = [];
shortOption && filters.push(makeFilter(`-${shortOption}`));
longOption && filters.push(makeFilter(`--${longOption}`));
return this.getCompletionCommand(cmd, filters);
}

private getCompletionCommand(cmd: string, filters: string[] = []): string {
const cmds = [
"commandline -tc",
...filters,
`string replace -r -- "^\-*" ""`,
];
const token = cmds.join(" | ");
return `'(${this.cmd.getName()} completions complete -t (${token}) ${cmd.trim()})'`;
}
}

Expand Down
51 changes: 30 additions & 21 deletions command/completions/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,42 @@ import { UnknownCompletionCommand } from "../_errors.ts";
import type { ICompletion } from "../types.ts";

/** Execute auto completion method of command and action. */
export class CompleteCommand
extends Command<void, [action: string, commandNames?: Array<string>]> {
export class CompleteCommand extends Command<
{ token: string },
[action: string, commandNames?: Array<string>]
> {
public constructor(cmd?: Command) {
super();
this.description("Get completions for given action from given command.")
.option("-t, --token [t:string]", "the current cmd token.")
.arguments("<action:string> [command...:string]")
.action(async (_, action: string, commandNames?: Array<string>) => {
let parent: Command | undefined;
const completeCommand: Command = commandNames
?.reduce((cmd: Command, name: string): Command => {
parent = cmd;
const childCmd: Command | undefined = cmd.getCommand(name, false);
if (!childCmd) {
throw new UnknownCompletionCommand(name, cmd.getCommands());
}
return childCmd;
}, cmd || this.getMainCommand()) ?? (cmd || this.getMainCommand());
.action(
async (
{ token = "" },
action: string,
commandNames?: Array<string>,
) => {
let parent: Command | undefined;
const completeCommand: Command = commandNames
?.reduce((cmd: Command, name: string): Command => {
parent = cmd;
const childCmd: Command | undefined = cmd.getCommand(name, false);
if (!childCmd) {
throw new UnknownCompletionCommand(name, cmd.getCommands());
}
return childCmd;
}, cmd || this.getMainCommand()) ?? (cmd || this.getMainCommand());

const completion: ICompletion | undefined = completeCommand
.getCompletion(action);
const result: Array<string | number> =
await completion?.complete(completeCommand, parent) ?? [];
const completion: ICompletion | undefined = completeCommand
.getCompletion(action);
const result: Array<string | number> =
await completion?.complete(token, completeCommand, parent) ?? [];

if (result?.length) {
Deno.stdout.writeSync(new TextEncoder().encode(result.join("\n")));
}
})
if (result?.length) {
Deno.stdout.writeSync(new TextEncoder().encode(result.join("\n")));
}
},
)
.reset();
}
}
46 changes: 23 additions & 23 deletions command/test/integration/fixtures/completions_generate_fish.out
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,54 @@ end

complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s V -l version -x -k -f -d 'Show the version number for this program.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s g -l global -k -f -r -a '(completions-test completions complete boolean)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s m -l main -k -f -r -a '(completions-test completions complete boolean)' -d 'Bar option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s c -l color -k -f -r -a '(completions-test completions complete color)' -d 'Color option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s m -l main -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -m "" | string replace -- --main "" | string replace -r -- "^-*" "") boolean)' -d 'Bar option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s c -l color -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -c "" | string replace -- --color "" | string replace -r -- "^-*" "") color)' -d 'Color option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a foo -d 'Foo command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo' -s g -l global -k -f -r -a '(completions-test completions complete boolean foo)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean foo)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo' -s f -l foo -k -f -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a help -d 'Show this help or the help of a sub-command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete -t (commandline -tc | string replace -r -- "^-*" "") command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo' -k -f -a bar -d 'Bar command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo_bar' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo_bar' -s g -l global -k -f -r -a '(completions-test completions complete boolean foo bar)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo_bar' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean foo bar)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo_bar' -s b -l bar -k -f -d 'Bar option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a help -d 'Show this help or the help of a sub-command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete -t (commandline -tc | string replace -r -- "^-*" "") command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a help -d 'Show this help or the help of a sub-command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete -t (commandline -tc | string replace -r -- "^-*" "") command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a completions -d 'Generate shell completions.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions' -s g -l global -k -f -r -a '(completions-test completions complete boolean completions)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean completions)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a help -d 'Show this help or the help of a sub-command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete -t (commandline -tc | string replace -r -- "^-*" "") command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions' -k -f -a bash -d 'Generate shell completions for bash.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions_bash' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions_bash' -s g -l global -k -f -r -a '(completions-test completions complete boolean completions bash)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions_bash' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean completions bash)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a help -d 'Show this help or the help of a sub-command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete -t (commandline -tc | string replace -r -- "^-*" "") command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions' -k -f -a fish -d 'Generate shell completions for fish.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions_fish' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions_fish' -s g -l global -k -f -r -a '(completions-test completions complete boolean completions fish)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions_fish' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean completions fish)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a help -d 'Show this help or the help of a sub-command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete -t (commandline -tc | string replace -r -- "^-*" "") command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions' -k -f -a zsh -d 'Generate shell completions for zsh.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions_zsh' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions_zsh' -s g -l global -k -f -r -a '(completions-test completions complete boolean completions zsh)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_completions_zsh' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean completions zsh)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a help -d 'Show this help or the help of a sub-command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete -t (commandline -tc | string replace -r -- "^-*" "") command help)'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete boolean help)' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s g -l global -k -f -r -a '(completions-test completions complete -t (commandline -tc | string replace -- -g "" | string replace -- --global "" | string replace -r -- "^-*" "") boolean help)' -d 'Foo option.'
1 change: 1 addition & 0 deletions command/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export abstract class Type<T> {
* values from the values method are used.
*/
public complete?(
token: string,
// deno-lint-ignore no-explicit-any
cmd: Command<any, any, any, any, any>,
// deno-lint-ignore no-explicit-any
Expand Down
6 changes: 5 additions & 1 deletion command/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ export type ICompleteHandler<
PG extends Record<string, any> | void = any,
// deno-lint-ignore no-explicit-any
P extends Command | undefined = any,
> = (cmd: Command<O, A, G, PG, P>, parent?: Command) => CompleteHandlerResult;
> = (
token: string,
cmd: Command<O, A, G, PG, P>,
parent?: Command,
) => CompleteHandlerResult;

/** Help callback method to print the help. Invoked by the `--help` option and `help` command and the `.getHelp()` and `.showHelp()` method's. */
export type IHelpHandler<
Expand Down
Loading