Use parsed replacement prefixes in shell completion

This commit is contained in:
2026-11-03 13:11:07 +00:00
parent 8b48115328
commit f39f27b5cd
3 changed files with 34 additions and 43 deletions

View File

@@ -29,7 +29,7 @@ func CompletionReply(result commandmeta.CompletionResult) (string, error) {
return "", fmt.Errorf("unsupported completion suggestion strategy")
}
var output strings.Builder
if result.Kind != commandmeta.CompletionNone && result.Prefix != "" && !strings.Contains(result.Prefix, "=") {
if result.Kind != commandmeta.CompletionNone && result.Prefix != "" {
output.WriteString(completionPathPrefixMarker)
output.WriteString(result.Prefix)
output.WriteByte('\n')
@@ -129,7 +129,7 @@ func adjustBashCompletion(script, name string) (string, error) {
# The Bash helpers may omit the empty word at a trailing space.
if ((${#requestWords[@]} <= cword)); then requestWords+=(""); fi
fi
if [[ ${cur} == -*=* ]]; then cur="${cur#*=}"; fi
cur=${requestWords[${#requestWords[@]}-1]}
out=$(COBRA_ACTIVE_HELP=0 "${requestWords[0]}" __complete "${requestWords[@]:1}" 2>/dev/null)
`)
@@ -148,9 +148,9 @@ func adjustBashCompletion(script, name string) (string, error) {
parsePrefix := `if [[ ${out} == ` + completionPathPrefixMarker + `-* ]]; then
completionPrefix=${out%%$'\n'*}
completionPrefix=${completionPrefix#` + completionPathPrefixMarker + `}
completionOriginalCur=${words[${#words[@]}-1]}
if [[ $completionPrefix != --* && $completionOriginalCur == "$completionPrefix"* ]]; then
out=${out#*$'\n'}
completionOriginalCur=$cur
if [[ $completionOriginalCur == "$completionPrefix"* ]]; then
if [[ $out == *$'\n'* ]]; then out=${out#*$'\n'}; else out=; fi
cur=${completionOriginalCur#"$completionPrefix"}
else
completionPrefix=""
@@ -164,7 +164,7 @@ func adjustBashCompletion(script, name string) (string, error) {
}
script, err = replaceCompletionText(script,
`if (((directive & shellCompDirectiveFilterFileExt) != 0)); then`,
`if [[ -n $completionPrefix ]] && (((directive & shellCompDirectiveFilterDirs) == 0)) && (((directive & shellCompDirectiveNoFileComp) == 0)); then
`if ((directive == 0)); then
compopt +o default
_filedir
elif (((directive & shellCompDirectiveFilterFileExt) != 0)); then`)
@@ -190,9 +190,6 @@ func adjustZshCompletion(script string) (string, error) {
return "", err
}
script, err = replaceCompletionSection(script, " # For zsh, when completing a flag", " # Extract the directive", ` local -a prefixArgs orderArgs spaceArgs
if [[ "$lastParam" == -*=* ]]; then
flagPrefix="${lastParam%%=*}="
fi
out=$(COBRA_ACTIVE_HELP=0 "${words[1]}" __complete "${words[@]:1}" 2>/dev/null)
`)
@@ -202,8 +199,8 @@ func adjustZshCompletion(script string) (string, error) {
script, err = replaceCompletionText(script, `local activeHelpMarker="_activeHelp_ "`, `if [[ "$out" == `+completionPathPrefixMarker+`-* ]]; then
local pathPrefix="${out%%$'\n'*}"
pathPrefix="${pathPrefix#`+completionPathPrefixMarker+`}"
if [[ "$pathPrefix" != --* && "$lastParam" == "$pathPrefix"* ]]; then
out="${out#*$'\n'}"
if [[ "$lastParam" == "$pathPrefix"* ]]; then
if [[ $out == *$'\n'* ]]; then out="${out#*$'\n'}"; else out=; fi
flagPrefix="$pathPrefix"
fi
fi
@@ -275,19 +272,20 @@ func adjustFishCompletion(script, name string) (string, error) {
if err != nil {
return "", err
}
script, err = replaceCompletionText(script, `string match -r -- '-.*=' "$lastArg"`, `string match -r -- '-[^=]*=' "$lastArg"`)
if err != nil {
return "", err
}
script, err = replaceCompletionText(script, `for comp in $comps`, `if set -q comps[1]; and string match -q -- '`+completionPathPrefixMarker+`-*' "$comps[1]"
set -l candidatePrefix (string replace -- '`+completionPathPrefixMarker+`' '' "$comps[1]")
if not string match -q -- '--*' "$candidatePrefix"; and test (string sub -l (string length -- "$candidatePrefix") -- "$lastArg") = "$candidatePrefix"
set flagPrefix "$candidatePrefix"
set -e comps[1]
printf "%s%s\n" '`+completionPathPrefixMarker+`' "$flagPrefix"
end
script, err = replaceCompletionSection(script, " # For Fish, when completing a flag", ` printf "%s\n" "$directiveLine"`, ` set -l flagPrefix ""
if set -q comps[1]; and string match -q -- '`+completionPathPrefixMarker+`-*' "$comps[1]"
set -l candidatePrefix (string replace -- '`+completionPathPrefixMarker+`' '' "$comps[1]")
if test (string sub -l (string length -- "$candidatePrefix") -- "$lastArg") = "$candidatePrefix"
set flagPrefix "$candidatePrefix"
set -e comps[1]
end
end
for comp in $comps`)
printf "%s%s\n" '`+completionPathPrefixMarker+`' "$flagPrefix"
for comp in $comps
printf "%s%s\n" "$flagPrefix" "$comp"
end
`)
if err != nil {
return "", err
}
@@ -302,10 +300,9 @@ func adjustFishCompletion(script, name string) (string, error) {
return "", err
}
pathContext := fmt.Sprintf(`set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) %% 2)
if test (count $%[1]s) -gt 0; and string match -q -- '%[2]s-*' "$%[1]s[1]"
if test (count $%[1]s) -gt 0; and string match -q -- '%[2]s*' "$%[1]s[1]"
set -l pathPrefix (string replace -- '%[2]s' '' "$%[1]s[1]")
set -l token (string unescape -- (commandline -ct))
if not string match -q -- '--*' "$pathPrefix"; and test (string sub -l (string length -- "$pathPrefix") -- "$token") = "$pathPrefix"
set -e %[1]s[1]
set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) %% 2)
if test $nofiles -eq 1
@@ -326,20 +323,10 @@ func adjustFishCompletion(script, name string) (string, error) {
end
__%[3]s_debug "Path completions: $%[1]s"
return 0
end
end`, variable, completionPathPrefixMarker, variableName)
script, err = replaceCompletionText(script, `set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2)`, pathContext)
if err != nil {
return "", err
}
directories := fmt.Sprintf(`if test $dirfilter -eq 1
set -l flagPrefix (string match -r -- '^-[^=]*=' (commandline -ct))
set --global %[1]s
for dir in (__fish_complete_directories)
set -a %[1]s "$flagPrefix$dir"
end
return 0
end
if test $filefilter -eq 1`, variable)
return replaceCompletionText(script, `if test $filefilter -eq 1; or test $dirfilter -eq 1`, directories)
return script, nil
}