Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 1 | " Tests for the various flags in the 'comments' option |
| 2 | |
| 3 | " Test for the 'n' flag in 'comments' |
| 4 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 20 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 21 | endfunc |
| 22 | |
| 23 | " Test for the 'b' flag in 'comments' |
| 24 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 39 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 40 | endfunc |
| 41 | |
| 42 | " Test for the 'f' flag in 'comments' (only the first line has a comment |
| 43 | " string) |
| 44 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 53 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 54 | endfunc |
| 55 | |
| 56 | " Test for the 's', 'm' and 'e' flags in 'comments' |
| 57 | " Test for automatically adding comment leaders in insert mode |
| 58 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 78 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 79 | endfunc |
| 80 | |
| 81 | " Test for the 'r' flag in 'comments' (right align comment) |
| 82 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 100 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 101 | endfunc |
| 102 | |
| 103 | " Test for the 'O' flag in 'comments' |
| 104 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 115 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 116 | endfunc |
| 117 | |
| 118 | " Test for using a multibyte character as a comment leader |
| 119 | func Test_comment_multibyte_leader() |
| 120 | new |
| 121 | let t =<< trim END |
| 122 | { |
| 123 | X |
| 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 | X |
| 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 | X |
| 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 | X |
| 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 188 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 189 | endfunc |
| 190 | |
| 191 | " Test for a space character in 'comments' setting |
| 192 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 208 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 209 | endfunc |
| 210 | |
| 211 | " Test for formatting lines with and without comments |
| 212 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 217 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 218 | endfunc |
| 219 | |
| 220 | " Test for using 'a' in 'formatoptions' with comments |
| 221 | func 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 | |
zeertzjq | 7ac1145 | 2024-03-06 20:54:22 +0100 | [diff] [blame] | 240 | 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 246 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 247 | endfunc |
| 248 | |
| 249 | " Test for joining lines with comments ('j' flag in 'formatoptions') |
| 250 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 265 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 266 | endfunc |
| 267 | |
| 268 | " Test for formatting lines where only the first line has a comment. |
| 269 | func 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 Brabandt | 35cb036 | 2025-03-24 20:38:42 +0100 | [diff] [blame] | 280 | bw! |
Bram Moolenaar | b7e2483 | 2020-06-24 13:37:35 +0200 | [diff] [blame] | 281 | endfunc |
| 282 | |
| 283 | " vim: shiftwidth=2 sts=2 expandtab |