fix(shell): updated completions settings

This commit is contained in:
2023-08-10 09:32:25 +03:00
parent 49362fcc9d
commit a3acd654f2
4 changed files with 202 additions and 10 deletions

View File

@@ -21,11 +21,20 @@ autoload -U colors zsh/terminfo
colors
setopt correct
export ZSH_CUSTOM_COMPLETION_PATH="$XDG_CONFIG_HOME/zsh/completion"
x-dc "$ZSH_CUSTOM_COMPLETION_PATH"
# Add completion scripts to zsh path
FPATH="~/.config/zsh/completion:$FPATH"
autoload -Uz compinit && compinit -i
FPATH="$ZSH_CUSTOM_COMPLETION_PATH:$FPATH"
autoload -Uz compinit
compinit -d "$XDG_CACHE_HOME"/zsh/zcompdump-"$ZSH_VERSION"
if type brew &>/dev/null
then
FPATH="$(brew --prefix)/share/zsh/site-functions:${FPATH}"
compinit
fi
# Run x-load-configs in your terminal to reload the files.
function x-load-configs()
{

View File

@@ -17,16 +17,15 @@ export XDG_BIN_HOME="$HOME/.local/bin"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_RUNTIME_DIR="$HOME/.local/run"
# Homebrew configuration
export HOMEBREW="/opt/homebrew"
export HOMEBREW_BIN="$HOMEBREW/bin"
export HOMEBREW_SBIN="$HOMEBREW/sbin"
export HOMEBREW_PKG="$HOMEBREW/opt"
export HOMEBREW_NO_ENV_HINTS=1
eval "$(brew shellenv)"
export HOMEBREW_PREFIX="$XDG_STATE_HOME/homebrew"
export HOMEBREW_BIN="$HOMEBREW_PREFIX/bin"
export HOMEBREW_PKG="$HOMEBREW_PREFIX/opt"
export HOMEBREW_SBIN="$HOMEBREW_PREFIX/sbin"
path_append "/usr/local/bin"
path_prepend "$HOMEBREW_SBIN"
path_prepend "$HOMEBREW_BIN"
path_prepend "$XDG_BIN_HOME"
# brew, https://brew.sh

View File

@@ -0,0 +1,177 @@
#compdef _git-profile git-profile
# zsh completion for git-profile -*- shell-script -*-
__git-profile_debug()
{
local file="$BASH_COMP_DEBUG_FILE"
if [[ -n ${file} ]]; then
echo "$*" >> "${file}"
fi
}
_git-profile()
{
local shellCompDirectiveError=1
local shellCompDirectiveNoSpace=2
local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace
local -a completions
__git-profile_debug "\n========= starting completion logic =========="
__git-profile_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"
# The user could have moved the cursor backwards on the command-line.
# We need to trigger completion from the $CURRENT location, so we need
# to truncate the command-line ($words) up to the $CURRENT location.
# (We cannot use $CURSOR as its value does not work when a command is an alias.)
words=("${=words[1,CURRENT]}")
__git-profile_debug "Truncated words[*]: ${words[*]},"
lastParam=${words[-1]}
lastChar=${lastParam[-1]}
__git-profile_debug "lastParam: ${lastParam}, lastChar: ${lastChar}"
# For zsh, when completing a flag with an = (e.g., git-profile -n=<TAB>)
# completions must be prefixed with the flag
setopt local_options BASH_REMATCH
if [[ "${lastParam}" =~ '-.*=' ]]; then
# We are dealing with a flag with an =
flagPrefix="-P ${BASH_REMATCH}"
fi
# Prepare the command to obtain completions
requestComp="${words[1]} __complete ${words[2,-1]}"
if [ "${lastChar}" = "" ]; then
# If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go completion code.
__git-profile_debug "Adding extra empty parameter"
requestComp="${requestComp} \"\""
fi
__git-profile_debug "About to call: eval ${requestComp}"
# Use eval to handle any environment variables and such
out=$(eval ${requestComp} 2>/dev/null)
__git-profile_debug "completion output: ${out}"
# Extract the directive integer following a : from the last line
local lastLine
while IFS='\n' read -r line; do
lastLine=${line}
done < <(printf "%s\n" "${out[@]}")
__git-profile_debug "last line: ${lastLine}"
if [ "${lastLine[1]}" = : ]; then
directive=${lastLine[2,-1]}
# Remove the directive including the : and the newline
local suffix
(( suffix=${#lastLine}+2))
out=${out[1,-$suffix]}
else
# There is no directive specified. Leave $out as is.
__git-profile_debug "No directive found. Setting do default"
directive=0
fi
__git-profile_debug "directive: ${directive}"
__git-profile_debug "completions: ${out}"
__git-profile_debug "flagPrefix: ${flagPrefix}"
if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
__git-profile_debug "Completion received error. Ignoring completions."
return
fi
while IFS='\n' read -r comp; do
if [ -n "$comp" ]; then
# If requested, completions are returned with a description.
# The description is preceded by a TAB character.
# For zsh's _describe, we need to use a : instead of a TAB.
# We first need to escape any : as part of the completion itself.
comp=${comp//:/\\:}
local tab=$(printf '\t')
comp=${comp//$tab/:}
__git-profile_debug "Adding completion: ${comp}"
completions+=${comp}
lastComp=$comp
fi
done < <(printf "%s\n" "${out[@]}")
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
__git-profile_debug "Activating nospace."
noSpace="-S ''"
fi
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
# File extension filtering
local filteringCmd
filteringCmd='_files'
for filter in ${completions[@]}; do
if [ ${filter[1]} != '*' ]; then
# zsh requires a glob pattern to do file filtering
filter="\*.$filter"
fi
filteringCmd+=" -g $filter"
done
filteringCmd+=" ${flagPrefix}"
__git-profile_debug "File filtering command: $filteringCmd"
_arguments '*:filename:'"$filteringCmd"
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
# File completion for directories only
local subdir
subdir="${completions[1]}"
if [ -n "$subdir" ]; then
__git-profile_debug "Listing directories in $subdir"
pushd "${subdir}" >/dev/null 2>&1
else
__git-profile_debug "Listing directories in ."
fi
local result
_arguments '*:dirname:_files -/'" ${flagPrefix}"
result=$?
if [ -n "$subdir" ]; then
popd >/dev/null 2>&1
fi
return $result
else
__git-profile_debug "Calling _describe"
if eval _describe "completions" completions $flagPrefix $noSpace; then
__git-profile_debug "_describe found some completions"
# Return the success of having called _describe
return 0
else
__git-profile_debug "_describe did not find completions."
__git-profile_debug "Checking if we should do file completion."
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
__git-profile_debug "deactivating file completion"
# We must return an error code here to let zsh know that there were no
# completions found by _describe; this is what will trigger other
# matching algorithms to attempt to find completions.
# For example zsh can match letters in the middle of words.
return 1
else
# Perform file completion
__git-profile_debug "Activating file completion"
# We must return the result of this command, so it must be the
# last command, or else we must store its result to return it.
_arguments '*:filename:_files'" ${flagPrefix}"
fi
fi
fi
}
# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_git-profile" ]; then
_git-profile
fi

View File

@@ -36,4 +36,11 @@ for pkg in "${packages[@]}"; do
echo ""
done
msg "Installing completions for selected packages"
have git-profile && {
git-profile completion zsh > "$ZSH_CUSTOM_COMPLETION_PATH/git-profile" \
&& msg_yay "Installed completions for git-profile"
}
msg_ok "Done"