Skip to content

Commit

Permalink
Fix bug in extracting variable values containing | (#81)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PatrickF1 authored Jan 15, 2021
1 parent 07eb7eb commit a734bbc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions functions/__fzf_extract_var_info.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/extract_var_info_test.fish
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a734bbc

Please sign in to comment.