Skip to content

Conversation

@Aloxaf
Copy link
Owner

@Aloxaf Aloxaf commented Mar 10, 2020

Fix #56

Aloxaf added 4 commits March 10, 2020 13:11
`-U` flag causes bug when compleing parameter. But remove it will
let _approximate won't work.
@Aloxaf
Copy link
Owner Author

Aloxaf commented Mar 10, 2020

I don't know why the result of _approximate can't be inserted. This may take a long time, I will merge some useful changes to master.

Aloxaf added a commit that referenced this pull request Mar 10, 2020
You can now use _approximate, but there is no fzf support.
And a better stability.
@intelfx
Copy link

intelfx commented May 19, 2024

I just tried to merge this on top of master. After resolving two obvious conflicts, this almost works, except tht if the _approximate completer is invoked, the captured prefix inserted into fzf contains a spurious (#a<N>) prefix (apologies for screenshot, not sure how to capture fzf UI reasonably):

image

I guess this is related to the games _approximate plays with compadd itself:

                if (( ! $+functions[compadd] ))
                then
                        dounfunction=1
                        compadd () {
                                local ppre="$argv[(I)-p]"
                                [[ ${argv[(I)-[a-zA-Z]#U[a-zA-Z]#]} -eq 0 && "${#:-$PREFIX$SUFFIX}" -le _comp_correct ]] && return
                                if [[ "$PREFIX" = \~* && ( ppre -eq 0 || "$argv[ppre+1]" != \~* ) ]]
                                then
                                        PREFIX="~(#a${_comp_correct})${PREFIX[2,-1]}"
                                else
                                        PREFIX="(#a${_comp_correct})$PREFIX"
                                fi
                                (( $_correct_group && ${${argv[1,(r)-(|-)]}[(I)-*[JV]]} )) && _correct_expl[_correct_group]=${argv[1,(r)-(-|)][(R)-*[JV]]}
                                builtin compadd "$_correct_expl[@]" "$@"
                        }
                fi

Any ideas or pointers to start chasing?

@intelfx
Copy link

intelfx commented May 19, 2024

I added a dirtiest hack in intelfx@7f1d458 and _approximate seems to work reasonably with fzf-tab now. The only thing left is to maybe preselect the [corrections] group if it exists — @Aloxaf, any ideas on what's happening here or how can I do that?

@alberti42
Copy link
Contributor

I wasn't aware of the 2020 attempt on the approximate branch - I just found out about it. I went back and skimmed the code to understand the direction it took.

My current PR #554 follows the same general idea (keeping _approximate inside fzf-tab), but it also addresses additional edge cases that were still causing the completion UI to fall back or insert the wrong prefix. It now passes the existing test suite.

I'd appreciate it if you could try the new implementation. Your feedback would be especially helpful to catch any remaining edge cases.

TL;DR:

What the old approximate branch did (high level)

  • It monkey-patched _approximate by copying its source and replacing builtin compadd with _fzf_tab_compadd, so fzf-tab could capture matches (fzf-tab.zsh:579).
  • It wrapped _approximate and temporarily removed the global compadd function while _approximate ran, so Zsh would define its local wrapper (fzf-tab.zsh:580-584).
  • It restored compadd and _approximate when disabling fzf-tab (fzf-tab.zsh:520-525).

That approach mostly targeted the "compadd bypass" problem. It didn't handle the other conflicts that make _approximate fall back to the standard menu or insert wrong prefixes.

How the current PR differs (high level)

  • It still patches _approximate, but does it in a more controlled wrapper (restores hooks in always, and forces IN_FZF_TAB so the capture path is reliable).
  • It sanitizes PREFIX when _approximate injects (#aN) so captured matches align with what Zsh expects to insert.
  • It respects compstate[list]=force so fzf-tab doesn't auto-accept an unambiguous prefix and skip the list.
  • It applies choices even when the original widget returns non-zero (common in _approximate/_correct list-only flows).
  • It passes the existing test suite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support _approximate completer

4 participants