]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0007: can select empty inner text blocks v9.1.0007
authorChristian Brabandt <cb@256bit.org>
Thu, 4 Jan 2024 20:43:36 +0000 (21:43 +0100)
committerChristian Brabandt <cb@256bit.org>
Thu, 4 Jan 2024 21:11:33 +0000 (22:11 +0100)
Problem:  can select empty inner text blocks
          (laurentalacoque)
Solution: make selecting empty inner text blocks an error

textobjects: Make selecting inner empty blocks an error

fixes: #13514
closes: #13523

Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/doc/motion.txt
src/testdir/test_textobjects.vim
src/textobject.c
src/version.c

index 448b68bce51a2e3691d829fb139280d7ec849022..8c07fbf53ef0cf4bb67a471f125e0541294773d3 100644 (file)
@@ -600,7 +600,8 @@ i]                                          *v_i]* *v_i[* *i]* *i[*
 i[                     "inner [] block", select [count] '[' ']' blocks.  This
                        goes backwards to the [count] unclosed '[', and finds
                        the matching ']'.  The enclosed text is selected,
-                       excluding the '[' and ']'.  The |cpo-M| option flag
+                       excluding the '[' and ']'.  It's an error to select an
+                       empty inner block like "[]".  The |cpo-M| option flag
                        is used to handle escaped brackets.
                        When used in Visual mode it is made characterwise.
 
@@ -618,7 +619,8 @@ i(                                                  *vib* *v_ib* *v_i(* *ib*
 ib                     "inner block", select [count] blocks, from "[count] [("
                        to the matching ')', excluding the '(' and ')' (see
                        |[(|).  If the cursor is not inside a () block, then
-                       find the next "(".  The |cpo-M| option flag
+                       find the next "(".  It's an error to select an empty
+                       inner block like "()".  The |cpo-M| option flag
                        is used to handle escaped parenthesis.
                        When used in Visual mode it is made characterwise.
 
@@ -632,8 +634,9 @@ a<                  "a <> block", select [count] <> blocks, from the
 i>                                             *v_i>* *v_i<* *i>* *i<*
 i<                     "inner <> block", select [count] <> blocks, from
                        the [count]'th unmatched '<' backwards to the matching
-                       '>', excluding the '<' and '>'.  The |cpo-M| option flag
-                       is used to handle escaped '<' and '>'.
+                       '>', excluding the '<' and '>'.  It's an error to
+                       select an empty inner block like "<>".  The |cpo-M|
+                       option flag is used to handle escaped '<' and '>'.
                        When used in Visual mode it is made characterwise.
 
                                                *v_at* *at*
@@ -663,7 +666,8 @@ i}                                                  *v_i}* *i}* *i{*
 i{                                                     *v_iB* *v_i{* *iB*
 iB                     "inner Block", select [count] Blocks, from "[count] [{"
                        to the matching '}', excluding the '{' and '}' (see
-                       |[{|).  The |cpo-M| option flag is used to handle
+                       |[{|).  It's an error to select an empty inner block
+                       like "{}".  The |cpo-M| option flag is used to handle
                        escaped braces.
                        When used in Visual mode it is made characterwise.
 
index 746b3268b2a1e0382d59a44772e1eb666efaa5d5..89e741c7158904ca7ee6f703a144074ab3aa5881 100644 (file)
@@ -400,7 +400,7 @@ func Test_paragraph()
   call assert_beeps("normal Vipip")
   exe "normal \<C-C>"
 
-  close!
+  bw!
 endfunc
 
 " Tests for text object aw
@@ -606,7 +606,7 @@ func Test_textobj_quote()
   normal $hhyi"
   call assert_equal('bar', @")
 
-  close!
+  bw!
 endfunc
 
 " Test for i(, i<, etc. when cursor is in front of a block
@@ -638,7 +638,115 @@ func Test_textobj_find_paren_forward()
   normal 0di)
   call assert_equal('foo ()', getline(1))
 
-  close!
+  bw!
+endfunc
+
+func Test_inner_block_empty_paren()
+  new
+  call setline(1, ["(text)()", "", "(text)(", ")", "", "()()"])
+
+  " Example 1
+  call cursor(1, 1)
+  let @" = ''
+  call assert_beeps(':call feedkeys("0f(viby","xt")')
+  call assert_equal(7, getpos('.')[2])
+  call assert_equal('(', @")
+
+  " Example 2
+  call cursor(3, 1)
+  let @" = ''
+  call assert_beeps('call feedkeys("0f(viby", "xt")')
+  call assert_equal(7, getpos('.')[2])
+  call assert_equal('(', @")
+
+  " Example 3
+  call cursor(6, 1)
+  let @" = ''
+  call assert_beeps('call feedkeys("0f(viby", "xt")')
+  call assert_equal(3, getpos('.')[2])
+  call assert_equal('(', @")
+  bwipe!
+endfunc
+
+func Test_inner_block_empty_bracket()
+  new
+  call setline(1, ["[text][]", "", "[text][", "]", "", "[][]"])
+
+  " Example 1
+  call cursor(1, 1)
+  let @" = ''
+  call assert_beeps(':call feedkeys("0f[viby","xt")')
+  call assert_equal(7, getpos('.')[2])
+  call assert_equal('[', @")
+
+  " Example 2
+  call cursor(3, 1)
+  let @" = ''
+  call assert_beeps('call feedkeys("0f[viby", "xt")')
+  call assert_equal(7, getpos('.')[2])
+  call assert_equal('[', @")
+
+  " Example 3
+  call cursor(6, 1)
+  let @" = ''
+  call assert_beeps('call feedkeys("0f[viby", "xt")')
+  call assert_equal(3, getpos('.')[2])
+  call assert_equal('[', @")
+  bwipe!
+endfunc
+
+func Test_inner_block_empty_brace()
+  new
+  call setline(1, ["{text}{}", "", "{text}{", "}", "", "{}{}"])
+
+  " Example 1
+  call cursor(1, 1)
+  let @" = ''
+  call assert_beeps(':call feedkeys("0f{viby","xt")')
+  call assert_equal(7, getpos('.')[2])
+  call assert_equal('{', @")
+
+  " Example 2
+  call cursor(3, 1)
+  let @" = ''
+  call assert_beeps('call feedkeys("0f{viby", "xt")')
+  call assert_equal(7, getpos('.')[2])
+  call assert_equal('{', @")
+
+  " Example 3
+  call cursor(6, 1)
+  let @" = ''
+  call assert_beeps('call feedkeys("0f{viby", "xt")')
+  call assert_equal(3, getpos('.')[2])
+  call assert_equal('{', @")
+  bwipe!
+endfunc
+
+func Test_inner_block_empty_lessthan()
+  new
+  call setline(1, ["<text><>", "", "<text><", ">", "", "<><>"])
+
+  " Example 1
+  call cursor(1, 1)
+  let @" = ''
+  call assert_beeps(':call feedkeys("0f<viby","xt")')
+  call assert_equal(7, getpos('.')[2])
+  call assert_equal('<', @")
+
+  " Example 2
+  call cursor(3, 1)
+  let @" = ''
+  call assert_beeps('call feedkeys("0f<viby", "xt")')
+  call assert_equal(7, getpos('.')[2])
+  call assert_equal('<', @")
+
+  " Example 3
+  call cursor(6, 1)
+  let @" = ''
+  call assert_beeps('call feedkeys("0f<viby", "xt")')
+  call assert_equal(3, getpos('.')[2])
+  call assert_equal('<', @")
+  bwipe!
 endfunc
 
 " vim: shiftwidth=2 sts=2 expandtab
index d03b6244ed8c685bec51e7613bcf953eba5c5ee9..af94d06bc806479a804cc3f77b3b91d81072e2bb 100644 (file)
@@ -1131,6 +1131,11 @@ current_block(
                break;
        }
 
+       if (EQUAL_POS(start_pos, *end_pos))
+           // empty block like this: ()
+           // there is no inner block to select, abort
+           return FAIL;
+
        /*
         * In Visual mode, when the resulting area is not bigger than what we
         * started with, extend it to the next block, and then exclude again.
index 3dc537d1803d3f357cabae82313d464c7fd670ec..d00e80276ce0688a85ca3594a505a5e0027fd56a 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    7,
 /**/
     6,
 /**/