Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions conf.d/fifc.fish
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ fifc \


# Fisher
function _fifc_install --on-event fifc_install
set -U _fifc_complist_sep ' / '
end

function _fifc_uninstall --on-event fifc_uninstall
set -e _fifc_complist_sep

for i in (seq (count $_fifc_unordered_comp))
set -e $_fifc_unordered_comp[$i]
end
Expand Down
32 changes: 12 additions & 20 deletions functions/_fifc.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ function _fifc
set -l fish_version (string split -- '.' $FISH_VERSION)
set -l result
set -Ux _fifc_extract_regex
set -gx _fifc_complist
set -gx _fifc_complist_path (string join '' (mktemp) "_fifc")
set -gx _fifc_custom_fzf_opts
set -gx fifc_token (commandline --current-token)
set -gx fifc_extracted
set -gx fifc_commandline
set -gx fifc_token (commandline --current-token)

# Get commandline buffer
if test "$argv" = ""
Expand All @@ -22,27 +22,19 @@ function _fifc
set complete_opts --escape
end

# Don't trigger fifc on empty commandline to avoid exceeding OS argument length limit
if test -n (string trim -- $fifc_commandline)
# Split using '/' as it can't be used in filenames
set -gx _fifc_complist ( \
complete -C $complete_opts "$fifc_commandline" \
| string join -- $_fifc_complist_sep \
)
complete -C $complete_opts "$fifc_commandline" | string split '\n' >$_fifc_complist_path

if string match --quiet --regex -- '\w+ +-+ *$' "$fifc_commandline"
set -e query
else
# Set intial query to the last token from commandline buffer
set query (string split -- ' ' $query)
set query $query[-1]
end
set -gx fifc_group (_fifc_completion_group)

set source_cmd (_fifc_action source)
if string match --quiet --regex -- '\w+ +-+ *$' "$fifc_commandline"
set -e query
else
set source_cmd "complete -C $complete_opts | awk -F \\t '{ print \$1 }'"
# Set intial query to the last token from commandline buffer
set query (string split -- ' ' $query)
set query $query[-1]
end

set source_cmd (_fifc_action source)
set -l fzf_cmd "
fzf \
-d \t \
Expand All @@ -64,7 +56,6 @@ function _fifc
set -l cmd (string join -- " | " $source_cmd $fzf_cmd)
# We use eval hack because wrapping source command
# inside a function cause some delay before fzf to show up
# set -l source_cmd (_fifc_source_cmd)
eval $cmd | while read -l token
set -a result (string escape -- $token)
# Perform extraction if needed
Expand All @@ -89,10 +80,11 @@ function _fifc

commandline --function repaint

rm $_fifc_complist_path
# Clean state
set -e _fifc_extract_regex
set -e _fifc_complist
set -e _fifc_custom_fzf_opts
set -e _fifc_complist_path
set -e fifc_token
set -e fifc_group
set -e fifc_extracted
Expand Down
16 changes: 7 additions & 9 deletions functions/_fifc_action.fish
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
function _fifc_action
# Can be either "preview", "action" or "source"
set -l action $argv[1]
set -l regex_val (string escape --style=regex -- "$argv[2]")
set -l comp $_fifc_ordered_comp $_fifc_unordered_comp
set -l regex_val (string escape --style=regex -- "$argv[2]")
# Escape '/' for sed processing
set regex_val (string replace '/' '\/' --all "$regex_val")

# Variables exposed to evaluated commands
set -x fifc_desc ( \
_fifc_split_complist \
| string match --regex --groups-only -- "$regex_val\h+(.*)" \
| string trim \
)
set -x fifc_group (_fifc_completion_group)
set -x fifc_desc (sed -nr (printf -- 's/^%s[[:blank:]]+(.*)/\\\1/p' "$regex_val") $_fifc_complist_path | string trim)
set -x fifc_candidate "$argv[2]"
set fifc_extracted (string match --regex --groups-only -- "$_fifc_extract_regex" "$argv[2]")
set -x fifc_extracted (string match --regex --groups-only -- "$_fifc_extract_regex" "$argv[2]")

if test "$action" = preview
set default_preview 1

else if test "$action" = source
set default_source 1
end
Expand All @@ -30,7 +28,7 @@ function _fifc_action
set condition true
end
if test -n "$$comp[$i][2]"
set regex "string match --regex --quiet -- '$$comp[$i][2]' '$fifc_commandline'"
set regex "string match --regex --quiet -- '$$comp[$i][2]' \"$fifc_commandline\""
else
set regex true
end
Expand Down
11 changes: 6 additions & 5 deletions functions/_fifc_completion_group.fish
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
function _fifc_completion_group -d "Determine completion group"
set -l path (_fifc_path_to_complete)
set -l path_candidate (_fifc_path_to_complete)
# Null means that either $path is empty or is not a directory
set -l is_null (ls -A $path 2> /dev/null | string collect)
set -l parsed_complist (_fifc_parse_complist)
set -l is_null (ls -A $path_candidate 2> /dev/null | string collect)
set -l complist (_fifc_parse_complist)

if test -n "$is_null"; and ls -d -- $parsed_complist &>/dev/null
# When complist is big, avoid calling ls with all arguments if first is neither a file nor a directory
if test -n "$is_null"; and test \( -f "$complist[1]" -o -d "$complist[1]" \); and echo (string escape -- $complist) | xargs ls -d -- &>/dev/null
echo files
else if string match --regex --quiet -- '\h+\-+\h*$' $fifc_commandline
echo options
else if _fifc_parse_complist | string join '' | string match --regex --quiet '^[0-9]+$'
else if string join -- '' (string escape -- $complist) | string match --regex --quiet '^[0-9]+$'
echo processes
end
end
2 changes: 1 addition & 1 deletion functions/_fifc_parse_complist.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function _fifc_parse_complist -d "Extract the first column of fish completion list"
_fifc_split_complist \
cat $_fifc_complist_path \
| string unescape \
| uniq \
| awk -F '\t' '{ print $1 }'
Expand Down
5 changes: 3 additions & 2 deletions functions/_fifc_path_to_complete.fish
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function _fifc_path_to_complete
if string match --regex --quiet -- '.*(\w|\.|/)+$' "$fifc_token"
echo "$fifc_token"
set -l token (string unescape $fifc_token)
if string match --regex --quiet -- '.*(\w|\.|/)+$' "$token"
echo "$token"
else
echo {$PWD}/
end
Expand Down
2 changes: 1 addition & 1 deletion functions/_fifc_source_files.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function _fifc_source_files -d "Return a command to recursively find files"
set -l path (_fifc_path_to_complete)
set -l path (_fifc_path_to_complete | string escape)
set -l hidden (string match "*." "$path")

if type -q fd
Expand Down
3 changes: 0 additions & 3 deletions functions/_fifc_split_complist.fish

This file was deleted.

14 changes: 3 additions & 11 deletions tests/exposed_vars.fish
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
set curr_fifc_unordered_comp $_fifc_unordered_comp
set dir "tests/_resources/dir with spaces"
set _fifc_complist_path (mktemp)

function _fifc_test_exposed_vars
switch $var
case candidate
echo -n "$fifc_candidate"
case group
echo -n "$fifc_group"
case token
echo -n "$fifc_token"
case commandline
echo -n "$fifc_commandline"
case extracted
echo -n "$fifc_extracted"
end
Expand All @@ -32,13 +27,10 @@ set var candidate
set actual (_fifc_action "preview" "$dir/file 1.txt")
@test "exposed vars fifc_candidate" "$actual" = "$dir/file 1.txt"

set var group
set _fifc_complist (string join $_fifc_complist_sep "$dir/file 1.txt" "$dir/file 2.txt")
set actual (_fifc_action "preview" "$dir/file 1.txt")
@test "exposed vars fifc_group" "$actual" = files

set var extracted
set -x fifc_extracted
set _fifc_extract_regex '.*/(.*\.txt)$'
set actual (_fifc_action "preview" "$dir/file 1.txt")
@test "exposed vars fifc_extracted" "$actual" = "file 1.txt"

rm $_fifc_complist_path
10 changes: 6 additions & 4 deletions tests/group.fish
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
set _fifc_complist_path (mktemp)

set _commandline "kill "
set _fifc_complist (complete -C --escape -- "$_commandline")
complete -C --escape -- "$_commandline" >$_fifc_complist_path
set fifc_commandline "$_commandline"
set actual (_fifc_completion_group)
@test "group test pid" "$actual" = processes

set _commandline "ls tests/_resources/dir with spaces/"
set _fifc_complist (complete -C --escape -- "$_commandline")
set _commandline "ls tests/_resources/dir\ with\ spaces/"
complete -C --escape -- "$_commandline" >$_fifc_complist_path
set fifc_commandline "$_commandline"
set actual (_fifc_completion_group)
@test "group test files" "$actual" = files

set _commandline "ls -"
set _fifc_complist (complete -C --escape -- "$_commandline")
complete -C --escape -- "$_commandline" >$_fifc_complist_path
set fifc_commandline "$_commandline"
set actual (_fifc_completion_group)
@test "group test options" "$actual" = options

set -e _fifc_complist
set -e fifc_commandline
rm $_fifc_complist_path
4 changes: 3 additions & 1 deletion tests/match_order.fish
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set curr_fifc_unordered_comp $_fifc_unordered_comp
set curr_fifc_ordered_comp $_fifc_ordered_comp
set dir "tests/_resources/dir with spaces"
set _fifc_complist_path (mktemp)

# Add unordered completions
set comp_1 \
Expand Down Expand Up @@ -31,7 +32,7 @@ set fifc_commandline "ls "
set actual (_fifc_action "preview" "$dir")
@test "preview match condition and regex second completion" "$actual" = comp_2

set _fifc_complist "fallback description"
echo "fallback description" >$_fifc_complist_path
set fifc_commandline "fallback "
set actual (_fifc_action "preview" 'fallback')
@test "preview fallback fish description" "$actual" = description
Expand All @@ -54,3 +55,4 @@ set actual (_fifc_action "preview" "$dir/file 1.txt")
set -e fifc_commandline
set -gx _fifc_unordered_comp $curr_fifc_unordered_comp
set -gx _fifc_ordered_comp $curr_fifc_ordered_comp
rm $_fifc_complist_path
2 changes: 2 additions & 0 deletions tests/source.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set curr_fifc_unordered_comp $_fifc_unordered_comp
set curr_fifc_ordered_comp $_fifc_ordered_comp
set _fifc_complist_path (mktemp)

# Add unordered sources
set comp_1 'test "$any" = "1"' '^kill $' '' '' 'echo comp_1'
Expand All @@ -23,3 +24,4 @@ set actual (_fifc_action "source")
set -e fifc_commandline
set -gx _fifc_unordered_comp $curr_fifc_unordered_comp
set -gx _fifc_unordered_comp $curr_fifc_ordered_comp
rm $_fifc_complist_path