From a85d2bf27ad273ef495bc97731f8a766048b8c70 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Mon, 5 Mar 2018 08:28:19 -0600 Subject: [PATCH] Add intelligent package completion to yarn Now parses package.json and uses results to provide a list of possible completions to `yarn remove`. There may be other subcommands that could benefit from this. Could have parsed yarn output, but yarn is slow and packages.json format is generally standard since it's machine-generated json. --- CHANGELOG.md | 1 + share/completions/yarn.fish | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba83484cb..56830a217 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ This section is for changes merged to the `major` branch that are not also merge - `git` (#4395, #4396, #4592) - `brew` - `diskutil` + - `yarn` -- diff --git a/share/completions/yarn.fish b/share/completions/yarn.fish index 4266e9909..b27470fa6 100644 --- a/share/completions/yarn.fish +++ b/share/completions/yarn.fish @@ -2,6 +2,54 @@ # see https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_seen_subcommand_from.fish # and https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_use_subcommand.fish +function __yarn_find_package_json + set parents (__fish_parent_directories (pwd)) + + for p in $parents + if test -f "$p/package.json" + echo "$p/package.json" + return 0 + end + end + + return 1 +end + +function __yarn_list_packages + set -l package_json (__yarn_find_package_json) + if not test $status -eq 0 + # no package.json in tree + return 1 + end + + set -l depsFound 0 + for line in (cat $package_json) + # echo "evaluating $line" + if test $depsFound -eq 0 + # echo "mode: noDeps" + if string match -qr '(devD|d)ependencies"' -- $line + # echo "switching to mode: deps" + set depsFound 1 + continue + end + continue + end + + if string match -qr '\}' -- $line + # echo "switching to mode: noDeps" + set depsFound 0 + continue + end + + # echo "mode: deps" + + string replace -r '^\s*"([^"]+)".*' '$1' -- $line + end +end + + +complete -f -c yarn -n '__fish_seen_subcommand_from remove' -a (set -l packages (__yarn_list_packages); echo $packages) + complete -f -c yarn -n '__fish_use_subcommand' -a help complete -f -c yarn -n '__fish_use_subcommand' -a access