From d30913c6a83c08509478de2b9d8e60d640480a1e Mon Sep 17 00:00:00 2001 From: Vladimir Zhelezov Date: Fri, 11 Dec 2020 08:50:03 +0100 Subject: [PATCH] Refactor _hledger_compreply_optarg() It used to do a lot more work parsing the command line that is no longer necessary. Clean up redundant code. --- shell-completion/hledger-completion.bash | 20 ++++---------------- shell-completion/hledger-completion.bash.m4 | 20 ++++---------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/shell-completion/hledger-completion.bash b/shell-completion/hledger-completion.bash index 967a8aa59..e5fa7e2a6 100644 --- a/shell-completion/hledger-completion.bash +++ b/shell-completion/hledger-completion.bash @@ -188,16 +188,12 @@ _hledger_compgen() { # Try required option argument completion. Set COMPREPLY and return 0 on # success, 1 if option doesn't require an argument or out of context _hledger_compreply_optarg() { - local optionIndex=${1:-$((cword - 1))} - local recursionLevel=${2:-0} - local wordlist - local error=0 - local match + local match wordlist # Match the empty string on --file= [[ $cur == = ]] || match=$cur - case ${words[optionIndex]} in + case $prev in --alias) compopt -o nospace _hledger_compreply "$(_hledger_compgen "$(_hledger accounts --flat)" "" "$match")" @@ -238,20 +234,12 @@ _hledger_compreply_optarg() { -b|--begin|-e|--end|-p|--period|--depth|--drop) _hledger_compreply "" ;; - =) - # Recurse only once! - ((recursionLevel > 1)) && return 1 - if [[ ${words[optionIndex - 1]} == -* ]]; then - _hledger_compreply_optarg $((optionIndex - 1)) $((recursionLevel + 1)) - error=$? - fi - ;; *) - error=1 + return 1 ;; esac - return $error + return 0 } # Query filter completion through introspection diff --git a/shell-completion/hledger-completion.bash.m4 b/shell-completion/hledger-completion.bash.m4 index 21b406e30..00948ccc1 100644 --- a/shell-completion/hledger-completion.bash.m4 +++ b/shell-completion/hledger-completion.bash.m4 @@ -188,16 +188,12 @@ _hledger_compgen() { # Try required option argument completion. Set COMPREPLY and return 0 on # success, 1 if option doesn't require an argument or out of context _hledger_compreply_optarg() { - local optionIndex=${1:-$((cword - 1))} - local recursionLevel=${2:-0} - local wordlist - local error=0 - local match + local match wordlist # Match the empty string on --file= [[ $cur == = ]] || match=$cur - case ${words[optionIndex]} in + case $prev in --alias) compopt -o nospace _hledger_compreply "$(_hledger_compgen "$(_hledger accounts --flat)" "" "$match")" @@ -238,20 +234,12 @@ _hledger_compreply_optarg() { -b|--begin|-e|--end|-p|--period|--depth|--drop) _hledger_compreply "" ;; - =) - # Recurse only once! - ((recursionLevel > 1)) && return 1 - if [[ ${words[optionIndex - 1]} == -* ]]; then - _hledger_compreply_optarg $((optionIndex - 1)) $((recursionLevel + 1)) - error=$? - fi - ;; *) - error=1 + return 1 ;; esac - return $error + return 0 } # Query filter completion through introspection