zsh-workers
 help / color / mirror / code / Atom feed
From: Marlon Richert <marlon.richert@gmail.com>
To: "Jun . T" <takimoto-j@kba.biglobe.ne.jp>
Cc: zsh-workers@zsh.org
Subject: Re: [PATCH 1/3] Fix _prefix insertion logic
Date: Thu, 15 Jun 2023 17:11:12 +0300	[thread overview]
Message-ID: <20230615141112.12919-1-marlon.richert@gmail.com> (raw)
In-Reply-To: <CAHLkEDtPeQBvSzUvaqrq1wQLzAuJHMQLuFBGHzntwOaARUVnvg@mail.gmail.com>

You're right, the test cases still fail. I added test cases to the build and
wrote code until they past. However, I had bugs in the test cases and thus ended
up not solving the problem. Here's a new patch, below. I added more exhaustive
testing to make sure it definitely works correctly now.

---
Fix _prefix insertion logic

This solves the following problems in the _prefix completer:
- The old code had logic for dealing with compstate[unambiguous] that
  was unnecessary. It works fine without it.
- Because of this logic, if a widget set compstate[insert]=1 after
  calling _main_complete, an `x` was left after the completion on the
  command line.
- If the same widget also set `compstate[to_end]=`, then instead, the
  last character of the inserted completion would be treated as an
  autoremovable suffix, with the actual suffix being inserted to the
  line as a normal character.
- After inserting a completion, the cursor would move to the end of the
  entire current word on the command, not the end of word that was
  inserted. This is not what you want with _prefix, since you are trying
  to complete a word _before_ the one on the command line, after which
  you usually want to insert a separator, such as a space or slash,
  before the next word.

Discussed in workers/51641.
---
 Completion/Base/Completer/_prefix |  9 ++----
 Test/Y01completion.ztst           | 48 +++++++++++++++++++++++++++++++
 Test/comptest                     | 10 +++++--
 3 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/Completion/Base/Completer/_prefix b/Completion/Base/Completer/_prefix
index 74be5f47d..aea2f7863 100644
--- a/Completion/Base/Completer/_prefix
+++ b/Completion/Base/Completer/_prefix
@@ -49,13 +49,8 @@ for tmp in "$comp[@]"; do
     fi

     if [[ "$tmp" != _prefix ]] && "$tmp"; then
-      [[ compstate[nmatches] -gt 1 ]] && return 0
-      compadd -U -i "$IPREFIX" -I "$ISUFFIX" - "${compstate[unambiguous]%$suf}x"
-      compstate[list]=
-      if [[ -n $compstate[unambiguous] ]]; then
-        compstate[insert]=unambiguous
-      else
-        compstate[insert]=0
+      if [[ -n $compstate[old_list] || ${compstate[unambiguous]%$suf} == $PREFIX ]]; then
+        compstate[to_end]=match
       fi
       return 0
     fi
diff --git a/Test/Y01completion.ztst b/Test/Y01completion.ztst
index fb369ea69..fc18b19a4 100644
--- a/Test/Y01completion.ztst
+++ b/Test/Y01completion.ztst
@@ -35,6 +35,54 @@
 >line: {: dir1/}{}
 >line: {: dir2/}{}

+  comptest $': d\t\t\t\t\t \t'
+0:unambiguous prefix and autoremovable suffix
+>line: {: dir}{}
+>line: {: dir}{}
+>DESCRIPTION:{file}
+>DI:{dir1}
+>DI:{dir2}
+>line: {: dir1/}{}
+>line: {: dir2/}{}
+>line: {: dir1/}{}
+>line: {: dir1 }{}
+>DESCRIPTION:{file}
+>DI:{dir1}
+>DI:{dir2}
+>FI:{file1}
+>FI:{file2}
+
+  comptest $': suf\ebd\t\t\t\t\t \t'
+0:unambiguous prefix and autoremovable suffix with _prefix completer
+>line: {: dir}{suf}
+>line: {: dir}{suf}
+>DESCRIPTION:{file}
+>DI:{dir1}
+>DI:{dir2}
+>line: {: dir1/}{suf}
+>line: {: dir2/}{suf}
+>line: {: dir1/}{suf}
+>line: {: dir1 }{suf}
+>DESCRIPTION:{file}
+>DI:{dir1}
+>DI:{dir2}
+>FI:{file1}
+>FI:{file2}
+F:regression test workers/51641
+
+  comptesteval 'comptest-postfunc() { compstate[insert]=1 compstate[list]= }'
+  comptest $': \t \t'
+0:compstate[insert]=1 compstate[list]=
+>line: {: dir1/}{}
+>line: {: dir1 dir1/}{}
+
+  comptest $': suf\eb\t \t'
+0:compstate[insert]=1 compstate[list]= with _prefix completer
+>line: {: dir1/}{suf}
+>line: {: dir1 dir1/}{suf}
+F:regression test workers/51641
+
+  comptesteval 'comptest-postfunc() {}'
   comptest $': *\t\t\t\t\t\t'
 0:_expand shows file types
 >line: {: dir1/}{}
diff --git a/Test/comptest b/Test/comptest
index 79c69979a..4a9bd9b7f 100644
--- a/Test/comptest
+++ b/Test/comptest
@@ -21,7 +21,7 @@ comptestinit () {
   export PS1="<PROMPT>"
   zpty zsh "$comptest_zsh -f +Z"

-  zpty -r zsh log1 "*<PROMPT>*" || {
+  zpty -r zsh log1 "*<PROMPT>*" || {
     print "first prompt hasn't appeared."
     return 1
   }
@@ -40,7 +40,7 @@ KEYTIMEOUT=1
 setopt zle
 autoload -U compinit
 compinit -u
-zstyle ":completion:*" completer _expand _complete _ignored
+zstyle ":completion:*" completer _expand _complete _prefix _ignored
 zstyle ":completion:*:default" list-colors "no=<NO>" "fi=<FI>" "di=<DI>" "ln=<LN>" "pi=<PI>" "so=<SO>" "bd=<BD>" "cd=<CD>" "ex=<EX>" "mi=<MI>" "tc=<TC>" "sp=<SP>" "lc=<LC>" "ec=<EC>\n" "rc=<RC>"
 zstyle ":completion:*" group-name ""
 zstyle ":completion:*:messages" format "<MESSAGE>%d</MESSAGE>
@@ -51,6 +51,12 @@ zstyle ":completion:*:options" verbose yes
 zstyle ":completion:*:values" verbose yes
 setopt noalwayslastprompt listrowsfirst completeinword
 zmodload zsh/complist
+zle -C complete-word complete-word complete-word-with-postfunc
+complete-word-with-postfunc() {
+  local +h -a comppostfuncs=( comptest-postfunc )
+  _main_complete "$@"
+}
+comptest-postfunc() {}
 complete-word-with-report () {
   print -lr "<WIDGET><complete-word>"
   zle complete-word
--
2.39.2 (Apple Git-143)



  parent reply	other threads:[~2023-06-15 14:12 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-29 18:07 Patch 1/3: Fix prefix " Marlon Richert
2023-04-29 18:08 ` Patch 2/3: Make dynamic dir completion easier to implement Marlon Richert
2023-04-29 18:09   ` Patch 3/3: Fix subscript completion bugs inside ~[...] Marlon Richert
2023-04-29 18:58 ` Patch 1/3: Fix prefix insertion logic Felipe Contreras
2023-04-29 19:02   ` Marlon Richert
2023-04-29 19:17     ` Bart Schaefer
2023-05-01 21:08     ` Aaron Schrab
2023-05-01 22:03       ` Bart Schaefer
2023-05-05 11:41         ` [PATCH 1/3] Fix _prefix " Marlon Richert
2023-06-07  6:03           ` Marlon Richert
2023-06-08 12:41             ` Jun. T
2023-06-15 14:11             ` Marlon Richert [this message]
2023-05-05 11:41         ` [PATCH 2/3] Make dynamic dir completion easier to implement Marlon Richert
2023-05-06 17:28           ` Bart Schaefer
2023-05-13 17:30             ` Bart Schaefer
2023-05-15  9:04           ` Oliver Kiddle
2023-05-05 11:41         ` [PATCH 3/3] Fix subscript completion bugs inside ~[...] Marlon Richert
2023-05-01 22:37     ` Patch 1/3: Fix prefix insertion logic Felipe Contreras
2023-05-01 22:52       ` Bart Schaefer
2023-05-02  4:49         ` Felipe Contreras
2023-05-02 19:16           ` Bart Schaefer
2023-05-02 20:14             ` Felipe Contreras
2023-05-05 11:51           ` Marlon Richert
2023-05-05 12:38             ` Felipe Contreras
2023-05-05 15:04             ` Mikael Magnusson
2023-05-06 17:23               ` Bart Schaefer
2023-05-01 23:36       ` Vin Shelton

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=20230615141112.12919-1-marlon.richert@gmail.com \
    --to=marlon.richert@gmail.com \
    --cc=takimoto-j@kba.biglobe.ne.jp \
    --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).