From 11be48fc38ef3f009f23445f00f1f836e132929d Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sun, 7 Jul 2024 14:05:12 -0500 Subject: [PATCH] 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. --- share/functions/__fish_npm_helper.fish | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/share/functions/__fish_npm_helper.fish b/share/functions/__fish_npm_helper.fish index 6addae48f..ea84dd325 100644 --- a/share/functions/__fish_npm_helper.fish +++ b/share/functions/__fish_npm_helper.fish @@ -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