zsh-workers
 help / color / mirror / code / Atom feed
From: Oliver Kiddle <okiddle@yahoo.co.uk>
To: Zsh workers <zsh-workers@zsh.org>
Subject: PATCH: description alignment with _regex_words
Date: Thu, 17 Aug 2017 18:19:56 +0200	[thread overview]
Message-ID: <26671.1502986796@thecus.kiddle.eu> (raw)

Currently _regex_words generates a regex that is roughly:
  ( /word1/ ':tag:desc:(( word1:desc1 ))' $word1_morestuff |
    /word2/ ':tag:desc:(( word2:desc2 ))' $word2_morestuff |
    /word3/ ':tag:desc:(( word3:desc3 ))' $word3_morestuff )

This results in ugly misaligned descriptions and is calling _describe
three times underneath which is not too efficient. With this patch,
you'll instead get:

  ( /word1/ $word1_morestuff |
    /word2/ $word2_morestuff |
    /word3/ $word3_morestuff |
    /'[]'/ ':tag:desc:(( word1:desc1 word2:desc2 word3:desc3 ))' )

I'm fairly sure this will be functionally identical other than fixing
the alignment. The case with -t passed to _regex_words (which uses
_values) is also handled.

It also is unfortunate that _regex_words encourages exact matching
patterns which tends to lead to things like ip completion printing
"parse failed before current word" when a new -c option has been added.
I'd recommend using generous patterns when not branching down separate
parts of the regex tree based on what it matches. That way new
subcommands and options don't break the existing completions.

Oliver

diff --git a/Completion/Base/Utility/_regex_words b/Completion/Base/Utility/_regex_words
index 62c2491bb..90b4a9735 100644
--- a/Completion/Base/Utility/_regex_words
+++ b/Completion/Base/Utility/_regex_words
@@ -1,6 +1,6 @@
 #autoload
 
-local opt OPTARG
+local opt OPTARG matches end
 local term=$'\0'
 
 while getopts "t:" opt; do
@@ -31,19 +31,22 @@ fi
 integer i
 local -a wds
 
+if [[ $term = $'\0' ]]; then
+  matches=":${tag}:${desc}:(( "
+  end="))"
+else
+  matches=":${tag}:${desc}:_values -s ${(q)term} ${(q)desc}"
+fi
+
 for (( i = 1; i <= $#; i++ )); do
   wds=(${(s.:.)argv[i]})
   reply+=(/${wds[1]//\**/"[^$term]#"}"$term"/)
   if [[ $term = $'\0' ]]; then
-    reply+=(":${tag}:${desc}:(( ${wds[1]//\*}:${wds[2]//(#m)[: \(\)]/\\$MATCH} ))")
+    matches+="${wds[1]//\*}${wds[2]:+\\:${wds[2]//(#m)[: \(\)]/\\$MATCH}} "
   else
-    reply+=(":${tag}:${desc}:_values -s ${(q)term} ${(q)desc} \
-${(q)${${wds[1]//\*}//(#m)[:\[\]]/\\$MATCH}}\\[${(q)${wds[2]//(#m)[:\[\]]/\\$MATCH}}\\]")
+    matches+=" ${(q)${${wds[1]//\*}//(#m)[:\[\]]/\\$MATCH}}\\[${(q)${wds[2]//(#m)[:\[\]]/\\$MATCH}}\\]"
   fi
   eval "reply+=($wds[3])"
-  if (( $i == $# )); then
-    reply+=(\))
-  else
-    reply+=(\|)
-  fi
+  reply+=(\|)
 done
+reply+=( /'[]'/ "${matches}${end}" \) )


                 reply	other threads:[~2017-08-17 16:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=26671.1502986796@thecus.kiddle.eu \
    --to=okiddle@yahoo.co.uk \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).