Repository: rsteube/carapace
Pull request #630 (2022-12-03 16:45:17)

elvish: support usage


Patch:
diff --git a/action.go b/action.go
index 975ebcd..ecfdfa1 100644
--- a/action.go
+++ b/action.go
@@ -185,13 +185,6 @@ func (a Action) Suppress(expr ...string) Action {
 	})
 }
 
-// MultiParts splits values of an Action by given dividers and completes each segment separately.
-func (a Action) MultiParts(dividers ...string) Action {
-	return ActionCallback(func(c Context) Action {
-		return a.Invoke(c).ToMultiPartsA(dividers...)
-	})
-}
-
 // List wraps the Action in an ActionMultiParts with given divider.
 func (a Action) List(divider string) Action {
 	return ActionMultiParts(divider, func(c Context) Action {
diff --git a/example/cmd/_test_files/colon_:.txt b/example/cmd/_test_files/colon_:.txt
deleted file mode 100644
index e69de29..0000000
diff --git a/internal/shell/elvish/action.go b/internal/shell/elvish/action.go
index d840508..3a59203 100644
--- a/internal/shell/elvish/action.go
+++ b/internal/shell/elvish/action.go
@@ -63,6 +63,10 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
 		vals[index] = complexCandidate{Value: val.Value, Display: val.Display, Description: val.Description, CodeSuffix: suffix, Style: val.Style}
 	}
 
+	if len(values) > 0 {
+		meta.Usage = "" // TODO edit:notify is persistent, so avoid spamming the user for now
+	}
+
 	m, _ := json.Marshal(completion{
 		Usage:            meta.Usage,
 		Messages:         meta.Messages,
diff --git a/internal/shell/elvish/snippet.go b/internal/shell/elvish/snippet.go
index f792efc..9c5a8b6 100644
--- a/internal/shell/elvish/snippet.go
+++ b/internal/shell/elvish/snippet.go
@@ -15,6 +15,9 @@ func Snippet(cmd *cobra.Command) string {
 		put $completion[Messages] | all (one) | each {|m|
 			edit:notify (styled "error: " red)$m
 		}
+		if (not-eq $completion[Usage] "") {
+			edit:notify (styled "usage: " $completion[DescriptionStyle])$completion[Usage]
+		}
 		put $completion[Candidates] | all (one) | each {|c|
 			if (eq $c[Description] "") {
 		    	edit:complex-candidate $c[Value] &display=(styled $c[Display] $c[Style]) &code-suffix=$c[CodeSuffix]
diff --git a/internal/shell/zsh/action.go b/internal/shell/zsh/action.go
index f326219..fda374c 100644
--- a/internal/shell/zsh/action.go
+++ b/internal/shell/zsh/action.go
@@ -15,7 +15,6 @@ var sanitizer = strings.NewReplacer(
 
 // TODO verify these are correct/complete (copied from bash)
 var quoter = strings.NewReplacer(
-	`\`, `\\`,
 	`&`, `\&`,
 	`<`, `\<`,
 	`>`, `\>`,
@@ -35,7 +34,9 @@ var quoter = strings.NewReplacer(
 	`[`, `\[`,
 	`]`, `\]`,
 	`*`, `\*`,
+	`\`, `\\`,
 	`~`, `\~`,
+	`:`, `\:`,
 )
 
 func quoteValue(s string) string {
@@ -58,14 +59,10 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
 	for index, val := range values {
 		val.Value = sanitizer.Replace(val.Value)
 		val.Value = quoteValue(val.Value)
-		val.Value = strings.ReplaceAll(val.Value, `\`, `\\`) // TODO find out why `_describe` needs another backslash
-		val.Value = strings.ReplaceAll(val.Value, `:`, `\:`) // TODO find out why `_describe` needs another backslash
 		if !meta.Nospace.Matches(val.Value) {
 			val.Value = val.Value + " "
 		}
 		val.Display = sanitizer.Replace(val.Display)
-		val.Display = strings.ReplaceAll(val.Display, `\`, `\\`) // TODO find out why `_describe` needs another backslash
-		val.Display = strings.ReplaceAll(val.Display, `:`, `\:`) // TODO find out why `_describe` needs another backslash
 		val.Description = sanitizer.Replace(val.Description)
 
 		if strings.TrimSpace(val.Description) == "" {
diff --git a/internal/shell/zsh/snippet.go b/internal/shell/zsh/snippet.go
index 1ef4e45..0542948 100644
--- a/internal/shell/zsh/snippet.go
+++ b/internal/shell/zsh/snippet.go
@@ -26,8 +26,7 @@ function _%v_completion {
     local lines=($(echo ${words} | sed 's/$/"/' | CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs %v _carapace zsh))
   fi
 
-  zstyle ":completion:${curcontext}:*" list-colors "${lines[1]}":
-
+  export ZLS_COLOURS="${lines[1]}"
   local line_break=$'\n'
   [[ ! "${lines[2]}" == "NONE" ]] && _message -r "${lines[2]//$'\t'/${line_break}}"
   
@@ -38,7 +37,8 @@ function _%v_completion {
   # shellcheck disable=SC2034,2206
   local displays=(${lines##*$'\t'})
 
-  _describe -t "sometag" "sometag" displays vals -Q -S ''
+  local compOpts=( -qS "" -Q )
+  _describe -t "sometag" "sometag" displays vals "${compOpts[@]}"
 }
 compquote '' 2>/dev/null && _%v_completion
 compdef _%v_completion %v


Test patch:
diff --git a/example/cmd/_test/elvish.elv b/example/cmd/_test/elvish.elv
index 5f67c9d..0c54237 100644
--- a/example/cmd/_test/elvish.elv
+++ b/example/cmd/_test/elvish.elv
@@ -3,6 +3,9 @@ set edit:completion:arg-completer[example] = {|@arg|
 		put $completion[Messages] | all (one) | each {|m|
 			edit:notify (styled "error: " red)$m
 		}
+		if (not-eq $completion[Usage] "") {
+			edit:notify (styled "usage: " $completion[DescriptionStyle])$completion[Usage]
+		}
 		put $completion[Candidates] | all (one) | each {|c|
 			if (eq $c[Description] "") {
 		    	edit:complex-candidate $c[Value] &display=(styled $c[Display] $c[Style]) &code-suffix=$c[CodeSuffix]
diff --git a/example/cmd/_test/zsh.sh b/example/cmd/_test/zsh.sh
index 8188e35..bfc53cd 100644
--- a/example/cmd/_test/zsh.sh
+++ b/example/cmd/_test/zsh.sh
@@ -14,8 +14,7 @@ function _example_completion {
     local lines=($(echo ${words} | sed 's/$/"/' | CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs example _carapace zsh))
   fi
 
-  zstyle ":completion:${curcontext}:*" list-colors "${lines[1]}":
-
+  export ZLS_COLOURS="${lines[1]}"
   local line_break=$'\n'
   [[ ! "${lines[2]}" == "NONE" ]] && _message -r "${lines[2]//$'\t'/${line_break}}"
   
@@ -26,7 +25,8 @@ function _example_completion {
   # shellcheck disable=SC2034,2206
   local displays=(${lines##*$'\t'})
 
-  _describe -t "sometag" "sometag" displays vals -Q -S ''
+  local compOpts=( -qS "" -Q )
+  _describe -t "sometag" "sometag" displays vals "${compOpts[@]}"
 }
 compquote '' 2>/dev/null && _example_completion
 compdef _example_completion example