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.
This commit is contained in:
Mahmoud Al-Qudsi 2018-10-19 17:45:02 -05:00
parent 13c2b4cdc3
commit 1288877033
2 changed files with 21 additions and 5 deletions

View file

@ -12,10 +12,18 @@ function __yarn_list_packages
all-the-package-names all-the-package-names
end end
# Entire list of packages is too long to be used in a `complete` subcommand # Entire list of packages is too long to be used efficiently in a `complete` subcommand.
# Search it for matches instead # Search it for matches instead.
function __yarn_filtered_list_packages 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 end
function __yarn_find_package_json function __yarn_find_package_json
@ -68,8 +76,12 @@ function __yarn_installed_packages
end end
complete -f -c yarn -n '__fish_seen_subcommand_from remove' -xa '(__yarn_installed_packages)' # Typically there is no need to check if (commandline -ct) begins with `--`
complete -f -c yarn -n '__fish_seen_subcommand_from add' -a '(__yarn_filtered_list_packages)' # 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 complete -f -c yarn -n '__fish_use_subcommand' -a help

View file

@ -0,0 +1,4 @@
# Whether or not the current token is a switch
function __fish_is_switch
string match -qr -- '^-' ""(commandline -ct)
end