Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'complete-common-prefix' option. #384

Closed
Closed
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
60 changes: 40 additions & 20 deletions fzf-tab.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@

emulate -L zsh -o extended_glob

local _ftb_query _ftb_complist=() _ftb_headers=() command opts
local _ftb_query _ftb_complist=() _ftb_headers=() _ftb_query_prefix command opts
-ftb-generate-complist # sets `_ftb_complist`

-ftb-zstyle -s continuous-trigger continuous_trigger || {
Expand All @@ -127,37 +127,57 @@
fi
;;
*)
-ftb-generate-query # sets `_ftb_query`
-ftb-generate-header # sets `_ftb_headers`
-ftb-zstyle -s print-query print_query || print_query=alt-enter
-ftb-zstyle -s accept-line accept_line
-ftb-zstyle -s complete-common-prefix complete_common_prefix || complete_common_prefix=true

choices=("${(@f)"$(builtin print -rl -- $_ftb_headers $_ftb_complist | -ftb-fzf)"}")
ret=$?
# choices=(query_string expect_key returned_word)
-ftb-generate-query # sets `_ftb_query` and `_ftb_query_prefix`

# insert query string directly
if [[ $choices[2] == $print_query ]] || [[ -n $choices[1] && $#choices == 1 ]] ; then
local curr_prefix="${PREFIX##*/}"
if [[ "$complete_common_prefix" == true && $_ftb_query_prefix == 1 && "$curr_prefix" != "$_ftb_query"* ]]; then
local -A v=("${(@0)${_ftb_compcap[1]}}")
local -a args=("${(@ps:\1:)v[args]}")
[[ -z $args[1] ]] && args=() # don't pass an empty string
IPREFIX=$v[IPREFIX] PREFIX=$v[PREFIX] SUFFIX=$v[SUFFIX] ISUFFIX=$v[ISUFFIX]
# NOTE: should I use `-U` here?, ../f\tabcd -> ../abcd
builtin compadd "${args[@]:--Q}" -Q -- $choices[1]
builtin compadd "${${(@)args:#-f}[@]}" -- $_ftb_query

compstate[list]=
compstate[insert]=
if (( $#choices[1] > 0 )); then
compstate[insert]='2'
[[ $RBUFFER == ' '* ]] || compstate[insert]+=' '
if (( $#_ftb_query > 0 )); then
compstate[insert]='2'
[[ $RBUFFER == ' '* ]] && compstate[insert]+=' '
fi
return $ret
fi
choices[1]=()
return 0
else
-ftb-generate-header # sets `_ftb_headers`
-ftb-zstyle -s print-query print_query || print_query=alt-enter
-ftb-zstyle -s accept-line accept_line

choices=("${(@f)"$(builtin print -rl -- $_ftb_headers $_ftb_complist | -ftb-fzf)"}")
ret=$?
# choices=(query_string expect_key returned_word)

# insert query string directly
if [[ $choices[2] == $print_query ]] || [[ -n $choices[1] && $#choices == 1 ]] ; then
local -A v=("${(@0)${_ftb_compcap[1]}}")
local -a args=("${(@ps:\1:)v[args]}")
[[ -z $args[1] ]] && args=() # don't pass an empty string
IPREFIX=$v[IPREFIX] PREFIX=$v[PREFIX] SUFFIX=$v[SUFFIX] ISUFFIX=$v[ISUFFIX]
# NOTE: should I use `-U` here?, ../f\tabcd -> ../abcd
builtin compadd "${args[@]:--Q}" -Q -- $choices[1]

compstate[list]=
compstate[insert]=
if (( $#choices[1] > 0 )); then
compstate[insert]='2'
[[ $RBUFFER == ' '* ]] || compstate[insert]+=' '
fi
return $ret
fi
choices[1]=()

choices=("${(@)${(@)choices%$nul*}#*$nul}")
choices=("${(@)${(@)choices%$nul*}#*$nul}")

unset CTXT
unset CTXT
fi
;;
esac

Expand Down
3 changes: 3 additions & 0 deletions lib/-ftb-generate-query
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fi

local key qtype tmp query_string
typeset -g _ftb_query=
typeset -g _ftb_query_prefix=0
-ftb-zstyle -a query-string query_string || query_string=(prefix input first)
for qtype in $query_string; do
if [[ $qtype == prefix ]]; then
Expand All @@ -23,6 +24,7 @@ for qtype in $query_string; do
tmp[$#MATCH/2+1,-1]=""
prefix[$#MATCH/2+1,-1]=()
done
_ftb_query_prefix=1
elif [[ $qtype == input ]]; then
local fv=${_ftb_compcap[1]#*$'\2'}
local -A v=("${(@0)fv}")
Expand All @@ -31,6 +33,7 @@ for qtype in $query_string; do
tmp=${tmp/%$v[SUFFIX]}
fi
tmp=${${tmp#$v[hpre]}#$v[apre]}
_ftb_query_prefix=0
fi
if (( $query_string[(I)longest] )); then
(( $#tmp > $#_ftb_query )) && _ftb_query=$tmp
Expand Down
30 changes: 28 additions & 2 deletions test/fzftab.ztst
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,20 @@
>C1:{file1}
>C1:{file2}

comptesteval "zstyle ':fzf-tab:*' complete-common-prefix false"
comptest $': d\t'
0:prefix
0:prefix (complete-common disabled)
>line: {: dir1/}{}
>QUERY:{dir}
>DESCRIPTION:{file}
>C1:{dir1/}
>C1:{dir2/}

comptesteval "zstyle ':fzf-tab:*' complete-common-prefix true"
comptest $': d\t'
0:prefix (complete-common enabled)
>line: {: dir}{}

comptesteval '_tst () { compadd d c b a }'
comptest $'tst \t'
0:normal
Expand Down Expand Up @@ -125,14 +131,34 @@
0:complete in word(with known bug)
>line: {: ./abc\ def}{ def}

comptesteval "zstyle ':fzf-tab:*' complete-common-prefix false"
comptesteval 'mkdir -p abc/def/hij abc/dfe/hij'
comptest $': ./a/d/h\t'
comptesteval 'rm -rd abc'
0:nested directory
0:nested directory (common prefix disabled)
>line: {: ./abc/def/h}{}
>QUERY:{d}
>DESCRIPTION:{file}
>C1:{def/}
>C1:{dfe/}

comptesteval "zstyle ':fzf-tab:*' complete-common-prefix true"
comptesteval 'mkdir -p abc/def/hij abc/dfe/hij'
comptest $': ./a/d/h\t'
comptesteval 'rm -rd abc'
0:nested directory, 1 tab (common prefix enabled)
>line: {: ./abc/d}{/h}

comptesteval "zstyle ':fzf-tab:*' complete-common-prefix true"
comptesteval 'mkdir -p abc/def/hij abc/dfe/hij'
comptest $': ./a/d/h\t\t'
comptesteval 'rm -rd abc'
0:nested directory, 2 tabs (common prefix enabled)
>line: {: ./abc/d}{/h}
>line: {: ./abc/d}{/h}
>QUERY:{d}
>DESCRIPTION:{file}
>C1:{def/}
>C1:{dfe/}

comptesteval '_tst() { a=(a); _describe "group1" a; a=(b); _describe "group2" a }'
Expand Down
Loading