Skip to content

Commit

Permalink
add token argument to ICompleteHandler
Browse files Browse the repository at this point in the history
Sometimes you want to provide completion alternatives based on the
commandline buffer. This adds `token` (string) to both option- and
argument completion, containing the partially written word to complete.
  • Loading branch information
marcushultman committed Jul 3, 2021
1 parent f3103b9 commit 5515dc7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
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
17 changes: 13 additions & 4 deletions command/completions/_fish_completions_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ ${this.generateCompletions(this.cmd).trim()}
if (commandArgs.length) {
result += "\n" + this.complete(command, {
arguments: commandArgs.length
? this.getCompletionCommand(
? this.getArgumentCompletionCommand(
commandArgs[0].action + " " + getCompletionsPath(command),
)
: undefined,
Expand Down Expand Up @@ -108,7 +108,9 @@ ${this.generateCompletions(this.cmd).trim()}
required: true,
standalone: option.standalone,
arguments: option.args.length
? this.getCompletionCommand(
? this.getOptionCompletionCommand(
shortOption,
longOption,
option.args[0].action + " " + getCompletionsPath(command),
)
: undefined,
Expand Down Expand Up @@ -137,8 +139,15 @@ ${this.generateCompletions(this.cmd).trim()}
return cmd.join(" ");
}

private getCompletionCommand(cmd: string): string {
return `'(${this.cmd.getName()} completions complete ${cmd.trim()})'`;
private getArgumentCompletionCommand(cmd: string): string {
const token = `commandline -tc`;
return `'(${this.cmd.getName()} completions complete -t (${token}) ${cmd.trim()})'`;
}

private getOptionCompletionCommand(shortOption: string | undefined, longOption: string | undefined, cmd: string): string {
const pipeReplace = (prefix: string, opt?: string) => opt ? `| string replace -- ${prefix}${opt} ""` : '';
const token = `commandline -tc ${pipeReplace('-', shortOption)} ${pipeReplace('--', longOption)}`;
return `'(${this.cmd.getName()} completions complete -t (${token}) ${cmd.trim()})'`;
}
}

Expand Down
7 changes: 4 additions & 3 deletions command/completions/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ 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>]> {
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>) => {
.action(async ({ token = '' }, action: string, commandNames?: Array<string>) => {
let parent: Command | undefined;
const completeCommand: Command = commandNames
?.reduce((cmd: Command, name: string): Command => {
Expand All @@ -24,7 +25,7 @@ export class CompleteCommand
const completion: ICompletion | undefined = completeCommand
.getCompletion(action);
const result: Array<string | number> =
await completion?.complete(completeCommand, parent) ?? [];
await completion?.complete(token, completeCommand, parent) ?? [];

if (result?.length) {
Deno.stdout.writeSync(new TextEncoder().encode(result.join("\n")));
Expand Down
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
2 changes: 1 addition & 1 deletion command/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ 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
2 changes: 1 addition & 1 deletion command/types/child_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ChildCommandType extends StringType {
}

/** Complete child command names. */
public complete(cmd: Command): string[] {
public complete(_token: string, cmd: Command): string[] {
return (this.#cmd ?? cmd)?.getCommands(false)
.map((cmd: Command) => cmd.getName()) || [];
}
Expand Down
2 changes: 1 addition & 1 deletion command/types/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StringType } from "./string.ts";
/** String type with auto completion of sibling command names. */
export class CommandType extends StringType {
/** Complete sub-command names of global parent command. */
public complete(_cmd: Command, parent?: Command): string[] {
public complete(_token: string, _cmd: Command, parent?: Command): string[] {
return parent?.getCommands(false)
.map((cmd: Command) => cmd.getName()) || [];
}
Expand Down

0 comments on commit 5515dc7

Please sign in to comment.