test_matchadd_conceal.vim 14 KB

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