From 99f94e0f1ef39def38da0690ef963aef44d783d1 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 23 Jun 2019 02:39:56 +0700 Subject: [PATCH 1/3] _cargo: Move _cargo_cmds to near end of file Sublime Text couldn't handle the parameter expansion correctly. --- src/etc/_cargo | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/etc/_cargo b/src/etc/_cargo index 1c8c413f16c..a087d706a66 100644 --- a/src/etc/_cargo +++ b/src/etc/_cargo @@ -420,24 +420,6 @@ _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() { @@ -497,6 +479,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 + # 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. From 36785ea6de0cd5a9383d372b1df7ddae95b5c281 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 23 Jun 2019 02:42:01 +0700 Subject: [PATCH 2/3] _cargo: Collapse if else --- src/etc/_cargo | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/etc/_cargo b/src/etc/_cargo index a087d706a66..52e0e65f140 100644 --- a/src/etc/_cargo +++ b/src/etc/_cargo @@ -450,17 +450,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 From 6d70bf4835589a2701de17136741608c9d48a2a7 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 23 Jun 2019 10:11:13 +0700 Subject: [PATCH 3/3] _cargo: Add package completion --- src/etc/_cargo | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/etc/_cargo b/src/etc/_cargo index 52e0e65f140..eabdbece2c7 100644 --- a/src/etc/_cargo +++ b/src/etc/_cargo @@ -420,10 +420,43 @@ _cargo() { esac } -#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.