blob: f22f90300e78de2a10cff149d3ecbc6d9c4e0061 [file] [log] [blame]
Bram Moolenaarb7e24832020-06-24 13:37:35 +02001" Tests for the various flags in the 'comments' option
2
3" Test for the 'n' flag in 'comments'
4func Test_comment_nested()
5 new
6 setlocal comments=n:> fo+=ro
7 exe "normal i> B\nD\<C-C>ggOA\<C-C>joC\<C-C>Go\<BS>>>> F\nH"
8 exe "normal 5GOE\<C-C>6GoG"
9 let expected =<< trim END
10 > A
11 > B
12 > C
13 > D
14 >>>> E
15 >>>> F
16 >>>> G
17 >>>> H
18 END
19 call assert_equal(expected, getline(1, '$'))
Christian Brabandt35cb0362025-03-24 20:38:42 +010020 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +020021endfunc
22
23" Test for the 'b' flag in 'comments'
24func Test_comment_blank()
25 new
26 setlocal comments=b:* fo+=ro
27 exe "normal i* E\nF\n\<BS>G\nH\<C-C>ggOC\<C-C>O\<BS>B\<C-C>OA\<C-C>2joD"
28 let expected =<< trim END
29 A
30 *B
31 * C
32 * D
33 * E
34 * F
35 *G
36 H
37 END
38 call assert_equal(expected, getline(1, '$'))
Christian Brabandt35cb0362025-03-24 20:38:42 +010039 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +020040endfunc
41
42" Test for the 'f' flag in 'comments' (only the first line has a comment
43" string)
44func Test_comment_firstline()
45 new
46 setlocal comments=f:- fo+=ro
47 exe "normal i- B\nD\<C-C>ggoC\<C-C>ggOA\<C-C>"
48 call assert_equal(['A', '- B', ' C', ' D'], getline(1, '$'))
49 %d
50 setlocal comments=:-
51 exe "normal i- B\nD\<C-C>ggoC\<C-C>ggOA\<C-C>"
52 call assert_equal(['- A', '- B', '- C', '- D'], getline(1, '$'))
Christian Brabandt35cb0362025-03-24 20:38:42 +010053 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +020054endfunc
55
56" Test for the 's', 'm' and 'e' flags in 'comments'
57" Test for automatically adding comment leaders in insert mode
58func Test_comment_threepiece()
59 new
60 setlocal expandtab
61 call setline(1, ["\t/*"])
62 setlocal formatoptions=croql
63 call cursor(1, 3)
64 call feedkeys("A\<cr>\<cr>/", 'tnix')
65 call assert_equal(["\t/*", " *", " */"], getline(1, '$'))
66
67 " If a comment ends in a single line, then don't add it in the next line
68 %d
69 call setline(1, '/* line1 */')
70 call feedkeys("A\<CR>next line", 'xt')
71 call assert_equal(['/* line1 */', 'next line'], getline(1, '$'))
72
73 %d
74 " Copy the trailing indentation from the leader comment to a new line
75 setlocal autoindent noexpandtab
76 call feedkeys("a\t/*\tone\ntwo\n/", 'xt')
77 call assert_equal(["\t/*\tone", "\t *\ttwo", "\t */"], getline(1, '$'))
Christian Brabandt35cb0362025-03-24 20:38:42 +010078 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +020079endfunc
80
81" Test for the 'r' flag in 'comments' (right align comment)
82func Test_comment_rightalign()
83 new
84 setlocal comments=sr:/***,m:**,ex-2:******/ fo+=ro
85 exe "normal i=\<C-C>o\t /***\nD\n/"
86 exe "normal 2GOA\<C-C>joB\<C-C>jOC\<C-C>joE\<C-C>GOF\<C-C>joG"
87 let expected =<< trim END
88 =
89 A
90 /***
91 ** B
92 ** C
93 ** D
94 ** E
95 ** F
96 ******/
97 G
98 END
99 call assert_equal(expected, getline(1, '$'))
Christian Brabandt35cb0362025-03-24 20:38:42 +0100100 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200101endfunc
102
103" Test for the 'O' flag in 'comments'
104func Test_comment_O()
105 new
106 setlocal comments=Ob:* fo+=ro
107 exe "normal i* B\nD\<C-C>kOA\<C-C>joC"
108 let expected =<< trim END
109 A
110 * B
111 * C
112 * D
113 END
114 call assert_equal(expected, getline(1, '$'))
Christian Brabandt35cb0362025-03-24 20:38:42 +0100115 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200116endfunc
117
118" Test for using a multibyte character as a comment leader
119func Test_comment_multibyte_leader()
120 new
121 let t =<< trim END
122 {
123
124 Xa
125 XaY
126 XY
127 XYZ
128 X Y
129 X YZ
130 XX
131 XXa
132 XXY
133 }
134 END
135 call setline(1, t)
136 call cursor(2, 1)
137
138 set tw=2 fo=cqm comments=n:X
139 exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
140 let t =<< trim END
141
142 Xa
143 XaY
144 XY
145 XYZ
146 X Y
147 X YZ
148 XX
149 XXa
150 XXY
151 END
152 exe "normal o\n" . join(t, "\n")
153
154 let expected =<< trim END
155 {
156
157 Xa
158 Xa
159 XY
160 XY
161 XY
162 XZ
163 X Y
164 X Y
165 X Z
166 XX
167 XXa
168 XXY
169
170
171 Xa
172 Xa
173 XY
174 XY
175 XY
176 XZ
177 X Y
178 X Y
179 X Z
180 XX
181 XXa
182 XXY
183 }
184 END
185 call assert_equal(expected, getline(1, '$'))
186
187 set tw& fo& comments&
Christian Brabandt35cb0362025-03-24 20:38:42 +0100188 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200189endfunc
190
191" Test for a space character in 'comments' setting
192func Test_comment_space()
193 new
194 setlocal comments=b:\ > fo+=ro
195 exe "normal i> B\nD\<C-C>ggOA\<C-C>joC"
196 exe "normal Go > F\nH\<C-C>kOE\<C-C>joG"
197 let expected =<< trim END
198 A
199 > B
200 C
201 D
202 > E
203 > F
204 > G
205 > H
206 END
207 call assert_equal(expected, getline(1, '$'))
Christian Brabandt35cb0362025-03-24 20:38:42 +0100208 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200209endfunc
210
211" Test for formatting lines with and without comments
212func Test_comment_format_lines()
213 new
214 call setline(1, ['one', '/* two */', 'three'])
215 normal gggqG
216 call assert_equal(['one', '/* two */', 'three'], getline(1, '$'))
Christian Brabandt35cb0362025-03-24 20:38:42 +0100217 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200218endfunc
219
220" Test for using 'a' in 'formatoptions' with comments
221func Test_comment_autoformat()
222 new
223 setlocal formatoptions+=a
224 call feedkeys("a- one\n- two\n", 'xt')
225 call assert_equal(['- one', '- two', ''], getline(1, '$'))
226
227 %d
228 call feedkeys("a\none\n", 'xt')
229 call assert_equal(['', 'one', ''], getline(1, '$'))
230
231 setlocal formatoptions+=aw
232 %d
233 call feedkeys("aone \ntwo\n", 'xt')
234 call assert_equal(['one two', ''], getline(1, '$'))
235
236 %d
237 call feedkeys("aone\ntwo\n", 'xt')
238 call assert_equal(['one', 'two', ''], getline(1, '$'))
239
zeertzjq7ac11452024-03-06 20:54:22 +0100240 set backspace=indent,eol,start
241 %d
242 call feedkeys("aone \n\<BS>", 'xt')
243 call assert_equal(['one'], getline(1, '$'))
244 set backspace&
245
Christian Brabandt35cb0362025-03-24 20:38:42 +0100246 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200247endfunc
248
249" Test for joining lines with comments ('j' flag in 'formatoptions')
250func Test_comment_join_lines_fo_j()
251 new
252 setlocal fo+=j comments=://
253 call setline(1, ['i++; // comment1', ' // comment2'])
254 normal J
255 call assert_equal('i++; // comment1 comment2', getline(1))
256 setlocal fo-=j
257 call setline(1, ['i++; // comment1', ' // comment2'])
258 normal J
259 call assert_equal('i++; // comment1 // comment2', getline(1))
260 " Test with nested comments
261 setlocal fo+=j comments=n:>,n:)
262 call setline(1, ['i++; > ) > ) comment1', ' > ) comment2'])
263 normal J
264 call assert_equal('i++; > ) > ) comment1 comment2', getline(1))
Christian Brabandt35cb0362025-03-24 20:38:42 +0100265 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200266endfunc
267
268" Test for formatting lines where only the first line has a comment.
269func Test_comment_format_firstline_comment()
270 new
271 setlocal formatoptions=tcq
272 call setline(1, ['- one two', 'three'])
273 normal gggqG
274 call assert_equal(['- one two three'], getline(1, '$'))
275
276 %d
277 call setline(1, ['- one', '- two'])
278 normal gggqG
279 call assert_equal(['- one', '- two'], getline(1, '$'))
Christian Brabandt35cb0362025-03-24 20:38:42 +0100280 bw!
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200281endfunc
282
283" vim: shiftwidth=2 sts=2 expandtab