Dramatically speed up npm/yarn completions

These go from a minimum of 5s in release mode to instantaneous. Worst case was
more than 30s when we didn't find enough matches to end early.
This commit is contained in:
Mahmoud Al-Qudsi 2024-07-07 14:05:12 -05:00
parent a33d12fe53
commit 11be48fc38

View File

@ -32,8 +32,15 @@ function __npm_filtered_list_packages
# Do not provide any completions if nothing has been entered yet to avoid long hang.
if string match -rq . (commandline -ct)
# Filter the results here rather than in the C++ code due to #5267
all-the-package-names | string match -er -- '(?:\b|_|^)'(commandline -ct |
string escape --style=regex) | head -n1000
# `all-the-package-names` reads a billion line JSON file using node (slow) then prints
# it all (slow) using node (slow). Directly parse the `names.json` file that ships with it instead.
for path in {$HOME/.config/yarn/global,$HOME/.npm-global/lib,/usr/local/lib}/node_modules/all-the-package-names/names.json
test -f $path || continue
# Using `string replace` directly is slow because it doesn't know to stop looking after a match cannot be
# found in the alphabetically sorted list. Use `look` for its binary search support.
look ' "'(commandline -ct) $path | string replace -rf '^ "(.*?)".*' '$1'
break
end
end
end