Skip to content

Commit

Permalink
zsh: fix quoting state
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jan 19, 2025
1 parent c9cfc31 commit a5fa547
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@


────────────────────────────────────────────────────────────────────────────────
> example action embeddedP1 "embeddedP2\ with\ space
> example action embeddedP1 "embeddedP2\ with\ space"






────────────────────────────────────────────────────────────────────────────────
> example action embeddedP1 "embeddedP2\ with\ space
> example action embeddedP1 "embeddedP2\ with\ space"



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@


────────────────────────────────────────────────────────────────────────────────
> example action embeddedP1 'embeddedP2\ with\ space
> example action embeddedP1 'embeddedP2\ with\ space'






────────────────────────────────────────────────────────────────────────────────
> example action embeddedP1 'embeddedP2\ with\ space
> example action embeddedP1 'embeddedP2\ with\ space'



Expand Down
4 changes: 2 additions & 2 deletions example/cmd/_test/zsh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ function _example_completion {
if echo ${words}"''" | xargs echo 2>/dev/null > /dev/null; then
local lines="$(echo ${words}"''" | CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs example _carapace zsh )"
elif echo ${words} | sed "s/\$/'/" | xargs echo 2>/dev/null > /dev/null; then
local lines="$(echo ${words} | sed "s/\$/'/" | CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs example _carapace zsh)"
local lines="$(echo ${words} | sed "s/\$/'/" | CARAPACE_STATE=QUOTING_STATE CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs example _carapace zsh)"
else
local lines="$(echo ${words} | sed 's/$/"/' | CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs example _carapace zsh)"
local lines="$(echo ${words} | sed 's/$/"/' | CARAPACE_STATE=QUOTING_ESCAPING_STATE CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs example _carapace zsh)"
fi

local zstyle message data
Expand Down
11 changes: 11 additions & 0 deletions internal/shell/zsh/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zsh

import (
"fmt"
"os"
"strings"

"github.com/carapace-sh/carapace/internal/common"
Expand Down Expand Up @@ -63,6 +64,16 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
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

switch os.Getenv("CARAPACE_STATE") {
// TODO depending on state value needs to be formatted differently
// TODO backspace strings are currently an issue
case "QUOTING_STATE":
val.Value = val.Value + `'`
case "QUOTING_ESCAPING_STATE":
val.Value = val.Value + `"`
}

if !meta.Nospace.Matches(val.Value) {
val.Value = val.Value + " "
}
Expand Down
4 changes: 2 additions & 2 deletions internal/shell/zsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function _%v_completion {
if echo ${words}"''" | xargs echo 2>/dev/null > /dev/null; then
local lines="$(echo ${words}"''" | CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs %v _carapace zsh )"
elif echo ${words} | sed "s/\$/'/" | xargs echo 2>/dev/null > /dev/null; then
local lines="$(echo ${words} | sed "s/\$/'/" | CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs %v _carapace zsh)"
local lines="$(echo ${words} | sed "s/\$/'/" | CARAPACE_STATE=QUOTING_STATE CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs %v _carapace zsh)"
else
local lines="$(echo ${words} | sed 's/$/"/' | CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs %v _carapace zsh)"
local lines="$(echo ${words} | sed 's/$/"/' | CARAPACE_STATE=QUOTING_ESCAPING_STATE CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs %v _carapace zsh)"
fi
local zstyle message data
Expand Down

0 comments on commit a5fa547

Please sign in to comment.