bufhl_spec.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local Screen = require('test.functional.ui.screen')
  3. local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert
  4. local command, neq = helpers.command, helpers.neq
  5. local meths = helpers.meths
  6. local curbufmeths, eq = helpers.curbufmeths, helpers.eq
  7. describe('Buffer highlighting', function()
  8. local screen
  9. before_each(function()
  10. clear()
  11. command('syntax on')
  12. screen = Screen.new(40, 8)
  13. screen:attach()
  14. screen:set_default_attr_ids({
  15. [1] = {bold=true, foreground=Screen.colors.Blue},
  16. [2] = {foreground = Screen.colors.Fuchsia}, -- String
  17. [3] = {foreground = Screen.colors.Brown, bold = true}, -- Statement
  18. [4] = {foreground = Screen.colors.SlateBlue}, -- Special
  19. [5] = {bold = true, foreground = Screen.colors.SlateBlue},
  20. [6] = {foreground = Screen.colors.DarkCyan}, -- Identifier
  21. [7] = {bold = true},
  22. [8] = {underline = true, bold = true, foreground = Screen.colors.SlateBlue},
  23. [9] = {foreground = Screen.colors.SlateBlue, underline = true},
  24. [10] = {foreground = Screen.colors.Red},
  25. [11] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red},
  26. [12] = {foreground = Screen.colors.Blue1},
  27. [13] = {background = Screen.colors.LightGrey},
  28. [14] = {background = Screen.colors.Gray90},
  29. [15] = {background = Screen.colors.Gray90, bold = true, foreground = Screen.colors.Brown},
  30. [16] = {foreground = Screen.colors.Magenta, background = Screen.colors.Gray90},
  31. })
  32. end)
  33. after_each(function()
  34. screen:detach()
  35. end)
  36. local add_highlight = curbufmeths.add_highlight
  37. local clear_namespace = curbufmeths.clear_namespace
  38. it('works', function()
  39. insert([[
  40. these are some lines
  41. with colorful text]])
  42. feed('+')
  43. screen:expect([[
  44. these are some lines |
  45. with colorful tex^t |
  46. {1:~ }|
  47. {1:~ }|
  48. {1:~ }|
  49. {1:~ }|
  50. {1:~ }|
  51. |
  52. ]])
  53. add_highlight(-1, "String", 0 , 10, 14)
  54. add_highlight(-1, "Statement", 1 , 5, -1)
  55. screen:expect([[
  56. these are {2:some} lines |
  57. with {3:colorful tex^t} |
  58. {1:~ }|
  59. {1:~ }|
  60. {1:~ }|
  61. {1:~ }|
  62. {1:~ }|
  63. |
  64. ]])
  65. feed("ggo<esc>")
  66. screen:expect([[
  67. these are {2:some} lines |
  68. ^ |
  69. with {3:colorful text} |
  70. {1:~ }|
  71. {1:~ }|
  72. {1:~ }|
  73. {1:~ }|
  74. |
  75. ]])
  76. clear_namespace(-1, 0, -1)
  77. screen:expect([[
  78. these are some lines |
  79. ^ |
  80. with colorful text |
  81. {1:~ }|
  82. {1:~ }|
  83. {1:~ }|
  84. {1:~ }|
  85. |
  86. ]])
  87. end)
  88. describe('support using multiple namespaces', function()
  89. local id1, id2
  90. before_each(function()
  91. insert([[
  92. a longer example
  93. in order to demonstrate
  94. combining highlights
  95. from different sources]])
  96. command("hi ImportantWord gui=bold cterm=bold")
  97. id1 = add_highlight(0, "ImportantWord", 0, 2, 8)
  98. add_highlight(id1, "ImportantWord", 1, 12, -1)
  99. add_highlight(id1, "ImportantWord", 2, 0, 9)
  100. add_highlight(id1, "ImportantWord", 3, 5, 14)
  101. -- add_highlight can be called like this to get a new source
  102. -- without adding any highlight
  103. id2 = add_highlight(0, "", 0, 0, 0)
  104. neq(id1, id2)
  105. add_highlight(id2, "Special", 0, 2, 8)
  106. add_highlight(id2, "Identifier", 1, 3, 8)
  107. add_highlight(id2, "Special", 1, 14, 20)
  108. add_highlight(id2, "Underlined", 2, 6, 12)
  109. add_highlight(id2, "Underlined", 3, 0, 9)
  110. screen:expect([[
  111. a {5:longer} example |
  112. in {6:order} to {7:de}{5:monstr}{7:ate} |
  113. {7:combin}{8:ing}{9: hi}ghlights |
  114. {9:from }{8:diff}{7:erent} source^s |
  115. {1:~ }|
  116. {1:~ }|
  117. {1:~ }|
  118. |
  119. ]])
  120. end)
  121. it('and clearing the first added', function()
  122. clear_namespace(id1, 0, -1)
  123. screen:expect([[
  124. a {4:longer} example |
  125. in {6:order} to de{4:monstr}ate |
  126. combin{9:ing hi}ghlights |
  127. {9:from diff}erent source^s |
  128. {1:~ }|
  129. {1:~ }|
  130. {1:~ }|
  131. |
  132. ]])
  133. end)
  134. it('and clearing using deprecated name', function()
  135. curbufmeths.clear_highlight(id1, 0, -1)
  136. screen:expect([[
  137. a {4:longer} example |
  138. in {6:order} to de{4:monstr}ate |
  139. combin{9:ing hi}ghlights |
  140. {9:from diff}erent source^s |
  141. {1:~ }|
  142. {1:~ }|
  143. {1:~ }|
  144. |
  145. ]])
  146. end)
  147. it('and clearing the second added', function()
  148. clear_namespace(id2, 0, -1)
  149. screen:expect([[
  150. a {7:longer} example |
  151. in order to {7:demonstrate} |
  152. {7:combining} highlights |
  153. from {7:different} source^s |
  154. {1:~ }|
  155. {1:~ }|
  156. {1:~ }|
  157. |
  158. ]])
  159. end)
  160. it('and clearing line ranges', function()
  161. clear_namespace(-1, 0, 1)
  162. clear_namespace(id1, 1, 2)
  163. clear_namespace(id2, 2, -1)
  164. screen:expect([[
  165. a longer example |
  166. in {6:order} to de{4:monstr}ate |
  167. {7:combining} highlights |
  168. from {7:different} source^s |
  169. {1:~ }|
  170. {1:~ }|
  171. {1:~ }|
  172. |
  173. ]])
  174. end)
  175. it('and renumbering lines', function()
  176. feed('3Gddggo<esc>')
  177. screen:expect([[
  178. a {5:longer} example |
  179. ^ |
  180. in {6:order} to {7:de}{5:monstr}{7:ate} |
  181. {9:from }{8:diff}{7:erent} sources |
  182. {1:~ }|
  183. {1:~ }|
  184. {1:~ }|
  185. |
  186. ]])
  187. command(':3move 4')
  188. screen:expect([[
  189. a {5:longer} example |
  190. |
  191. {9:from }{8:diff}{7:erent} sources |
  192. ^in {6:order} to {7:de}{5:monstr}{7:ate} |
  193. {1:~ }|
  194. {1:~ }|
  195. {1:~ }|
  196. |
  197. ]])
  198. end)
  199. end)
  200. it('prioritizes latest added highlight', function()
  201. insert([[
  202. three overlapping colors]])
  203. add_highlight(0, "Identifier", 0, 6, 17)
  204. add_highlight(0, "String", 0, 14, 23)
  205. local id = add_highlight(0, "Special", 0, 0, 9)
  206. screen:expect([[
  207. {4:three ove}{6:rlapp}{2:ing color}^s |
  208. {1:~ }|
  209. {1:~ }|
  210. {1:~ }|
  211. {1:~ }|
  212. {1:~ }|
  213. {1:~ }|
  214. |
  215. ]])
  216. clear_namespace(id, 0, 1)
  217. screen:expect([[
  218. three {6:overlapp}{2:ing color}^s |
  219. {1:~ }|
  220. {1:~ }|
  221. {1:~ }|
  222. {1:~ }|
  223. {1:~ }|
  224. {1:~ }|
  225. |
  226. ]])
  227. end)
  228. it('works with multibyte text', function()
  229. insert([[
  230. Ta båten över sjön!]])
  231. add_highlight(-1, "Identifier", 0, 3, 9)
  232. add_highlight(-1, "String", 0, 16, 21)
  233. screen:expect([[
  234. Ta {6:båten} över {2:sjön}^! |
  235. {1:~ }|
  236. {1:~ }|
  237. {1:~ }|
  238. {1:~ }|
  239. {1:~ }|
  240. {1:~ }|
  241. |
  242. ]])
  243. end)
  244. it('works with new syntax groups', function()
  245. insert([[
  246. fancy code in a new fancy language]])
  247. add_highlight(-1, "FancyLangItem", 0, 0, 5)
  248. screen:expect([[
  249. fancy code in a new fancy languag^e |
  250. {1:~ }|
  251. {1:~ }|
  252. {1:~ }|
  253. {1:~ }|
  254. {1:~ }|
  255. {1:~ }|
  256. |
  257. ]])
  258. command('hi FancyLangItem guifg=red')
  259. screen:expect([[
  260. {10:fancy} code in a new fancy languag^e |
  261. {1:~ }|
  262. {1:~ }|
  263. {1:~ }|
  264. {1:~ }|
  265. {1:~ }|
  266. {1:~ }|
  267. |
  268. ]])
  269. end)
  270. describe('virtual text annotations', function()
  271. local set_virtual_text = curbufmeths.set_virtual_text
  272. local id1, id2
  273. before_each(function()
  274. insert([[
  275. 1 + 2
  276. 3 +
  277. x = 4]])
  278. feed('O<esc>20A5, <esc>gg')
  279. screen:expect([[
  280. ^1 + 2 |
  281. 3 + |
  282. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  283. , 5, 5, 5, 5, 5, 5, |
  284. x = 4 |
  285. {1:~ }|
  286. {1:~ }|
  287. |
  288. ]])
  289. id1 = set_virtual_text(0, 0, {{"=", "Statement"}, {" 3", "Number"}}, {})
  290. set_virtual_text(id1, 1, {{"ERROR:", "ErrorMsg"}, {" invalid syntax"}}, {})
  291. id2 = set_virtual_text(0, 2, {{"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."}}, {})
  292. neq(id2, id1)
  293. end)
  294. it('works', function()
  295. screen:expect([[
  296. ^1 + 2 {3:=}{2: 3} |
  297. 3 + {11:ERROR:} invalid syntax |
  298. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  299. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  300. x = 4 |
  301. {1:~ }|
  302. {1:~ }|
  303. |
  304. ]])
  305. clear_namespace(id1, 0, -1)
  306. screen:expect([[
  307. ^1 + 2 |
  308. 3 + |
  309. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  310. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  311. x = 4 |
  312. {1:~ }|
  313. {1:~ }|
  314. |
  315. ]])
  316. -- Handles doublewidth chars, leaving a space if truncating
  317. -- in the middle of a char
  318. eq(-1, set_virtual_text(-1, 1, {{"暗x事zz速野谷質結育副住新覚丸活解終事", "Comment"}}, {}))
  319. screen:expect([[
  320. ^1 + 2 |
  321. 3 + {12:暗x事zz速野谷質結育副住新覚丸活解終 }|
  322. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  323. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  324. x = 4 |
  325. {1:~ }|
  326. {1:~ }|
  327. |
  328. ]])
  329. feed("2Gx")
  330. screen:expect([[
  331. 1 + 2 |
  332. ^ + {12:暗x事zz速野谷質結育副住新覚丸活解終事}|
  333. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  334. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  335. x = 4 |
  336. {1:~ }|
  337. {1:~ }|
  338. |
  339. ]])
  340. feed("2Gdd")
  341. screen:expect([[
  342. 1 + 2 |
  343. ^5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  344. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  345. x = 4 |
  346. {1:~ }|
  347. {1:~ }|
  348. {1:~ }|
  349. |
  350. ]])
  351. end)
  352. it('is not highlighted by visual selection', function()
  353. feed("ggVG")
  354. screen:expect([[
  355. {13:1 + 2} {3:=}{2: 3} |
  356. {13:3 +} {11:ERROR:} invalid syntax |
  357. {13:5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}|
  358. {13:, 5, 5, 5, 5, 5, 5, } Lorem ipsum dolor s|
  359. ^x{13: = 4} |
  360. {1:~ }|
  361. {1:~ }|
  362. {7:-- VISUAL LINE --} |
  363. ]])
  364. feed("<esc>")
  365. screen:expect([[
  366. 1 + 2 {3:=}{2: 3} |
  367. 3 + {11:ERROR:} invalid syntax |
  368. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  369. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  370. ^x = 4 |
  371. {1:~ }|
  372. {1:~ }|
  373. |
  374. ]])
  375. -- special case: empty line has extra eol highlight
  376. feed("ggd$")
  377. screen:expect([[
  378. ^ {3:=}{2: 3} |
  379. 3 + {11:ERROR:} invalid syntax |
  380. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  381. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  382. x = 4 |
  383. {1:~ }|
  384. {1:~ }|
  385. |
  386. ]])
  387. feed("jvk")
  388. screen:expect([[
  389. ^ {3:=}{2: 3} |
  390. {13:3} + {11:ERROR:} invalid syntax |
  391. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  392. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  393. x = 4 |
  394. {1:~ }|
  395. {1:~ }|
  396. {7:-- VISUAL --} |
  397. ]])
  398. feed("o")
  399. screen:expect([[
  400. {13: }{3:=}{2: 3} |
  401. ^3 + {11:ERROR:} invalid syntax |
  402. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  403. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  404. x = 4 |
  405. {1:~ }|
  406. {1:~ }|
  407. {7:-- VISUAL --} |
  408. ]])
  409. end)
  410. it('works with listchars', function()
  411. command("set list listchars+=eol:$")
  412. screen:expect([[
  413. ^1 + 2{1:$}{3:=}{2: 3} |
  414. 3 +{1:$}{11:ERROR:} invalid syntax |
  415. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  416. , 5, 5, 5, 5, 5, 5,{1:-$}Lorem ipsum dolor s|
  417. x = 4{1:$} |
  418. {1:~ }|
  419. {1:~ }|
  420. |
  421. ]])
  422. clear_namespace(-1, 0, -1)
  423. screen:expect([[
  424. ^1 + 2{1:$} |
  425. 3 +{1:$} |
  426. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  427. , 5, 5, 5, 5, 5, 5,{1:-$} |
  428. x = 4{1:$} |
  429. {1:~ }|
  430. {1:~ }|
  431. |
  432. ]])
  433. end)
  434. it('works with cursorline', function()
  435. command("set cursorline")
  436. screen:expect([[
  437. {14:^1 + 2 }{15:=}{16: 3}{14: }|
  438. 3 + {11:ERROR:} invalid syntax |
  439. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  440. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  441. x = 4 |
  442. {1:~ }|
  443. {1:~ }|
  444. |
  445. ]])
  446. feed('j')
  447. screen:expect([[
  448. 1 + 2 {3:=}{2: 3} |
  449. {14:^3 + }{11:ERROR:}{14: invalid syntax }|
  450. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
  451. , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s|
  452. x = 4 |
  453. {1:~ }|
  454. {1:~ }|
  455. |
  456. ]])
  457. feed('j')
  458. screen:expect([[
  459. 1 + 2 {3:=}{2: 3} |
  460. 3 + {11:ERROR:} invalid syntax |
  461. {14:^5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}|
  462. {14:, 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s}|
  463. x = 4 |
  464. {1:~ }|
  465. {1:~ }|
  466. |
  467. ]])
  468. end)
  469. end)
  470. it('and virtual text use the same namespace counter', function()
  471. local set_virtual_text = curbufmeths.set_virtual_text
  472. eq(1, add_highlight(0, "String", 0 , 0, -1))
  473. eq(2, set_virtual_text(0, 0, {{"= text", "Comment"}}, {}))
  474. eq(3, meths.create_namespace("my-ns"))
  475. eq(4, add_highlight(0, "String", 0 , 0, -1))
  476. eq(5, set_virtual_text(0, 0, {{"= text", "Comment"}}, {}))
  477. eq(6, meths.create_namespace("other-ns"))
  478. end)
  479. end)