Skip to content

Commit

Permalink
Improve zsh completions (ajeetdsouza#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza authored Feb 20, 2024
1 parent e0158a7 commit 5592506
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- zsh: improved Space-Tab completions.

## [0.9.3] - 2024-02-13

### Added
Expand Down
69 changes: 41 additions & 28 deletions templates/zsh.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,17 @@ fi
# When using zoxide with --no-cmd, alias these internal functions as desired.
#

__zoxide_z_prefix='z#'

# Jump to a directory using only keywords.
function __zoxide_z() {
# shellcheck disable=SC2199
if [[ "$#" -eq 0 ]]; then
__zoxide_cd ~
elif [[ "$#" -eq 1 ]] && { [[ -d "$1" ]] || [[ "$1" = '-' ]] || [[ "$1" =~ ^[-+][0-9]$ ]]; }; then
__zoxide_cd "$1"
elif [[ "$@[-1]" == "${__zoxide_z_prefix}"?* ]]; then
# shellcheck disable=SC2124
\builtin local result="${@[-1]}"
__zoxide_cd "{{ "${result:${#__zoxide_z_prefix}}" }}"
else
\builtin local result
# shellcheck disable=SC2312
result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" &&
__zoxide_cd "${result}"
result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" && __zoxide_cd "${result}"
fi
}

Expand All @@ -79,41 +72,61 @@ function __zoxide_zi() {
result="$(\command zoxide query --interactive -- "$@")" && __zoxide_cd "${result}"
}

{{ section }}
# Commands for zoxide. Disable these using --no-cmd.
#

{%- match cmd %}
{%- when Some with (cmd) %}

function {{ cmd }}() {
__zoxide_z "$@"
}

function {{ cmd }}i() {
__zoxide_zi "$@"
}

# Completions.
if [[ -o zle ]]; then
__zoxide_result=''

function __zoxide_z_complete() {
# Only show completions when the cursor is at the end of the line.
# shellcheck disable=SC2154
[[ "{{ "${#words[@]}" }}" -eq "${CURRENT}" ]] || return 0

if [[ "{{ "${#words[@]}" }}" -eq 2 ]]; then
# Show completions for local directories.
_files -/
elif [[ "${words[-1]}" == '' ]] && [[ "${words[-2]}" != "${__zoxide_z_prefix}"?* ]]; then
\builtin local result
# shellcheck disable=SC2086,SC2312
if result="$(\command zoxide query --exclude "$(__zoxide_pwd)" --interactive -- ${words[2,-1]})"; then
result="${__zoxide_z_prefix}${result}"
# shellcheck disable=SC2296
compadd -Q "${(q-)result}"
fi
elif [[ "${words[-1]}" == '' ]]; then
# Show completions for Space-Tab.
# shellcheck disable=SC2086
__zoxide_result="$(\command zoxide query --exclude "$(__zoxide_pwd || \builtin true)" --interactive -- ${words[2,-1]})" || __zoxide_result=''
# Sends '\e[0n' to console input.
\builtin printf '\e[5n'
fi

# Report that the completion was successful, so that we don't fall back
# to another completion function.
return 0
}

\builtin bindkey '\e[0n' 'reset-prompt'
[[ "${+functions[compdef]}" -ne 0 ]] && \compdef __zoxide_z_complete __zoxide_z
fi

{{ section }}
# Commands for zoxide. Disable these using --no-cmd.
#

{%- match cmd %}
{%- when Some with (cmd) %}
function __zoxide_z_complete_helper() {
if [[ -n "${__zoxide_result}" ]]; then
# shellcheck disable=SC2034,SC2296
BUFFER="{{ cmd }} ${(q-)__zoxide_result}"
\builtin zle reset-prompt
\builtin zle accept-line
else
\builtin zle reset-prompt
fi
}
\builtin zle -N __zoxide_z_complete_helper

\builtin alias {{cmd}}=__zoxide_z
\builtin alias {{cmd}}i=__zoxide_zi
\builtin bindkey '\e[0n' '__zoxide_z_complete_helper'
[[ "${+functions[compdef]}" -ne 0 ]] && \compdef __zoxide_z_complete {{ cmd }}
fi

{%- when None %}

Expand Down

0 comments on commit 5592506

Please sign in to comment.