completions/git: Remove a for-loop

This is an easy win for `git add ` completion time if we have multiple descriptions.

What happened was we did things once per description string, but the
things included a bunch of computation (including multiple `string`
calls and even a `realpath`!). Because these don't change, we can
simply do them once.

And it turns out we can just use a cartesian product:

for d in $desc
    printf '%s\t%s\n' $file $d
end

becomes

printf '%s\n' $file\t$desc
This commit is contained in:
Fabian Boehm 2023-02-06 21:22:43 +01:00
parent 96deaae7d8
commit f6b390dc61

View File

@ -324,33 +324,31 @@ function __fish_git_files
end
# Only try printing if the file was selected.
if set -q file[1]
for d in $desc
# Without "-z", git sometimes _quotes_ filenames.
# It adds quotes around it _and_ escapes the character.
# e.g. `"a\\b"`.
# We just remove the quotes and hope it works out.
# If this contains newlines or tabs,
# there is nothing we can do, but that's a general issue with scripted completions.
set file (string trim -c \" -- $file)
# The relative filename.
if string match -q './*' -- (commandline -ct)
printf './%s\t%s\n' $file $d
else
printf '%s\t%s\n' "$file" $d
end
# Now from repo root.
# Only do this if the filename isn't a simple child,
# or the current token starts with ":"
if string match -q '../*' -- $file
or string match -q ':*' -- (commandline -ct)
set -l fromroot (builtin realpath -- $file 2>/dev/null)
# `:` starts pathspec "magic", and the second `:` terminates it.
# `/` is the magic letter for "from repo root".
# If we didn't terminate it we'd have to escape any special chars
# (non-alphanumeric, glob or regex special characters, in whatever dialect git uses)
and set fromroot (string replace -- "$root/" ":/:" "$fromroot")
and printf '%s\t%s\n' "$fromroot" $d
end
# Without "-z", git sometimes _quotes_ filenames.
# It adds quotes around it _and_ escapes the character.
# e.g. `"a\\b"`.
# We just remove the quotes and hope it works out.
# If this contains newlines or tabs,
# there is nothing we can do, but that's a general issue with scripted completions.
set file (string trim -c \" -- $file)
# The relative filename.
if string match -q './*' -- (commandline -ct)
printf './%s\n' $file\t$desc
else
printf '%s\n' "$file"\t$desc
end
# Now from repo root.
# Only do this if the filename isn't a simple child,
# or the current token starts with ":"
if string match -q '../*' -- $file
or string match -q ':*' -- (commandline -ct)
set -l fromroot (builtin realpath -- $file 2>/dev/null)
# `:` starts pathspec "magic", and the second `:` terminates it.
# `/` is the magic letter for "from repo root".
# If we didn't terminate it we'd have to escape any special chars
# (non-alphanumeric, glob or regex special characters, in whatever dialect git uses)
and set fromroot (string replace -- "$root/" ":/:" "$fromroot")
and printf '%s\n' "$fromroot"\t$desc
end
end
end