zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Marlon Richert <marlon.richert@gmail.com>
Cc: Peter Stephenson <p.w.stephenson@ntlworld.com>,
	Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [BUG] COMPLETE_IN_WORD fails to recognize brace_parameter context
Date: Fri, 7 May 2021 17:04:06 -0700	[thread overview]
Message-ID: <CAH+w=7ajH4H9eJwB5=jEGm77VZXhRGD5h53wT7Un=py8jeb-dg@mail.gmail.com> (raw)
In-Reply-To: <CAHLkEDsH+a4vRXsgoZkuPEd1Yqjnd66Jj7JMQmmwhMJz8g+q6Q@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1267 bytes --]

On Fri, May 7, 2021 at 7:09 AM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> This bug is similar to the one I reported in workers/47858.

Only superficially.

> This works correctly:
>
> % setopt completeinword
> % tst() { print "\n$compstate[context]"; zle -I }

I think there's something missing here for the test case.  A style or
a bindkey?  Something to run "tst"?

> % # place cursor after '${(' and press Tab:
> % : ${()foo}

This is actually a failure in the interpretation of the cursor
position within the C code.  An existing comment says:
 * We are still within the parameter flags.  There's no
 * point trying to do anything clever here with
 * parameter names.  Instead, just report that we are in
 * a brace parameter but let the completion function
 * decide what to do about it.

The issue was that "still within the flags" assumed complete_in_word
was not set.  An earlier test had a similar mistake when the closing
brace of the entire parameter expression was already present.  Patch
below attempts to fix those assumptions, but still leaves further
effort up to the completion function ... which currently completes
nothing when the parens are empty.  I'm leaving that for someone else
to work on.

I also expanded on an existing comment.

[-- Attachment #2: check_param.txt --]
[-- Type: text/plain, Size: 2137 bytes --]

diff --git a/Src/Zle/compcore.c b/Src/Zle/compcore.c
index 5162d97dc..131e86825 100644
--- a/Src/Zle/compcore.c
+++ b/Src/Zle/compcore.c
@@ -1125,6 +1125,18 @@ check_param(char *s, int set, int test)
      *
      * TODO: passing s as a parameter while we get some mysterious
      * offset "offs" into it via a global sucks badly.
+     *
+     * From ../lex.c we know:
+     * wb is the beginning position of the current word in the line
+     * we is the position of the end of the current word in the line
+     * From zle_tricky.c we know:
+     * offs is position within the word where we are completing
+     *
+     * So wb + offs is the current cursor position if COMPLETE_IN_WORD
+     * is set, otherwise it is the end of the word (same as we).
+     * 
+     * Note below we are thus stepping backward from our completion
+     * position to find a '$' in the current word (if any).
      */ 
     for (p = s + offs; ; p--) {
 	if (*p == String || *p == Qstring) {
@@ -1171,13 +1183,13 @@ check_param(char *s, int set, int test)
 	    char *tb = b;
 
 	    /* If this is a ${...}, see if we are before the '}'. */
-	    if (!skipparens(Inbrace, Outbrace, &tb))
+	    if (!skipparens(Inbrace, Outbrace, &tb) && tb - s <= offs)
 		return NULL;
 
 	    /* Ignore the possible (...) flags. */
-	    b++, br++;
-	    if ((qstring ? skipparens('(', ')', &b) :
-		 skipparens(Inpar, Outpar, &b)) > 0) {
+	    tb = ++b, br++;
+	    if ((qstring ? skipparens('(', ')', &tb) :
+		 skipparens(Inpar, Outpar, &tb)) > 0 || tb - s >= offs) {
 		/*
 		 * We are still within the parameter flags.  There's no
 		 * point trying to do anything clever here with
@@ -1188,6 +1200,14 @@ check_param(char *s, int set, int test)
 		ispar = 2;
 		return NULL;
 	    }
+	    if ((qstring ? '(' : Inpar) == *b) {
+		/*
+		 * We are inside the braces but on the opening paren.
+		 * There is nothing useful to complete here?
+		 */
+		return NULL;
+	    } else
+		b = tb;	/* Skip over the flags */
 
 	    for (tb = p - 1; tb > s && *tb != Outbrace && *tb != Inbrace; tb--);
 	    if (tb > s && *tb == Inbrace && (tb[-1] == String || *tb == Qstring))

  reply	other threads:[~2021-05-08 18:37 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-21 14:37 Bug report: Completion for dynamically named dirs fails when $SUFFIX is not empty Marlon Richert
2021-01-25 15:07 ` Peter Stephenson
2021-03-31  8:10   ` Marlon Richert
2021-03-31  8:25     ` Peter Stephenson
2021-03-31  8:29       ` Marlon Richert
2021-03-31  8:37         ` Marlon Richert
2021-03-31  8:56           ` Peter Stephenson
2021-05-07 14:08   ` [BUG] COMPLETE_IN_WORD fails to recognize brace_parameter context Marlon Richert
2021-05-08  0:04     ` Bart Schaefer [this message]
2021-05-08 13:04       ` Marlon Richert
2021-05-08  4:37     ` Bart Schaefer
2023-02-22 11:55   ` Bug report: Completion for dynamically named dirs fails when $SUFFIX is not empty Marlon Richert

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='CAH+w=7ajH4H9eJwB5=jEGm77VZXhRGD5h53wT7Un=py8jeb-dg@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=marlon.richert@gmail.com \
    --cc=p.w.stephenson@ntlworld.com \
    --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).