fix: drop empty suggestions

Signed-off-by: Chapman Pendery <cpendery@vt.edu>
This commit is contained in:
Chapman Pendery
2023-10-07 21:26:33 -07:00
parent 376bbea088
commit fa58576401
2 changed files with 36 additions and 23 deletions
+17 -13
View File
@@ -15,23 +15,27 @@ const getGeneratorContext = (): Fig.GeneratorContext => {
// TODO: add support for caching, trigger, & getQueryTerm
export const runGenerator = async (generator: Fig.Generator, tokens: string[]): Promise<Fig.Suggestion[]> => {
const { script, postProcess, scriptTimeout, splitOn, custom, template } = generator;
const executeShellCommand = buildExecuteShellCommand(scriptTimeout ?? 5000);
const suggestions = [];
if (script) {
const scriptOutput = typeof script === "function" ? script(tokens) : script != null ? await executeShellCommand(script) : "";
if (postProcess) {
suggestions.push(...postProcess(scriptOutput, tokens));
} else if (splitOn) {
suggestions.push(...scriptOutput.split(splitOn).map((s) => ({ name: s })));
try {
if (script) {
const scriptOutput = typeof script === "function" ? script(tokens) : script != null ? await executeShellCommand(script) : "";
if (postProcess) {
suggestions.push(...postProcess(scriptOutput, tokens));
} else if (splitOn) {
suggestions.push(...scriptOutput.split(splitOn).map((s) => ({ name: s })));
}
}
}
if (custom) {
suggestions.push(...(await custom(tokens, executeShellCommand, getGeneratorContext())));
}
if (custom) {
suggestions.push(...(await custom(tokens, executeShellCommand, getGeneratorContext())));
}
if (template != null) {
suggestions.push(...runTemplates(template));
}
if (template != null) {
suggestions.push(...runTemplates(template));
}
return suggestions;
} catch (e) {}
return suggestions;
};
+19 -10
View File
@@ -174,6 +174,10 @@ const removeDuplicateSuggestions = (suggestions: Suggestion[], acceptedTokens: C
return suggestions.filter((s) => s.allNames.every((n) => !seen.has(n)));
};
const removeEmptySuggestion = (suggestions: Suggestion[]): Suggestion[] => {
return suggestions.filter((s) => s.name.length > 0);
};
export const getSubcommandDrivenRecommendation = async (
subcommand: Fig.Subcommand,
persistentOptions: Fig.Option[],
@@ -188,21 +192,24 @@ export const getSubcommandDrivenRecommendation = async (
const suggestions: Suggestion[] = [];
const argLength = subcommand.args instanceof Array ? subcommand.args.length : subcommand.args ? 1 : 0;
const allOptions = persistentOptions.concat(subcommand.options ?? []);
if (!argsFromSubcommand) {
suggestions.push(...subcommandSuggestions(subcommand.subcommands, subcommand.filterStrategy, partialCmd));
suggestions.push(...optionSuggestions(allOptions, acceptedTokens, subcommand.filterStrategy, partialCmd));
}
if (argLength != 0) {
const activeArg = subcommand.args instanceof Array ? subcommand.args[0] : subcommand.args;
suggestions.push(...(await generatorSuggestions(activeArg?.generators, acceptedTokens, activeArg?.filterStrategy, partialCmd)));
suggestions.push(...suggestionSuggestions(activeArg?.suggestions, activeArg?.filterStrategy, partialCmd));
suggestions.push(...templateSuggestions(activeArg?.template, activeArg?.filterStrategy, partialCmd));
}
if (!argsFromSubcommand) {
suggestions.push(...optionSuggestions(allOptions, acceptedTokens, subcommand.filterStrategy, partialCmd));
suggestions.push(...subcommandSuggestions(subcommand.subcommands, subcommand.filterStrategy, partialCmd));
}
return {
suggestions: removeDuplicateSuggestions(
suggestions.sort((a, b) => b.priority - a.priority),
acceptedTokens
suggestions: removeEmptySuggestion(
removeDuplicateSuggestions(
suggestions.sort((a, b) => b.priority - a.priority),
acceptedTokens
)
),
};
};
@@ -229,9 +236,11 @@ export const getArgDrivenRecommendation = async (
}
return {
suggestions: removeDuplicateSuggestions(
suggestions.sort((a, b) => b.priority - a.priority),
acceptedTokens
suggestions: removeEmptySuggestion(
removeDuplicateSuggestions(
suggestions.sort((a, b) => b.priority - a.priority),
acceptedTokens
)
),
argumentDescription: activeArg.description ?? activeArg.name,
};