Skip to content

Commit

Permalink
Give search directory and search variables a simpler, canonical funct…
Browse files Browse the repository at this point in the history
…ion name

I've wanted to rename these functions for a while now ever since I
decided to refer to them in the documentation as [Search directory] and
[Search variables]. These names are not only shorter but now directly
map to what they do. Futhermore, all the other function names followed
the same standard. However, I could not make this change until I
overhauled custom key bindings, as it would have broken custom key
bindings for users and would require some manual and tedious effort to
migrate. Now that users have to migrate to custom key bindings anyway,
and it is easy to do, this is the opportune time to make this change.
  • Loading branch information
PatrickF1 committed Jun 11, 2021
1 parent a7b9608 commit ed97b9e
Show file tree
Hide file tree
Showing 16 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions conf.d/fzf.fish
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Because of scoping rules, to capture the shell variables exactly as they are, we must read
# them before even executing _fzf_search_shell_variables. We use psub to store the
# them before even executing _fzf_search_variables. We use psub to store the
# variables' info in temporary files and pass in the filenames as arguments.
# # This variable is global so that it can be referenced by fzf_configure_bindings and in tests
set --global _fzf_search_vars_command '_fzf_search_shell_variables (set --show | psub) (set --names | psub)'
set --global _fzf_search_vars_command '_fzf_search_variables (set --show | psub) (set --names | psub)'

# Install the default bindings, which are mnemonic and minimally conflict with fish's preset bindings
fzf_configure_bindings
Expand Down
2 changes: 1 addition & 1 deletion functions/_fzf_extract_var_info.fish
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# helper function for _fzf_search_shell_variables
# helper function for _fzf_search_variables
function _fzf_extract_var_info --argument-names variable_name set_show_output --description "Extract and reformat lines pertaining to \$variable_name from \$set_show_output."
# Extract only the lines about the variable, all of which begin with either
# $variable_name: ...or... $variable_name[
Expand Down
4 changes: 2 additions & 2 deletions functions/_fzf_preview_file.fish
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# helper function for _fzf_search_current_dir
# helper function for _fzf_search_directory
function _fzf_preview_file --description "Print a preview for the given file based on its file type."
# because there's no way to guarantee that _fzf_search_current_dir passes the path to _fzf_preview_file
# because there's no way to guarantee that _fzf_search_directory passes the path to _fzf_preview_file
# as one argument, we collect all the arguments into one single variable and treat that as the path
set file_path $argv

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function _fzf_search_current_dir --description "Search the current directory. Replace the current token with the selected file paths."
function _fzf_search_directory --description "Search the current directory. Replace the current token with the selected file paths."
# Make sure that fzf uses fish to execute _fzf_preview_file.
# See similar comment in _fzf_search_shell_variables.fish.
# See similar comment in _fzf_search_variables.fish.
set --local --export SHELL (command --search fish)

set fd_opts --color=always $fzf_fd_opts
Expand Down
2 changes: 1 addition & 1 deletion functions/_fzf_search_git_log.fish
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function _fzf_search_git_log --description "Search the output of git log and pre
echo '_fzf_search_git_log: Not in a git repository.' >&2
else
# Make sure that fzf uses fish to execute git show.
# See similar comment in _fzf_search_shell_variables.fish.
# See similar comment in _fzf_search_variables.fish.
set --local --export SHELL (command --search fish)

# see documentation for git format placeholders at https://git-scm.com/docs/git-log#Documentation/git-log.txt-emnem
Expand Down
2 changes: 1 addition & 1 deletion functions/_fzf_search_history.fish
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function _fzf_search_history --description "Search command history. Replace the
builtin history merge

# Make sure that fzf uses fish so we can run fish_indent.
# See similar comment in _fzf_search_shell_variables.fish.
# See similar comment in _fzf_search_variables.fish.
set --local --export SHELL (command --search fish)

set command_with_ts (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# This function expects the following two arguments:
# argument 1 = output of (set --show | psub), i.e. a file with the scope info and values of all variables
# argument 2 = output of (set --names | psub), i.e. a file with all variable names
function _fzf_search_shell_variables --argument-names set_show_output set_names_output --description "Search and preview shell variables. Replace the current token with the selected variable."
function _fzf_search_variables --argument-names set_show_output set_names_output --description "Search and preview shell variables. Replace the current token with the selected variable."
if test -z "$set_names_output"
printf '%s\n' '_fzf_search_shell_variables requires 2 arguments.' >&2
printf '%s\n' '_fzf_search_variables requires 2 arguments.' >&2

commandline --function repaint
return 22 # 22 means invalid argument in POSIX
Expand Down
2 changes: 1 addition & 1 deletion functions/fzf_configure_bindings.fish
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function fzf_configure_bindings --description "Installs the default key bindings
end

for mode in default insert
test -n $key_sequences[1] && bind --mode $mode $key_sequences[1] _fzf_search_current_dir
test -n $key_sequences[1] && bind --mode $mode $key_sequences[1] _fzf_search_directory
test -n $key_sequences[2] && bind --mode $mode $key_sequences[2] _fzf_search_git_log
test -n $key_sequences[3] && bind --mode $mode $key_sequences[3] _fzf_search_git_status
test -n $key_sequences[4] && bind --mode $mode $key_sequences[4] _fzf_search_history
Expand Down
6 changes: 3 additions & 3 deletions tests/configure_bindings/key_sequences.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ function binding_contains_func --argument-names sequence function_
string match --entire $function_ (bind $sequence) >/dev/null
end

@test "default binding for directory works" (binding_contains_func \e\cf _fzf_search_current_dir) $status -eq 0
@test "default binding for directory works" (binding_contains_func \e\cf _fzf_search_directory) $status -eq 0
@test "default binding for git log works" (binding_contains_func \e\cl _fzf_search_git_log) $status -eq 0
@test "default binding for git status works" (binding_contains_func \e\cs _fzf_search_git_status) $status -eq 0
@test "default binding for history works" (binding_contains_func \cr _fzf_search_history) $status -eq 0
@test "default binding for variables works" (binding_contains_func \cv $_fzf_search_vars_command) $status -eq 0

fzf_configure_bindings --directory=\ca --git_log=\cb --git_status=\cc --history=\cd --variables=\ce
@test "can override the default binding for directory" (binding_contains_func \ca _fzf_search_current_dir) $status -eq 0
@test "can override the default binding for directory" (binding_contains_func \ca _fzf_search_directory) $status -eq 0
@test "can override the default binding for git log" (binding_contains_func \cb _fzf_search_git_log) $status -eq 0
@test "can override the default binding for git status" (binding_contains_func \cc _fzf_search_git_status) $status -eq 0
@test "can override the default binding for history" (binding_contains_func \cd _fzf_search_history) $status -eq 0
Expand All @@ -23,7 +23,7 @@ _fzf_uninstall_bindings

# intentionally test both style of passing options with no value
fzf_configure_bindings --directory --git_status=
@test "can erase bindings by passing no key sequence" -z (bind --user | string match --entire --regex '_fzf_search_current_dir|_fzf_search_git_status')
@test "can erase bindings by passing no key sequence" -z (bind --user | string match --entire --regex '_fzf_search_directory|_fzf_search_git_status')
binding_contains_func \e\cl _fzf_search_git_log && binding_contains_func \cr _fzf_search_history && binding_contains_func \cv $_fzf_search_vars_command
@test "installs default bindings that aren't customized" $status -eq 0

Expand Down
2 changes: 1 addition & 1 deletion tests/search_current_dir/base_dir_with_spaces.fish
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ mock commandline "--current-token --replace --" "echo \$argv"
mock commandline \* ""
set --export --append FZF_DEFAULT_OPTS "--filter=''" # automatically select all input lines sent to fzf

set actual (_fzf_search_current_dir)
set actual (_fzf_search_directory)
set expected "'tests/_resources/multi word dir/file 1.txt' 'tests/_resources/multi word dir/file 2.txt'"
@test "uses current token as base directory if it ends in / and is a directory" "$actual" = $expected
2 changes: 1 addition & 1 deletion tests/search_current_dir/expands_var_in_query.fish
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ mock commandline --current-token "echo \$target"
mock commandline "--current-token --replace --" "echo \$argv"
mock commandline \* ""
# string unescape because the args passed into commandline --current-token --replace are escaped
set actual (string unescape (_fzf_search_current_dir))
set actual (string unescape (_fzf_search_directory))

@test "expands variables in current token" (basename $actual) = $target
2 changes: 1 addition & 1 deletion tests/search_current_dir/expands_~_base_dir.fish
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ mock fzf \* ""
mock commandline --current-token "echo \~/"
mock commandline "--current-token --replace" ""
mock commandline \* ""
_fzf_search_current_dir
_fzf_search_directory
set expected_arg "--base-directory=$HOME"
@test "~/ is expanded to HOME" -n (string match --entire -- $expected_arg $fd_captured_opts)
2 changes: 1 addition & 1 deletion tests/search_current_dir/no_base_dir_if_no_slash.fish
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ function fzf
end
end

_fzf_search_current_dir
_fzf_search_directory

@test "doesn't change fd's base directory if no slash on current token" $searched_functions_dir = true
2 changes: 1 addition & 1 deletion tests/search_current_dir/no_dot_slash_if_unneeded.fish
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ mock fd \* ""
mock fzf \* "echo /Users/patrickf"

# since there is already a /, no ./ should be prepended
set result (_fzf_search_current_dir)
set result (_fzf_search_directory)
@test "doesn't prepend ./ if path already starts with /" "$result" = /Users/patrickf
2 changes: 1 addition & 1 deletion tests/search_current_dir/pass_custom_fd_opts.fish
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ end
mock commandline "*" ""
set fzf_fd_opts --hidden --exclude=.git

_fzf_search_current_dir
_fzf_search_directory
@test "uses fzf_fd_opts when executing fd" "$searched_hidden" = true
2 changes: 1 addition & 1 deletion tests/search_shell_variables/no_args.fish
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mock fzf \* "" # do nothing if we reach fzf so we don't get stuck
_fzf_search_shell_variables 2>/dev/null
_fzf_search_variables 2>/dev/null
@test "fails if no arguments given" $status -ne 0

0 comments on commit ed97b9e

Please sign in to comment.