From e2c44f73d495d1e19ccbf82bee7e6fe492b3b9b7 Mon Sep 17 00:00:00 2001 From: Simon Templer Date: Tue, 5 Jul 2016 23:44:31 +0200 Subject: [PATCH] include current word index for bash completion --- etc/bash_completion.sh | 7 ++++++- src/main/groovy/to/wetransform/halecli/Command.java | 5 +++-- .../to/wetransform/halecli/DelegatingCommand.groovy | 9 +++++---- src/main/groovy/to/wetransform/halecli/Runner.groovy | 2 +- .../halecli/project/AbstractProjectCommand.groovy | 3 ++- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/etc/bash_completion.sh b/etc/bash_completion.sh index 1a37271..9d569e4 100644 --- a/etc/bash_completion.sh +++ b/etc/bash_completion.sh @@ -1,3 +1,5 @@ +_hale_join() { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; } + _hale() { compopt +o default @@ -5,7 +7,10 @@ _hale() local cur="${COMP_WORDS[COMP_CWORD]}" # delegate to hale command - local hale_result=$(hale --complete $COMP_CWORD ${COMP_WORDS[@]}) + #local joined=$(_hale_join "' '" "${COMP_WORDS[@]}") + #local joined="'$joined'" + #local hale_result=$(hale --complete $COMP_CWORD $joined) + local hale_result=$(hale --complete $COMP_CWORD "${COMP_WORDS[@]}") case "${hale_result}" in # Reserved word FILE diff --git a/src/main/groovy/to/wetransform/halecli/Command.java b/src/main/groovy/to/wetransform/halecli/Command.java index 56870b7..58a6ff3 100644 --- a/src/main/groovy/to/wetransform/halecli/Command.java +++ b/src/main/groovy/to/wetransform/halecli/Command.java @@ -25,11 +25,12 @@ public interface Command { /** * Return a Unix command to use to generate bash completions. * - * @param args the list of arguments (ending with the proposal to complete) + * @param args the list of arguments + * @param current the index of the current argument to be completed * @return the Unix command or null */ @Nullable - default String bashCompletion(List args) { + default String bashCompletion(List args, int current) { return null; } diff --git a/src/main/groovy/to/wetransform/halecli/DelegatingCommand.groovy b/src/main/groovy/to/wetransform/halecli/DelegatingCommand.groovy index 6434e53..d2cf29e 100644 --- a/src/main/groovy/to/wetransform/halecli/DelegatingCommand.groovy +++ b/src/main/groovy/to/wetransform/halecli/DelegatingCommand.groovy @@ -48,13 +48,14 @@ abstract class DelegatingCommand implements Command { } } - String bashCompletion(List args) { - if (args.size() > 1) { + @Override + String bashCompletion(List args, int current) { + if (current > 0) { // delegate to command String commandName = args[0] Command command = subCommands[commandName] if (command) { - command.bashCompletion(args[1..-1]) + command.bashCompletion(args[1..-1], current - 1) } else { null @@ -62,7 +63,7 @@ abstract class DelegatingCommand implements Command { } else { // complete subcommand - 'compgen -W "help ' + subCommands.keySet().join(' ') + '" -- ' + args[-1] + 'compgen -W "help ' + subCommands.keySet().join(' ') + '" -- ' + args[current] } } diff --git a/src/main/groovy/to/wetransform/halecli/Runner.groovy b/src/main/groovy/to/wetransform/halecli/Runner.groovy index e31d8e6..ad2aa10 100644 --- a/src/main/groovy/to/wetransform/halecli/Runner.groovy +++ b/src/main/groovy/to/wetransform/halecli/Runner.groovy @@ -49,7 +49,7 @@ class Runner extends DelegatingCommand { // strip first word (which is the base command) words = words.size() > 1 ? words[1..-1] : [] - String completion = bashCompletion(words) + String completion = bashCompletion(words, currentWord - 1) if (completion) { println completion return 0 diff --git a/src/main/groovy/to/wetransform/halecli/project/AbstractProjectCommand.groovy b/src/main/groovy/to/wetransform/halecli/project/AbstractProjectCommand.groovy index c3e7b43..189f4db 100644 --- a/src/main/groovy/to/wetransform/halecli/project/AbstractProjectCommand.groovy +++ b/src/main/groovy/to/wetransform/halecli/project/AbstractProjectCommand.groovy @@ -130,7 +130,8 @@ abstract class AbstractProjectCommand implements Command { abstract boolean runForProject(ProjectTransformationEnvironment projectEnv, URI projectLocation, OptionAccessor options, CommandContext context) - String bashCompletion(List args) { + @Override + String bashCompletion(List args, int current) { //TODO handling for options? how to adapt in subclasses? if (args) {