From 285e2fa7c1e45dd279a2b415e3735a03ca6deb6d Mon Sep 17 00:00:00 2001 From: rsteube Date: Tue, 10 Nov 2020 13:17:34 +0100 Subject: [PATCH] fix callbackvalue for positionalany --- elvish/snippet.go | 3 +++ example/cmd/multiparts.go | 4 ++++ example/cmd/root_test.go | 10 ++++++++-- uid/uid.go | 10 +++++++--- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/elvish/snippet.go b/elvish/snippet.go index 037243ce9..fd77dd07b 100644 --- a/elvish/snippet.go +++ b/elvish/snippet.go @@ -38,6 +38,9 @@ edit:completion:arg-completer[%v] = [@arg]{ fn _%v_callback [uid]{ # TODO there is no 'eval' in elvish and '-source' needs a file so use a tempary one for callback tmpfile=(mktemp -t carapace_%v_callback-XXXXX.elv) + if (eq $arg[-1] "") { + arg[-1] = "''" + } echo (str:join ' ' $arg) | xargs %v _carapace elvish $uid > $tmpfile -source $tmpfile rm $tmpfile diff --git a/example/cmd/multiparts.go b/example/cmd/multiparts.go index 560a08d19..f49e9a1f8 100644 --- a/example/cmd/multiparts.go +++ b/example/cmd/multiparts.go @@ -36,6 +36,10 @@ func init() { return carapace.ActionValuesDescribed("a", "first", "b", "second", "c", "third", "d", "fourth").Invoke(args).Filter(strings.Split(carapace.CallbackValue, "")).ToA() }), }) + + carapace.Gen(multipartsCmd).PositionalCompletion(ActionMultipartsTest(",")) + + carapace.Gen(multipartsCmd).PositionalAnyCompletion(ActionMultipartsTest("/")) } func ActionMultipartsTest(divider string) carapace.Action { diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go index d1a3b6244..d7382eaa3 100644 --- a/example/cmd/root_test.go +++ b/example/cmd/root_test.go @@ -286,6 +286,9 @@ edit:completion:arg-completer[example] = [@arg]{ fn _example_callback [uid]{ # TODO there is no 'eval' in elvish and '-source' needs a file so use a tempary one for callback tmpfile=(mktemp -t carapace_example_callback-XXXXX.elv) + if (eq $arg[-1] "") { + arg[-1] = "''" + } echo (str:join ' ' $arg) | xargs example _carapace elvish $uid > $tmpfile -source $tmpfile rm $tmpfile @@ -449,7 +452,9 @@ edit:complex-candidate 'invalid' &display='invalid' }] [&long='slash' &desc='multiparts with / as divider' &arg-required=$true &completer=[_]{ _example_callback '_example__multiparts##slash' }] ] arg-handlers = [ - + [_]{ _example_callback '_example__multiparts#1' } + [_]{ _example_callback '_example__multiparts#0' } + ... ] subargs = $arg[(subindex multiparts):] if (> (count $subargs) 0) { @@ -1348,7 +1353,8 @@ function _example__multiparts { "--equals[multiparts with = as divider]: :{_example_callback '_example__multiparts##equals'}" \ "--none[multiparts without divider]: :{_example_callback '_example__multiparts##none'}" \ "--slash[multiparts with / as divider]: :{_example_callback '_example__multiparts##slash'}" \ - "*::arg:->args" + "1: :{_example_callback '_example__multiparts#1'}" \ + "*: :{_example_callback '_example__multiparts#0'}" } if compquote '' 2>/dev/null; then _example; else compdef _example example; fi ` diff --git a/uid/uid.go b/uid/uid.go index cbf9b62bf..7cece8b60 100644 --- a/uid/uid.go +++ b/uid/uid.go @@ -68,9 +68,13 @@ func Value(cmd *cobra.Command, args []string, uid string) string { } else if strings.Contains(uid, "#") { split := strings.Split(uid, "#") if index, err := strconv.Atoi(split[len(split)-1]); err == nil { - index = index - 1 - if len(args)-1 >= index && index >= 0 { - return args[index] + if index > 0 { + index = index - 1 + if len(args)-1 >= index && index >= 0 { + return args[index] + } + } else if len(args) > 0 && os.Args[len(os.Args)-1] != "" { + return args[len(args)-1] } } }