Skip to content

_cargo: Add cargo package completion #7064

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

Closed
wants to merge 3 commits into from
Closed
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
89 changes: 60 additions & 29 deletions src/etc/_cargo
Original file line number Diff line number Diff line change
Expand Up @@ -420,28 +420,43 @@ _cargo() {
esac
}

_cargo_cmds() {
local -a commands
# This uses Parameter Expansion Flags, which is an Zsh built-in features.
# See more: http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
# and http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion
#
# # How this work?
#
# First it splits the result of `cargo --list` at newline, then it removes the first line.
# Then it removes indentation (4 whitespaces) before each items. (Note the x## pattern [1]).
# Then it replaces those spaces between item and description with a `:`
#
# [1]: https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org#patterns
commands=( ${${${(M)"${(f)$(cargo --list)}":# *}/ ##/}/ ##/:} )
_describe 'command' commands
}


#FIXME: Disabled until fixed
#gets package names from the manifest file
_get_package_names() {
:
local manifest=$(_locate_manifest)
if [[ -z $manifest ]]; then
return 0
fi

local last_line=''
local -a names
local in_block=false
names=()
while read -r line; do
if [[ $line =~ '^\s*\[.*' ]]; then
in_block=false
if [[ $line =~ '.*dependencies\..+\]$' ]]; then
regexp-replace line '^\[.*dependencies\.|\]$' ''
names+=( "$line" )
fi
fi

if [[ $last_line =~ '^\s*\[.*dependencies]$' ]]; then
in_block=true
fi

if [[ $in_block == true ]]; then
line=${line## }
if [[ -n $line ]]; then
if [[ ! $line =~ '\s*#.*' ]]; then
regexp-replace line '\s*=\s*.*["}]$' ''
names+=( "$line" )
fi
fi
fi

last_line=$line
done < "$manifest"
_describe 'packages' names
}

#TODO:see if it makes sense to have 'locate-project' to have non-json output.
Expand All @@ -468,17 +483,13 @@ _get_names_from_array() {
while read -r line; do
if [[ $last_line == "[[$block_name]]" ]]; then
in_block=true
else
if [[ $last_line =~ '\s*\[\[.*' ]]; then
in_block=false
fi
elif [[ $last_line =~ '\s*\[\[.*' ]]; then
in_block=false
fi

if [[ $in_block == true ]]; then
if [[ $line =~ '\s*name\s*=' ]]; then
regexp-replace line '^\s*name\s*=\s*|"' ''
names+=( "$line" )
fi
if [[ $in_block == true ]] && [[ $line =~ '\s*name\s*=' ]]; then
regexp-replace line '^\s*name\s*=\s*|"' ''
names+=( "$line" )
fi

last_line=$line
Expand All @@ -497,6 +508,26 @@ _benchmark_names() {
_get_names_from_array "bench"
}

_cargo_cmds() {
local -a commands
# This uses Parameter Expansion Flags, which is an Zsh built-in features.
# See more: http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
# and http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion
#
# # How this work?
#
# First it splits the result of `cargo --list` at newline, then it removes the first line.
# Then it removes indentation (4 whitespaces) before each items. (Note the x## pattern [1]).
# Then it replaces those spaces between item and description with a `:`.
# You could use handful command:
# print -ln <pattern>
# to test the pattern.
#
# [1]: https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org#patterns
commands=( ${${${(M)"${(f)$(cargo --list)}":# *}/ ##/}/ ##/:} )
_describe 'command' commands
}

# These flags are mutually exclusive specifiers for the scope of a command; as
# they are used in multiple places without change, they are expanded into the
# appropriate command's `_arguments` where appropriate.
Expand Down