From 128887703301f795fda405aeb7b06ffab630641c Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Fri, 19 Oct 2018 17:45:02 -0500 Subject: [PATCH] Update yarn completions Don't attempt to complete against package names if the user is trying to enter a switch to speed things up. Also work around #5267 by not wrapping unfiltered `all-the-package-name` calls in a function. --- share/completions/yarn.fish | 22 +++++++++++++++++----- share/functions/__fish_is_switch.fish | 4 ++++ 2 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 share/functions/__fish_is_switch.fish diff --git a/share/completions/yarn.fish b/share/completions/yarn.fish index 70e4acd2a..e9afc3a55 100644 --- a/share/completions/yarn.fish +++ b/share/completions/yarn.fish @@ -12,10 +12,18 @@ function __yarn_list_packages all-the-package-names end -# Entire list of packages is too long to be used in a `complete` subcommand -# Search it for matches instead +# Entire list of packages is too long to be used efficiently in a `complete` subcommand. +# Search it for matches instead. function __yarn_filtered_list_packages - __yarn_list_packages | grep (commandline -ct) | head -n 50 + # We used to avoid the duplication of this check by calling __yarn_list_packages + # instead of all-the-package-names directly below, but a) that breaks IO buffering + # because the output of all-the-package-names is > 10 MiB (#5267), and b) IO + # buffering slowed down the call considerably in all cases. + if not type -q all-the-package-names + return + end + + all-the-package-names | string match -e -- (commandline -ct) end function __yarn_find_package_json @@ -68,8 +76,12 @@ function __yarn_installed_packages end -complete -f -c yarn -n '__fish_seen_subcommand_from remove' -xa '(__yarn_installed_packages)' -complete -f -c yarn -n '__fish_seen_subcommand_from add' -a '(__yarn_filtered_list_packages)' +# Typically there is no need to check if (commandline -ct) begins with `--` +# because it won't be matched. But we can prevent the slowdown from getting +# a list of all packages and filtering through it if we only do that when +# completing what seems to be a package name. +complete -f -c yarn -n '__fish_seen_subcommand_from remove; and not __fish_is_switch' -xa '(__yarn_installed_packages)' +complete -f -c yarn -n '__fish_seen_subcommand_from add; and not __fish_is_switch' -xa '(__yarn_filtered_list_packages)' complete -f -c yarn -n '__fish_use_subcommand' -a help diff --git a/share/functions/__fish_is_switch.fish b/share/functions/__fish_is_switch.fish new file mode 100644 index 000000000..c513f0e09 --- /dev/null +++ b/share/functions/__fish_is_switch.fish @@ -0,0 +1,4 @@ +# Whether or not the current token is a switch +function __fish_is_switch + string match -qr -- '^-' ""(commandline -ct) +end