diff --git a/src/runtime/generator.ts b/src/runtime/generator.ts index d634dcb..a4996bf 100644 --- a/src/runtime/generator.ts +++ b/src/runtime/generator.ts @@ -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 => { 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; }; diff --git a/src/runtime/suggestion.ts b/src/runtime/suggestion.ts index a619a4b..26eec85 100644 --- a/src/runtime/suggestion.ts +++ b/src/runtime/suggestion.ts @@ -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, };