patch 9.1.1259: some issues with comment package and tailing spaces

Problem:  some issues with comment package and tailing spaces
Solution: correctly capture trailing spaces with the ac/ic text object
          (Maxim Kim)

This commit fixes a few issues with the comment package:

1) both ac and ic incorrectly miss the last //

```
// hello trailing spaces
//
```

2) fix ac/ic with last empty comment line,
   vac should also select last line with #

```py
 # print("hello")
 # print("world")
 #
 #
$endofbuffer$
```

closes: #17013

Signed-off-by: Maxim Kim <habamax@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Maxim Kim
2025-03-30 14:55:26 +02:00
committed by Christian Brabandt
parent bb8e5ddb97
commit a580761a45
3 changed files with 74 additions and 6 deletions

View File

@ -1,7 +1,7 @@
vim9script
# Maintainer: Maxim Kim <habamax@gmail.com>
# Last Update: 2025 Mar 21
# Last Update: 2025-03-30
#
# Toggle comments
# Usage:
@ -107,8 +107,8 @@ export def ObjComment(inner: bool)
# Search for the beginning of the comment block
if IsComment()
if search('\v%(\S+)|$', 'bW', 0, 200, IsComment) > 0
search('\v%(\S)|$', 'W', 0, 200, () => !IsComment())
if search('\v%(\S+)|%(^\s*$)', 'bW', 0, 200, IsComment) > 0
search('\v%(\S)|%(^\s*$)', 'W', 0, 200, () => !IsComment())
else
cursor(1, 1)
search('\v\S+', 'cW', 0, 200)
@ -130,11 +130,11 @@ export def ObjComment(inner: bool)
if pos_init[1] > pos_start[1]
cursor(pos_init[1], pos_init[2])
endif
if search('\v%(\S+)|$', 'W', 0, 200, IsComment) > 0
if search('\v%(\S+)|%(^\s*$)', 'W', 0, 200, IsComment) > 0
search('\S', 'beW', 0, 200, () => !IsComment())
else
if search('\%$', 'W', 0, 200) > 0
search('\ze\S', 'beW', 0, 200, () => !IsComment())
search('\ze\S', 'beW', line('.'), 200, () => !IsComment())
endif
endif
@ -144,7 +144,7 @@ export def ObjComment(inner: bool)
var spaces = matchstr(getline(pos_end[1]), '\%>.c\s*')
pos_end[2] += spaces->len()
if getline(pos_end[1])[pos_end[2] : ] =~ '^\s*$'
&& (pos_start[2] == 1 || getline(pos_start[1])[ : pos_start[2]] =~ '^\s*$')
&& (pos_start[2] <= 1 || getline(pos_start[1])[ : pos_start[2]] =~ '^\s*$')
if search('\v\s*\_$(\s*\n)+', 'eW', 0, 200) > 0
pos_end = getcurpos()
endif