Repository: rsteube/carapace
Pull request #221 (2021-01-13 13:15:23)

elvish: removed eval step
related #3 
related #192
related #194
related #215



Patch:
diff --git a/internal/elvish/action.go b/internal/elvish/action.go
index 4ee1729..27791ba 100644
--- a/internal/elvish/action.go
+++ b/internal/elvish/action.go
@@ -1,45 +1,26 @@
 package elvish
 
 import (
+	"encoding/json"
 	"fmt"
-	"strings"
-
 	"github.com/rsteube/carapace/internal/common"
 )
 
-var sanitizer = strings.NewReplacer(
-	`$`, ``,
-	"`", ``,
-	"\n", ``,
-	`\`, ``,
-	`"`, ``,
-	`'`, ``,
-	`|`, ``,
-	`>`, ``,
-	`<`, ``,
-	`&`, ``,
-	`(`, ``,
-	`)`, ``,
-	`;`, ``,
-	`#`, ``,
-)
-
-func Sanitize(values ...string) []string {
-	sanitized := make([]string, len(values))
-	for index, value := range values {
-		sanitized[index] = sanitizer.Replace(value)
-	}
-	return sanitized
+type complexCandidate struct {
+	Value   string
+	Display string
 }
 
 func ActionRawValues(callbackValue string, values ...common.RawValue) string {
-	vals := make([]string, len(values))
+	vals := make([]complexCandidate, len(values))
 	for index, val := range values {
+		// TODO have a look at this again later: seems elvish does a good job quoting any problematic characterS so the sanitize step was removed
 		if val.Description == "" {
-			vals[index] = fmt.Sprintf(`edit:complex-candidate '%v' &display='%v'`, sanitizer.Replace(val.Value), sanitizer.Replace(val.Display))
+			vals[index] = complexCandidate{Value: val.Value, Display: val.Display}
 		} else {
-			vals[index] = fmt.Sprintf(`edit:complex-candidate '%v' &display='%v (%v)'`, sanitizer.Replace(val.Value), sanitizer.Replace(val.Display), sanitizer.Replace(val.Description))
+			vals[index] = complexCandidate{Value: val.Value, Display: fmt.Sprintf(`%v (%v)`, val.Display, val.Description)}
 		}
 	}
-	return strings.Join(vals, "\n")
+	m, _ := json.Marshal(vals)
+	return string(m)
 }
diff --git a/internal/elvish/snippet.go b/internal/elvish/snippet.go
index 238b6a5..3610ec3 100644
--- a/internal/elvish/snippet.go
+++ b/internal/elvish/snippet.go
@@ -4,16 +4,6 @@ import (
 	"fmt"
 	"github.com/rsteube/carapace/internal/uid"
 	"github.com/spf13/cobra"
-	"strings"
-)
-
-var replacer = strings.NewReplacer( // TODO
-	`:`, `\:`,
-	"\n", ``,
-	`"`, `\"`,
-	`[`, `\[`,
-	`]`, `\]`,
-	`'`, `\"`,
 )
 
 func Snippet(cmd *cobra.Command, actions map[string]string) string {
@@ -21,7 +11,7 @@ func Snippet(cmd *cobra.Command, actions map[string]string) string {
     if (eq $arg[-1] "") {
         arg[-1] = "''"
     }
-    eval (%v _carapace elvish _ (all $arg) | slurp) &ns=(ns [&arg=$arg])
+    %v _carapace elvish _ (all $arg) | from-json | all (one) | each [c]{ edit:complex-candidate $c[Value] &display=$c[Display] }
 }
 `, cmd.Name(), uid.Executable())
 }


Test patch:
diff --git a/example/cmd/_test/elvish.elv b/example/cmd/_test/elvish.elv
index 1b18f2c..5580805 100644
--- a/example/cmd/_test/elvish.elv
+++ b/example/cmd/_test/elvish.elv
@@ -2,6 +2,6 @@ edit:completion:arg-completer[example] = [@arg]{
     if (eq $arg[-1] "") {
         arg[-1] = "''"
     }
-    eval (example _carapace elvish _ (all $arg) | slurp) &ns=(ns [&arg=$arg])
+    example _carapace elvish _ (all $arg) | from-json | all (one) | each [c]{ edit:complex-candidate $c[Value] &display=$c[Display] }
 }