Skip to content

Commit 9712a25

Browse files
habamaxchrisbra
authored andcommitted
patch 9.1.1229: the comment plugin can be improved
Problem: the comment plugin can be improved Solution: add comment text objects "ic" and "ac" (Maxim Kim) closes: #16938 Signed-off-by: Maxim Kim <habamax@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 51a06ec commit 9712a25

6 files changed

Lines changed: 451 additions & 8 deletions

File tree

runtime/pack/dist/opt/comment/autoload/comment.vim

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
vim9script
22

33
# Maintainer: Maxim Kim <habamax@gmail.com>
4-
# Last Update: 2024 Oct 05
4+
# Last Update: 2025 Mar 21
55
#
66
# Toggle comments
77
# Usage:
@@ -76,3 +76,88 @@ export def Toggle(...args: list<string>): string
7676
noautocmd keepjumps setline(lnum1, lines)
7777
return ''
7878
enddef
79+
80+
81+
# Comment text object
82+
# Usage:
83+
# import autoload 'dist/comment.vim'
84+
# onoremap <silent>ic <scriptcmd>comment.ObjComment(v:true)<CR>
85+
# onoremap <silent>ac <scriptcmd>comment.ObjComment(v:false)<CR>
86+
# xnoremap <silent>ic <esc><scriptcmd>comment.ObjComment(v:true)<CR>
87+
# xnoremap <silent>ac <esc><scriptcmd>comment.ObjComment(v:false)<CR>
88+
export def ObjComment(inner: bool)
89+
def IsComment(): bool
90+
var stx = map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')->join()
91+
return stx =~? 'Comment'
92+
enddef
93+
94+
# requires syntax support
95+
if !exists("g:syntax_on")
96+
return
97+
endif
98+
99+
var pos_init = getcurpos()
100+
101+
# If not in comment, search next one,
102+
if !IsComment()
103+
if search('\v\k+', 'W', line(".") + 100, 100, () => !IsComment()) <= 0
104+
return
105+
endif
106+
endif
107+
108+
# Search for the beginning of the comment block
109+
if IsComment()
110+
if search('\v%(\S+)|$', 'bW', 0, 200, IsComment) > 0
111+
search('\v%(\S)|$', 'W', 0, 200, () => !IsComment())
112+
else
113+
cursor(1, 1)
114+
search('\v\S+', 'cW', 0, 200)
115+
endif
116+
endif
117+
118+
var pos_start = getcurpos()
119+
120+
if !inner
121+
var col = pos_start[2]
122+
var prefix = getline(pos_start[1])[ : col - 2]
123+
while col > 0 && prefix[col - 2] =~ '\s'
124+
col -= 1
125+
endwhile
126+
pos_start[2] = col
127+
endif
128+
129+
# Search for the comment end.
130+
if pos_init[1] > pos_start[1]
131+
cursor(pos_init[1], pos_init[2])
132+
endif
133+
if search('\v%(\S+)|$', 'W', 0, 200, IsComment) > 0
134+
search('\S', 'beW', 0, 200, () => !IsComment())
135+
else
136+
if search('\%$', 'W', 0, 200) > 0
137+
search('\ze\S', 'beW', 0, 200, () => !IsComment())
138+
endif
139+
endif
140+
141+
var pos_end = getcurpos()
142+
143+
if !inner
144+
var spaces = matchstr(getline(pos_end[1]), '\%>.c\s*')
145+
pos_end[2] += spaces->len()
146+
if getline(pos_end[1])[pos_end[2] : ] =~ '^\s*$'
147+
&& (pos_start[2] == 1 || getline(pos_start[1])[ : pos_start[2]] =~ '^\s*$')
148+
if search('\v\s*\_$(\s*\n)+', 'eW', 0, 200) > 0
149+
pos_end = getcurpos()
150+
endif
151+
endif
152+
endif
153+
154+
if (pos_end[2] == (getline(pos_end[1])->len() ?? 1)) && pos_start[2] == 1
155+
cursor(pos_end[1], 1)
156+
normal! V
157+
cursor(pos_start[1], 1)
158+
else
159+
cursor(pos_end[1], pos_end[2])
160+
normal! v
161+
cursor(pos_start[1], pos_start[2])
162+
endif
163+
enddef

runtime/pack/dist/opt/comment/doc/comment.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*comment.txt* For Vim version 9.1. Last change: 2024 Oct 01
1+
*comment.txt* For Vim version 9.1. Last change: 2025 Mar 21
22

33

44
VIM REFERENCE MANUAL
@@ -32,11 +32,20 @@ back to `gcc` otherwise, add the following mapping to your vimrc: >
3232
Note: using `gC` may not always result in valid comment markers depending on
3333
the language used.
3434

35+
Additionally, the plugin defines a comment text-object which requires syntax
36+
highlighting to be enabled.
37+
*v_ac* *ac*
38+
ac "a comment", select current or next comment.
39+
Leading and trailing white space is included.
40+
Trailing newlines are included too.
41+
*v_ic* *ic*
42+
ic "inner comment", select current or next comment.
43+
3544
This plugin uses the buffer-local 'commentstring' option value to add or remove
3645
comment markers to the selected lines. Whether it will comment or un-comment
37-
depends on the first line of the range of lines to act upon. When it matches
38-
a comment marker, the line will be un-commented, if it doesn't, the line will
39-
be commented out. Blank and empty lines are ignored.
46+
depends on the range of lines to act upon. When all of the lines in range
47+
have comment markers, all lines will be un-commented, if it doesn't, the lines
48+
will be commented out. Blank and empty lines are ignored.
4049

4150
The value of 'commentstring' is the same for the entire buffer and determined
4251
by its filetype (|filetypes|). To adapt it within the buffer for embedded
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
ac comment.txt /*ac*
12
b:comment_first_col comment.txt /*b:comment_first_col*
23
comment.txt comment.txt /*comment.txt*
34
g:comment_first_col comment.txt /*g:comment_first_col*
45
gcc comment.txt /*gcc*
6+
ic comment.txt /*ic*
57
o_gc comment.txt /*o_gc*
8+
v_ac comment.txt /*v_ac*
69
v_gc comment.txt /*v_gc*
10+
v_ic comment.txt /*v_ic*
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
vim9script
22

33
# Maintainer: Maxim Kim <habamax@gmail.com>
4-
# Last Update: 2024-04-26
4+
# Last Update: 2025 Mar 21
55

66
import autoload 'comment.vim'
7+
78
nnoremap <silent> <expr> gc comment.Toggle()
89
xnoremap <silent> <expr> gc comment.Toggle()
910
nnoremap <silent> <expr> gcc comment.Toggle() .. '_'
11+
12+
onoremap <silent>ic <scriptcmd>comment.ObjComment(v:true)<CR>
13+
onoremap <silent>ac <scriptcmd>comment.ObjComment(v:false)<CR>
14+
xnoremap <silent>ic <esc><scriptcmd>comment.ObjComment(v:true)<CR>
15+
xnoremap <silent>ac <esc><scriptcmd>comment.ObjComment(v:false)<CR>

0 commit comments

Comments
 (0)