First of all, thanks for this awesome project, which has given me a lot of inspiration in the areas of terminal experience and smart completion. I have recently been trying to integrate it with Xterm.js. However, I encountered some issues while using it and reading the code.
What is the problem ?
When I type npm run, the terminal autocomplete returns the incorrect result "--registry", instead of the scripts in package.json or other run parameters.
This behavior is somewhat strange, so I delved into the source code to look for the issue.
After spending some time debugging, the problem was finally located in the genSubCommand method of the runtime.ts file.
Here, we can see that when typing npm run, function genSubCommand attempting to find s.name === 'run'within ·parentCommand.subcommands· is unsuccessful, returning subcommandIdx is -1.
However, the judgment logic in the code is SubCommand == null, which obviously does not apply when subCommand is -1. As a result, it proceeds to parentCommand.subcommands.at(-1), effectively retrieving the last item in the subcommands list (the 69th item). Therefore, the output becomes --registry instead of the correct result.
How to Fix
By inspecting the contents of parentCommand.subcommands through the Debugger, it can be observed that the name of a subCommand might be an array of strings. And the type of subCommand also prove this.
Therefore, we need to fix the find matching logic for subCommands to handle both arrays and strings during the match.
const subcommandIdx = parentCommand.subcommands?.findIndex((s) => {
// subCommand.name can be a string or an array of strings
if (Array.isArray(s.name)) {
// if it's an array, we check if the command is included in the array
return s.name.includes(command);
} else {
return s.name === command;
}
});
Additionally, we also need to optimize the verification logic before and after the find match to handle exceptional cases.
if (!parentCommand.subcommands || parentCommand.subcommands.length === 0) return;
// ... find logic
if (subcommandIdx === -1) return;
First of all, thanks for this awesome project, which has given me a lot of inspiration in the areas of terminal experience and smart completion. I have recently been trying to integrate it with Xterm.js. However, I encountered some issues while using it and reading the code.
What is the problem ?
When I type
npm run
, the terminal autocomplete returns the incorrect result "--registry", instead of the scripts in package.json or other run parameters.This behavior is somewhat strange, so I delved into the source code to look for the issue.
After spending some time debugging, the problem was finally located in the
genSubCommand
method of theruntime.ts
file.Here, we can see that when typing
npm run
, functiongenSubCommand
attempting to finds.name === 'run'
within ·parentCommand.subcommands· is unsuccessful, returningsubcommandIdx
is-1
.However, the judgment logic in the code is
SubCommand == null
, which obviously does not apply whensubCommand
is -1. As a result, it proceeds toparentCommand.subcommands.at(-1)
, effectively retrieving the last item in thesubcommands
list (the 69th item). Therefore, the output becomes--registry
instead of the correct result.How to Fix
By inspecting the contents of
parentCommand.subcommands
through the Debugger, it can be observed that thename
of asubCommand
might be an array of strings. And the type of subCommand also prove this.Therefore, we need to fix the find matching logic for
subCommands
to handle both arrays and strings during the match.Additionally, we also need to optimize the verification logic before and after the find match to handle exceptional cases.
After fix
After fix,
npm run
looks great !