aboutsummaryrefslogtreecommitdiff
path: root/test/functional/treesitter
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-11-30 20:35:25 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-11-30 20:35:25 +0000
commit1b7b916b7631ddf73c38e3a0070d64e4636cb2f3 (patch)
treecd08258054db80bb9a11b1061bb091c70b76926a /test/functional/treesitter
parenteaa89c11d0f8aefbb512de769c6c82f61a8baca3 (diff)
parent4a8bf24ac690004aedf5540fa440e788459e5e34 (diff)
downloadrneovim-aucmd_textputpost.tar.gz
rneovim-aucmd_textputpost.tar.bz2
rneovim-aucmd_textputpost.zip
Merge remote-tracking branch 'upstream/master' into aucmd_textputpostaucmd_textputpost
Diffstat (limited to 'test/functional/treesitter')
-rw-r--r--test/functional/treesitter/fold_spec.lua474
-rw-r--r--test/functional/treesitter/highlight_spec.lua291
-rw-r--r--test/functional/treesitter/language_spec.lua18
-rw-r--r--test/functional/treesitter/node_spec.lua28
-rw-r--r--test/functional/treesitter/parser_spec.lua573
-rw-r--r--test/functional/treesitter/utils_spec.lua17
6 files changed, 1225 insertions, 176 deletions
diff --git a/test/functional/treesitter/fold_spec.lua b/test/functional/treesitter/fold_spec.lua
new file mode 100644
index 0000000000..a8abbc002b
--- /dev/null
+++ b/test/functional/treesitter/fold_spec.lua
@@ -0,0 +1,474 @@
+local helpers = require('test.functional.helpers')(after_each)
+local clear = helpers.clear
+local eq = helpers.eq
+local insert = helpers.insert
+local exec_lua = helpers.exec_lua
+local command = helpers.command
+local feed = helpers.feed
+local Screen = require('test.functional.ui.screen')
+
+before_each(clear)
+
+describe('treesitter foldexpr', function()
+ clear()
+
+ local test_text = [[
+void ui_refresh(void)
+{
+ int width = INT_MAX, height = INT_MAX;
+ bool ext_widgets[kUIExtCount];
+ for (UIExtension i = 0; (int)i < kUIExtCount; i++) {
+ ext_widgets[i] = true;
+ }
+
+ bool inclusive = ui_override();
+ for (size_t i = 0; i < ui_count; i++) {
+ UI *ui = uis[i];
+ width = MIN(ui->width, width);
+ height = MIN(ui->height, height);
+ foo = BAR(ui->bazaar, bazaar);
+ for (UIExtension j = 0; (int)j < kUIExtCount; j++) {
+ ext_widgets[j] &= (ui->ui_ext[j] || inclusive);
+ }
+ }
+}]]
+
+ local function get_fold_levels()
+ return exec_lua([[
+ local res = {}
+ for i = 1, vim.api.nvim_buf_line_count(0) do
+ res[i] = vim.treesitter.foldexpr(i)
+ end
+ return res
+ ]])
+ end
+
+ it("can compute fold levels", function()
+ insert(test_text)
+
+ exec_lua([[vim.treesitter.get_parser(0, "c")]])
+
+ eq({
+ [1] = '>1',
+ [2] = '1',
+ [3] = '1',
+ [4] = '1',
+ [5] = '>2',
+ [6] = '2',
+ [7] = '2',
+ [8] = '1',
+ [9] = '1',
+ [10] = '>2',
+ [11] = '2',
+ [12] = '2',
+ [13] = '2',
+ [14] = '2',
+ [15] = '>3',
+ [16] = '3',
+ [17] = '3',
+ [18] = '2',
+ [19] = '1' }, get_fold_levels())
+
+ end)
+
+ it("recomputes fold levels after lines are added/removed", function()
+ insert(test_text)
+
+ exec_lua([[vim.treesitter.get_parser(0, "c")]])
+
+ command('1,2d')
+
+ eq({
+ [1] = '0',
+ [2] = '0',
+ [3] = '>1',
+ [4] = '1',
+ [5] = '1',
+ [6] = '0',
+ [7] = '0',
+ [8] = '>1',
+ [9] = '1',
+ [10] = '1',
+ [11] = '1',
+ [12] = '1',
+ [13] = '>2',
+ [14] = '2',
+ [15] = '2',
+ [16] = '1',
+ [17] = '0' }, get_fold_levels())
+
+ command('1put!')
+
+ eq({
+ [1] = '>1',
+ [2] = '1',
+ [3] = '1',
+ [4] = '1',
+ [5] = '>2',
+ [6] = '2',
+ [7] = '2',
+ [8] = '1',
+ [9] = '1',
+ [10] = '>2',
+ [11] = '2',
+ [12] = '2',
+ [13] = '2',
+ [14] = '2',
+ [15] = '>3',
+ [16] = '3',
+ [17] = '3',
+ [18] = '2',
+ [19] = '1' }, get_fold_levels())
+ end)
+
+ it("updates folds in all windows", function()
+ local screen = Screen.new(60, 48)
+ screen:attach()
+ screen:set_default_attr_ids({
+ [1] = {background = Screen.colors.Grey, foreground = Screen.colors.DarkBlue};
+ [2] = {bold = true, foreground = Screen.colors.Blue1};
+ [3] = {bold = true, reverse = true};
+ [4] = {reverse = true};
+ })
+
+ exec_lua([[vim.treesitter.get_parser(0, "c")]])
+ command([[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=9]])
+ command('split')
+
+ insert(test_text)
+
+ screen:expect{grid=[[
+ {1:-}void ui_refresh(void) |
+ {1:│}{ |
+ {1:│} int width = INT_MAX, height = INT_MAX; |
+ {1:│} bool ext_widgets[kUIExtCount]; |
+ {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
+ {1:2} ext_widgets[i] = true; |
+ {1:2} } |
+ {1:│} |
+ {1:│} bool inclusive = ui_override(); |
+ {1:-} for (size_t i = 0; i < ui_count; i++) { |
+ {1:2} UI *ui = uis[i]; |
+ {1:2} width = MIN(ui->width, width); |
+ {1:2} height = MIN(ui->height, height); |
+ {1:2} foo = BAR(ui->bazaar, bazaar); |
+ {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
+ {1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
+ {1:3} } |
+ {1:2} } |
+ {1:│}^} |
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {3:[No Name] [+] }|
+ {1:-}void ui_refresh(void) |
+ {1:│}{ |
+ {1:│} int width = INT_MAX, height = INT_MAX; |
+ {1:│} bool ext_widgets[kUIExtCount]; |
+ {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
+ {1:2} ext_widgets[i] = true; |
+ {1:2} } |
+ {1:│} |
+ {1:│} bool inclusive = ui_override(); |
+ {1:-} for (size_t i = 0; i < ui_count; i++) { |
+ {1:2} UI *ui = uis[i]; |
+ {1:2} width = MIN(ui->width, width); |
+ {1:2} height = MIN(ui->height, height); |
+ {1:2} foo = BAR(ui->bazaar, bazaar); |
+ {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
+ {1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
+ {1:3} } |
+ {1:2} } |
+ {1:│}} |
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {4:[No Name] [+] }|
+ |
+ ]]}
+
+ command('1,2d')
+
+ screen:expect{grid=[[
+ {1: } ^int width = INT_MAX, height = INT_MAX; |
+ {1: } bool ext_widgets[kUIExtCount]; |
+ {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
+ {1:│} ext_widgets[i] = true; |
+ {1:│} } |
+ {1: } |
+ {1: } bool inclusive = ui_override(); |
+ {1:-} for (size_t i = 0; i < ui_count; i++) { |
+ {1:│} UI *ui = uis[i]; |
+ {1:│} width = MIN(ui->width, width); |
+ {1:│} height = MIN(ui->height, height); |
+ {1:│} foo = BAR(ui->bazaar, bazaar); |
+ {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
+ {1:2} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
+ {1:2} } |
+ {1:│} } |
+ {1: }} |
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {3:[No Name] [+] }|
+ {1: } int width = INT_MAX, height = INT_MAX; |
+ {1: } bool ext_widgets[kUIExtCount]; |
+ {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
+ {1:│} ext_widgets[i] = true; |
+ {1:│} } |
+ {1: } |
+ {1: } bool inclusive = ui_override(); |
+ {1:-} for (size_t i = 0; i < ui_count; i++) { |
+ {1:│} UI *ui = uis[i]; |
+ {1:│} width = MIN(ui->width, width); |
+ {1:│} height = MIN(ui->height, height); |
+ {1:│} foo = BAR(ui->bazaar, bazaar); |
+ {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
+ {1:2} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
+ {1:2} } |
+ {1:│} } |
+ {1: }} |
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {4:[No Name] [+] }|
+ |
+ ]]}
+
+
+ feed([[O<C-u><C-r>"<BS><Esc>]])
+
+ screen:expect{grid=[[
+ {1:-}void ui_refresh(void) |
+ {1:│}^{ |
+ {1:│} int width = INT_MAX, height = INT_MAX; |
+ {1:│} bool ext_widgets[kUIExtCount]; |
+ {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
+ {1:2} ext_widgets[i] = true; |
+ {1:2} } |
+ {1:│} |
+ {1:│} bool inclusive = ui_override(); |
+ {1:-} for (size_t i = 0; i < ui_count; i++) { |
+ {1:2} UI *ui = uis[i]; |
+ {1:2} width = MIN(ui->width, width); |
+ {1:2} height = MIN(ui->height, height); |
+ {1:2} foo = BAR(ui->bazaar, bazaar); |
+ {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
+ {1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
+ {1:3} } |
+ {1:2} } |
+ {1:│}} |
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {3:[No Name] [+] }|
+ {1:-}void ui_refresh(void) |
+ {1:│}{ |
+ {1:│} int width = INT_MAX, height = INT_MAX; |
+ {1:│} bool ext_widgets[kUIExtCount]; |
+ {1:-} for (UIExtension i = 0; (int)i < kUIExtCount; i++) { |
+ {1:2} ext_widgets[i] = true; |
+ {1:2} } |
+ {1:│} |
+ {1:│} bool inclusive = ui_override(); |
+ {1:-} for (size_t i = 0; i < ui_count; i++) { |
+ {1:2} UI *ui = uis[i]; |
+ {1:2} width = MIN(ui->width, width); |
+ {1:2} height = MIN(ui->height, height); |
+ {1:2} foo = BAR(ui->bazaar, bazaar); |
+ {1:-} for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
+ {1:3} ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
+ {1:3} } |
+ {1:2} } |
+ {1:│}} |
+ {2:~ }|
+ {2:~ }|
+ {2:~ }|
+ {4:[No Name] [+] }|
+ |
+ ]]}
+
+ end)
+
+ it("doesn't open folds in diff mode", function()
+ local screen = Screen.new(60, 36)
+ screen:attach()
+
+ exec_lua([[vim.treesitter.get_parser(0, "c")]])
+ command([[set foldmethod=expr foldexpr=v:lua.vim.treesitter.foldexpr() foldcolumn=1 foldlevel=9]])
+ insert(test_text)
+ command('16d')
+
+ command('new')
+ insert(test_text)
+
+ command('windo diffthis')
+ feed('do')
+
+ screen:expect{grid=[[
+ {1:+ }{2:+-- 9 lines: void ui_refresh(void)·······················}|
+ {1: } for (size_t i = 0; i < ui_count; i++) { |
+ {1: } UI *ui = uis[i]; |
+ {1: } width = MIN(ui->width, width); |
+ {1: } height = MIN(ui->height, height); |
+ {1: } foo = BAR(ui->bazaar, bazaar); |
+ {1: } for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
+ {1: } ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
+ {1: } } |
+ {1: } } |
+ {1: }} |
+ {3:~ }|
+ {3:~ }|
+ {3:~ }|
+ {3:~ }|
+ {3:~ }|
+ {3:~ }|
+ {4:[No Name] [+] }|
+ {1:+ }{2:+-- 9 lines: void ui_refresh(void)·······················}|
+ {1: } for (size_t i = 0; i < ui_count; i++) { |
+ {1: } UI *ui = uis[i]; |
+ {1: } width = MIN(ui->width, width); |
+ {1: } height = MIN(ui->height, height); |
+ {1: } foo = BAR(ui->bazaar, bazaar); |
+ {1: } for (UIExtension j = 0; (int)j < kUIExtCount; j++) { |
+ {1: } ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
+ {1: } ^} |
+ {1: } } |
+ {1: }} |
+ {3:~ }|
+ {3:~ }|
+ {3:~ }|
+ {3:~ }|
+ {3:~ }|
+ {5:[No Name] [+] }|
+ |
+ ]], attr_ids={
+ [1] = {background = Screen.colors.Grey, foreground = Screen.colors.Blue4};
+ [2] = {background = Screen.colors.LightGrey, foreground = Screen.colors.Blue4};
+ [3] = {foreground = Screen.colors.Blue, bold = true};
+ [4] = {reverse = true};
+ [5] = {reverse = true, bold = true};
+ }}
+ end)
+
+end)
+
+describe('treesitter foldtext', function()
+ local test_text = [[
+void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *))
+{
+ int width = INT_MAX, height = INT_MAX;
+ bool ext_widgets[kUIExtCount];
+ for (UIExtension i = 0; (int)i < kUIExtCount; i++) {
+ ext_widgets[i] = true;
+ }
+
+ bool inclusive = ui_override();
+ for (size_t i = 0; i < ui_count; i++) {
+ UI *ui = uis[i];
+ width = MIN(ui->width, width);
+ height = MIN(ui->height, height);
+ foo = BAR(ui->bazaar, bazaar);
+ for (UIExtension j = 0; (int)j < kUIExtCount; j++) {
+ ext_widgets[j] &= (ui->ui_ext[j] || inclusive);
+ }
+ }
+}]]
+ local screen
+
+ before_each(function()
+ screen = Screen.new(60, 5)
+ screen:set_default_attr_ids({
+ [0] = {foreground = Screen.colors.Blue, bold = true},
+ [1] = {foreground = Screen.colors.DarkBlue, background = Screen.colors.LightGray};
+ [2] = {bold = true, background = Screen.colors.LightGray, foreground = Screen.colors.SeaGreen};
+ [3] = {foreground = Screen.colors.DarkCyan, background = Screen.colors.LightGray};
+ [4] = {foreground = Screen.colors.SlateBlue, background = Screen.colors.LightGray};
+ [5] = {bold = true, background = Screen.colors.LightGray, foreground = Screen.colors.Brown};
+ [6] = {background = Screen.colors.Red1};
+ [7] = {foreground = Screen.colors.DarkBlue, background = Screen.colors.Red};
+ [8] = {foreground = Screen.colors.Brown, bold = true, background = Screen.colors.Red};
+ [9] = {foreground = Screen.colors.SlateBlue, background = Screen.colors.Red};
+ [10] = {bold = true};
+ })
+ screen:attach()
+ end)
+
+ it('displays highlighted content', function()
+ command([[set foldmethod=manual foldtext=v:lua.vim.treesitter.foldtext() updatetime=50]])
+ insert(test_text)
+ exec_lua([[vim.treesitter.get_parser(0, "c")]])
+
+ feed('ggVGzf')
+ screen:expect{grid=[[
+ {2:^void}{1: }{3:qsort}{4:(}{2:void}{1: }{5:*}{3:base}{4:,}{1: }{2:size_t}{1: }{3:nel}{4:,}{1: }{2:size_t}{1: }{3:width}{4:,}{1: }{2:int}{1: }{4:(}{5:*}{3:compa}|
+ {0:~ }|
+ {0:~ }|
+ {0:~ }|
+ |
+ ]]}
+ end)
+
+ it('handles deep nested captures', function()
+ command([[set foldmethod=manual foldtext=v:lua.vim.treesitter.foldtext() updatetime=50]])
+ insert([[
+function FoldInfo.new()
+ return setmetatable({
+ start_counts = {},
+ stop_counts = {},
+ levels0 = {},
+ levels = {},
+ }, FoldInfo)
+end]])
+ exec_lua([[vim.treesitter.get_parser(0, "lua")]])
+
+ feed('ggjVGkzfgg')
+ screen:expect{grid=[[
+ ^function FoldInfo.new() |
+ {1: }{5:return}{1: }{4:setmetatable({}{1:·····································}|
+ end |
+ {0:~ }|
+ |
+ ]]}
+
+ command('hi! Visual guibg=Red')
+ feed('GVgg')
+ screen:expect{grid=[[
+ ^f{6:unction FoldInfo.new()} |
+ {7: }{8:return}{7: }{9:setmetatable({}{7:·····································}|
+ {6:end} |
+ {0:~ }|
+ {10:-- VISUAL LINE --} |
+ ]]}
+
+ feed('10l<C-V>')
+ screen:expect{grid=[[
+ {6:function F}^oldInfo.new() |
+ {7: }{8:return}{7: }{9:se}{4:tmetatable({}{1:·····································}|
+ {6:end} |
+ {0:~ }|
+ {10:-- VISUAL BLOCK --} |
+ ]]}
+ end)
+
+ it('falls back to default', function()
+ command([[set foldmethod=manual foldtext=v:lua.vim.treesitter.foldtext()]])
+ insert(test_text)
+
+ feed('ggVGzf')
+ screen:expect{grid=[[
+ {1:^+-- 19 lines: void qsort(void *base, size_t nel, size_t widt}|
+ {0:~ }|
+ {0:~ }|
+ {0:~ }|
+ |
+ ]]}
+ end)
+end)
diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua
index 2a2311c0fa..e037c9e215 100644
--- a/test/functional/treesitter/highlight_spec.lua
+++ b/test/functional/treesitter/highlight_spec.lua
@@ -11,7 +11,7 @@ local eq = helpers.eq
before_each(clear)
-local hl_query = [[
+local hl_query_c = [[
(ERROR) @error
"if" @keyword
@@ -47,7 +47,7 @@ local hl_query = [[
(comment) @comment
]]
-local hl_text = [[
+local hl_text_c = [[
/// Schedule Lua callback on main loop's event queue
static int nlua_schedule(lua_State *const lstate)
{
@@ -64,7 +64,7 @@ static int nlua_schedule(lua_State *const lstate)
return 0;
}]]
-local test_text = [[
+local test_text_c = [[
void ui_refresh(void)
{
int width = INT_MAX, height = INT_MAX;
@@ -85,7 +85,57 @@ void ui_refresh(void)
}
}]]
-describe('treesitter highlighting', function()
+local injection_text_c = [[
+int x = INT_MAX;
+#define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
+#define foo void main() { \
+ return 42; \
+ }
+]]
+
+local injection_grid_c = [[
+ int x = INT_MAX; |
+ #define READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) |
+ #define foo void main() { \ |
+ return 42; \ |
+ } |
+ ^ |
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ |
+]]
+
+local injection_grid_expected_c = [[
+ {3:int} x = {5:INT_MAX}; |
+ #define {5:READ_STRING}(x, y) ({3:char} *)read_string((x), ({3:size_t})(y)) |
+ #define foo {3:void} main() { \ |
+ {4:return} {5:42}; \ |
+ } |
+ ^ |
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ {1:~ }|
+ |
+]]
+
+describe('treesitter highlighting (C)', function()
local screen
before_each(function()
@@ -105,13 +155,13 @@ describe('treesitter highlighting', function()
[11] = {foreground = Screen.colors.Cyan4};
}
- exec_lua([[ hl_query = ... ]], hl_query)
+ exec_lua([[ hl_query = ... ]], hl_query_c)
command [[ hi link @error ErrorMsg ]]
command [[ hi link @warning WarningMsg ]]
end)
it('is updated with edits', function()
- insert(hl_text)
+ insert(hl_text_c)
screen:expect{grid=[[
/// Schedule Lua callback on main loop's event queue |
static int nlua_schedule(lua_State *const lstate) |
@@ -274,7 +324,7 @@ describe('treesitter highlighting', function()
end)
it('is updated with :sort', function()
- insert(test_text)
+ insert(test_text_c)
exec_lua [[
local parser = vim.treesitter.get_parser(0, "c")
test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query}})
@@ -351,7 +401,7 @@ describe('treesitter highlighting', function()
[1] = {bold = true, foreground = Screen.colors.SeaGreen4};
}
- insert(test_text)
+ insert(test_text_c)
screen:expect{ grid= [[
int width = INT_MAX, height = INT_MAX; |
@@ -376,7 +426,7 @@ describe('treesitter highlighting', function()
exec_lua [[
parser = vim.treesitter.get_parser(0, "c")
- query = vim.treesitter.parse_query("c", "(declaration) @decl")
+ query = vim.treesitter.query.parse("c", "(declaration) @decl")
local nodes = {}
for _, node in query:iter_captures(parser:parse()[1]:root(), 0, 0, 19) do
@@ -411,85 +461,58 @@ describe('treesitter highlighting', function()
end)
it("supports injected languages", function()
- insert([[
- int x = INT_MAX;
- #define READ_STRING(x, y) (char_u *)read_string((x), (size_t)(y))
- #define foo void main() { \
- return 42; \
- }
- ]])
+ insert(injection_text_c)
- screen:expect{grid=[[
- int x = INT_MAX; |
- #define READ_STRING(x, y) (char_u *)read_string((x), (size_t)(y))|
- #define foo void main() { \ |
- return 42; \ |
- } |
- ^ |
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- |
- ]]}
+ screen:expect{grid=injection_grid_c}
exec_lua [[
local parser = vim.treesitter.get_parser(0, "c", {
- injections = {c = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @c)"}
+ injections = {c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'}
})
local highlighter = vim.treesitter.highlighter
test_hl = highlighter.new(parser, {queries = {c = hl_query}})
]]
- screen:expect{grid=[[
- {3:int} x = {5:INT_MAX}; |
- #define {5:READ_STRING}(x, y) ({3:char_u} *)read_string((x), ({3:size_t})(y))|
- #define foo {3:void} main() { \ |
- {4:return} {5:42}; \ |
- } |
- ^ |
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- {1:~ }|
- |
- ]]}
+ screen:expect{grid=injection_grid_expected_c}
+ end)
+
+ it("supports injecting by ft name in metadata['injection.language']", function()
+ insert(injection_text_c)
+
+ screen:expect{grid=injection_grid_c}
+
+ 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"))'}
+ })
+ local highlighter = vim.treesitter.highlighter
+ test_hl = highlighter.new(parser, {queries = {c = hl_query}})
+ ]]
+
+ screen:expect{grid=injection_grid_expected_c}
end)
it("supports overriding queries, like ", function()
insert([[
int x = INT_MAX;
- #define READ_STRING(x, y) (char_u *)read_string((x), (size_t)(y))
+ #define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
#define foo void main() { \
return 42; \
}
]])
exec_lua [[
- local injection_query = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @c)"
- require('vim.treesitter.query').set_query("c", "highlights", hl_query)
- require('vim.treesitter.query').set_query("c", "injections", injection_query)
+ local injection_query = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'
+ vim.treesitter.query.set("c", "highlights", hl_query)
+ vim.treesitter.query.set("c", "injections", injection_query)
vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, "c"))
]]
screen:expect{grid=[[
{3:int} x = {5:INT_MAX}; |
- #define {5:READ_STRING}(x, y) ({3:char_u} *)read_string((x), ({3:size_t})(y))|
+ #define {5:READ_STRING}(x, y) ({3:char} *)read_string((x), ({3:size_t})(y)) |
#define foo {3:void} main() { \ |
{4:return} {5:42}; \ |
} |
@@ -510,7 +533,7 @@ describe('treesitter highlighting', function()
end)
it("supports highlighting with custom highlight groups", function()
- insert(hl_text)
+ insert(hl_text_c)
exec_lua [[
local parser = vim.treesitter.get_parser(0, "c")
@@ -567,7 +590,7 @@ describe('treesitter highlighting', function()
it("supports highlighting with priority", function()
insert([[
int x = INT_MAX;
- #define READ_STRING(x, y) (char_u *)read_string((x), (size_t)(y))
+ #define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
#define foo void main() { \
return 42; \
}
@@ -575,12 +598,12 @@ describe('treesitter highlighting', function()
exec_lua [[
local parser = vim.treesitter.get_parser(0, "c")
- test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query..'\n((translation_unit) @Error (set! "priority" 101))\n'}})
+ test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query..'\n((translation_unit) @constant (#set! "priority" 101))\n'}})
]]
- -- expect everything to have Error highlight
+ -- expect everything to have Constant highlight
screen:expect{grid=[[
{12:int}{8: x = INT_MAX;} |
- {8:#define READ_STRING(x, y) (}{12:char_u}{8: *)read_string((x), (}{12:size_t}{8:)(y))}|
+ {8:#define READ_STRING(x, y) (}{12:char}{8: *)read_string((x), (}{12:size_t}{8:)(y))} |
{8:#define foo }{12:void}{8: main() { \} |
{8: }{12:return}{8: 42; \} |
{8: }} |
@@ -599,13 +622,13 @@ describe('treesitter highlighting', function()
|
]], attr_ids={
[1] = {bold = true, foreground = Screen.colors.Blue1};
- [8] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red};
+ [8] = {foreground = Screen.colors.Magenta1};
-- bold will not be overwritten at the moment
- [12] = {background = Screen.colors.Red, bold = true, foreground = Screen.colors.Grey100};
+ [12] = {bold = true, foreground = Screen.colors.Magenta1};
}}
eq({
- {capture='Error', metadata = { priority='101' }, lang='c' };
+ {capture='constant', metadata = { priority='101' }, lang='c' };
{capture='type', metadata = { }, lang='c' };
}, exec_lua [[ return vim.treesitter.get_captures_at_pos(0, 0, 2) ]])
end)
@@ -692,7 +715,7 @@ describe('treesitter highlighting', function()
end)
it("supports conceal attribute", function()
- insert(hl_text)
+ insert(hl_text_c)
-- conceal can be empty or a single cchar.
exec_lua [=[
@@ -753,3 +776,129 @@ describe('treesitter highlighting', function()
eq(nil, get_hl"@total.nonsense.but.a.lot.of.dots")
end)
end)
+
+describe('treesitter highlighting (help)', function()
+ local screen
+
+ bef