Skip to content
Myr

[fish] Refactor __fzf_parse_commandline, remove __fzf_get_dir

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The __fzf_get_dir function was called only once, and was basically a single command in a while loop.

authored by bitraid · committed by Junegunn Choi · parent f232df2 · Browse files · 1 file+22 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+22 added−38 removed

  • other:+22 added−38 removed

1 file+22 added−38 removed

modified:shell/key-bindings.fish+22 added-38 removed
134 unmodified lines
135 bind -M insert \ec fzf-cd-widget136 end137138 function __fzf_parse_commandline -d 'Parse the current command line token and return split of existing filepath, fzf query, and optional -option= prefix'139 set -l commandline (commandline -t)139 set -l dir '.'140 set -l query141 set -l commandline (commandline -t | string unescape -n)142141 # strip -option= from token if present143 # Strip -option= from token if present144 set -l prefix (string match -r -- '^-[^\s=]+=' $commandline)145 set commandline (string replace -- "$prefix" '' $commandline)146147 # Enable home directory expansion of leading ~/148 set commandline (string replace -r -- '^~/' '\$HOME/' $commandline)149148 # escape special characters, except for the $ sign of valid variable names,149 # so that after eval, the original string is returned, but with the150 # variable names replaced by their values.150 # Escape special characters, except for the $ sign of valid variable names,151 # so that the original string with expanded variables is returned after eval.152 set commandline (string escape -n -- $commandline)152 set commandline (string replace -r -a -- '\x5c\$(?=[\w])' '\$' $commandline)153 set commandline (string replace -r -a -- '\\\\\$(?=[\w])' '\$' $commandline)154155 # eval is used to do shell expansion on paths156 eval set commandline $commandline157157 # Combine multiple consecutive slashes into one, and unescape.158 set commandline (string replace -r -a -- '/+' '/' $commandline | string unescape -n)158 # Combine multiple consecutive slashes into one.159 set commandline (string replace -r -a -- '/+' '/' $commandline)160160 if test -z "$commandline"161 # Default to current directory with no --query162 set dir '.'163 set fzf_query ''164 else165 set dir (__fzf_get_dir $commandline)161 if test -n "$commandline"162 # Strip trailing slash, unless $dir is root dir (/)163 set dir (string replace -r -- '(?<!^)/$' '' $commandline)164167 # BUG: on combined expressions, if a left argument is a single `!`, the168 # builtin test command of fish will treat it as the ! operator. To169 # overcome this, have the variable parts on the right.170 if test "." = "$dir" -a "./" != (string sub -l 2 -- $commandline)171 # if $dir is "." but commandline is not a relative path, this means no file path found165 # Set $dir to the longest existing filepath166 while not test -d "$dir"167 # If path is absolute, this can keep going until ends up at /168 # If path is relative, this can keep going until entire input is consumed, dirname returns "."169 set dir (dirname -- $dir)170 end171172 if test "$dir" = '.'; and test (string sub -l 2 -- $commandline) != './'173 # If $dir is "." but commandline is not a relative path, this means no file path found174 set fzf_query $commandline175 else176 # Also remove trailing slash after dir, to "split" input properly177 set fzf_query (string replace -r -- "^$dir/?" '' $commandline)178 end179 end180179 echo -- $dir180 string escape -- $fzf_query181 echo -- $prefix181 string escape -n -- "$dir" "$fzf_query" "$prefix"182 end183184 function __fzf_get_dir -d 'Find the longest existing filepath from input string'185 set dir $argv186187 # Strip trailing slash, unless $dir is root dir (/)188 set dir (string replace -r -- '(?<!^)/$' '' $dir)189190 # Iteratively check if dir exists and strip tail end of path191 while test ! -d "$dir"192 # If path is absolute, this can keep going until ends up at /193 # If path is relative, this can keep going until entire input is consumed, dirname returns "."194 set dir (dirname -- "$dir")195 end196197 string escape -n -- $dir198 end199184end