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