From 65ef28df6dac2b5fe3b405a534515567a2f60b6f Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Thu, 10 Jul 2025 17:59:27 -0500 Subject: [PATCH] completions: Show description in zsh completions Uses the second part of NIX_GET_COMPLETIONS if available. Change-Id: I35efaaea3f35eea25a8de723ab32ceecd054e7df Fixes: https://git.lix.systems/lix-project/lix/issues/910 --- doc/manual/rl-next/zsh-completions-desc.md | 10 +++++++++ misc/zsh/completion.zsh | 24 +++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 doc/manual/rl-next/zsh-completions-desc.md diff --git a/doc/manual/rl-next/zsh-completions-desc.md b/doc/manual/rl-next/zsh-completions-desc.md new file mode 100644 index 000000000..ed96da09b --- /dev/null +++ b/doc/manual/rl-next/zsh-completions-desc.md @@ -0,0 +1,10 @@ +--- +synopsis: add description to zsh completions +issues: [fj#910] +cls: [3632] +category: "Fixes" +credits: [matthewbauer] +--- + +Emit descriptions when completing args in zsh completions. This uses the descriptions we already +provided in NIX\_GET\_COMPLETIONS. diff --git a/misc/zsh/completion.zsh b/misc/zsh/completion.zsh index f9b3dca74..8a899ec99 100644 --- a/misc/zsh/completion.zsh +++ b/misc/zsh/completion.zsh @@ -1,24 +1,28 @@ #compdef nix function _nix() { - local ifs_bk="$IFS" local input=("${(Q)words[@]}") - IFS=$'\n' - local res=($(NIX_GET_COMPLETIONS=$((CURRENT - 1)) "$input[@]" 2>/dev/null)) + local -a suggestions # what value to actually use on completion + local -a suggestions_display # what to display to the user + local tpe suggestion description + local ifs_bk="$IFS" + IFS=$'\t' + NIX_GET_COMPLETIONS=$((CURRENT - 1)) "$input[@]" 2>/dev/null | {read tpe; while IFS=$'\t' read -r suggestion description; do + suggestions+=("$suggestion") + if [ -n "$description" ]; then + suggestions_display+=("$suggestion -- $description") + else + suggestions_display+=("$suggestion") + fi + done} IFS="$ifs_bk" - local tpe="${${res[1]}%%> *}" - local -a suggestions - declare -a suggestions - for suggestion in ${res:1}; do - suggestions+=("${suggestion%% *}") - done local -a args if [[ "$tpe" == filenames ]]; then args+=('-f') elif [[ "$tpe" == attrs ]]; then args+=('-S' '') fi - compadd -J nix "${args[@]}" -a suggestions + compadd -J nix "${args[@]}" -d suggestions_display -a suggestions } _nix "$@"