Repository: rsteube/carapace (go)

Issue:
zsh: expand static named directories
Expand [static named directories](https://zsh.sourceforge.io/Doc/Release/Expansion.html#Static-named-directories) similar to home (`~`) for zsh file completion:

```sh
zsh ❯ hash -d
avahi=/
bin=/
colord=/var/lib/colord
cups=/
daemon=/
dbus=/
deluge=/srv/deluge
dhcpcd=/var/lib/dhcpcd
dnsmasq=/
ftp=/srv/ftp
git=/
http=/srv/http
lightdm=/var/lib/lightdm
...
```

related #313 

Pull request:
zsh: support static named directories in file completion
fix #549

Patch:
diff --git a/context.go b/context.go
index bdcc45b..baa3c11 100644
--- a/context.go
+++ b/context.go
@@ -66,6 +66,20 @@ func (c Context) Command(name string, arg ...string) *execabs.Cmd {
 
 func expandHome(s string) (string, error) {
 	if strings.HasPrefix(s, "~") {
+		// TODO use env from context
+		if hash, ok := os.LookupEnv("CARAPACE_ZSH_HASH"); ok { // support zsh static named directories
+			for _, line := range strings.Split(hash, "\n") {
+				if splitted := strings.SplitN(line, "=", 2); len(splitted) == 2 {
+					if staticNamedDirectory := fmt.Sprintf("~%v/", splitted[0]); strings.HasPrefix(s, staticNamedDirectory) {
+						if !strings.HasSuffix(splitted[1], "/") {
+							splitted[1] = splitted[1] + "/"
+						}
+						return strings.Replace(s, staticNamedDirectory, splitted[1], 1), nil
+					}
+				}
+			}
+		}
+
 		home, err := os.UserHomeDir() // TODO duplicated code
 		if err != nil {
 			return "", err
@@ -77,7 +91,7 @@ func expandHome(s string) (string, error) {
 
 func (c Context) Abs(s string) (string, error) {
 	var path string
-	if strings.HasPrefix(s, "/") || strings.HasPrefix(s, "~/") {
+	if strings.HasPrefix(s, "/") || strings.HasPrefix(s, "~") {
 		path = s // path is absolute
 	} else {
 		expanded, err := expandHome(c.Dir)
diff --git a/internal/shell/zsh/snippet.go b/internal/shell/zsh/snippet.go
index 8d033f3..371c29e 100644
--- a/internal/shell/zsh/snippet.go
+++ b/internal/shell/zsh/snippet.go
@@ -17,13 +17,13 @@ function _%v_completion {
   # shellcheck disable=SC2207,SC2086,SC2154
   if echo ${words}"''" | xargs echo 2>/dev/null > /dev/null; then
     # shellcheck disable=SC2207,SC2086
-    local lines=($(echo ${words}"''" | xargs %v _carapace zsh ))
+    local lines=($(echo ${words}"''" | CARAPACE_ZSH_HASH="$(hash -d)" xargs %v _carapace zsh ))
   elif echo ${words} | sed "s/\$/'/" | xargs echo 2>/dev/null > /dev/null; then
     # shellcheck disable=SC2207,SC2086
-    local lines=($(echo ${words} | sed "s/\$/'/" | xargs %v _carapace zsh))
+    local lines=($(echo ${words} | sed "s/\$/'/" | CARAPACE_ZSH_HASH="$(hash -d)" xargs %v _carapace zsh))
   else
     # shellcheck disable=SC2207,SC2086
-    local lines=($(echo ${words} | sed 's/$/"/'  | xargs %v _carapace zsh))
+    local lines=($(echo ${words} | sed 's/$/"/'  | CARAPACE_ZSH_HASH="$(hash -d)" xargs %v _carapace zsh))
   fi
 
   export ZLS_COLOURS="${lines[1]}"


Test patch:
diff --git a/example/cmd/_test/zsh.sh b/example/cmd/_test/zsh.sh
index fc4df7c..2bf133c 100644
--- a/example/cmd/_test/zsh.sh
+++ b/example/cmd/_test/zsh.sh
@@ -5,13 +5,13 @@ function _example_completion {
   # shellcheck disable=SC2207,SC2086,SC2154
   if echo ${words}"''" | xargs echo 2>/dev/null > /dev/null; then
     # shellcheck disable=SC2207,SC2086
-    local lines=($(echo ${words}"''" | xargs example _carapace zsh ))
+    local lines=($(echo ${words}"''" | CARAPACE_ZSH_HASH="$(hash -d)" xargs example _carapace zsh ))
   elif echo ${words} | sed "s/\$/'/" | xargs echo 2>/dev/null > /dev/null; then
     # shellcheck disable=SC2207,SC2086
-    local lines=($(echo ${words} | sed "s/\$/'/" | xargs example _carapace zsh))
+    local lines=($(echo ${words} | sed "s/\$/'/" | CARAPACE_ZSH_HASH="$(hash -d)" xargs example _carapace zsh))
   else
     # shellcheck disable=SC2207,SC2086
-    local lines=($(echo ${words} | sed 's/$/"/'  | xargs example _carapace zsh))
+    local lines=($(echo ${words} | sed 's/$/"/'  | CARAPACE_ZSH_HASH="$(hash -d)" xargs example _carapace zsh))
   fi
 
   export ZLS_COLOURS="${lines[1]}"