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

zsh completion fixes #688

Merged
merged 4 commits into from
Mar 25, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions command/completions/_zsh_completions_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,25 @@ function _${replaceSpecialChars(path)}() {` +

// only complete first argument, rest arguments are completed with _arguments.
if (command.hasArguments()) {
const completionsPath: string = path.split(" ").slice(1).join(" ");
const arg: Argument = command.getArguments()[0];
const action = this.addAction(arg, completionsPath);
if (action && command.getCompletion(arg.action)) {
completions += `\n __${
replaceSpecialChars(this.name)
}_complete ${action.arg.name} ${action.arg.action} ${action.cmd}`;
const type = command.getType(arg.type);
let action: ICompletionAction | undefined;

if (type && type.handler instanceof FileType) {
const fileCompletions = this.getFileCompletions(type);
completions += `\n ${fileCompletions}`;
} else {
const completionsPath: string = path.split(" ").slice(1).join(" ");
action = this.addAction(arg, completionsPath);
if (action && command.getCompletion(arg.action)) {
completions += `\n __${
replaceSpecialChars(this.name)
}_complete ${action.arg.name} ${action.arg.action} ${action.cmd}`;
}
}
}

if (completions) {
if (command.hasArguments() || command.hasCommands(false)) {
completions = `\n\n function _commands() {${completions}\n }`;
}

Expand All @@ -154,11 +162,12 @@ function _${replaceSpecialChars(path)}() {` +
if (command.hasCommands(false)) {
const actions: string = command
.getCommands(false)
.map((command: Command) =>
`${command.getName()}) _${
replaceSpecialChars(path + " " + command.getName())
} ;;`
)
.map((command: Command) => {
const aliases = [command.getName(), ...command.getAliases()]
.join("|");
const action = replaceSpecialChars(path + " " + command.getName());
return `${aliases}) _${action} ;;`;
})
.join("\n ");

return `\n
Expand All @@ -185,25 +194,24 @@ function _${replaceSpecialChars(path)}() {` +
argsCommand += ` \\\n ${options.join(" \\\n ")}`;
}

if (
command.hasCommands(false) || (
command.getArguments()
.filter((arg) => command.getCompletion(arg.action)).length
)
) {
argsCommand += ` \\\n '${++argIndex}:command:_commands'`;
}

if (command.hasArguments() || command.hasCommands(false)) {
const commandArgs = command.getArguments();
const isVariadic = commandArgs.at(argIndex)?.variadic;
const selector = isVariadic ? "*" : ++argIndex;

argsCommand += ` \\\n '${selector}:command:_commands'`;
const args: string[] = [];

// first argument is completed together with commands.
for (const arg of command.getArguments().slice(1)) {
for (const arg of commandArgs.slice(1)) {
const type = command.getType(arg.type);

if (type && type.handler instanceof FileType) {
const fileCompletions = this.getFileCompletions(type);

if (arg.variadic) {
argIndex++;

for (let i = 0; i < 5; i++) {
args.push(
`${argIndex + i}${
Expand Down
Loading
Loading