Refactor mvn completion

- use local variables for cache variables
- use if for conditional executions
This commit is contained in:
Shohei YOSHIDA 2026-03-26 15:14:58 +09:00
parent b1ffb771c9
commit 519f6d6cee
No known key found for this signature in database
GPG Key ID: C9A1BB11BB940CF2
1 changed files with 51 additions and 28 deletions

View File

@ -266,14 +266,19 @@ _mvn_groupIds() {
[[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" cache-policy _mvn_groupIds_caching_policy
if [[ -d $repository_location ]]; then
unset _groupIds
if ( [[ ${+_groupIds} -eq 0 ]] || _cache_invalid "mvn/repositories/${repository_location}/groupIds" ) && ! _retrieve_cache "mvn/repositories/${repository_location}/groupIds"; then
_groupIds=($repository_location/**/)
_groupIds=(${${${(u)_groupIds:h:h}#"$repository_location/"}//\//.})
[[ $#_groupIds -gt 0 ]] && _store_cache "mvn/repositories/${repository_location}/groupIds" _groupIds
local -a groupIds
local cache_key="mvn/repositories/${repository_location}/groupIds"
if ( _cache_invalid $cache_key ) && ! _retrieve_cache $cache_key; then
groupIds=($repository_location/**/)
groupIds=(${${${(u)_groupIds:h:h}#"$repository_location/"}//\//.})
if (( $#groupIds > 0 )); then
_store_cache $cache_key groupIds
fi
fi
[[ $#_groupIds -gt 0 ]] && _multi_parts $@ . _groupIds && ret=0
if (( $#groupIds > 0 )); then
_multi_parts $@ . groupIds && ret=0
fi
fi
return ret
@ -310,14 +315,19 @@ _mvn_plugin_goals() {
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
[[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" cache-policy _mvn_goals_caching_policy
unset _goals
if ( [[ ${+_goals} -eq 0 ]] || _cache_invalid "mvn/plugins/${plugin}" ) && ! _retrieve_cache "mvn/plugins/${plugin}"; then
local -a goals
local cache_key="mvn/plugins/${plugin}"
if _cache_invalid $cache_key && ! _retrieve_cache $cache_key; then
setopt localoptions extendedglob
_goals=(${(s:,,,:)${${${(f)${${${(f)${${${${(F)${(S)${(f)"$(_call_program goals $words[1] -N org.apache.maven.plugins:maven-help-plugin:2.1.1:describe -Dplugin=$plugin)"}//#$(__mvn_get_plugin_prefix $plugin):/,,,}}:#*BUILD FAILURE*}#*This plugin has*goals#:}%For more information, run \'mvn help:describe*}}//:/\\:}}}// ##/ }// Description\\: /:}})
[[ $#_goals -gt 0 ]] && _store_cache "mvn/plugins/${plugin}" _goals
if (( $#goals > 0 )); then
_store_cache "mvn/plugins/${plugin}" goals
fi
fi
[[ $#_goals -gt 0 ]] && _describe -t "goals" "${plugin} goal" _goals $@[0,-2] && ret=0
if (( $#goals > 0 )); then
_describe -t "goals" "${plugin} goal" goals $@[0,-2] && ret=0
fi
return ret
}
@ -338,44 +348,52 @@ _mvn_profiles() {
# Resolve profiles from settings.xml
if [[ -f $settings_file ]]; then
unset _profiles
local -a profiles
cache_name="mvn/profiles${settings_file:A}" # FIXME Don't use A modifier, it is only available on Zsh >= 4.3.10
if ( [[ ${+_profiles} -eq 0 ]] || _cache_invalid "$cache_name" ) && ! _retrieve_cache "$cache_name"; then
_profiles=()
if _cache_invalid "$cache_name" && ! _retrieve_cache "$cache_name"; then
profiles=()
profiles_section="${(M)${(f)$(<$settings_file)}:#*<profiles>*}"
if [[ -n "$profiles_section" ]]; then
for profile in ${(s:,,,:)${${${(S)${(S)${(S)${(S)${${profiles_section#*<profile>}%</profile>*}//<repositories>*<\/repositories>}//<pluginRepositories>*<\/pluginRepositories>}//<build>*<\/build>}//<\/id>*<id>/,,,}##*<id>}%%</id>*}}; do
[[ -z ${(M)profiles:#"$profile"*} ]] && _profiles+=("$profile"'['"in settings file"']')
[[ -z ${(M)profiles:#"$profile"*} ]] && profiles+=("$profile"'['"in settings file"']')
done
fi
[[ $#_profiles -gt 0 ]] && _store_cache "$cache_name" _profiles
if (( $#profiles > 0 )); then
_store_cache "$cache_name" profiles
fi
fi
profs+=($_profiles)
profs+=($profiles)
fi
# Resolve project profiles
if [[ -f $parent_pom_file ]]; then
unset _profiles
local -a profiles
cache_name="mvn/profiles${parent_pom_file:A}" # FIXME Don't use A modifier, it is only available on Zsh >= 4.3.10
if ( [[ ${+_profiles} -eq 0 ]] || _cache_invalid "$cache_name" ) && ! _retrieve_cache "$cache_name"; then
_profiles=()
if _cache_invalid "$cache_name" && ! _retrieve_cache "$cache_name"; then
profiles=()
setopt localoptions extendedglob
for file in ${parent_pom_file:h}/**/pom.xml~*target\/*; do # FIXME project.build.directory is not always target/
profiles_section="${(M)${(f)$(<$file)}:#*<profiles>*}"
if [[ -n "$profiles_section" ]]; then
for profile in ${(s:,,,:)${${${(S)${(S)${(S)${(S)${${profiles_section#*<profile>}%</profile>*}//<repositories>*<\/repositories>}//<pluginRepositories>*<\/pluginRepositories>}//<build>*<\/build>}//<\/id>*<id>/,,,}##*<id>}%%</id>*}}; do
[[ -z ${(M)profiles:#"$profile"*} ]] && _profiles+=("$profile"'['"in ${file#${parent_pom_file:h}\/}"']')
[[ -z ${(M)profiles:#"$profile"*} ]] && profiles+=("$profile"'['"in ${file#${parent_pom_file:h}\/}"']')
done
fi
done
[[ $#_profiles -gt 0 ]] && _store_cache "$cache_name" _profiles
if (( $#profiles > 0)); then
_store_cache "$cache_name" profiles
fi
fi
profs+=($_profiles)
profs+=($profiles)
fi
compset -P '-'; compset -P '+'; compset -P '!' # FIXME Only works for the first profile
[[ $#profs -gt 0 ]] && _values $@ 'profile' "${profs[@]}" && ret=0
if (( $profs > 0 )); then
_values $@ 'profile' "${profs[@]}" && ret=0
fi
return ret
}
@ -437,16 +455,21 @@ _mvn_plugin_goal_property_names() {
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
[[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" cache-policy _mvn_properties_caching_policy
unset _properties
if ( [[ ${+_properties} -eq 0 ]] || _cache_invalid "mvn/plugins/${plugin_colon_goal}" ) && ! _retrieve_cache "mvn/plugins/${plugin_colon_goal}"; then
local -a properties
local cache_key="mvn/plugins/${plugin_colon_goal}"
if _cache_invalid $cache_key && ! _retrieve_cache $cache_key; then
# FIXME Does not work for:
# android:apk (new line before expression)
# ear:ear (unknown cause)
_properties=(${(M)${(ps:,,,:)${${${${(pj: :)${${${(f)${"$(_call_program properties $words[1] -N org.apache.maven.plugins:maven-help-plugin:2.1.1:describe -Dplugin=${plugin_colon_goal%:*} -Dgoal=${plugin_colon_goal##*:} -Ddetail)"#*Available parameters:}%%\[INFO\]*}//# [a-z]*/,,,}##*Expression: \$\{}}//\}[[:space:]]##/:}//[[:space:]]##/ }//[[:space:]]#,,,[[:space:]]#/,,,}}:#[a-zA-Z]##:*})
[[ $#_properties -gt 0 ]] && _store_cache "mvn/plugins/${plugin_colon_goal}" _properties
properties=(${(M)${(ps:,,,:)${${${${(pj: :)${${${(f)${"$(_call_program properties $words[1] -N org.apache.maven.plugins:maven-help-plugin:2.1.1:describe -Dplugin=${plugin_colon_goal%:*} -Dgoal=${plugin_colon_goal##*:} -Ddetail)"#*Available parameters:}%%\[INFO\]*}//# [a-z]*/,,,}##*Expression: \$\{}}//\}[[:space:]]##/:}//[[:space:]]##/ }//[[:space:]]#,,,[[:space:]]#/,,,}}:#[a-zA-Z]##:*})
if (( $#properties > 0 )); then
_store_cache $cache_key properties
fi
fi
[[ $#_properties -gt 0 ]] && _describe -t "${plugin_colon_goal//:/-}-property-names" "${plugin_colon_goal} property name" _properties $@[0,-2] && ret=0
if (( $#properties > 0 )); then
_describe -t "${plugin_colon_goal//:/-}-property-names" "${plugin_colon_goal} property name" properties $@[0,-2] && ret=0
fi
return ret
}