junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
63.78k stars 2.37k forks source link

Completion for `unset` or `unalias` not working #3679

Closed norioxkimura closed 6 months ago

norioxkimura commented 6 months ago

Info

Problem / Steps to reproduce

Reproducing the bug #1795 in version 0.48.0.

It appears that the function __fzf_orig_completion() only handles complete -F, but not complete -a for unalias or complete -v for unset.

I am using Ubuntu 22.04 with the bash-completion package version 2.11.

junegunn commented 6 months ago

Thanks, I can reproduce.

This patch fixes the problem because bash-completion package doesn't add anything to alias or variable completion, but I'm not sure if this is the right way to do it. What do you think?

diff --git a/shell/completion.bash b/shell/completion.bash
index 7d34af2..c3571e2 100644
--- a/shell/completion.bash
+++ b/shell/completion.bash
@@ -291,6 +291,8 @@ _fzf_handle_dynamic_completion() {
     "$REPLY" "$@"
   elif [[ -n "${_fzf_completion_loader-}" ]]; then
     orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)
+    [[ $orig_complete =~ ' '-[av]' ' ]] && return
+
     _completion_loader "$@"
     ret=$?
     # _completion_loader may not have updated completion for the command
junegunn commented 6 months ago

A patch that works with both bash-completion 2.11 and 2.12.

diff --git a/shell/completion.bash b/shell/completion.bash
index 7d34af2..31cef6e 100644
--- a/shell/completion.bash
+++ b/shell/completion.bash
@@ -291,7 +291,7 @@ _fzf_handle_dynamic_completion() {
     "$REPLY" "$@"
   elif [[ -n "${_fzf_completion_loader-}" ]]; then
     orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)
-    _completion_loader "$@"
+    $_fzf_completion_loader "$@"
     ret=$?
     # _completion_loader may not have updated completion for the command
     if [[ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]]; then
@@ -516,8 +516,17 @@ a_cmds="
 # Preserve existing completion
 __fzf_orig_completion < <(complete -p $d_cmds $a_cmds ssh 2> /dev/null)

-if type _completion_loader > /dev/null 2>&1; then
-  _fzf_completion_loader=1
+if type _comp_load > /dev/null 2>&1; then
+  # _comp_load was added in bash-completion 2.12 to replace _completion_loader.
+  # We use it without -D option so that the function fails if no completion is
+  # provided for the command.
+  _fzf_completion_loader=_comp_load
+elif type __load_completion > /dev/null 2>&1; then
+  # In bash-completion 2.11, _completion_loader internally calls __load_completion
+  # and if it returns a non-zero status, it sets the default 'minimal' completion.
+  _fzf_completion_loader=__load_completion
+elif type _completion_loader > /dev/null 2>&1; then
+  _fzf_completion_loader=_completion_loader
 fi

 __fzf_defc() {