Skip to content

Commit

Permalink
[bash-completion] Fix custom completion with dynamic loader enabled
Browse files Browse the repository at this point in the history
After _completion_loader is called, instead of loading the entire
completion.bash file, just restore the fzf completion for the current
command. `_fzf_orig_completion_$cmd` is only set if _completion_loader
actually changed the completion options to avoid infinite loop.

Close junegunn#1170
  • Loading branch information
junegunn committed Dec 3, 2017
1 parent 5a7b41a commit d6588fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions shell/completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ _fzf_opts_completion() {
}

_fzf_handle_dynamic_completion() {
local cmd orig_var orig ret orig_cmd
local cmd orig_var orig ret orig_cmd orig_complete
cmd="$1"
shift
orig_cmd="$1"
Expand All @@ -122,10 +122,14 @@ _fzf_handle_dynamic_completion() {
if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then
$orig "$@"
elif [ -n "$_fzf_completion_loader" ]; then
orig_complete=$(complete -p "$cmd")
_completion_loader "$@"
ret=$?
eval "$(complete | command grep "\-F.* $orig_cmd$" | _fzf_orig_completion_filter)"
source "${BASH_SOURCE[0]}"
# _completion_loader may not have updated completion for the command
if [ "$(complete -p "$cmd")" != "$orig_complete" ]; then
eval "$(complete | command grep "\-F.* $orig_cmd$" | _fzf_orig_completion_filter)"
eval "$orig_complete"
fi
return $ret
fi
}
Expand Down
21 changes: 21 additions & 0 deletions test/test_go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,27 @@ def setup
super
@tmux = Tmux.new :bash
end

def test_dynamic_completion_loader
tmux.paste 'touch /tmp/foo; _fzf_completion_loader=1'
tmux.paste '_completion_loader() { complete -o default fake; }'
tmux.paste 'complete -F _fzf_path_completion -o default -o bashdefault fake'
tmux.send_keys 'fake /tmp/foo**', :Tab
tmux.until do |lines|
lines.item_count.positive? && lines.item_count == lines.match_count
end
tmux.send_keys 'C-c'

tmux.prepare
tmux.send_keys 'fake /tmp/foo'
tmux.send_keys :Tab , 'C-u'

tmux.prepare
tmux.send_keys 'fake /tmp/foo**', :Tab
tmux.until do |lines|
lines.item_count.positive? && lines.item_count == lines.match_count
end
end
end

class TestZsh < TestBase
Expand Down

0 comments on commit d6588fc

Please sign in to comment.