]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1770: Vim9: wrong behaviour with trailing comments in command blocks v9.1.1770
authorYegappan Lakshmanan <yegappan@yahoo.com>
Thu, 18 Sep 2025 19:40:22 +0000 (19:40 +0000)
committerChristian Brabandt <cb@256bit.org>
Thu, 18 Sep 2025 19:40:22 +0000 (19:40 +0000)
Problem:  Vim9: wrong behaviour with trailing comments in command blocks
          (balki)
Solution: Correctly skip over trailing comments (Yegappan Lakshmanan).

fixes: #18268
closes: #18327

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/ex_docmd.c
src/testdir/test_vim9_script.vim
src/version.c

index 9d7e58ed9c1b9ec28a24188cc2f80062a442ec22..ea524486f296d247bf0aeccbb50baa93222c8909 100644 (file)
@@ -5353,6 +5353,7 @@ repl_cmdline(
     void
 separate_nextcmd(exarg_T *eap, int keep_backslash)
 {
+    int                vim9script = in_vim9script();
     char_u     *p;
 
 #ifdef FEAT_QUICKFIX
@@ -5389,14 +5390,14 @@ separate_nextcmd(exarg_T *eap, int keep_backslash)
        // :@" and :*" do not start a comment!
        // :redir @" doesn't either.
        else if ((*p == '"'
-                   && !in_vim9script()
+                   && !vim9script
                    && !(eap->argt & EX_NOTRLCOM)
                    && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
                                                              || p != eap->arg)
                    && (eap->cmdidx != CMD_redir
                                         || p != eap->arg + 1 || p[-1] != '@'))
                || (*p == '#'
-                   && in_vim9script()
+                   && vim9script
                    && !(eap->argt & EX_NOTRLCOM)
                    && p > eap->cmd && VIM_ISWHITE(p[-1]))
                || (*p == '|'
@@ -5420,7 +5421,7 @@ separate_nextcmd(exarg_T *eap, int keep_backslash)
            }
            else
            {
-               eap->nextcmd = check_nextcmd(p);
+               set_nextcmd(eap, p);
                *p = NUL;
                break;
            }
@@ -5932,7 +5933,16 @@ check_nextcmd(char_u *p)
     void
 set_nextcmd(exarg_T *eap, char_u *arg)
 {
-    char_u *p = check_nextcmd(arg);
+    char_u *p = skipwhite(arg);
+
+    if (in_vim9script() && *p == '#')
+    {
+       char_u *nl = vim_strchr(p, NL);
+       if (nl != NULL)
+           p = nl;
+    }
+
+    p = check_nextcmd(p);
 
     if (eap->nextcmd == NULL)
        eap->nextcmd = p;
index 8726ee4836d38846c3b540342f29a8ef2753c27b..823a6cf2599c7de8f8afb4502372e0a0a9407f69 100644 (file)
@@ -613,6 +613,52 @@ def Test_block_in_a_string()
   v9.CheckSourceSuccess(lines)
 enddef
 
+" Test for a block in a command with comments
+def Test_block_command_with_comment()
+  var lines =<< trim END
+    vim9script
+
+    g:Str = ''
+    command Cmd1 {
+      g:Str = 'Hello' # comment1
+      var x: string # comment2
+      g:Str ..= ' World' # comment3
+    }
+    Cmd1
+    assert_equal('Hello World', g:Str)
+
+    g:Str = ''
+    command Cmd2 {
+      # comment1
+      g:Str = 'Hello'
+      # comment2
+      g:Str ..= ' World'
+      # comment3
+    }
+    Cmd2
+    assert_equal('Hello World', g:Str)
+
+    command Cmd3 {
+      new # comment1
+      setline(1, 'hello') # comment2
+    }
+    Cmd3
+    assert_equal(['hello'], getline(1, '$'))
+    :bw!
+
+    command Cmd4 {
+      # comment1
+      new
+      # comment2
+      setline(1, 'hello') # comment2
+    }
+    Cmd4
+    assert_equal(['hello'], getline(1, '$'))
+    :bw!
+  END
+  v9.CheckSourceSuccess(lines)
+enddef
+
 " Test for using too many nested blocks
 def Test_too_many_nested_blocks()
   var lines = ['vim9script']
index 7f307167c96d0add06ebb1442a671e085f227e4e..bd78cbf1d5fadb17d313dd51ead3d1c85f82e57c 100644 (file)
@@ -724,6 +724,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1770,
 /**/
     1769,
 /**/