fix: interactive completion for fish (#385)

Co-authored-by: Ajeet D'Souza <98ajeet@gmail.com>
This commit is contained in:
NextAlone 2022-04-27 21:35:52 +08:00 committed by GitHub
parent 6b2c6a2bc3
commit 7d0ddedc6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 46 deletions

View File

@ -15,7 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Fish: fix paths on Cygwin. - Bash/Fish/Posix/Zsh: paths on Cygwin.
- Fish: completions not working on certain systems.
## [0.8.1] - 2021-04-23 ## [0.8.1] - 2021-04-23

View File

@ -34,7 +34,8 @@ function __zoxide_cd() {
{%- if hook == InitHook::Prompt %} {%- if hook == InitHook::Prompt %}
function __zoxide_hook() { function __zoxide_hook() {
\builtin local -r retval="$?" \builtin local -r retval="$?"
\command zoxide add -- "$(__zoxide_pwd || \builtin true)" # shellcheck disable=SC2312
\command zoxide add -- "$(__zoxide_pwd)"
return "${retval}" return "${retval}"
} }
{%- else if hook == InitHook::Pwd %} {%- else if hook == InitHook::Pwd %}
@ -80,7 +81,8 @@ function __zoxide_z() {
__zoxide_cd "{{ "${result:${#__zoxide_z_prefix}}" }}" __zoxide_cd "{{ "${result:${#__zoxide_z_prefix}}" }}"
else else
\builtin local result \builtin local result
result="$(\command zoxide query --exclude "$(__zoxide_pwd || \builtin true)" -- "$@")" && # shellcheck disable=SC2312
result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" &&
__zoxide_cd "${result}" __zoxide_cd "${result}"
fi fi
} }
@ -98,19 +100,12 @@ function __zoxide_zi() {
{%- match cmd %} {%- match cmd %}
{%- when Some with (cmd) %} {%- when Some with (cmd) %}
# Remove definitions. \builtin unalias {{cmd}} &>/dev/null || \builtin true
function __zoxide_unset() {
\builtin unset -f "$@" &>/dev/null
\builtin unset -v "$@" &>/dev/null
\builtin unalias "$@" &>/dev/null || \builtin :
}
__zoxide_unset {{cmd}}
function {{cmd}}() { function {{cmd}}() {
__zoxide_z "$@" __zoxide_z "$@"
} }
__zoxide_unset {{cmd}}i \builtin unalias {{cmd}}i &>/dev/null || \builtin true
function {{cmd}}i() { function {{cmd}}i() {
__zoxide_zi "$@" __zoxide_zi "$@"
} }
@ -136,13 +131,15 @@ if [[ ${BASH_VERSINFO[0]:-0} -eq 4 && ${BASH_VERSINFO[1]:-0} -ge 4 || ${BASH_VER
# If there is a space after the last word, use interactive selection. # If there is a space after the last word, use interactive selection.
elif [[ -z ${COMP_WORDS[-1]} ]]; then elif [[ -z ${COMP_WORDS[-1]} ]]; then
\builtin local result \builtin local result
result="$(\command zoxide query -i -- "{{ "${COMP_WORDS[@]:1:${#COMP_WORDS[@]}-2}" }}")" && # shellcheck disable=SC2312
result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -i -- "{{ "${COMP_WORDS[@]:1:${#COMP_WORDS[@]}-2}" }}")" &&
COMPREPLY=("${__zoxide_z_prefix}${result@Q}") COMPREPLY=("${__zoxide_z_prefix}${result@Q}")
\builtin printf '\e[5n' \builtin printf '\e[5n'
fi fi
} }
\builtin complete -F __zoxide_z_complete -o nospace -- {{cmd}} \builtin complete -F __zoxide_z_complete -o nospace -- {{cmd}}
\builtin complete -r {{cmd}}i &>/dev/null || \builtin true
fi fi
{%- when None %} {%- when None %}

View File

@ -62,15 +62,21 @@ end
# When using zoxide with --no-cmd, alias these internal functions as desired. # When using zoxide with --no-cmd, alias these internal functions as desired.
# #
set __zoxide_z_prefix 'z#'
# Jump to a directory using only keywords. # Jump to a directory using only keywords.
function __zoxide_z function __zoxide_z
set argc (count $argv) set -l argc (count $argv)
set -l completion_regex '^'(string escape --style=regex $__zoxide_z_prefix)'(.*)$'
if test $argc -eq 0 if test $argc -eq 0
__zoxide_cd $HOME __zoxide_cd $HOME
else if test "$argv" = - else if test "$argv" = -
__zoxide_cd - __zoxide_cd -
else if test $argc -eq 1 -a -d $argv[1] else if test $argc -eq 1 -a -d $argv[1]
__zoxide_cd $argv[1] __zoxide_cd $argv[1]
else if set -l result (string match --groups-only --regex $completion_regex $argv[-1])
__zoxide_cd $result
else else
set -l result (command zoxide query --exclude (__zoxide_pwd) -- $argv) set -l result (command zoxide query --exclude (__zoxide_pwd) -- $argv)
and __zoxide_cd $result and __zoxide_cd $result
@ -88,8 +94,8 @@ function __zoxide_z_complete
else if test (count $tokens) -eq (count $curr_tokens) else if test (count $tokens) -eq (count $curr_tokens)
# If the last argument is empty, use interactive selection. # If the last argument is empty, use interactive selection.
set -l query $tokens[2..-1] set -l query $tokens[2..-1]
set -l result (zoxide query -i -- $query) set -l result (zoxide query --exclude (__zoxide_pwd) -i -- $query)
and commandline --current-process "$tokens[1] "(string escape $result) and echo $__zoxide_z_prefix$result
commandline --function repaint commandline --function repaint
end end
end end
@ -107,18 +113,18 @@ end
{%- match cmd %} {%- match cmd %}
{%- when Some with (cmd) %} {%- when Some with (cmd) %}
abbr --erase {{cmd}}
complete -c {{cmd}} -e
function {{cmd}} function {{cmd}}
__zoxide_z $argv __zoxide_z $argv
end end
abbr --erase {{cmd}}
complete -c {{cmd}} -e
complete -c {{cmd}} -f -a '(__zoxide_z_complete)' complete -c {{cmd}} -f -a '(__zoxide_z_complete)'
abbr --erase {{cmd}}i
complete -c {{cmd}}i -e
function {{cmd}}i function {{cmd}}i
__zoxide_zi $argv __zoxide_zi $argv
end end
abbr --erase {{cmd}}i
complete -c {{cmd}}i -e
{%- when None %} {%- when None %}

View File

@ -7,7 +7,9 @@
# pwd based on the value of _ZO_RESOLVE_SYMLINKS. # pwd based on the value of _ZO_RESOLVE_SYMLINKS.
__zoxide_pwd() { __zoxide_pwd() {
{%- if resolve_symlinks %} {%- if cfg!(windows) %}
\command cygpath -w "$(\builtin pwd -P)"
{%- else if resolve_symlinks %}
\command pwd -P \command pwd -P
{%- else %} {%- else %}
\command pwd -L \command pwd -L
@ -82,20 +84,12 @@ __zoxide_zi() {
{%- match cmd %} {%- match cmd %}
{%- when Some with (cmd) %} {%- when Some with (cmd) %}
# Remove definitions. \command unalias {{cmd}} >/dev/null 2>&1 || \true
__zoxide_unset() {
\command unset -f "$@" >/dev/null 2>&1
\command unset -v "$@" >/dev/null 2>&1
# shellcheck disable=SC1001
\command unalias "$@" >/dev/null 2>&1 || \:
}
__zoxide_unset '{{cmd}}'
{{cmd}}() { {{cmd}}() {
__zoxide_z "$@" __zoxide_z "$@"
} }
__zoxide_unset '{{cmd}}i' \command unalias {{cmd}}i >/dev/null 2>&1 || \true
{{cmd}}i() { {{cmd}}i() {
__zoxide_zi "$@" __zoxide_zi "$@"
} }

View File

@ -32,7 +32,8 @@ function __zoxide_cd() {
{% else -%} {% else -%}
# Hook to add new entries to the database. # Hook to add new entries to the database.
function __zoxide_hook() { function __zoxide_hook() {
\command zoxide add -- "$(__zoxide_pwd || \builtin true)" # shellcheck disable=SC2312
\command zoxide add -- "$(__zoxide_pwd)"
} }
# Initialize hook. # Initialize hook.
@ -74,7 +75,8 @@ function __zoxide_z() {
__zoxide_cd "{{ "${result:${#__zoxide_z_prefix}}" }}" __zoxide_cd "{{ "${result:${#__zoxide_z_prefix}}" }}"
else else
\builtin local result \builtin local result
result="$(\command zoxide query --exclude "$(__zoxide_pwd || \builtin true)" -- "$@")" && # shellcheck disable=SC2312
result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" &&
__zoxide_cd "${result}" __zoxide_cd "${result}"
fi fi
} }
@ -92,25 +94,17 @@ function __zoxide_zi() {
{%- match cmd %} {%- match cmd %}
{%- when Some with (cmd) %} {%- when Some with (cmd) %}
# Remove definitions. \builtin unalias {{cmd}} &>/dev/null || \builtin true
function __zoxide_unset() {
\builtin unalias "$@" &>/dev/null || \builtin true
\builtin unfunction "$@" &>/dev/null || \builtin true
\builtin unset "$@" &>/dev/null
}
__zoxide_unset {{cmd}}
function {{cmd}}() { function {{cmd}}() {
__zoxide_z "$@" __zoxide_z "$@"
} }
__zoxide_unset {{cmd}}i \builtin unalias {{cmd}}i &>/dev/null || \builtin true
function {{cmd}}i() { function {{cmd}}i() {
__zoxide_zi "$@" __zoxide_zi "$@"
} }
if [[ -o zle ]]; then if [[ -o zle ]]; then
__zoxide_unset __zoxide_z_complete
function __zoxide_z_complete() { function __zoxide_z_complete() {
# Only show completions when the cursor is at the end of the line. # Only show completions when the cursor is at the end of the line.
# shellcheck disable=SC2154 # shellcheck disable=SC2154
@ -120,8 +114,8 @@ if [[ -o zle ]]; then
_files -/ _files -/
elif [[ "${words[-1]}" == '' ]]; then elif [[ "${words[-1]}" == '' ]]; then
\builtin local result \builtin local result
# shellcheck disable=SC2086 # shellcheck disable=SC2086,SC2312
if result="$(\command zoxide query -i -- ${words[2,-1]})"; then if result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -i -- ${words[2,-1]})"; then
__zoxide_result="${result}" __zoxide_result="${result}"
else else
__zoxide_result='' __zoxide_result=''
@ -130,7 +124,6 @@ if [[ -o zle ]]; then
fi fi
} }
__zoxide_unset __zoxide_z_complete_helper
function __zoxide_z_complete_helper() { function __zoxide_z_complete_helper() {
\builtin local result="${__zoxide_z_prefix}${__zoxide_result}" \builtin local result="${__zoxide_z_prefix}${__zoxide_result}"
# shellcheck disable=SC2296 # shellcheck disable=SC2296
@ -142,6 +135,7 @@ if [[ -o zle ]]; then
\builtin bindkey "\e[0n" __zoxide_z_complete_helper \builtin bindkey "\e[0n" __zoxide_z_complete_helper
if [[ "${+functions[compdef]}" -ne 0 ]]; then if [[ "${+functions[compdef]}" -ne 0 ]]; then
\compdef -d {{cmd}} \compdef -d {{cmd}}
\compdef -d {{cmd}}i
\compdef __zoxide_z_complete {{cmd}} \compdef __zoxide_z_complete {{cmd}}
fi fi
fi fi