From ed97b9eab63f53512afd8c64dd7f504a785efb28 Mon Sep 17 00:00:00 2001 From: Patrick Fong Date: Fri, 11 Jun 2021 10:10:08 -0700 Subject: [PATCH] Give search directory and search variables a simpler, canonical function 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. --- conf.d/fzf.fish | 4 ++-- functions/_fzf_extract_var_info.fish | 2 +- functions/_fzf_preview_file.fish | 4 ++-- ...f_search_current_dir.fish => _fzf_search_directory.fish} | 4 ++-- functions/_fzf_search_git_log.fish | 2 +- functions/_fzf_search_history.fish | 2 +- ...arch_shell_variables.fish => _fzf_search_variables.fish} | 4 ++-- functions/fzf_configure_bindings.fish | 2 +- tests/configure_bindings/key_sequences.fish | 6 +++--- tests/search_current_dir/base_dir_with_spaces.fish | 2 +- tests/search_current_dir/expands_var_in_query.fish | 2 +- tests/search_current_dir/expands_~_base_dir.fish | 2 +- tests/search_current_dir/no_base_dir_if_no_slash.fish | 2 +- tests/search_current_dir/no_dot_slash_if_unneeded.fish | 2 +- tests/search_current_dir/pass_custom_fd_opts.fish | 2 +- tests/search_shell_variables/no_args.fish | 2 +- 16 files changed, 22 insertions(+), 22 deletions(-) rename functions/{_fzf_search_current_dir.fish => _fzf_search_directory.fish} (92%) rename functions/{_fzf_search_shell_variables.fish => _fzf_search_variables.fish} (88%) diff --git a/conf.d/fzf.fish b/conf.d/fzf.fish index 9f448cd8..a419aec7 100644 --- a/conf.d/fzf.fish +++ b/conf.d/fzf.fish @@ -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 diff --git a/functions/_fzf_extract_var_info.fish b/functions/_fzf_extract_var_info.fish index 3e3c0207..716416cf 100644 --- a/functions/_fzf_extract_var_info.fish +++ b/functions/_fzf_extract_var_info.fish @@ -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[ diff --git a/functions/_fzf_preview_file.fish b/functions/_fzf_preview_file.fish index eedb4aac..29e74057 100644 --- a/functions/_fzf_preview_file.fish +++ b/functions/_fzf_preview_file.fish @@ -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 diff --git a/functions/_fzf_search_current_dir.fish b/functions/_fzf_search_directory.fish similarity index 92% rename from functions/_fzf_search_current_dir.fish rename to functions/_fzf_search_directory.fish index 8f0faaf4..dec6cc0e 100644 --- a/functions/_fzf_search_current_dir.fish +++ b/functions/_fzf_search_directory.fish @@ -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 diff --git a/functions/_fzf_search_git_log.fish b/functions/_fzf_search_git_log.fish index 98e62977..9d92c922 100644 --- a/functions/_fzf_search_git_log.fish +++ b/functions/_fzf_search_git_log.fish @@ -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 diff --git a/functions/_fzf_search_history.fish b/functions/_fzf_search_history.fish index 248bfe81..ce7b9c8b 100644 --- a/functions/_fzf_search_history.fish +++ b/functions/_fzf_search_history.fish @@ -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 ( diff --git a/functions/_fzf_search_shell_variables.fish b/functions/_fzf_search_variables.fish similarity index 88% rename from functions/_fzf_search_shell_variables.fish rename to functions/_fzf_search_variables.fish index 0dead936..86235e41 100644 --- a/functions/_fzf_search_shell_variables.fish +++ b/functions/_fzf_search_variables.fish @@ -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 diff --git a/functions/fzf_configure_bindings.fish b/functions/fzf_configure_bindings.fish index 60de0246..1c693e63 100644 --- a/functions/fzf_configure_bindings.fish +++ b/functions/fzf_configure_bindings.fish @@ -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 diff --git a/tests/configure_bindings/key_sequences.fish b/tests/configure_bindings/key_sequences.fish index fdaa446b..d770f5bf 100644 --- a/tests/configure_bindings/key_sequences.fish +++ b/tests/configure_bindings/key_sequences.fish @@ -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 @@ -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 diff --git a/tests/search_current_dir/base_dir_with_spaces.fish b/tests/search_current_dir/base_dir_with_spaces.fish index 8a270f4f..395b2830 100644 --- a/tests/search_current_dir/base_dir_with_spaces.fish +++ b/tests/search_current_dir/base_dir_with_spaces.fish @@ -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 diff --git a/tests/search_current_dir/expands_var_in_query.fish b/tests/search_current_dir/expands_var_in_query.fish index d357d5d2..b9ebd546 100644 --- a/tests/search_current_dir/expands_var_in_query.fish +++ b/tests/search_current_dir/expands_var_in_query.fish @@ -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 diff --git a/tests/search_current_dir/expands_~_base_dir.fish b/tests/search_current_dir/expands_~_base_dir.fish index 9bbd754a..053689d3 100644 --- a/tests/search_current_dir/expands_~_base_dir.fish +++ b/tests/search_current_dir/expands_~_base_dir.fish @@ -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) diff --git a/tests/search_current_dir/no_base_dir_if_no_slash.fish b/tests/search_current_dir/no_base_dir_if_no_slash.fish index 9130320d..ad3ad12c 100644 --- a/tests/search_current_dir/no_base_dir_if_no_slash.fish +++ b/tests/search_current_dir/no_base_dir_if_no_slash.fish @@ -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 diff --git a/tests/search_current_dir/no_dot_slash_if_unneeded.fish b/tests/search_current_dir/no_dot_slash_if_unneeded.fish index 21e87553..c21baa9c 100644 --- a/tests/search_current_dir/no_dot_slash_if_unneeded.fish +++ b/tests/search_current_dir/no_dot_slash_if_unneeded.fish @@ -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 diff --git a/tests/search_current_dir/pass_custom_fd_opts.fish b/tests/search_current_dir/pass_custom_fd_opts.fish index 0ad07230..bd7613b0 100644 --- a/tests/search_current_dir/pass_custom_fd_opts.fish +++ b/tests/search_current_dir/pass_custom_fd_opts.fish @@ -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 diff --git a/tests/search_shell_variables/no_args.fish b/tests/search_shell_variables/no_args.fish index a2c9ccf0..2ccc0ef2 100644 --- a/tests/search_shell_variables/no_args.fish +++ b/tests/search_shell_variables/no_args.fish @@ -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