highlight_spec.lua 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local clear = n.clear
  5. local insert = n.insert
  6. local exec_lua = n.exec_lua
  7. local feed = n.feed
  8. local command = n.command
  9. local api = n.api
  10. local fn = n.fn
  11. local eq = t.eq
  12. local hl_query_c = [[
  13. ; query
  14. (ERROR) @error
  15. "if" @keyword
  16. "else" @keyword
  17. "for" @keyword
  18. "return" @keyword
  19. "const" @type
  20. "static" @type
  21. "struct" @type
  22. "enum" @type
  23. "extern" @type
  24. ; nonexistent specializer for string should fallback to string
  25. (string_literal) @string.nonexistent_specializer
  26. (number_literal) @number
  27. (char_literal) @string
  28. (type_identifier) @type
  29. ((type_identifier) @constant.builtin (#eq? @constant.builtin "LuaRef"))
  30. (primitive_type) @type
  31. (sized_type_specifier) @type
  32. ; Use lua regexes
  33. ((identifier) @function (#contains? @function "lua_"))
  34. ((identifier) @Constant (#lua-match? @Constant "^[A-Z_]+$"))
  35. ((identifier) @Normal (#vim-match? @Normal "^lstate$"))
  36. ((binary_expression left: (identifier) @warning.left right: (identifier) @warning.right) (#eq? @warning.left @warning.right))
  37. (comment) @comment
  38. ]]
  39. local hl_text_c = [[
  40. /// Schedule Lua callback on main loop's event queue
  41. static int nlua_schedule(lua_State *const lstate)
  42. {
  43. if (lua_type(lstate, 1) != LUA_TFUNCTION
  44. || lstate != lstate) {
  45. lua_pushliteral(lstate, "vim.schedule: expected function");
  46. return lua_error(lstate);
  47. }
  48. LuaRef cb = nlua_ref(lstate, 1);
  49. multiqueue_put(main_loop.events, nlua_schedule_event,
  50. 1, (void *)(ptrdiff_t)cb);
  51. return 0;
  52. }]]
  53. local hl_grid_legacy_c = [[
  54. {18:^/// Schedule Lua callback on main loop's event queue} |
  55. {6:static} {6:int} nlua_schedule(lua_State *{6:const} lstate) |
  56. { |
  57. {15:if} (lua_type(lstate, {26:1}) != LUA_TFUNCTION |
  58. || lstate != lstate) { |
  59. lua_pushliteral(lstate, {26:"vim.schedule: expected function"}); |
  60. {15:return} lua_error(lstate); |
  61. } |
  62. |
  63. LuaRef cb = nlua_ref(lstate, {26:1}); |
  64. |
  65. multiqueue_put(main_loop.events, nlua_schedule_event, |
  66. {26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
  67. {15:return} {26:0}; |
  68. } |
  69. {1:~ }|*2
  70. 14 more lines |
  71. ]]
  72. local hl_grid_ts_c = [[
  73. {18:^/// Schedule Lua callback on main loop's event queue} |
  74. {6:static} {6:int} {25:nlua_schedule}({6:lua_State} *{6:const} lstate) |
  75. { |
  76. {15:if} ({25:lua_type}(lstate, {26:1}) != {26:LUA_TFUNCTION} |
  77. || {19:lstate} != {19:lstate}) { |
  78. {25:lua_pushliteral}(lstate, {26:"vim.schedule: expected function"}); |
  79. {15:return} {25:lua_error}(lstate); |
  80. } |
  81. |
  82. {29:LuaRef} cb = {25:nlua_ref}(lstate, {26:1}); |
  83. |
  84. multiqueue_put(main_loop.events, {25:nlua_schedule_event}, |
  85. {26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
  86. {15:return} {26:0}; |
  87. } |
  88. {1:~ }|*2
  89. {MATCH:1?4? m?o?r?e? l?i?n?e?s?.*}|
  90. ]]
  91. local test_text_c = [[
  92. void ui_refresh(void)
  93. {
  94. int width = INT_MAX, height = INT_MAX;
  95. bool ext_widgets[kUIExtCount];
  96. for (UIExtension i = 0; (int)i < kUIExtCount; i++) {
  97. ext_widgets[i] = true;
  98. }
  99. bool inclusive = ui_override();
  100. for (size_t i = 0; i < ui_count; i++) {
  101. UI *ui = uis[i];
  102. width = MIN(ui->width, width);
  103. height = MIN(ui->height, height);
  104. foo = BAR(ui->bazaar, bazaar);
  105. for (UIExtension j = 0; (int)j < kUIExtCount; j++) {
  106. ext_widgets[j] &= (ui->ui_ext[j] || inclusive);
  107. }
  108. }
  109. }]]
  110. local injection_text_c = [[
  111. int x = INT_MAX;
  112. #define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  113. #define foo void main() { \
  114. return 42; \
  115. }
  116. ]]
  117. local injection_grid_c = [[
  118. int x = INT_MAX; |
  119. #define READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) |
  120. #define foo void main() { \ |
  121. return 42; \ |
  122. } |
  123. ^ |
  124. {1:~ }|*11
  125. |
  126. ]]
  127. local injection_grid_expected_c = [[
  128. {6:int} x = {26:INT_MAX}; |
  129. #define {26:READ_STRING}(x, y) ({6:char} *)read_string((x), ({6:size_t})(y)) |
  130. #define foo {6:void} main() { \ |
  131. {15:return} {26:42}; \ |
  132. } |
  133. ^ |
  134. {1:~ }|*11
  135. |
  136. ]]
  137. describe('treesitter highlighting (C)', function()
  138. local screen --- @type test.functional.ui.screen
  139. before_each(function()
  140. clear()
  141. screen = Screen.new(65, 18)
  142. command [[ hi link @error ErrorMsg ]]
  143. command [[ hi link @warning WarningMsg ]]
  144. end)
  145. it('starting and stopping treesitter highlight works', function()
  146. command('setfiletype c | syntax on')
  147. fn.setreg('r', hl_text_c)
  148. feed('i<C-R><C-O>r<Esc>gg')
  149. -- legacy syntax highlighting is used by default
  150. screen:expect(hl_grid_legacy_c)
  151. exec_lua(function()
  152. vim.treesitter.query.set('c', 'highlights', hl_query_c)
  153. vim.treesitter.start()
  154. end)
  155. -- treesitter highlighting is used
  156. screen:expect(hl_grid_ts_c)
  157. exec_lua(function()
  158. vim.treesitter.stop()
  159. end)
  160. -- legacy syntax highlighting is used
  161. screen:expect(hl_grid_legacy_c)
  162. exec_lua(function()
  163. vim.treesitter.start()
  164. end)
  165. -- treesitter highlighting is used
  166. screen:expect(hl_grid_ts_c)
  167. exec_lua(function()
  168. vim.treesitter.stop()
  169. end)
  170. -- legacy syntax highlighting is used
  171. screen:expect(hl_grid_legacy_c)
  172. end)
  173. it('is updated with edits', function()
  174. insert(hl_text_c)
  175. feed('gg')
  176. screen:expect {
  177. grid = [[
  178. ^/// Schedule Lua callback on main loop's event queue |
  179. static int nlua_schedule(lua_State *const lstate) |
  180. { |
  181. if (lua_type(lstate, 1) != LUA_TFUNCTION |
  182. || lstate != lstate) { |
  183. lua_pushliteral(lstate, "vim.schedule: expected function"); |
  184. return lua_error(lstate); |
  185. } |
  186. |
  187. LuaRef cb = nlua_ref(lstate, 1); |
  188. |
  189. multiqueue_put(main_loop.events, nlua_schedule_event, |
  190. 1, (void *)(ptrdiff_t)cb); |
  191. return 0; |
  192. } |
  193. {1:~ }|*2
  194. |
  195. ]],
  196. }
  197. exec_lua(function()
  198. local parser = vim.treesitter.get_parser(0, 'c')
  199. local highlighter = vim.treesitter.highlighter
  200. highlighter.new(parser, { queries = { c = hl_query_c } })
  201. end)
  202. screen:expect(hl_grid_ts_c)
  203. feed('5Goc<esc>dd')
  204. screen:expect({
  205. grid = [[
  206. {18:/// Schedule Lua callback on main loop's event queue} |
  207. {6:static} {6:int} {25:nlua_schedule}({6:lua_State} *{6:const} lstate) |
  208. { |
  209. {15:if} ({25:lua_type}(lstate, {26:1}) != {26:LUA_TFUNCTION} |
  210. || {19:lstate} != {19:lstate}) { |
  211. {25:^lua_pushliteral}(lstate, {26:"vim.schedule: expected function"}); |
  212. {15:return} {25:lua_error}(lstate); |
  213. } |
  214. |
  215. {29:LuaRef} cb = {25:nlua_ref}(lstate, {26:1}); |
  216. |
  217. multiqueue_put(main_loop.events, {25:nlua_schedule_event}, |
  218. {26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
  219. {15:return} {26:0}; |
  220. } |
  221. {1:~ }|*2
  222. |
  223. ]],
  224. })
  225. feed('7Go*/<esc>')
  226. screen:expect({
  227. grid = [[
  228. {18:/// Schedule Lua callback on main loop's event queue} |
  229. {6:static} {6:int} {25:nlua_schedule}({6:lua_State} *{6:const} lstate) |
  230. { |
  231. {15:if} ({25:lua_type}(lstate, {26:1}) != {26:LUA_TFUNCTION} |
  232. || {19:lstate} != {19:lstate}) { |
  233. {25:lua_pushliteral}(lstate, {26:"vim.schedule: expected function"}); |
  234. {15:return} {25:lua_error}(lstate); |
  235. {9:*^/} |
  236. } |
  237. |
  238. {29:LuaRef} cb = {25:nlua_ref}(lstate, {26:1}); |
  239. |
  240. multiqueue_put(main_loop.events, {25:nlua_schedule_event}, |
  241. {26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
  242. {15:return} {26:0}; |
  243. } |
  244. {1:~ }|
  245. |
  246. ]],
  247. })
  248. feed('3Go/*<esc>')
  249. screen:expect({
  250. grid = [[
  251. {18:/// Schedule Lua callback on main loop's event queue} |
  252. {6:static} {6:int} {25:nlua_schedule}({6:lua_State} *{6:const} lstate) |
  253. { |
  254. {18:/^*} |
  255. {18: if (lua_type(lstate, 1) != LUA_TFUNCTION} |
  256. {18: || lstate != lstate) {} |
  257. {18: lua_pushliteral(lstate, "vim.schedule: expected function");} |
  258. {18: return lua_error(lstate);} |
  259. {18:*/} |
  260. } |
  261. |
  262. {29:LuaRef} cb = {25:nlua_ref}(lstate, {26:1}); |
  263. |
  264. multiqueue_put(main_loop.events, {25:nlua_schedule_event}, |
  265. {26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
  266. {15:return} {26:0}; |
  267. {9:}} |
  268. |
  269. ]],
  270. })
  271. feed('gg$')
  272. feed('~')
  273. screen:expect({
  274. grid = [[
  275. {18:/// Schedule Lua callback on main loop's event queu^E} |
  276. {6:static} {6:int} {25:nlua_schedule}({6:lua_State} *{6:const} lstate) |
  277. { |
  278. {18:/*} |
  279. {18: if (lua_type(lstate, 1) != LUA_TFUNCTION} |
  280. {18: || lstate != lstate) {} |
  281. {18: lua_pushliteral(lstate, "vim.schedule: expected function");} |
  282. {18: return lua_error(lstate);} |
  283. {18:*/} |
  284. } |
  285. |
  286. {29:LuaRef} cb = {25:nlua_ref}(lstate, {26:1}); |
  287. |
  288. multiqueue_put(main_loop.events, {25:nlua_schedule_event}, |
  289. {26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
  290. {15:return} {26:0}; |
  291. {9:}} |
  292. |
  293. ]],
  294. })
  295. feed('re')
  296. screen:expect({
  297. grid = [[
  298. {18:/// Schedule Lua callback on main loop's event queu^e} |
  299. {6:static} {6:int} {25:nlua_schedule}({6:lua_State} *{6:const} lstate) |
  300. { |
  301. {18:/*} |
  302. {18: if (lua_type(lstate, 1) != LUA_TFUNCTION} |
  303. {18: || lstate != lstate) {} |
  304. {18: lua_pushliteral(lstate, "vim.schedule: expected function");} |
  305. {18: return lua_error(lstate);} |
  306. {18:*/} |
  307. } |
  308. |
  309. {29:LuaRef} cb = {25:nlua_ref}(lstate, {26:1}); |
  310. |
  311. multiqueue_put(main_loop.events, {25:nlua_schedule_event}, |
  312. {26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
  313. {15:return} {26:0}; |
  314. {9:}} |
  315. |
  316. ]],
  317. })
  318. end)
  319. it('is updated with :sort', function()
  320. insert(test_text_c)
  321. exec_lua(function()
  322. local parser = vim.treesitter.get_parser(0, 'c')
  323. vim.treesitter.highlighter.new(parser, { queries = { c = hl_query_c } })
  324. end)
  325. screen:expect({
  326. grid = [[
  327. {6:int} width = {26:INT_MAX}, height = {26:INT_MAX}; |
  328. {6:bool} ext_widgets[kUIExtCount]; |
  329. {15:for} ({6:UIExtension} i = {26:0}; ({6:int})i < kUIExtCount; i++) { |
  330. ext_widgets[i] = true; |
  331. } |
  332. |
  333. {6:bool} inclusive = ui_override(); |
  334. {15:for} ({6:size_t} i = {26:0}; i < ui_count; i++) { |
  335. {6:UI} *ui = uis[i]; |
  336. width = {26:MIN}(ui->width, width); |
  337. height = {26:MIN}(ui->height, height); |
  338. foo = {26:BAR}(ui->bazaar, bazaar); |
  339. {15:for} ({6:UIExtension} j = {26:0}; ({6:int})j < kUIExtCount; j++) { |
  340. ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  341. } |
  342. } |
  343. ^} |
  344. |
  345. ]],
  346. })
  347. feed ':sort<cr>'
  348. screen:expect({
  349. grid = [[
  350. ^ |
  351. ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  352. {6:UI} *ui = uis[i]; |
  353. ext_widgets[i] = true; |
  354. foo = {26:BAR}(ui->bazaar, bazaar); |
  355. {15:for} ({6:UIExtension} j = {26:0}; ({6:int})j < kUIExtCount; j++) { |
  356. height = {26:MIN}(ui->height, height); |
  357. width = {26:MIN}(ui->width, width); |
  358. } |
  359. {6:bool} ext_widgets[kUIExtCount]; |
  360. {6:bool} inclusive = ui_override(); |
  361. {15:for} ({6:UIExtension} i = {26:0}; ({6:int})i < kUIExtCount; i++) { |
  362. {15:for} ({6:size_t} i = {26:0}; i < ui_count; i++) { |
  363. {6:int} width = {26:INT_MAX}, height = {26:INT_MAX}; |
  364. } |*2
  365. {6:void} ui_refresh({6:void}) |
  366. :sort |
  367. ]],
  368. })
  369. feed 'u:<esc>'
  370. screen:expect({
  371. grid = [[
  372. {6:int} width = {26:INT_MAX}, height = {26:INT_MAX}; |
  373. {6:bool} ext_widgets[kUIExtCount]; |
  374. {15:for} ({6:UIExtension} i = {26:0}; ({6:int})i < kUIExtCount; i++) { |
  375. ext_widgets[i] = true; |
  376. } |
  377. |
  378. {6:bool} inclusive = ui_override(); |
  379. {15:for} ({6:size_t} i = {26:0}; i < ui_count; i++) { |
  380. {6:UI} *ui = uis[i]; |
  381. width = {26:MIN}(ui->width, width); |
  382. height = {26:MIN}(ui->height, height); |
  383. foo = {26:BAR}(ui->bazaar, bazaar); |
  384. {15:for} ({6:UIExtension} j = {26:0}; ({6:int})j < kUIExtCount; j++) { |
  385. ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  386. } |
  387. } |
  388. ^} |
  389. |
  390. ]],
  391. })
  392. end)
  393. it('supports with custom parser', function()
  394. insert(test_text_c)
  395. screen:expect {
  396. grid = [[
  397. int width = INT_MAX, height = INT_MAX; |
  398. bool ext_widgets[kUIExtCount]; |
  399. for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
  400. ext_widgets[i] = true; |
  401. } |
  402. |
  403. bool inclusive = ui_override(); |
  404. for (size_t i = 0; i < ui_count; i++) { |
  405. UI *ui = uis[i]; |
  406. width = MIN(ui->width, width); |
  407. height = MIN(ui->height, height); |
  408. foo = BAR(ui->bazaar, bazaar); |
  409. for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
  410. ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  411. } |
  412. } |
  413. ^} |
  414. |
  415. ]],
  416. }
  417. exec_lua(function()
  418. local parser = vim.treesitter.get_parser(0, 'c')
  419. local query = vim.treesitter.query.parse('c', '(declaration) @decl')
  420. local nodes = {}
  421. for _, node in query:iter_captures(parser:parse()[1]:root(), 0, 0, 19) do
  422. table.insert(nodes, node)
  423. end
  424. parser:set_included_regions({ nodes })
  425. vim.treesitter.highlighter.new(parser, { queries = { c = '(identifier) @type' } })
  426. end)
  427. screen:expect({
  428. grid = [[
  429. int {6:width} = {6:INT_MAX}, {6:height} = {6:INT_MAX}; |
  430. bool {6:ext_widgets}[{6:kUIExtCount}]; |
  431. for (UIExtension {6:i} = 0; (int)i < kUIExtCount; i++) { |
  432. ext_widgets[i] = true; |
  433. } |
  434. |
  435. bool {6:inclusive} = {6:ui_override}(); |
  436. for (size_t {6:i} = 0; i < ui_count; i++) { |
  437. UI *{6:ui} = {6:uis}[{6:i}]; |
  438. width = MIN(ui->width, width); |
  439. height = MIN(ui->height, height); |
  440. foo = BAR(ui->bazaar, bazaar); |
  441. for (UIExtension {6:j} = 0; (int)j < kUIExtCount; j++) { |
  442. ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
  443. } |
  444. } |
  445. ^} |
  446. |
  447. ]],
  448. })
  449. end)
  450. it('supports injected languages', function()
  451. insert(injection_text_c)
  452. screen:expect { grid = injection_grid_c }
  453. exec_lua(function()
  454. local parser = vim.treesitter.get_parser(0, 'c', {
  455. injections = {
  456. c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))',
  457. },
  458. })
  459. local highlighter = vim.treesitter.highlighter
  460. highlighter.new(parser, { queries = { c = hl_query_c } })
  461. end)
  462. screen:expect { grid = injection_grid_expected_c }
  463. end)
  464. it('supports combined injections #31777', function()
  465. insert([=[
  466. -- print([[
  467. -- some
  468. -- random
  469. -- text
  470. -- here]])
  471. ]=])
  472. exec_lua(function()
  473. local parser = vim.treesitter.get_parser(0, 'lua', {
  474. injections = {
  475. lua = [[
  476. ; query
  477. ((comment_content) @injection.content
  478. (#set! injection.self)
  479. (#set! injection.combined))
  480. ]],
  481. },
  482. })
  483. local highlighter = vim.treesitter.highlighter
  484. highlighter.new(parser, {
  485. queries = {
  486. lua = [[
  487. ; query
  488. (string) @string
  489. (comment) @comment
  490. (function_call (identifier) @function.call)
  491. [ "(" ")" ] @punctuation.bracket
  492. ]],
  493. },
  494. })
  495. end)
  496. screen:expect([=[
  497. {18:-- }{25:print}{16:(}{26:[[} |
  498. {18:--}{26: some} |
  499. {18:--}{26: random} |
  500. {18:--}{26: text} |
  501. {18:--}{26: here]]}{16:)} |
  502. ^ |
  503. {1:~ }|*11
  504. |
  505. ]=])
  506. end)
  507. it('supports complicated combined injections', function()
  508. insert([[
  509. -- # Markdown here
  510. --
  511. -- ```c
  512. -- int main() {
  513. -- printf("Hello, world!");
  514. -- }
  515. -- ```
  516. ]])
  517. exec_lua(function()
  518. local parser = vim.treesitter.get_parser(0, 'lua', {
  519. injections = {
  520. lua = [[
  521. ; query
  522. ((comment) @injection.content
  523. (#offset! @injection.content 0 3 0 1)
  524. (#lua-match? @injection.content "[-][-] ")
  525. (#set! injection.combined)
  526. (#set! injection.include-children)
  527. (#set! injection.language "markdown"))
  528. ]],
  529. },
  530. })
  531. local highlighter = vim.treesitter.highlighter
  532. highlighter.new(parser, {
  533. queries = {
  534. lua = [[
  535. ; query
  536. (string) @string
  537. (comment) @comment
  538. (function_call (identifier) @function.call)
  539. [ "(" ")" ] @punctuation.bracket
  540. ]],
  541. },
  542. })
  543. end)
  544. screen:add_extra_attr_ids({
  545. [131] = { foreground = Screen.colors.Fuchsia, bold = true },
  546. })
  547. screen:expect([[
  548. {18:-- }{131:# Markdown here} |
  549. {18:--} |
  550. {18:-- ```}{15:c} |
  551. {18:-- }{16:int}{18: }{25:main}{16:()}{18: }{16:{} |
  552. {18:-- }{25:printf}{16:(}{26:"Hello, world!"}{16:);} |
  553. {18:-- }{16:}} |
  554. {18:-- ```} |
  555. ^ |
  556. {1:~ }|*9
  557. |
  558. ]])
  559. end)
  560. it("supports injecting by ft name in metadata['injection.language']", function()
  561. insert(injection_text_c)
  562. screen:expect { grid = injection_grid_c }
  563. exec_lua(function()
  564. vim.treesitter.language.register('c', 'foo')
  565. local parser = vim.treesitter.get_parser(0, 'c', {
  566. injections = {
  567. c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "foo")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "foo"))',
  568. },
  569. })
  570. local highlighter = vim.treesitter.highlighter
  571. highlighter.new(parser, { queries = { c = hl_query_c } })
  572. end)
  573. screen:expect { grid = injection_grid_expected_c }
  574. end)
  575. it('supports overriding queries, like ', function()
  576. insert([[
  577. int x = INT_MAX;
  578. #define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  579. #define foo void main() { \
  580. return 42; \
  581. }
  582. ]])
  583. exec_lua(function()
  584. local injection_query =
  585. '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'
  586. vim.treesitter.query.set('c', 'highlights', hl_query_c)
  587. vim.treesitter.query.set('c', 'injections', injection_query)
  588. vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c'))
  589. end)
  590. screen:expect({
  591. grid = [[
  592. {6:int} x = {26:INT_MAX}; |
  593. #define {26:READ_STRING}(x, y) ({6:char} *)read_string((x), ({6:size_t})(y)) |
  594. #define foo {6:void} main() { \ |
  595. {15:return} {26:42}; \ |
  596. } |
  597. ^ |
  598. {1:~ }|*11
  599. |
  600. ]],
  601. })
  602. end)
  603. it('supports highlighting with custom highlight groups', function()
  604. insert(hl_text_c)
  605. feed('gg')
  606. exec_lua(function()
  607. local parser = vim.treesitter.get_parser(0, 'c')
  608. vim.treesitter.highlighter.new(parser, { queries = { c = hl_query_c } })
  609. end)
  610. screen:expect(hl_grid_ts_c)
  611. -- This will change ONLY the literal strings to look like comments
  612. -- The only literal string is the "vim.schedule: expected function" in this test.
  613. exec_lua [[vim.cmd("highlight link @string.nonexistent_specializer comment")]]
  614. screen:expect({
  615. grid = [[
  616. {18:^/// Schedule Lua callback on main loop's event queue} |
  617. {6:static} {6:int} {25:nlua_schedule}({6:lua_State} *{6:const} lstate) |
  618. { |
  619. {15:if} ({25:lua_type}(lstate, {26:1}) != {26:LUA_TFUNCTION} |
  620. || {19:lstate} != {19:lstate}) { |
  621. {25:lua_pushliteral}(lstate, {18:"vim.schedule: expected function"}); |
  622. {15:return} {25:lua_error}(lstate); |
  623. } |
  624. |
  625. {29:LuaRef} cb = {25:nlua_ref}(lstate, {26:1}); |
  626. |
  627. multiqueue_put(main_loop.events, {25:nlua_schedule_event}, |
  628. {26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
  629. {15:return} {26:0}; |
  630. } |
  631. {1:~ }|*2
  632. |
  633. ]],
  634. })
  635. screen:expect { unchanged = true }
  636. end)
  637. it('supports highlighting with priority', function()
  638. insert([[
  639. int x = INT_MAX;
  640. #define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
  641. #define foo void main() { \
  642. return 42; \
  643. }
  644. ]])
  645. exec_lua(function()
  646. local parser = vim.treesitter.get_parser(0, 'c')
  647. vim.treesitter.highlighter.new(parser, {
  648. queries = {
  649. c = hl_query_c .. '\n((translation_unit) @constant (#set! "priority" 101))\n',
  650. },
  651. })
  652. end)
  653. -- expect everything to have Constant highlight
  654. screen:expect {
  655. grid = [[
  656. {12:int}{8: x = INT_MAX;} |
  657. {8:#define READ_STRING(x, y) (}{12:char}{8: *)read_string((x), (}{12:size_t}{8:)(y))} |
  658. {8:#define foo }{12:void}{8: main() { \} |
  659. {8: }{12:return}{8: 42; \} |
  660. {8: }} |
  661. ^ |
  662. {1:~ }|*11
  663. |
  664. ]],
  665. attr_ids = {
  666. [1] = { bold = true, foreground = Screen.colors.Blue1 },
  667. [8] = { foreground = Screen.colors.Magenta1 },
  668. -- bold will not be overwritten at the moment
  669. [12] = { bold = true, foreground = Screen.colors.Magenta1 },
  670. },
  671. }
  672. eq({
  673. {
  674. capture = 'constant',
  675. metadata = { priority = '101' },
  676. lang = 'c',
  677. id = 14,
  678. pattern_id = 23,
  679. },
  680. { capture = 'type', metadata = {}, lang = 'c', id = 3, pattern_id = 16 },
  681. }, exec_lua [[ return vim.treesitter.get_captures_at_pos(0, 0, 2) ]])
  682. end)
  683. it(
  684. "allows to use captures with dots (don't use fallback when specialization of foo exists)",
  685. function()
  686. insert([[
  687. char* x = "Will somebody ever read this?";
  688. ]])
  689. screen:expect {
  690. grid = [[
  691. char* x = "Will somebody ever read this?"; |
  692. ^ |
  693. {1:~ }|*15
  694. |
  695. ]],
  696. }
  697. command [[
  698. hi link @foo.bar Type
  699. hi link @foo String
  700. ]]
  701. exec_lua(function()
  702. local parser = vim.treesitter.get_parser(0, 'c', {})
  703. local highlighter = vim.treesitter.highlighter
  704. highlighter.new(
  705. parser,
  706. { queries = { c = '(primitive_type) @foo.bar (string_literal) @foo' } }
  707. )
  708. end)
  709. screen:expect({
  710. grid = [[
  711. {6:char}* x = {26:"Will somebody ever read this?"}; |
  712. ^ |
  713. {1:~ }|*15
  714. |
  715. ]],
  716. })
  717. -- clearing specialization reactivates fallback
  718. command [[ hi clear @foo.bar ]]
  719. screen:expect({
  720. grid = [[
  721. {26:char}* x = {26:"Will somebody ever read this?"}; |
  722. ^ |
  723. {1:~ }|*15
  724. |
  725. ]],
  726. })
  727. end
  728. )
  729. it('supports conceal attribute', function()
  730. insert(hl_text_c)
  731. -- conceal can be empty or a single cchar.
  732. exec_lua(function()
  733. vim.opt.cole = 2
  734. local parser = vim.treesitter.get_parser(0, 'c')
  735. vim.treesitter.highlighter.new(parser, {
  736. queries = {
  737. c = [[
  738. ("static" @keyword
  739. (#set! conceal "R"))
  740. ((identifier) @Identifier
  741. (#set! conceal "")
  742. (#eq? @Identifier "lstate"))
  743. ((call_expression
  744. function: (identifier) @function
  745. arguments: (argument_list) @arguments)
  746. (#eq? @function "multiqueue_put")
  747. (#set! @function conceal "V"))
  748. ]],
  749. },
  750. })
  751. end)
  752. screen:expect({
  753. grid = [[
  754. /// Schedule Lua callback on main loop's event queue |
  755. {15:R} int nlua_schedule(lua_State *const ) |
  756. { |
  757. if (lua_type(, 1) != LUA_TFUNCTION |
  758. || != ) { |
  759. lua_pushliteral(, "vim.schedule: expected function"); |
  760. return lua_error(); |
  761. } |
  762. |
  763. LuaRef cb = nlua_ref(, 1); |
  764. |
  765. {25:V}(main_loop.events, nlua_schedule_event, |
  766. 1, (void *)(ptrdiff_t)cb); |
  767. return 0; |
  768. ^} |
  769. {1:~ }|*2
  770. |
  771. ]],
  772. })
  773. end)
  774. it('@foo.bar groups has the correct fallback behavior', function()
  775. local get_hl = function(name)
  776. return api.nvim_get_hl_by_name(name, 1).foreground
  777. end
  778. api.nvim_set_hl(0, '@foo', { fg = 1 })
  779. api.nvim_set_hl(0, '@foo.bar', { fg = 2 })
  780. api.nvim_set_hl(0, '@foo.bar.baz', { fg = 3 })
  781. eq(1, get_hl '@foo')
  782. eq(1, get_hl '@foo.a.b.c.d')
  783. eq(2, get_hl '@foo.bar')
  784. eq(2, get_hl '@foo.bar.a.b.c.d')
  785. eq(3, get_hl '@foo.bar.baz')
  786. eq(3, get_hl '@foo.bar.baz.d')
  787. -- lookup is case insensitive
  788. eq(2, get_hl '@FOO.BAR.SPAM')
  789. api.nvim_set_hl(0, '@foo.missing.exists', { fg = 3 })
  790. eq(1, get_hl '@foo.missing')
  791. eq(3, get_hl '@foo.missing.exists')
  792. eq(3, get_hl '@foo.missing.exists.bar')
  793. eq(nil, get_hl '@total.nonsense.but.a.lot.of.dots')
  794. end)
  795. it('supports multiple nodes assigned to the same capture #17060', function()
  796. insert([[
  797. int x = 4;
  798. int y = 5;
  799. int z = 6;
  800. ]])
  801. exec_lua(function()
  802. local query = '((declaration)+ @string)'
  803. vim.treesitter.query.set('c', 'highlights', query)
  804. vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c'))
  805. end)
  806. screen:expect({
  807. grid = [[
  808. {26:int x = 4;} |
  809. {26:int y = 5;} |
  810. {26:int z = 6;} |
  811. ^ |
  812. {1:~ }|*13
  813. |
  814. ]],
  815. })
  816. end)
  817. it('gives higher priority to more specific captures #27895', function()
  818. insert([[
  819. void foo(int *bar);
  820. ]])
  821. local query = [[
  822. "*" @operator
  823. (parameter_declaration
  824. declarator: (pointer_declarator) @variable.parameter)
  825. ]]
  826. exec_lua(function()
  827. vim.treesitter.query.set('c', 'highlights', query)
  828. vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c'))
  829. end)
  830. screen:expect({
  831. grid = [[
  832. void foo(int {15:*}{25:bar}); |
  833. ^ |
  834. {1:~ }|*15
  835. |
  836. ]],
  837. })
  838. end)
  839. it('highlights applied to first line of closed fold', function()
  840. insert(hl_text_c)
  841. exec_lua(function()
  842. vim.treesitter.query.set('c', 'highlights', hl_query_c)
  843. vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c'))
  844. end)
  845. feed('ggjzfj')
  846. command('set foldtext=')
  847. screen:add_extra_attr_ids({
  848. [100] = {
  849. bold = true,
  850. background = Screen.colors.LightGray,
  851. foreground = Screen.colors.SeaGreen4,
  852. },
  853. [101] = { background = Screen.colors.LightGray, foreground = Screen.colors.DarkCyan },
  854. })
  855. screen:expect({
  856. grid = [[
  857. {18:/// Schedule Lua callback on main loop's event queue} |
  858. {100:^static}{13: }{100:int}{13: }{101:nlua_schedule}{13:(}{100:lua_State}{13: *}{100:const}{13: lstate)················}|
  859. {15:if} ({25:lua_type}(lstate, {26:1}) != {26:LUA_TFUNCTION} |
  860. || {19:lstate} != {19:lstate}) { |
  861. {25:lua_pushliteral}(lstate, {26:"vim.schedule: expected function"}); |
  862. {15:return} {25:lua_error}(lstate); |
  863. } |
  864. |
  865. {29:LuaRef} cb = {25:nlua_ref}(lstate, {26:1}); |
  866. |
  867. multiqueue_put(main_loop.events, {25:nlua_schedule_event}, |
  868. {26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
  869. {15:return} {26:0}; |
  870. } |
  871. {1:~ }|*3
  872. |
  873. ]],
  874. })
  875. end)
  876. end)
  877. describe('treesitter highlighting (lua)', function()
  878. local screen
  879. before_each(function()
  880. clear()
  881. screen = Screen.new(65, 18)
  882. end)
  883. it('supports language injections', function()
  884. insert [[
  885. local ffi = require('ffi')
  886. ffi.cdef("int (*fun)(int, char *);")
  887. ]]
  888. exec_lua(function()
  889. vim.bo.filetype = 'lua'
  890. vim.treesitter.start()
  891. end)
  892. screen:expect({
  893. grid = [[
  894. {15:local} {25:ffi} {15:=} {16:require(}{26:'ffi'}{16:)} |
  895. {25:ffi}{16:.}{25:cdef}{16:(}{26:"}{16:int}{26: }{16:(}{15:*}{26:fun}{16:)(int,}{26: }{16:char}{26: }{15:*}{16:);}{26:"}{16:)} |
  896. ^ |
  897. {1:~ }|*14
  898. |
  899. ]],
  900. })
  901. end)
  902. it('removes outdated highlights', function()
  903. insert('-- int main() {}' .. string.rep("\nprint('test')", 20) .. '\n-- int other() {}')
  904. exec_lua(function()
  905. vim.cmd.norm('gg')
  906. vim.treesitter.query.set(
  907. 'lua',
  908. 'injections',
  909. [[((comment_content) @injection.content
  910. (#set! injection.combined)
  911. (#set! injection.language "c"))]]
  912. )
  913. vim.bo.filetype = 'lua'
  914. vim.treesitter.start()
  915. end)
  916. screen:expect([[
  917. {18:^-- }{16:int}{18: }{25:main}{16:()}{18: }{16:{}} |
  918. {16:print(}{26:'test'}{16:)} |*16
  919. |
  920. ]])
  921. exec_lua(function()
  922. vim.cmd.norm('gg0dw')
  923. end)
  924. screen:expect([[
  925. {25:^int} {25:main}{16:()} {16:{}} |
  926. {16:print(}{26:'test'}{16:)} |*16
  927. |
  928. ]])
  929. end)
  930. end)
  931. describe('treesitter highlighting (help)', function()
  932. local screen
  933. before_each(function()
  934. clear()
  935. screen = Screen.new(40, 6)
  936. end)
  937. it('defaults in vimdoc/highlights.scm', function()
  938. -- Avoid regressions when syncing upstream vimdoc queries.
  939. insert [[
  940. ==============================================================================
  941. NVIM DOCUMENTATION
  942. ------------------------------------------------------------------------------
  943. ABOUT NVIM *tag-1* *tag-2*
  944. |news| News
  945. |nvim| NVim
  946. ]]
  947. feed('gg')
  948. exec_lua(function()
  949. vim.wo.wrap = false
  950. vim.bo.filetype = 'help'
  951. vim.treesitter.start()
  952. end)
  953. screen:add_extra_attr_ids({
  954. [100] = { nocombine = true, underdouble = true },
  955. [101] = { foreground = Screen.colors.Fuchsia, bold = true },
  956. [102] = { underline = true, nocombine = true },
  957. })
  958. screen:expect({
  959. grid = [[
  960. {100:^========================================}|
  961. {101:NVIM DOCUMENTATION} |
  962. |
  963. {102:----------------------------------------}|
  964. {101:ABOUT NVIM} |
  965. |
  966. ]],
  967. })
  968. end)
  969. it('correctly redraws added/removed injections', function()
  970. insert [[
  971. >ruby
  972. -- comment
  973. local this_is = 'actually_lua'
  974. <
  975. ]]
  976. exec_lua(function()
  977. vim.bo.filetype = 'help'
  978. vim.treesitter.start()
  979. end)
  980. screen:expect({
  981. grid = [[
  982. {18:>}{15:ruby} |
  983. {18: -- comment} |
  984. {18: local this_is = 'actually_lua'} |
  985. {18:<} |
  986. ^ |
  987. |
  988. ]],
  989. })
  990. n.api.nvim_buf_set_text(0, 0, 1, 0, 5, { 'lua' })
  991. screen:expect({
  992. grid = [[
  993. {18:>}{15:lua} |
  994. {18: -- comment} |
  995. {18: }{15:local}{18: }{25:this_is}{18: }{15:=}{18: }{26:'actually_lua'} |
  996. {18:<} |
  997. ^ |
  998. |
  999. ]],
  1000. })
  1001. n.api.nvim_buf_set_text(0, 0, 1, 0, 4, { 'ruby' })
  1002. screen:expect({
  1003. grid = [[
  1004. {18:>}{15:ruby} |
  1005. {18: -- comment} |
  1006. {18: local this_is = 'actually_lua'} |
  1007. {18:<} |
  1008. ^ |
  1009. |
  1010. ]],
  1011. })
  1012. end)
  1013. it('correctly redraws injections subpriorities', function()
  1014. -- The top level string node will be highlighted first
  1015. -- with an extmark spanning multiple lines.
  1016. -- When the next line is drawn, which includes an injection,
  1017. -- make sure the highlight appears above the base tree highlight
  1018. insert([=[
  1019. local s = [[
  1020. local also = lua
  1021. ]]
  1022. ]=])
  1023. exec_lua(function()
  1024. local parser = vim.treesitter.get_parser(0, 'lua', {
  1025. injections = {
  1026. lua = '(string content: (_) @injection.content (#set! injection.language lua))',
  1027. },
  1028. })
  1029. vim.treesitter.highlighter.new(parser)
  1030. end)
  1031. screen:expect({
  1032. grid = [=[
  1033. {15:local} {25:s} {15:=} {26:[[} |
  1034. {26: }{15:local}{26: }{25:also}{26: }{15:=}{26: }{25:lua} |
  1035. {26:]]} |
  1036. ^ |
  1037. {1:~ }|
  1038. |
  1039. ]=],
  1040. })
  1041. end)
  1042. end)
  1043. describe('treesitter highlighting (nested injections)', function()
  1044. local screen --- @type test.functional.ui.screen
  1045. before_each(function()
  1046. clear()
  1047. screen = Screen.new(80, 7)
  1048. end)
  1049. it('correctly redraws nested injections (GitHub #25252)', function()
  1050. insert [=[
  1051. function foo() print("Lua!") end
  1052. local lorem = {
  1053. ipsum = {},
  1054. bar = {},
  1055. }
  1056. vim.cmd([[
  1057. augroup RustLSP
  1058. autocmd CursorHold silent! lua vim.lsp.buf.document_highlight()
  1059. augroup END
  1060. ]])
  1061. ]=]
  1062. exec_lua(function()
  1063. vim.opt.scrolloff = 0
  1064. vim.bo.filetype = 'lua'
  1065. vim.treesitter.start()
  1066. end)
  1067. -- invalidate the language tree
  1068. feed('ggi--[[<ESC>04x')
  1069. screen:expect({
  1070. grid = [[
  1071. {15:^function} {25:foo}{16:()} {16:print(}{26:"Lua!"}{16:)} {15:end} |
  1072. |
  1073. {15:local} {25:lorem} {15:=} {16:{} |
  1074. {25:ipsum} {15:=} {16:{},} |
  1075. {25:bar} {15:=} {16:{},} |
  1076. {16:}} |
  1077. |
  1078. ]],
  1079. })
  1080. -- spam newline insert/delete to invalidate Lua > Vim > Lua region
  1081. feed('3jo<ESC>ddko<ESC>ddko<ESC>ddko<ESC>ddk0')
  1082. screen:expect({
  1083. grid = [[
  1084. {15:function} {25:foo}{16:()} {16:print(}{26:"Lua!"}{16:)} {15:end} |
  1085. |
  1086. {15:local} {25:lorem} {15:=} {16:{} |
  1087. ^ {25:ipsum} {15:=} {16:{},} |
  1088. {25:bar} {15:=} {16:{},} |
  1089. {16:}} |
  1090. |
  1091. ]],
  1092. })
  1093. end)
  1094. end)
  1095. describe('treesitter highlighting (markdown)', function()
  1096. local screen
  1097. before_each(function()
  1098. clear()
  1099. screen = Screen.new(40, 6)
  1100. exec_lua(function()
  1101. vim.bo.filetype = 'markdown'
  1102. vim.treesitter.start()
  1103. end)
  1104. end)
  1105. it('supports hyperlinks', function()
  1106. local url = 'https://example.com'
  1107. insert(string.format('[This link text](%s) is a hyperlink.', url))
  1108. screen:add_extra_attr_ids({
  1109. [100] = { foreground = Screen.colors.DarkCyan, url = 'https://example.com' },
  1110. [101] = {
  1111. foreground = Screen.colors.SlateBlue,
  1112. url = 'https://example.com',
  1113. underline = true,
  1114. },
  1115. })
  1116. screen:expect({
  1117. grid = [[
  1118. {100:[This link text](}{101:https://example.com}{100:)} is|
  1119. a hyperlink^. |
  1120. {1:~ }|*3
  1121. |
  1122. ]],
  1123. })
  1124. end)
  1125. local code_block = [[
  1126. - $f(0)=\sum_{k=1}^{\infty}\frac{2}{\pi^{2}k^{2}}+\lim_{w \to 0}x$.
  1127. ```c
  1128. printf('Hello World!');
  1129. ```
  1130. ]]
  1131. it('works with spellchecked and smoothscrolled topline', function()
  1132. insert(code_block)
  1133. command('set spell smoothscroll')
  1134. feed('gg<C-E>')
  1135. screen:add_extra_attr_ids({ [100] = { undercurl = true, special = Screen.colors.Red } })
  1136. screen:expect({
  1137. grid = [[
  1138. {1:<<<}k^{2}}+\{100:lim}_{w \to 0}x$^. |
  1139. |
  1140. {18:```}{15:c} |
  1141. {25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1142. {18:```} |
  1143. |
  1144. ]],
  1145. })
  1146. end)
  1147. it('works with concealed lines', function()
  1148. insert(code_block)
  1149. screen:expect({
  1150. grid = [[
  1151. |
  1152. {18:```}{15:c} |
  1153. {25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1154. {18:```} |
  1155. ^ |
  1156. |
  1157. ]],
  1158. })
  1159. feed('ggj')
  1160. command('set number conceallevel=3')
  1161. screen:expect({
  1162. grid = [[
  1163. {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{|
  1164. {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. |
  1165. {8: 2 }^ |
  1166. {8: 4 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1167. {8: 6 } |
  1168. |
  1169. ]],
  1170. })
  1171. feed('j')
  1172. screen:expect({
  1173. grid = [[
  1174. {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{|
  1175. {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. |
  1176. {8: 2 } |
  1177. {8: 3 }{18:^```}{15:c} |
  1178. {8: 4 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1179. |
  1180. ]],
  1181. })
  1182. feed('j')
  1183. screen:expect({
  1184. grid = [[
  1185. {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{|
  1186. {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. |
  1187. {8: 2 } |
  1188. {8: 4 }{25:^printf}{16:(}{26:'Hello World!'}{16:);} |
  1189. {8: 6 } |
  1190. |
  1191. ]],
  1192. })
  1193. feed('j')
  1194. screen:expect({
  1195. grid = [[
  1196. {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{|
  1197. {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. |
  1198. {8: 2 } |
  1199. {8: 4 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1200. {8: 5 }{18:^```} |
  1201. |
  1202. ]],
  1203. })
  1204. -- Concealed lines highlight until changed botline
  1205. screen:try_resize(screen._width, 16)
  1206. feed('y3k30P:<Esc><C-F><C-B>')
  1207. screen:expect([[
  1208. {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{|
  1209. {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. |
  1210. {8: 2 } |
  1211. {8: 4 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1212. {8: 6 } |
  1213. {8: 8 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1214. {8: 10 } |
  1215. {8: 12 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1216. {8: 14 } |
  1217. {8: 16 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1218. {8: 18 } |
  1219. {8: 20 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1220. {8: 22 } |
  1221. {8: 24 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1222. {8: 25 }{18:^```} |
  1223. |
  1224. ]])
  1225. feed('G')
  1226. screen:expect([[
  1227. {8: 98 } |
  1228. {8:100 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1229. {8:102 } |
  1230. {8:104 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1231. {8:106 } |
  1232. {8:108 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1233. {8:110 } |
  1234. {8:112 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1235. {8:114 } |
  1236. {8:116 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1237. {8:118 } |
  1238. {8:120 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1239. {8:122 } |
  1240. {8:124 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1241. {8:126 }^ |
  1242. |
  1243. ]])
  1244. feed('ggdj')
  1245. command('set concealcursor=n')
  1246. screen:expect([[
  1247. {8: 2 }{25:^printf}{16:(}{26:'Hello World!'}{16:);} |
  1248. {8: 4 } |
  1249. {8: 6 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1250. {8: 8 } |
  1251. {8: 10 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1252. {8: 12 } |
  1253. {8: 14 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1254. {8: 16 } |
  1255. {8: 18 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1256. {8: 20 } |
  1257. {8: 22 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1258. {8: 24 } |
  1259. {8: 26 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1260. {8: 28 } |
  1261. {8: 30 }{25:printf}{16:(}{26:'Hello World!'}{16:);} |
  1262. |
  1263. ]])
  1264. exec_lua(function()
  1265. vim.api.nvim_buf_set_lines(0, 0, -1, false, {})
  1266. assert(vim.api.nvim_win_text_height(0, {}).all == 1, 'line concealed')
  1267. end)
  1268. end)
  1269. end)
  1270. it('starting and stopping treesitter highlight in init.lua works #29541', function()
  1271. t.write_file(
  1272. 'Xinit.lua',
  1273. [[
  1274. vim.bo.ft = 'c'
  1275. vim.treesitter.start()
  1276. vim.treesitter.stop()
  1277. ]]
  1278. )
  1279. finally(function()
  1280. os.remove('Xinit.lua')
  1281. end)
  1282. clear({ args = { '-u', 'Xinit.lua' } })
  1283. eq('', api.nvim_get_vvar('errmsg'))
  1284. local screen = Screen.new(65, 18)
  1285. fn.setreg('r', hl_text_c)
  1286. feed('i<C-R><C-O>r<Esc>gg')
  1287. -- legacy syntax highlighting is used
  1288. screen:expect(hl_grid_legacy_c)
  1289. end)
  1290. it('no nil index for missing highlight query', function()
  1291. clear()
  1292. local cqueries = vim.uv.cwd() .. '/runtime/queries/c/'
  1293. os.rename(cqueries .. 'highlights.scm', cqueries .. '_highlights.scm')
  1294. finally(function()
  1295. os.rename(cqueries .. '_highlights.scm', cqueries .. 'highlights.scm')
  1296. end)
  1297. exec_lua([[
  1298. local parser = vim.treesitter.get_parser(0, 'c')
  1299. vim.treesitter.highlighter.new(parser)
  1300. ]])
  1301. end)