From 7db09588046a07fbf8e8c25e5dcc3317b32c7842 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Thu, 19 Apr 2018 17:52:20 -0500 Subject: [PATCH] Add `__fish_can_complete_switches` & `__fish_should_complete_switches` To be used by completions to directly determine whether it is either possible or preferable to complete a switch (instead of a subcommand), (presuming that switches must come before subcommands). * __fish_can_complete_switches: we are in a position where a switch may be placed. * __fish_should_complete_switches: we're in a position to accept a switch and the current token starts with `-` so we have no choice but to do so. --- share/functions/__fish_can_complete_switches.fish | 14 ++++++++++++++ .../functions/__fish_should_complete_switches.fish | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 share/functions/__fish_can_complete_switches.fish create mode 100644 share/functions/__fish_should_complete_switches.fish diff --git a/share/functions/__fish_can_complete_switches.fish b/share/functions/__fish_can_complete_switches.fish new file mode 100644 index 000000000..9354f0892 --- /dev/null +++ b/share/functions/__fish_can_complete_switches.fish @@ -0,0 +1,14 @@ +# Returns whether it is at all possible (even if not recommended) +# to complete a -s or --long argument. +function __fish_can_complete_switches + # Search backwards + for arg in (commandline -ct)[-1..1] + if test "$arg" = "" + continue + else if not string match -qr -- "^-\S*\$" "$arg" + return 1 + end + end + + return 0 +end diff --git a/share/functions/__fish_should_complete_switches.fish b/share/functions/__fish_should_complete_switches.fish new file mode 100644 index 000000000..876a6a673 --- /dev/null +++ b/share/functions/__fish_should_complete_switches.fish @@ -0,0 +1,12 @@ +# Returns whether we *should* complete a -s or --long argument. +# The preference is NOT to do so, i.e. prefer subcommands over switches. +function __fish_should_complete_switches + if not __fish_can_complete_switches + return 1 + end + if string match -qr -- "^-" (commandline -ct)[-1] + return 0 + end + + return 1 +end