neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

test_matchadd_conceal.vim (15120B)


      1 " Test for matchadd() and conceal feature
      2 
      3 source check.vim
      4 CheckFeature conceal
      5 
      6 source shared.vim
      7 source term_util.vim
      8 source view_util.vim
      9 
     10 func Test_simple_matchadd()
     11  new
     12 
     13  1put='# This is a Test'
     14  "             1234567890123456
     15  let expect = '# This is a Test'
     16 
     17  call cursor(1, 1)
     18  call matchadd('Conceal', '\%2l ')
     19  redraw!
     20  let lnum = 2
     21  call assert_equal(expect, Screenline(lnum))
     22  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
     23  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
     24  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
     25  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
     26  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
     27  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
     28 
     29  quit!
     30 endfunc
     31 
     32 func Test_simple_matchadd_and_conceal()
     33  new
     34  setlocal concealcursor=n conceallevel=1
     35 
     36  1put='# This is a Test'
     37  "             1234567890123456
     38  let expect = '#XThisXisXaXTest'
     39 
     40  call cursor(1, 1)
     41  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'})
     42  redraw!
     43  let lnum = 2
     44  call assert_equal(expect, Screenline(lnum))
     45  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
     46  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
     47  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
     48  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
     49  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
     50 
     51  quit!
     52 endfunc
     53 
     54 func Test_matchadd_and_conceallevel_3()
     55  new
     56 
     57  setlocal conceallevel=3
     58  " set filetype and :syntax on to change screenattr()
     59  setlocal filetype=conf
     60  syntax on
     61 
     62  1put='# This is a Test  $'
     63  "             1234567890123
     64  let expect = '#ThisisaTest$'
     65 
     66  call cursor(1, 1)
     67  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'})
     68  redraw!
     69  let lnum = 2
     70  call assert_equal(expect, Screenline(lnum))
     71  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2))
     72  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 7))
     73  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 10))
     74  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 12))
     75  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 13))
     76  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 14))
     77  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 16))
     78 
     79  " more matchadd()
     80  "             12345678901234
     81  let expect = '#Thisisa Test$'
     82 
     83  call matchadd('ErrorMsg', '\%2l Test', 20, -1, {'conceal': 'X'})
     84  redraw!
     85  call assert_equal(expect, Screenline(lnum))
     86  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 2))
     87  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 7))
     88  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 10))
     89  call assert_equal(screenattr(lnum, 10), screenattr(lnum, 13))
     90  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 14))
     91  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 16))
     92  call assert_notequal(screenattr(lnum, 10), screenattr(lnum, 16))
     93 
     94  syntax off
     95  quit!
     96 endfunc
     97 
     98 func Test_default_conceal_char()
     99  new
    100  setlocal concealcursor=n conceallevel=1
    101 
    102  1put='# This is a Test'
    103  "             1234567890123456
    104  let expect = '# This is a Test'
    105 
    106  call cursor(1, 1)
    107  call matchadd('Conceal', '\%2l ', 10, -1, {})
    108  redraw!
    109  let lnum = 2
    110  call assert_equal(expect, Screenline(lnum))
    111  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
    112  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
    113  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
    114  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    115  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
    116 
    117  "             1234567890123456
    118  let expect = '#+This+is+a+Test'
    119  let listchars_save = &listchars
    120  set listchars=conceal:+
    121  redraw!
    122 
    123  call assert_equal(expect, Screenline(lnum))
    124  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
    125  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
    126  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
    127  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    128  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
    129 
    130  let &listchars = listchars_save
    131  quit!
    132 endfunc
    133 
    134 func Test_syn_and_match_conceal()
    135  new
    136  setlocal concealcursor=n conceallevel=1
    137 
    138  1put='# This is a Test  '
    139 
    140  let lnum = 2
    141  call cursor(1, 1)
    142 
    143  "             123456789012345678
    144  let expect = '#ZThisZisZaZTestZZ'
    145  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'Z'})
    146  syntax match MyConceal /\%2l / conceal containedin=ALL
    147  hi MyConceal ctermbg=4 ctermfg=2
    148  redraw!
    149 
    150  call assert_equal(expect, Screenline(lnum))
    151  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
    152  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
    153  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
    154  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    155  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
    156 
    157  syntax clear MyConceal
    158  syntax match MyConceal /\%2l / conceal containedin=ALL cchar=*
    159  redraw!
    160 
    161  call assert_equal(expect, Screenline(lnum))
    162  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
    163  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
    164  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
    165  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    166  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
    167 
    168  "             123456789012345678
    169  let expect = '#*This*is*a*Test**'
    170  call clearmatches()
    171  redraw!
    172 
    173  call assert_equal(expect, Screenline(lnum))
    174  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
    175  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
    176  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
    177  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    178  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
    179 
    180  "             123456789012345678
    181  let expect = '#*ThisXis*a*Test**'
    182  call matchadd('Conceal', '\%2l\%7c ', 10, -1, {'conceal': 'X'})
    183  redraw!
    184 
    185  call assert_equal(expect, Screenline(lnum))
    186  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
    187  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
    188  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
    189  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    190  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
    191 
    192  "             123456789012345678
    193  let expect = '#*ThisXis*a*Test**'
    194  call matchadd('ErrorMsg', '\%2l Test', 20, -1)
    195  redraw!
    196 
    197  call assert_equal(expect, Screenline(lnum))
    198  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
    199  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    200  call assert_notequal(screenattr(lnum, 12), screenattr(lnum, 13))
    201  call assert_equal(screenattr(lnum, 13), screenattr(lnum, 16))
    202  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 17))
    203  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 18))
    204  call assert_notequal(screenattr(lnum, 18), screenattr(lnum, 19))
    205 
    206  5new | setlocal conceallevel=2 concealcursor=n
    207  redraw!
    208  call assert_equal(expect, Screenline(6 + lnum))
    209 
    210  " Syntax conceal shouldn't interfere with matchadd() in another buffer.
    211  call setline(1, 'foo bar baz')
    212  call matchadd('Conceal', 'bar')
    213  redraw!
    214  call assert_equal('foo  baz', Screenline(1))
    215  call assert_equal(expect, Screenline(6 + lnum))
    216 
    217  " Syntax conceal shouldn't interfere with matchadd() in the same buffer.
    218  syntax match MyOtherConceal /foo/ conceal cchar=!
    219  redraw!
    220  call assert_equal('!  baz', Screenline(1))
    221  call assert_equal(expect, Screenline(6 + lnum))
    222 
    223  syntax clear
    224  redraw!
    225  call assert_equal('foo  baz', Screenline(1))
    226  call assert_equal(expect, Screenline(6 + lnum))
    227  bwipe!
    228 
    229  "             123456789012345678
    230  let expect = '# ThisXis a Test'
    231  syntax clear MyConceal
    232  syntax match MyConceal /\%2l / conceal containedin=ALL
    233  redraw!
    234 
    235  call assert_equal(expect, Screenline(lnum))
    236  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
    237  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    238  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 12))
    239  call assert_notequal(screenattr(lnum, 12), screenattr(lnum, 13))
    240  call assert_equal(screenattr(lnum, 13), screenattr(lnum, 16))
    241  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 17))
    242  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 18))
    243  call assert_notequal(screenattr(lnum, 18), screenattr(lnum, 19))
    244 
    245  syntax off
    246  quit!
    247 endfunc
    248 
    249 func Test_clearmatches()
    250  new
    251  setlocal concealcursor=n conceallevel=1
    252 
    253  1put='# This is a Test'
    254  "             1234567890123456
    255  let expect = '# This is a Test'
    256 
    257  call cursor(1, 1)
    258  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'Z'})
    259  let a = getmatches()
    260  call clearmatches()
    261  redraw!
    262 
    263  let lnum = 2
    264  call assert_equal(expect, Screenline(lnum))
    265  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2))
    266  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
    267  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
    268  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    269  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
    270 
    271  " reset match using setmatches()
    272  "             1234567890123456
    273  let expect = '#ZThisZisZaZTest'
    274  call setmatches(a)
    275  redraw!
    276 
    277  call assert_equal(expect, Screenline(lnum))
    278  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
    279  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
    280  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
    281  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
    282  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
    283  call assert_equal({'group': 'Conceal', 'pattern': '\%2l ', 'priority': 10, 'id': a[0].id, 'conceal': 'Z'}, a[0])
    284 
    285  quit!
    286 endfunc
    287 
    288 func Test_using_matchaddpos()
    289  new
    290  setlocal concealcursor=n conceallevel=1
    291  " set filetype and :syntax on to change screenattr()
    292  setlocal filetype=conf
    293  syntax on
    294 
    295  1put='# This is a Test'
    296  "             1234567890123456
    297  let expect = '#Pis a Test'
    298 
    299  call cursor(1, 1)
    300  call matchaddpos('Conceal', [[2,2,6]], 10, -1, {'conceal': 'P'})
    301  let a = getmatches()
    302  redraw!
    303 
    304  let lnum = 2
    305  call assert_equal(expect, Screenline(lnum))
    306  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 2))
    307  call assert_notequal(screenattr(lnum, 2) , screenattr(lnum, 7))
    308  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 7))
    309  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 10))
    310  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 12))
    311  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 16))
    312  call assert_equal(screenattr(lnum, 12), screenattr(lnum, 16))
    313  call assert_equal({'group': 'Conceal', 'id': a[0].id, 'priority': 10, 'pos1': [2, 2, 6], 'conceal': 'P'}, a[0])
    314 
    315  syntax off
    316  quit!
    317 endfunc
    318 
    319 func Test_matchadd_repeat_conceal_with_syntax_off()
    320  new
    321 
    322  " To test targets in the same line string is replaced with conceal char
    323  " correctly, repeat 'TARGET'
    324  1put ='TARGET_TARGETTARGET'
    325  call cursor(1, 1)
    326  redraw
    327  call assert_equal('TARGET_TARGETTARGET', Screenline(2))
    328 
    329  setlocal conceallevel=2
    330  call matchadd('Conceal', 'TARGET', 10, -1, {'conceal': 't'})
    331 
    332  redraw
    333  call assert_equal('t_tt', Screenline(2))
    334 
    335  quit!
    336 endfunc
    337 
    338 func Test_matchadd_and_syn_conceal()
    339  new
    340  let cnt='Inductive bool : Type := | true : bool | false : bool.'
    341  let expect = 'Inductive - : Type := | true : - | false : -.'
    342  0put =cnt
    343  " set filetype and :syntax on to change screenattr()
    344  set cole=1 cocu=nv
    345  hi link CheckedByCoq WarningMsg
    346  syntax on
    347  syntax keyword coqKwd bool conceal cchar=-
    348  redraw!
    349  call assert_equal(expect, Screenline(1))
    350  call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
    351  call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
    352  call assert_equal(screenattr(1, 11) , screenattr(1, 32))
    353  call matchadd('CheckedByCoq', '\%<2l\%>9c\%<16c')
    354  redraw!
    355  call assert_equal(expect, Screenline(1))
    356  call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
    357  call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
    358  call assert_equal(screenattr(1, 11) , screenattr(1, 32))
    359 endfunc
    360 
    361 func Test_interaction_matchadd_syntax()
    362  new
    363  " Test for issue #7268 fix.
    364  " When redrawing the second column, win_line() was comparing the sequence
    365  " number of the syntax-concealed region with a bogus zero value that was
    366  " returned for the matchadd-concealed region. Before 8.0.0672 the sequence
    367  " number was never reset, thus masking the problem.
    368  call setline(1, 'aaa|bbb|ccc')
    369  call matchadd('Conceal', '^..', 10, -1, #{conceal: 'X'})
    370  syn match foobar '^.'
    371  setl concealcursor=n conceallevel=1
    372  redraw!
    373 
    374  call assert_equal('Xa|bbb|ccc', Screenline(1))
    375  call assert_notequal(screenattr(1, 1), screenattr(1, 2))
    376 
    377  bwipe!
    378 endfunc
    379 
    380 func Test_cursor_column_in_concealed_line_after_window_scroll()
    381  CheckRunVimInTerminal
    382 
    383  " Test for issue #5012 fix.
    384  " For a concealed line with cursor, there should be no window's cursor
    385  " position invalidation during win_update() after scrolling attempt that is
    386  " not successful and no real topline change happens. The invalidation would
    387  " cause a window's cursor position recalc outside of win_line() where it's
    388  " not possible to take conceal into account.
    389  let lines =<< trim END
    390    3split
    391    let m = matchadd('Conceal', '=')
    392    setl conceallevel=2 concealcursor=nc
    393    normal gg
    394    "==expr==
    395  END
    396  call writefile(lines, 'Xcolesearch')
    397  let buf = RunVimInTerminal('Xcolesearch', {})
    398  call TermWait(buf, 50)
    399 
    400  " Jump to something that is beyond the bottom of the window,
    401  " so there's a scroll down.
    402  call term_sendkeys(buf, ":so %\<CR>")
    403  call TermWait(buf, 50)
    404  call term_sendkeys(buf, "/expr\<CR>")
    405  call TermWait(buf, 50)
    406 
    407  " Are the concealed parts of the current line really hidden?
    408  let cursor_row = term_scrape(buf, '.')->map({_, e -> e.chars})->join('')
    409  call assert_equal('"expr', cursor_row)
    410 
    411  " BugFix check: Is the window's cursor column properly updated for hidden
    412  " parts of the current line?
    413  call assert_equal(2, term_getcursor(buf)[1])
    414 
    415  call StopVimInTerminal(buf)
    416  call delete('Xcolesearch')
    417 endfunc
    418 
    419 func Test_cursor_column_in_concealed_line_after_leftcol_change()
    420  CheckRunVimInTerminal
    421 
    422  " Test for issue #5214 fix.
    423  let lines =<< trim END
    424    0put = 'ab' .. repeat('-', &columns) .. 'c'
    425    call matchadd('Conceal', '-')
    426    set nowrap ss=0 cole=3 cocu=n
    427  END
    428  call writefile(lines, 'Xcurs-columns')
    429  let buf = RunVimInTerminal('-S Xcurs-columns', {})
    430 
    431  " Go to the end of the line (3 columns beyond the end of the screen).
    432  " Horizontal scroll would center the cursor in the screen line, but conceal
    433  " makes it go to screen column 1.
    434  call term_sendkeys(buf, "$")
    435  call TermWait(buf)
    436 
    437  " Are the concealed parts of the current line really hidden?
    438  call WaitForAssert({-> assert_equal('c', term_getline(buf, '.'))})
    439 
    440  " BugFix check: Is the window's cursor column properly updated for conceal?
    441  call assert_equal(1, term_getcursor(buf)[1])
    442 
    443  call StopVimInTerminal(buf)
    444  call delete('Xcurs-columns')
    445 endfunc