]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1508: catch does not work when lines are joined with a newline v9.0.1508
authorzeertzjq <zeertzjq@outlook.com>
Fri, 5 May 2023 16:22:35 +0000 (17:22 +0100)
committerBram Moolenaar <Bram@vim.org>
Fri, 5 May 2023 16:22:35 +0000 (17:22 +0100)
Problem:    Catch does not work when lines are joined with a newline.
Solution:   Set "nextcmd" appropriately. (closes #12348)

src/eval.c
src/testdir/test_trycatch.vim
src/version.c

index c31b0be072544ab074793a99dee604c1865a9686..bae728079525d227b81f1f06ec37415c9a2cb13d 100644 (file)
@@ -2699,12 +2699,15 @@ eval0_retarg(
                semsg(_(e_invalid_expression_str), arg);
        }
 
-       // Some of the expression may not have been consumed.  Do not check for
-       // a next command to avoid more errors, unless "|" is following, which
-       // could only be a command separator.
-       if (eap != NULL && p != NULL
-                         &&  skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
-           eap->nextcmd = check_nextcmd(p);
+       if (eap != NULL && p != NULL)
+       {
+           // Some of the expression may not have been consumed.
+           // Only execute a next command if it cannot be a "||" operator.
+           // The next command may be "catch".
+           char_u *nextcmd = check_nextcmd(p);
+           if (nextcmd != NULL && *nextcmd != '|')
+               eap->nextcmd = nextcmd;
+       }
        return FAIL;
     }
 
index 9d6208455ed2f7392d5e6ee1eb6e78833b53ac56..813ad08b1668b5c6c0cfe3adc3bf939cadc20961 100644 (file)
@@ -2220,6 +2220,36 @@ func Test_BufEnter_exception()
   %bwipe!
 endfunc
 
+" Test for using try/catch when lines are joined by "|" or "\n"           {{{1
+func Test_try_catch_nextcmd()
+  func Throw()
+    throw "Failure"
+  endfunc
+
+  let lines =<< trim END
+    try
+      let s:x = Throw()
+    catch
+      let g:caught = 1
+    endtry
+  END
+
+  let g:caught = 0
+  call execute(lines)
+  call assert_equal(1, g:caught)
+
+  let g:caught = 0
+  call execute(join(lines, '|'))
+  call assert_equal(1, g:caught)
+
+  let g:caught = 0
+  call execute(join(lines, "\n"))
+  call assert_equal(1, g:caught)
+
+  unlet g:caught
+  delfunc Throw
+endfunc
+
 " Test for using try/catch in a user command with a failing expression    {{{1
 func Test_user_command_try_catch()
   let lines =<< trim END
index 5db46d8c0ea753ae4a25a396a596bcd47bc030d7..5906b9a079a72d26a5ee66be99dd569d96401d98 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1508,
 /**/
     1507,
 /**/