aboutsummaryrefslogtreecommitdiff
path: root/test/functional/treesitter/highlight_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/functional/treesitter/highlight_spec.lua')
-rw-r--r--test/functional/treesitter/highlight_spec.lua183
1 files changed, 168 insertions, 15 deletions
diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua
index 2bf230fe69..69984b3233 100644
--- a/test/functional/treesitter/highlight_spec.lua
+++ b/test/functional/treesitter/highlight_spec.lua
@@ -1,13 +1,14 @@
-local helpers = require('test.functional.helpers')(after_each)
+local t = require('test.testutil')
+local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
-local clear = helpers.clear
-local insert = helpers.insert
-local exec_lua = helpers.exec_lua
-local feed = helpers.feed
-local command = helpers.command
-local api = helpers.api
-local eq = helpers.eq
+local clear = n.clear
+local insert = n.insert
+local exec_lua = n.exec_lua
+local feed = n.feed
+local command = n.command
+local api = n.api
+local eq = t.eq
before_each(clear)
@@ -483,7 +484,7 @@ describe('treesitter highlighting (C)', function()
exec_lua [[
vim.treesitter.language.register("c", "foo")
local parser = vim.treesitter.get_parser(0, "c", {
- injections = {c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "fOO")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "fOO"))'}
+ injections = {c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "foo")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "foo"))'}
})
local highlighter = vim.treesitter.highlighter
test_hl = highlighter.new(parser, {queries = {c = hl_query}})
@@ -681,6 +682,12 @@ describe('treesitter highlighting (C)', function()
((identifier) @Identifier
(#set! conceal "")
(#eq? @Identifier "lstate"))
+
+ ((call_expression
+ function: (identifier) @function
+ arguments: (argument_list) @arguments)
+ (#eq? @function "multiqueue_put")
+ (#set! @function conceal "V"))
]]}})
]=]
@@ -697,7 +704,7 @@ describe('treesitter highlighting (C)', function()
|
LuaRef cb = nlua_ref(, 1); |
|
- multiqueue_put(main_loop.events, nlua_schedule_event, |
+ {11:V}(main_loop.events, nlua_schedule_event, |
1, (void *)(ptrdiff_t)cb); |
return 0; |
^} |
@@ -756,6 +763,75 @@ describe('treesitter highlighting (C)', function()
]],
}
end)
+
+ it('gives higher priority to more specific captures #27895', function()
+ insert([[
+ void foo(int *bar);
+ ]])
+
+ local query = [[
+ "*" @operator
+
+ (parameter_declaration
+ declarator: (pointer_declarator) @variable.parameter)
+ ]]
+
+ exec_lua(
+ [[
+ local query = ...
+ vim.treesitter.query.set('c', 'highlights', query)
+ vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, 'c'))
+ ]],
+ query
+ )
+
+ screen:expect {
+ grid = [[
+ void foo(int {4:*}{11:bar}); |
+ ^ |
+ {1:~ }|*15
+ |
+ ]],
+ }
+ end)
+end)
+
+describe('treesitter highlighting (lua)', function()
+ local screen
+
+ before_each(function()
+ screen = Screen.new(65, 18)
+ screen:attach()
+ screen:set_default_attr_ids {
+ [1] = { bold = true, foreground = Screen.colors.Blue },
+ [2] = { foreground = Screen.colors.DarkCyan },
+ [3] = { foreground = Screen.colors.Magenta },
+ [4] = { foreground = Screen.colors.SlateBlue },
+ [5] = { bold = true, foreground = Screen.colors.Brown },
+ }
+ end)
+
+ it('supports language injections', function()
+ insert [[
+ local ffi = require('ffi')
+ ffi.cdef("int (*fun)(int, char *);")
+ ]]
+
+ exec_lua [[
+ vim.bo.filetype = 'lua'
+ vim.treesitter.start()
+ ]]
+
+ screen:expect {
+ grid = [[
+ {5:local} {2:ffi} {5:=} {4:require(}{3:'ffi'}{4:)} |
+ {2:ffi}{4:.}{2:cdef}{4:(}{3:"}{4:int}{3: }{4:(}{5:*}{3:fun}{4:)(int,}{3: }{4:char}{3: }{5:*}{4:);}{3:"}{4:)} |
+ ^ |
+ {1:~ }|*14
+ |
+ ]],
+ }
+ end)
end)
describe('treesitter highlighting (help)', function()
@@ -788,7 +864,7 @@ describe('treesitter highlighting (help)', function()
screen:expect {
grid = [[
- {1:>ruby} |
+ {1:>}{3:ruby} |
{1: -- comment} |
{1: local this_is = 'actually_lua'} |
{1:<} |
@@ -797,11 +873,11 @@ describe('treesitter highlighting (help)', function()
]],
}
- helpers.api.nvim_buf_set_text(0, 0, 1, 0, 5, { 'lua' })
+ n.api.nvim_buf_set_text(0, 0, 1, 0, 5, { 'lua' })
screen:expect {
grid = [[
- {1:>lua} |
+ {1:>}{3:lua} |
{1: -- comment} |
{1: }{3:local}{1: }{4:this_is}{1: }{3:=}{1: }{5:'actually_lua'} |
{1:<} |
@@ -810,11 +886,11 @@ describe('treesitter highlighting (help)', function()
]],
}
- helpers.api.nvim_buf_set_text(0, 0, 1, 0, 4, { 'ruby' })
+ n.api.nvim_buf_set_text(0, 0, 1, 0, 4, { 'ruby' })
screen:expect {
grid = [[
- {1:>ruby} |
+ {1:>}{3:ruby} |
{1: -- comment} |
{1: local this_is = 'actually_lua'} |
{1:<} |
@@ -823,6 +899,40 @@ describe('treesitter highlighting (help)', function()
]],
}
end)
+
+ it('correctly redraws injections subpriorities', function()
+ -- The top level string node will be highlighted first
+ -- with an extmark spanning multiple lines.
+ -- When the next line is drawn, which includes an injection,
+ -- make sure the highlight appears above the base tree highlight
+
+ insert([=[
+ local s = [[
+ local also = lua
+ ]]
+ ]=])
+
+ exec_lua [[
+ parser = vim.treesitter.get_parser(0, "lua", {
+ injections = {
+ lua = '(string content: (_) @injection.content (#set! injection.language lua))'
+ }
+ })
+
+ vim.treesitter.highlighter.new(parser)
+ ]]
+
+ screen:expect {
+ grid = [=[
+ {3:local} {4:s} {3:=} {5:[[} |
+ {5: }{3:local}{5: }{4:also}{5: }{3:=}{5: }{4:lua} |
+ {5:]]} |
+ ^ |
+ {2:~ }|
+ |
+ ]=],
+ }
+ end)
end)
describe('treesitter highlighting (nested injections)', function()
@@ -891,3 +1001,46 @@ vim.cmd([[
}
end)
end)
+
+describe('treesitter highlighting (markdown)', function()
+ local screen
+
+ before_each(function()
+ screen = Screen.new(40, 6)
+ screen:attach()
+ screen:set_default_attr_ids {
+ [1] = { foreground = Screen.colors.Blue1 },
+ [2] = { bold = true, foreground = Screen.colors.Blue1 },
+ [3] = { bold = true, foreground = Screen.colors.Brown },
+ [4] = { foreground = Screen.colors.Cyan4 },
+ [5] = { foreground = Screen.colors.Magenta1 },
+ }
+ end)
+
+ it('supports hyperlinks', function()
+ local url = 'https://example.com'
+ insert(string.format('[This link text](%s) is a hyperlink.', url))
+ exec_lua([[
+ vim.bo.filetype = 'markdown'
+ vim.treesitter.start()
+ ]])
+
+ screen:expect {
+ grid = [[
+ {4:[}{6:This link text}{4:](}{7:https://example.com}{4:)} is|
+ a hyperlink^. |
+ {2:~ }|*3
+ |
+ ]],
+ attr_ids = {
+ [1] = { foreground = Screen.colors.Blue1 },
+ [2] = { bold = true, foreground = Screen.colors.Blue1 },
+ [3] = { bold = true, foreground = Screen.colors.Brown },
+ [4] = { foreground = Screen.colors.Cyan4 },
+ [5] = { foreground = Screen.colors.Magenta },
+ [6] = { foreground = Screen.colors.Cyan4, url = url },
+ [7] = { underline = true, foreground = Screen.colors.SlateBlue },
+ },
+ }
+ end)
+end)