Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nushell: use lexer to patch arguments #869

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package carapace

import (
"github.com/rsteube/carapace/internal/config"
"github.com/rsteube/carapace/internal/shell/nushell"
"github.com/rsteube/carapace/pkg/ps"
"github.com/spf13/cobra"
)
Expand All @@ -14,6 +15,10 @@ func complete(cmd *cobra.Command, args []string) (string, error) {
return Gen(cmd).Snippet(args[0])
default:
initHelpCompletion(cmd)

if shell := ps.DetermineShell(); shell == "nushell" {
args = nushell.Patch(args)
}
action, context := traverse(cmd, args[2:])
if err := config.Load(); err != nil {
action = ActionMessage("failed to load config: " + err.Error())
Expand Down
12 changes: 11 additions & 1 deletion internal/shell/nushell/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ var sanitizer = strings.NewReplacer(
"\r", ``,
)

var escaper = strings.NewReplacer(
`\`, `\\`,
`"`, `\"`,
)

func sanitize(values []common.RawValue) []common.RawValue {
for index, v := range values {
(&values[index]).Value = sanitizer.Replace(v.Value)
Expand All @@ -32,7 +37,12 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
vals := make([]record, len(values))
for index, val := range sanitize(values) {
if strings.ContainsAny(val.Value, ` {}()[]<>$&"|;#\`+"`") {
val.Value = fmt.Sprintf("'%v'", val.Value)
switch {
case strings.HasPrefix(val.Value, "~"):
val.Value = fmt.Sprintf(`~"%v"`, escaper.Replace(val.Value[1:]))
default:
val.Value = fmt.Sprintf(`"%v"`, escaper.Replace(val.Value))
}
}

if !meta.Nospace.Matches(val.Value) {
Expand Down
29 changes: 29 additions & 0 deletions internal/shell/nushell/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nushell

import (
"strings"

"github.com/rsteube/carapace/internal/lexer"
)

// Patch uses the lexer to parse and patch given arguments which
// are currently passed unprocessed to the completion function.
//
// see https://www.nushell.sh/book/working_with_strings.html
func Patch(args []string) []string {
for index, arg := range args {
if len(arg) == 0 {
continue
}

switch arg[0] {
case '"', "'"[0]:
if tokenset, err := lexer.Split(arg, false); err == nil {
args[index] = tokenset.Tokens[0]
}
case '`':
args[index] = strings.Trim(arg, "`")
}
}
return args
}
Loading