-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
_fish_completions_generator.ts
185 lines (161 loc) 路 5.02 KB
/
_fish_completions_generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { MissingCommandNameCompletionsError } from "../_errors.ts";
import { getDescription } from "../_utils.ts";
import type { Command } from "../command.ts";
import type { Argument, Option } from "../types.ts";
import { FileType } from "../types/file.ts";
/** Generates fish completions script. */
interface CompleteOptions {
description?: string;
shortOption?: string;
longOption?: string;
required?: boolean;
standalone?: boolean;
arguments?: string;
}
/** Fish completions generator. */
export class FishCompletionsGenerator {
/** Generates fish completions script for given command. */
public static generate(name: string, cmd: Command) {
if (!name || name === "COMMAND") {
throw new MissingCommandNameCompletionsError("fish");
}
return new FishCompletionsGenerator(name, cmd).generate();
}
private constructor(
protected name: string,
protected cmd: Command,
) {}
/** Generates fish completions script. */
private generate(): string {
const path = this.cmd.getPath(this.name);
const version: string | undefined = this.cmd.getVersion()
? ` v${this.cmd.getVersion()}`
: "";
return `#!/usr/bin/env fish
# fish completion support for ${path}${version}
function __fish_${replaceSpecialChars(this.name)}_using_command
set -l cmds ${getCommandFnNames(this.name, this.cmd).join(" ")}
set -l words (commandline -opc)
set -l cmd "_"
for word in $words
switch $word
case '-*'
continue
case '*'
set word (string replace -r -a '\\W' '_' $word)
set -l cmd_tmp $cmd"_$word"
if contains $cmd_tmp $cmds
set cmd $cmd_tmp
end
end
end
if test "$cmd" = "$argv[1]"
return 0
end
return 1
end
${this.generateCompletions(this.name, this.cmd).trim()}`;
}
private generateCompletions(name: string, command: Command): string {
const parent: Command | undefined = command.getParent();
let result = ``;
if (parent) {
// command
result += "\n" + this.complete(parent, {
description: command.getShortDescription(),
arguments: name,
});
}
// arguments
const commandArgs = command.getArguments();
if (commandArgs.length) {
result += "\n" + this.complete(command, {
arguments: commandArgs.length
? this.getCompletionCommand(command, commandArgs[0])
: undefined,
});
}
// options
for (const option of command.getOptions(false)) {
result += "\n" + this.completeOption(command, option);
}
for (const subCommand of command.getCommands(false)) {
result += this.generateCompletions(subCommand.getName(), subCommand);
}
return result;
}
private completeOption(command: Command, option: Option) {
const shortOption: string | undefined = option.flags
.find((flag) => flag.length === 2)
?.replace(/^(-)+/, "");
const longOption: string | undefined = option.flags
.find((flag) => flag.length > 2)
?.replace(/^(-)+/, "");
return this.complete(command, {
description: getDescription(option.description),
shortOption: shortOption,
longOption: longOption,
// required: option.requiredValue,
required: true,
standalone: option.standalone,
arguments: option.args.length
? this.getCompletionCommand(command, option.args[0])
: undefined,
});
}
private complete(command: Command, options: CompleteOptions) {
const cmd = ["complete"];
cmd.push("-c", this.name);
cmd.push(
"-n",
`'__fish_${replaceSpecialChars(this.name)}_using_command __${
replaceSpecialChars(command.getPath(this.name))
}'`,
);
options.shortOption && cmd.push("-s", options.shortOption);
options.longOption && cmd.push("-l", options.longOption);
options.standalone && cmd.push("-x");
cmd.push("-k");
cmd.push("-f");
if (options.arguments) {
options.required && cmd.push("-r");
cmd.push("-a", options.arguments);
}
if (options.description) {
const description: string = getDescription(options.description, true)
// escape single quotes
.replace(/'/g, "\\'");
cmd.push("-d", `'${description}'`);
}
return cmd.join(" ");
}
private getCompletionCommand(cmd: Command, arg: Argument): string {
const type = cmd.getType(arg.type);
if (type && type.handler instanceof FileType) {
return `'(__fish_complete_path)'`;
}
return `'(${this.name} completions complete ${
arg.action + " " + getCompletionsPath(cmd)
})'`;
}
}
function getCommandFnNames(
name: string,
cmd: Command,
cmds: Array<string> = [],
): Array<string> {
cmds.push(`__${replaceSpecialChars(cmd.getPath(name))}`);
cmd.getCommands(false).forEach((command) => {
getCommandFnNames(name, command, cmds);
});
return cmds;
}
function getCompletionsPath(command: Command): string {
return command.getPath()
.split(" ")
.slice(1)
.join(" ");
}
function replaceSpecialChars(str: string): string {
return str.replace(/[^a-zA-Z0-9]/g, "_");
}