Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 1 | " Test for 'number' and 'relativenumber' |
| 2 | |
Christian Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 3 | source util/screendump.vim |
Bram Moolenaar | efae76a | 2019-10-27 22:54:58 +0100 | [diff] [blame] | 4 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 5 | func s:screen_lines(start, end) abort |
Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 6 | return ScreenLines([a:start, a:end], 8) |
| 7 | endfunc |
| 8 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 9 | func s:compare_lines(expect, actual) |
Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 10 | call assert_equal(a:expect, a:actual) |
| 11 | endfunc |
| 12 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 13 | func s:test_windows(h, w) abort |
Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 14 | call NewWindow(a:h, a:w) |
| 15 | endfunc |
| 16 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 17 | func s:close_windows() abort |
Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 18 | call CloseWindow() |
| 19 | endfunc |
| 20 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 21 | func s:validate_cursor() abort |
Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 22 | " update skipcol. |
| 23 | " wincol(): |
| 24 | " f_wincol |
| 25 | " -> validate_cursor |
| 26 | " -> curs_columns |
| 27 | call wincol() |
| 28 | endfunc |
| 29 | |
| 30 | func Test_set_options() |
| 31 | set nu rnu |
| 32 | call assert_equal(1, &nu) |
| 33 | call assert_equal(1, &rnu) |
| 34 | |
| 35 | call s:test_windows(10, 20) |
| 36 | call assert_equal(1, &nu) |
| 37 | call assert_equal(1, &rnu) |
| 38 | call s:close_windows() |
| 39 | |
| 40 | set nu& rnu& |
| 41 | endfunc |
| 42 | |
| 43 | func Test_set_global_and_local() |
| 44 | " setlocal must NOT reset the other global value |
| 45 | set nonu nornu |
| 46 | setglobal nu |
| 47 | setlocal rnu |
| 48 | call assert_equal(1, &g:nu) |
| 49 | |
| 50 | set nonu nornu |
| 51 | setglobal rnu |
| 52 | setlocal nu |
| 53 | call assert_equal(1, &g:rnu) |
| 54 | |
| 55 | " setglobal MUST reset the other global value |
| 56 | set nonu nornu |
| 57 | setglobal nu |
| 58 | setglobal rnu |
| 59 | call assert_equal(1, &g:nu) |
| 60 | |
| 61 | set nonu nornu |
| 62 | setglobal rnu |
| 63 | setglobal nu |
| 64 | call assert_equal(1, &g:rnu) |
| 65 | |
| 66 | " set MUST reset the other global value |
| 67 | set nonu nornu |
| 68 | set nu |
| 69 | set rnu |
| 70 | call assert_equal(1, &g:nu) |
| 71 | |
| 72 | set nonu nornu |
| 73 | set rnu |
| 74 | set nu |
| 75 | call assert_equal(1, &g:rnu) |
| 76 | |
| 77 | set nu& rnu& |
| 78 | endfunc |
| 79 | |
| 80 | func Test_number() |
| 81 | call s:test_windows(10, 20) |
| 82 | call setline(1, ["abcdefghij", "klmnopqrst", "uvwxyzABCD", "EFGHIJKLMN", "OPQRSTUVWX", "YZ"]) |
| 83 | setl number |
| 84 | let lines = s:screen_lines(1, 4) |
| 85 | let expect = [ |
| 86 | \ " 1 abcd", |
| 87 | \ " 2 klmn", |
| 88 | \ " 3 uvwx", |
| 89 | \ " 4 EFGH", |
| 90 | \ ] |
| 91 | call s:compare_lines(expect, lines) |
| 92 | call s:close_windows() |
| 93 | endfunc |
| 94 | |
| 95 | func Test_relativenumber() |
| 96 | call s:test_windows(10, 20) |
| 97 | call setline(1, ["abcdefghij", "klmnopqrst", "uvwxyzABCD", "EFGHIJKLMN", "OPQRSTUVWX", "YZ"]) |
| 98 | 3 |
| 99 | setl relativenumber |
| 100 | let lines = s:screen_lines(1, 6) |
| 101 | let expect = [ |
| 102 | \ " 2 abcd", |
| 103 | \ " 1 klmn", |
| 104 | \ " 0 uvwx", |
| 105 | \ " 1 EFGH", |
| 106 | \ " 2 OPQR", |
| 107 | \ " 3 YZ ", |
| 108 | \ ] |
| 109 | call s:compare_lines(expect, lines) |
| 110 | call s:close_windows() |
| 111 | endfunc |
| 112 | |
| 113 | func Test_number_with_relativenumber() |
| 114 | call s:test_windows(10, 20) |
| 115 | call setline(1, ["abcdefghij", "klmnopqrst", "uvwxyzABCD", "EFGHIJKLMN", "OPQRSTUVWX", "YZ"]) |
| 116 | 4 |
| 117 | setl number relativenumber |
| 118 | let lines = s:screen_lines(1, 6) |
| 119 | let expect = [ |
| 120 | \ " 3 abcd", |
| 121 | \ " 2 klmn", |
| 122 | \ " 1 uvwx", |
| 123 | \ "4 EFGH", |
| 124 | \ " 1 OPQR", |
| 125 | \ " 2 YZ ", |
| 126 | \ ] |
| 127 | call s:compare_lines(expect, lines) |
| 128 | call s:close_windows() |
| 129 | endfunc |
| 130 | |
| 131 | func Test_number_with_linewrap1() |
| 132 | call s:test_windows(3, 20) |
| 133 | normal! 61ia |
| 134 | setl number wrap |
| 135 | call s:validate_cursor() |
| 136 | let lines = s:screen_lines(1, 3) |
| 137 | let expect = [ |
Bram Moolenaar | 0466d39 | 2022-10-03 17:07:34 +0100 | [diff] [blame] | 138 | \ "<<< aaaa", |
Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 139 | \ " aaaa", |
| 140 | \ " aaaa", |
| 141 | \ ] |
| 142 | call s:compare_lines(expect, lines) |
| 143 | call s:close_windows() |
| 144 | endfunc |
| 145 | |
zeertzjq | ae07ebc | 2024-02-08 11:37:40 +0100 | [diff] [blame] | 146 | func Test_number_with_linewrap2() |
Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 147 | call s:test_windows(3, 20) |
| 148 | normal! 61ia |
| 149 | setl number wrap |
| 150 | call s:validate_cursor() |
| 151 | 0 |
| 152 | call s:validate_cursor() |
| 153 | let lines = s:screen_lines(1, 3) |
| 154 | let expect = [ |
| 155 | \ " 1 aaaa", |
| 156 | \ " aaaa", |
| 157 | \ " aaaa", |
| 158 | \ ] |
| 159 | call s:compare_lines(expect, lines) |
| 160 | call s:close_windows() |
| 161 | endfunc |
| 162 | |
zeertzjq | ae07ebc | 2024-02-08 11:37:40 +0100 | [diff] [blame] | 163 | func Test_number_with_linewrap3() |
Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 164 | call s:test_windows(4, 20) |
| 165 | normal! 81ia |
| 166 | setl number wrap |
| 167 | call s:validate_cursor() |
| 168 | setl nonumber |
| 169 | call s:validate_cursor() |
| 170 | let lines = s:screen_lines(1, 4) |
| 171 | let expect = [ |
zeertzjq | ae07ebc | 2024-02-08 11:37:40 +0100 | [diff] [blame] | 172 | \ "<<<aaaaa", |
Bram Moolenaar | dc9a081 | 2017-02-23 18:46:50 +0100 | [diff] [blame] | 173 | \ "aaaaaaaa", |
| 174 | \ "aaaaaaaa", |
| 175 | \ "a ", |
| 176 | \ ] |
| 177 | call s:compare_lines(expect, lines) |
| 178 | call s:close_windows() |
| 179 | endfunc |
| 180 | |
| 181 | func Test_numberwidth() |
| 182 | call s:test_windows(10, 20) |
| 183 | call setline(1, repeat(['aaaa'], 10)) |
| 184 | setl number numberwidth=6 |
| 185 | let lines = s:screen_lines(1, 3) |
| 186 | let expect = [ |
| 187 | \ " 1 aa", |
| 188 | \ " 2 aa", |
| 189 | \ " 3 aa", |
| 190 | \ ] |
| 191 | call s:compare_lines(expect, lines) |
| 192 | |
| 193 | set relativenumber |
| 194 | let lines = s:screen_lines(1, 3) |
| 195 | let expect = [ |
| 196 | \ "1 aa", |
| 197 | \ " 1 aa", |
| 198 | \ " 2 aa", |
| 199 | \ ] |
| 200 | call s:compare_lines(expect, lines) |
| 201 | |
| 202 | set nonumber |
| 203 | let lines = s:screen_lines(1, 3) |
| 204 | let expect = [ |
| 205 | \ " 0 aa", |
| 206 | \ " 1 aa", |
| 207 | \ " 2 aa", |
| 208 | \ ] |
| 209 | call s:compare_lines(expect, lines) |
| 210 | call s:close_windows() |
| 211 | endfunc |
| 212 | |
| 213 | func Test_numberwidth_adjusted() |
| 214 | call s:test_windows(10, 20) |
| 215 | call setline(1, repeat(['aaaa'], 10000)) |
| 216 | setl number numberwidth=4 |
| 217 | let lines = s:screen_lines(1, 3) |
| 218 | let expect = [ |
| 219 | \ " 1 aa", |
| 220 | \ " 2 aa", |
| 221 | \ " 3 aa", |
| 222 | \ ] |
| 223 | call s:compare_lines(expect, lines) |
| 224 | |
| 225 | $ |
| 226 | let lines = s:screen_lines(8, 10) |
| 227 | let expect = [ |
| 228 | \ " 9998 aa", |
| 229 | \ " 9999 aa", |
| 230 | \ "10000 aa", |
| 231 | \ ] |
| 232 | call s:compare_lines(expect, lines) |
| 233 | |
| 234 | setl relativenumber |
| 235 | let lines = s:screen_lines(8, 10) |
| 236 | let expect = [ |
| 237 | \ " 2 aa", |
| 238 | \ " 1 aa", |
| 239 | \ "10000 aa", |
| 240 | \ ] |
| 241 | call s:compare_lines(expect, lines) |
| 242 | |
| 243 | setl nonumber |
| 244 | let lines = s:screen_lines(8, 10) |
| 245 | let expect = [ |
| 246 | \ " 2 aaaa", |
| 247 | \ " 1 aaaa", |
| 248 | \ " 0 aaaa", |
| 249 | \ ] |
| 250 | call s:compare_lines(expect, lines) |
| 251 | call s:close_windows() |
| 252 | endfunc |
Bram Moolenaar | ec572ad | 2019-07-07 14:26:59 +0200 | [diff] [blame] | 253 | |
| 254 | " This was causing a memcheck error |
| 255 | func Test_relativenumber_uninitialised() |
| 256 | new |
| 257 | set rnu |
| 258 | call setline(1, ["a", "b"]) |
| 259 | redraw |
| 260 | call feedkeys("j", 'xt') |
| 261 | redraw |
| 262 | bwipe! |
| 263 | endfunc |
Bram Moolenaar | efae76a | 2019-10-27 22:54:58 +0100 | [diff] [blame] | 264 | |
| 265 | func Test_relativenumber_colors() |
| 266 | CheckScreendump |
| 267 | |
| 268 | let lines =<< trim [CODE] |
| 269 | call setline(1, range(200)) |
| 270 | 111 |
| 271 | set number relativenumber |
| 272 | hi LineNr ctermfg=red |
| 273 | [CODE] |
Bram Moolenaar | b152b6a | 2022-09-29 21:37:33 +0100 | [diff] [blame] | 274 | call writefile(lines, 'XTest_relnr', 'D') |
Bram Moolenaar | efae76a | 2019-10-27 22:54:58 +0100 | [diff] [blame] | 275 | |
Bram Moolenaar | efae76a | 2019-10-27 22:54:58 +0100 | [diff] [blame] | 276 | let buf = RunVimInTerminal('-S XTest_relnr', {'rows': 10, 'cols': 50}) |
Bram Moolenaar | efae76a | 2019-10-27 22:54:58 +0100 | [diff] [blame] | 277 | " Default colors |
| 278 | call VerifyScreenDump(buf, 'Test_relnr_colors_1', {}) |
| 279 | |
Bram Moolenaar | 8040a71 | 2020-02-23 13:38:08 +0100 | [diff] [blame] | 280 | call term_sendkeys(buf, ":hi LineNrAbove ctermfg=blue\<CR>:\<CR>") |
Bram Moolenaar | efae76a | 2019-10-27 22:54:58 +0100 | [diff] [blame] | 281 | call VerifyScreenDump(buf, 'Test_relnr_colors_2', {}) |
| 282 | |
Bram Moolenaar | 8040a71 | 2020-02-23 13:38:08 +0100 | [diff] [blame] | 283 | call term_sendkeys(buf, ":hi LineNrBelow ctermfg=green\<CR>:\<CR>") |
Bram Moolenaar | efae76a | 2019-10-27 22:54:58 +0100 | [diff] [blame] | 284 | call VerifyScreenDump(buf, 'Test_relnr_colors_3', {}) |
| 285 | |
| 286 | call term_sendkeys(buf, ":hi clear LineNrAbove\<CR>") |
| 287 | call VerifyScreenDump(buf, 'Test_relnr_colors_4', {}) |
| 288 | |
| 289 | " clean up |
| 290 | call StopVimInTerminal(buf) |
Bram Moolenaar | efae76a | 2019-10-27 22:54:58 +0100 | [diff] [blame] | 291 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 292 | |
zeertzjq | ebfd856 | 2024-02-06 10:59:03 +0100 | [diff] [blame] | 293 | func Test_relativenumber_colors_wrapped() |
| 294 | CheckScreendump |
| 295 | |
| 296 | let lines =<< trim [CODE] |
| 297 | set display=lastline scrolloff=0 |
| 298 | call setline(1, range(200)->map('v:val->string()->repeat(40)')) |
| 299 | 111 |
| 300 | set number relativenumber |
| 301 | hi LineNr ctermbg=red ctermfg=black |
| 302 | hi LineNrAbove ctermbg=blue ctermfg=black |
| 303 | hi LineNrBelow ctermbg=green ctermfg=black |
| 304 | [CODE] |
| 305 | call writefile(lines, 'XTest_relnr_wrap', 'D') |
| 306 | |
| 307 | let buf = RunVimInTerminal('-S XTest_relnr_wrap', {'rows': 20, 'cols': 50}) |
| 308 | |
| 309 | call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_1', {}) |
| 310 | call term_sendkeys(buf, "k") |
| 311 | call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_2', {}) |
| 312 | call term_sendkeys(buf, "2j") |
| 313 | call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_3', {}) |
| 314 | call term_sendkeys(buf, "2j") |
| 315 | call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_4', {}) |
| 316 | call term_sendkeys(buf, "k") |
| 317 | call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_5', {}) |
| 318 | |
| 319 | " clean up |
| 320 | call StopVimInTerminal(buf) |
| 321 | endfunc |
| 322 | |
zeertzjq | 3e559cd | 2022-03-27 19:26:55 +0100 | [diff] [blame] | 323 | func Test_relativenumber_callback() |
| 324 | CheckScreendump |
| 325 | CheckFeature timers |
| 326 | |
| 327 | let lines =<< trim END |
| 328 | call setline(1, ['aaaaa', 'bbbbb', 'ccccc', 'ddddd']) |
| 329 | set relativenumber |
| 330 | call cursor(4, 1) |
| 331 | |
| 332 | func Func(timer) |
| 333 | call cursor(1, 1) |
| 334 | endfunc |
| 335 | |
| 336 | call timer_start(300, 'Func') |
| 337 | END |
Bram Moolenaar | b152b6a | 2022-09-29 21:37:33 +0100 | [diff] [blame] | 338 | call writefile(lines, 'Xrnu_timer', 'D') |
zeertzjq | 3e559cd | 2022-03-27 19:26:55 +0100 | [diff] [blame] | 339 | |
| 340 | let buf = RunVimInTerminal('-S Xrnu_timer', #{rows: 8}) |
| 341 | call TermWait(buf, 310) |
| 342 | call VerifyScreenDump(buf, 'Test_relativenumber_callback_1', {}) |
| 343 | |
| 344 | call StopVimInTerminal(buf) |
zeertzjq | 3e559cd | 2022-03-27 19:26:55 +0100 | [diff] [blame] | 345 | endfunc |
| 346 | |
zeertzjq | ae07ebc | 2024-02-08 11:37:40 +0100 | [diff] [blame] | 347 | " Test that line numbers below inserted/deleted lines are updated. |
| 348 | func Test_number_insert_delete_lines() |
| 349 | CheckScreendump |
| 350 | |
| 351 | let lines =<< trim END |
| 352 | call setline(1, range(1, 7)) |
| 353 | set number |
| 354 | call cursor(2, 1) |
| 355 | END |
| 356 | call writefile(lines, 'Xnumber_insert_delete_lines', 'D') |
| 357 | |
| 358 | let buf = RunVimInTerminal('-S Xnumber_insert_delete_lines', #{rows: 8}) |
| 359 | call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_1', {}) |
| 360 | call term_sendkeys(buf, "dd") |
| 361 | call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_2', {}) |
| 362 | call term_sendkeys(buf, "P") |
| 363 | call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_1', {}) |
| 364 | call term_sendkeys(buf, "2dd") |
| 365 | call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_3', {}) |
| 366 | call term_sendkeys(buf, "P") |
| 367 | call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_1', {}) |
| 368 | |
| 369 | call StopVimInTerminal(buf) |
| 370 | endfunc |
| 371 | |
Christian Brabandt | 29f0dc3 | 2021-06-16 19:28:34 +0200 | [diff] [blame] | 372 | " Test for displaying line numbers with 'rightleft' |
| 373 | func Test_number_rightleft() |
| 374 | CheckFeature rightleft |
| 375 | new |
| 376 | setlocal number |
| 377 | setlocal rightleft |
| 378 | call setline(1, range(1, 1000)) |
| 379 | normal! 9Gzt |
| 380 | redraw! |
| 381 | call assert_match('^\s\+9 9$', Screenline(1)) |
| 382 | normal! 10Gzt |
| 383 | redraw! |
| 384 | call assert_match('^\s\+01 10$', Screenline(1)) |
| 385 | normal! 100Gzt |
| 386 | redraw! |
| 387 | call assert_match('^\s\+001 100$', Screenline(1)) |
| 388 | normal! 1000Gzt |
| 389 | redraw! |
| 390 | call assert_match('^\s\+0001 1000$', Screenline(1)) |
| 391 | bw! |
| 392 | endfunc |
| 393 | |
Bram Moolenaar | 02f8694 | 2021-08-17 22:14:29 +0200 | [diff] [blame] | 394 | " This used to cause a divide by zero |
| 395 | func Test_number_no_text_virtual_edit() |
| 396 | vnew |
| 397 | call setline(1, ['line one', 'line two']) |
| 398 | set number virtualedit=all |
| 399 | normal w |
| 400 | 4wincmd | |
| 401 | normal j |
| 402 | bwipe! |
| 403 | endfunc |
| 404 | |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 405 | " vim: shiftwidth=2 sts=2 expandtab |