Repository: rsteube/carapace (go)

Issue:
compadd escapes tilde
compadd adds `\` to tilde which breaks path completion

```sh
example action --files ~/<TAB>
# example action --files \~/
```

Either expand `~` to home dir or prevent compadd from escaping it when it is the first character.


Pull request description:
zsh: fix tilde expansion
fix #313
fix https://github.com/rsteube/carapace-bin/issues/1277

Patch:
diff --git a/internal/shell/zsh/action.go b/internal/shell/zsh/action.go
index b288e37..2688ddf 100644
--- a/internal/shell/zsh/action.go
+++ b/internal/shell/zsh/action.go
@@ -15,6 +15,38 @@ var sanitizer = strings.NewReplacer(
 	"\t", ``,
 )
 
+// TODO verify these are correct/complete (copied from bash)
+var quoter = strings.NewReplacer(
+	`&`, `\&`,
+	`<`, `\<`,
+	`>`, `\>`,
+	"`", "\\`",
+	`'`, `\'`,
+	`"`, `\"`,
+	`{`, `\{`,
+	`}`, `\}`,
+	`$`, `\$`,
+	`#`, `\#`,
+	`|`, `\|`,
+	`?`, `\?`,
+	`(`, `\(`,
+	`)`, `\)`,
+	`;`, `\;`,
+	` `, `\ `,
+	`[`, `\[`,
+	`]`, `\]`,
+	`*`, `\*`,
+	`\`, `\\`,
+	`~`, `\~`,
+)
+
+func quoteValue(s string) string {
+	if strings.HasPrefix(s, "~") {
+		return "~" + quoter.Replace(strings.TrimPrefix(s, "~")) // assume file path expansion
+	}
+	return quoter.Replace(s)
+}
+
 // ActionRawValues formats values for zsh
 func ActionRawValues(currentWord string, nospace bool, values common.RawValues) string {
 	filtered := make([]common.RawValue, 0)
@@ -45,6 +77,7 @@ func ActionRawValues(currentWord string, nospace bool, values common.RawValues)
 	vals := make([]string, len(filtered))
 	for index, val := range filtered {
 		val.Value = sanitizer.Replace(val.Value)
+		val.Value = quoteValue(val.Value)
 		if nospace {
 			val.Value = val.Value + "\001"
 		}
diff --git a/internal/shell/zsh/snippet.go b/internal/shell/zsh/snippet.go
index defe8e0..8d033f3 100644
--- a/internal/shell/zsh/snippet.go
+++ b/internal/shell/zsh/snippet.go
@@ -42,7 +42,7 @@ function _%v_completion {
   # shellcheck disable=SC2034,2206
   vals=(${vals%%%%$'\001'*})
 
-  compadd -S "${suffix}" -d displays -a -- vals
+  compadd -Q -S "${suffix}" -d displays -a -- vals
 }
 compquote '' 2>/dev/null && _%v_completion
 compdef _%v_completion %v


Test patch:
diff --git a/example/cmd/_test/zsh.sh b/example/cmd/_test/zsh.sh
index 28775ce..fc4df7c 100644
--- a/example/cmd/_test/zsh.sh
+++ b/example/cmd/_test/zsh.sh
@@ -30,7 +30,7 @@ function _example_completion {
   # shellcheck disable=SC2034,2206
   vals=(${vals%%$'\001'*})
 
-  compadd -S "${suffix}" -d displays -a -- vals
+  compadd -Q -S "${suffix}" -d displays -a -- vals
 }
 compquote '' 2>/dev/null && _example_completion
 compdef _example_completion example