From a734bbcaa7f4d944d30a70b622bda41443bd9af8 Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 15 Jan 2021 11:47:52 -0800 Subject: [PATCH] Fix bug in extracting variable values containing | (#81) The bug happens whenever the variable being previewed contains a |. What happens is that the third regex used in __fzf_extract_var_info is greedy and will over-remove text up to the second to last |. For example: $ set variable "a | b | c | d" $ __fzf_extract_var_info variable (set --show | psub) set in global scope, unexported, with 1 elements [1] d By making the third regex lazy, it now works properly: $ set variable "a | b | c | d" $ __fzf_extract_var_info variable (set --show | psub) set in global scope, unexported, with 1 elements [1] a | b | c | d Additionally, I've added a Fishtape test to make debugging these tricky regexes more easily in the future, which I expect to need to do as the master branch of Fish has already changed the output of set --show. --- functions/__fzf_extract_var_info.fish | 4 ++-- tests/extract_var_info_test.fish | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 tests/extract_var_info_test.fish diff --git a/functions/__fzf_extract_var_info.fish b/functions/__fzf_extract_var_info.fish index 0209c9ba..53b7f2f5 100644 --- a/functions/__fzf_extract_var_info.fish +++ b/functions/__fzf_extract_var_info.fish @@ -6,7 +6,7 @@ function __fzf_extract_var_info --argument-names variable_name set_show_output - # $variable_name[ string match --entire --regex "^\\\$$variable_name(?:: set|\[)" <$set_show_output | - # Strip the variable name from the variable info, replacing... + # Strip the variable name from the scope info, replacing... # $variable_name: set in global scope # ...with... # set in global scope @@ -16,7 +16,7 @@ function __fzf_extract_var_info --argument-names variable_name set_show_output - # $variable_name[1]: length=14 value=|variable_value| # ...with... # [1] variable_value - string replace --regex "^\\\$$variable_name(\[.+\]).+\|(.+)\|\$" '\$1 \$2' + string replace --regex "^\\\$$variable_name(\[\d+\]).+?\|(.+)\|\$" '\$1 \$2' # Final output example for $PATH: # set in global scope, unexported, with 5 elements diff --git a/tests/extract_var_info_test.fish b/tests/extract_var_info_test.fish new file mode 100644 index 00000000..53b86ea7 --- /dev/null +++ b/tests/extract_var_info_test.fish @@ -0,0 +1,7 @@ +@echo === extract_var_info === + +set --local variable "| a | b | c | d |" +set --local actual (__fzf_extract_var_info variable (set --show | psub) | string collect) +set --local expected "set in local scope, unexported, with 1 elements +[1] | a | b | c | d |" +@test "vars containing '|'" $actual = $expected