Commit 34597687 authored by Marc Cornellà's avatar Marc Cornellà

perf(dash): improve dash completion performance

parent c4699f8e
...@@ -5,76 +5,80 @@ compdef _dash dash ...@@ -5,76 +5,80 @@ compdef _dash dash
_dash() { _dash() {
# No sense doing this for anything except the 2nd position and if we haven't # No sense doing this for anything except the 2nd position and if we haven't
# specified which docset to query against # specified which docset to query against
if [[ $CURRENT -eq 2 && ! "$words[2]" =~ ":" ]]; then if [[ $CURRENT -ne 2 || "$words[2]" =~ ":" ]]; then
local -a _all_docsets return
_all_docsets=() fi
# Use defaults to get the array of docsets from preferences
# Have to smash it into one big line so that each docset is an element of
# our DOCSETS array
DOCSETS=("${(@f)$(defaults read com.kapeli.dashdoc docsets | tr -d '\n' | grep -oE '\{.*?\}')}")
# remove all newlines since defaults prints so pretty like local -aU docsets
# Now get each docset and output each on their own line docsets=()
for doc in "$DOCSETS[@]"; do
# Only output docsets that are actually enabled # Use defaults to get the array of docsets from preferences
if [[ "`echo $doc | grep -Eo \"isEnabled = .*?;\" | sed 's/[^01]//g'`" == "0" ]]; then # Have to smash it into one big line so that each docset is an element of our docsets array
continue # Only output docsets that are actually enabled
local -a enabled_docsets
enabled_docsets=("${(@f)$(defaults read com.kapeli.dashdoc docsets \
| tr -d '\n' | grep -oE '\{.*?\}' | grep -E 'isEnabled = 1;')}")
local docset name keyword
# Now get each docset and output each on their own line
for docset in "$enabled_docsets[@]"; do
keyword=''
# Order of preference as explained to me by @kapeli via email
for locator in keyword suggestedKeyword platform; do
# Echo the docset, try to find the appropriate keyword
# Strip doublequotes and colon from any keyword so that everything has the
# same format when output (we'll add the colon in the completion)
if [[ "$docset" =~ "$locator = ([^;]*);" ]]; then
keyword="${match[1]//[\":]}"
fi fi
keyword='' if [[ -z "$keyword" ]]; then
continue
fi
# Order of preference as explained to me by @kapeli via email # if we fall back to platform, we should do some checking per @kapeli
KEYWORD_LOCATORS=(keyword suggestedKeyword platform) if [[ "$locator" == "platform" ]]; then
for locator in "$KEYWORD_LOCATORS[@]"; do # Since these are the only special cases right now, let's not do the
# Echo the docset, try to find the appropriate keyword # expensive processing unless we have to
# Strip doublequotes and colon from any keyword so that everything has the if [[ "$keyword" = (python|java|qt|cocos2d) ]]; then
# same format when output (we'll add the colon in the completion) if [[ "$docset" =~ "docsetName = ([^;]*);" ]]; then
keyword=`echo $doc | grep -Eo "$locator = .*?;" | sed -e "s/$locator = \(.*\);/\1/" -e "s/[\":]//g"` name="${match[1]//[\":]}"
if [[ ! -z "$keyword" ]]; then case "$keyword" in
# if we fall back to platform, we should do some checking per @kapeli python)
if [[ "$locator" == "platform" ]]; then case "$name" in
# Since these are the only special cases right now, let's not do the "Python 2") keyword="python2" ;;
# expensive processing unless we have to "Python 3") keyword="python3" ;;
if [[ "$keyword" = (python|java|qt|cocos2d) ]]; then esac ;;
docsetName=`echo $doc | grep -Eo "docsetName = .*?;" | sed -e "s/docsetName = \(.*\);/\1/" -e "s/[\":]//g"` java)
case "$keyword" in case "$name" in
python) "Java SE7") keyword="java7" ;;
case "$docsetName" in "Java SE6") keyword="java6" ;;
"Python 2") keyword="python2" ;; "Java SE8") keyword="java8" ;;
"Python 3") keyword="python3" ;; esac ;;
esac ;; qt)
java) case "$name" in
case "$docsetName" in "Qt 5") keyword="qt5" ;;
"Java SE7") keyword="java7" ;; "Qt 4"|Qt) keyword="qt4" ;;
"Java SE6") keyword="java6" ;; esac ;;
"Java SE8") keyword="java8" ;; cocos2d)
esac ;; case "$name" in
qt) Cocos3D) keyword="cocos3d" ;;
case "$docsetName" in esac ;;
"Qt 5") keyword="qt5" ;; esac
"Qt 4"|Qt) keyword="qt4" ;;
esac ;;
cocos2d)
case "$docsetName" in
Cocos3D) keyword="cocos3d" ;;
esac ;;
esac
fi
fi fi
# Bail once we have a match
break
fi fi
done
# If we have a keyword, add it to the list!
if [[ ! -z "$keyword" ]]; then
_all_docsets+=($keyword)
fi fi
# Bail once we have a match
break
done done
# special thanks to [arx] on #zsh for getting me sorted on this piece # If we have a keyword, add it to the list!
compadd -qS: -- "$_all_docsets[@]" if [[ -n "$keyword" ]]; then
return docsets+=($keyword)
fi fi
done
# special thanks to [arx] on #zsh for getting me sorted on this piece
compadd -qS: -- "$docsets[@]"
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment