2021-01-03 06:38:12 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
|
|
|
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
|
2022-08-24 14:48:52 -07:00
|
|
|
local command = helpers.command
|
|
|
|
local meths = helpers.meths
|
|
|
|
local eq = helpers.eq
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
before_each(clear)
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
local hl_query_c = [[
|
2022-08-24 14:48:52 -07:00
|
|
|
(ERROR) @error
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
"if" @keyword
|
|
|
|
"else" @keyword
|
|
|
|
"for" @keyword
|
|
|
|
"return" @keyword
|
|
|
|
|
|
|
|
"const" @type
|
|
|
|
"static" @type
|
|
|
|
"struct" @type
|
|
|
|
"enum" @type
|
|
|
|
"extern" @type
|
|
|
|
|
2022-08-24 14:48:52 -07:00
|
|
|
; nonexistent specializer for string should fallback to string
|
|
|
|
(string_literal) @string.nonexistent_specializer
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
(number_literal) @number
|
|
|
|
(char_literal) @string
|
|
|
|
|
|
|
|
(type_identifier) @type
|
2022-08-24 14:48:52 -07:00
|
|
|
((type_identifier) @constant.builtin (#eq? @constant.builtin "LuaRef"))
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
(primitive_type) @type
|
|
|
|
(sized_type_specifier) @type
|
|
|
|
|
|
|
|
; Use lua regexes
|
2022-08-24 14:48:52 -07:00
|
|
|
((identifier) @function (#contains? @function "lua_"))
|
2021-01-03 06:38:12 -07:00
|
|
|
((identifier) @Constant (#lua-match? @Constant "^[A-Z_]+$"))
|
2022-08-24 14:48:52 -07:00
|
|
|
((identifier) @Normal (#vim-match? @Normal "^lstate$"))
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2022-08-24 14:48:52 -07:00
|
|
|
((binary_expression left: (identifier) @warning.left right: (identifier) @warning.right) (#eq? @warning.left @warning.right))
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
(comment) @comment
|
|
|
|
]]
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
local hl_text_c = [[
|
2021-01-03 06:38:12 -07:00
|
|
|
/// Schedule Lua callback on main loop's event queue
|
|
|
|
static int nlua_schedule(lua_State *const lstate)
|
|
|
|
{
|
|
|
|
if (lua_type(lstate, 1) != LUA_TFUNCTION
|
|
|
|
|| lstate != lstate) {
|
|
|
|
lua_pushliteral(lstate, "vim.schedule: expected function");
|
|
|
|
return lua_error(lstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaRef cb = nlua_ref(lstate, 1);
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, nlua_schedule_event,
|
|
|
|
1, (void *)(ptrdiff_t)cb);
|
|
|
|
return 0;
|
|
|
|
}]]
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
local test_text_c = [[
|
2021-01-03 06:38:12 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]]
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
describe('treesitter highlighting (C)', function()
|
2021-01-03 06:38:12 -07:00
|
|
|
local screen
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
screen = Screen.new(65, 18)
|
|
|
|
screen:attach()
|
|
|
|
screen:set_default_attr_ids {
|
|
|
|
[1] = {bold = true, foreground = Screen.colors.Blue1};
|
|
|
|
[2] = {foreground = Screen.colors.Blue1};
|
|
|
|
[3] = {bold = true, foreground = Screen.colors.SeaGreen4};
|
|
|
|
[4] = {bold = true, foreground = Screen.colors.Brown};
|
|
|
|
[5] = {foreground = Screen.colors.Magenta};
|
|
|
|
[6] = {foreground = Screen.colors.Red};
|
|
|
|
[7] = {bold = true, foreground = Screen.colors.SlateBlue};
|
|
|
|
[8] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red};
|
|
|
|
[9] = {foreground = Screen.colors.Magenta, background = Screen.colors.Red};
|
|
|
|
[10] = {foreground = Screen.colors.Red, background = Screen.colors.Red};
|
|
|
|
[11] = {foreground = Screen.colors.Cyan4};
|
|
|
|
}
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
exec_lua([[ hl_query = ... ]], hl_query_c)
|
2022-09-04 08:58:33 -07:00
|
|
|
command [[ hi link @error ErrorMsg ]]
|
2022-08-24 14:48:52 -07:00
|
|
|
command [[ hi link @warning WarningMsg ]]
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('is updated with edits', function()
|
2023-04-30 08:11:38 -07:00
|
|
|
insert(hl_text_c)
|
2021-01-03 06:38:12 -07:00
|
|
|
screen:expect{grid=[[
|
|
|
|
/// Schedule Lua callback on main loop's event queue |
|
|
|
|
static int nlua_schedule(lua_State *const lstate) |
|
|
|
|
{ |
|
|
|
|
if (lua_type(lstate, 1) != LUA_TFUNCTION |
|
|
|
|
|| lstate != lstate) { |
|
|
|
|
lua_pushliteral(lstate, "vim.schedule: expected function"); |
|
|
|
|
return lua_error(lstate); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
LuaRef cb = nlua_ref(lstate, 1); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, nlua_schedule_event, |
|
|
|
|
1, (void *)(ptrdiff_t)cb); |
|
|
|
|
return 0; |
|
|
|
|
^} |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
exec_lua [[
|
|
|
|
local parser = vim.treesitter.get_parser(0, "c")
|
|
|
|
local highlighter = vim.treesitter.highlighter
|
|
|
|
test_hl = highlighter.new(parser, {queries = {c = hl_query}})
|
|
|
|
]]
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{2:/// Schedule Lua callback on main loop's event queue} |
|
|
|
|
{3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) |
|
|
|
|
{ |
|
|
|
|
{4:if} ({11:lua_type}(lstate, {5:1}) != {5:LUA_TFUNCTION} |
|
|
|
|
|| {6:lstate} != {6:lstate}) { |
|
|
|
|
{11:lua_pushliteral}(lstate, {5:"vim.schedule: expected function"}); |
|
|
|
|
{4:return} {11:lua_error}(lstate); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, {11:nlua_schedule_event}, |
|
|
|
|
{5:1}, ({3:void} *)({3:ptrdiff_t})cb); |
|
|
|
|
{4:return} {5:0}; |
|
|
|
|
^} |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
feed("5Goc<esc>dd")
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{2:/// Schedule Lua callback on main loop's event queue} |
|
|
|
|
{3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) |
|
|
|
|
{ |
|
|
|
|
{4:if} ({11:lua_type}(lstate, {5:1}) != {5:LUA_TFUNCTION} |
|
|
|
|
|| {6:lstate} != {6:lstate}) { |
|
|
|
|
{11:^lua_pushliteral}(lstate, {5:"vim.schedule: expected function"}); |
|
|
|
|
{4:return} {11:lua_error}(lstate); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, {11:nlua_schedule_event}, |
|
|
|
|
{5:1}, ({3:void} *)({3:ptrdiff_t})cb); |
|
|
|
|
{4:return} {5:0}; |
|
|
|
|
} |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
feed('7Go*/<esc>')
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{2:/// Schedule Lua callback on main loop's event queue} |
|
|
|
|
{3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) |
|
|
|
|
{ |
|
|
|
|
{4:if} ({11:lua_type}(lstate, {5:1}) != {5:LUA_TFUNCTION} |
|
|
|
|
|| {6:lstate} != {6:lstate}) { |
|
|
|
|
{11:lua_pushliteral}(lstate, {5:"vim.schedule: expected function"}); |
|
|
|
|
{4:return} {11:lua_error}(lstate); |
|
|
|
|
{8:*^/} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, {11:nlua_schedule_event}, |
|
|
|
|
{5:1}, ({3:void} *)({3:ptrdiff_t})cb); |
|
|
|
|
{4:return} {5:0}; |
|
|
|
|
} |
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
feed('3Go/*<esc>')
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{2:/// Schedule Lua callback on main loop's event queue} |
|
|
|
|
{3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) |
|
|
|
|
{ |
|
|
|
|
{2:/^*} |
|
|
|
|
{2: if (lua_type(lstate, 1) != LUA_TFUNCTION} |
|
|
|
|
{2: || lstate != lstate) {} |
|
|
|
|
{2: lua_pushliteral(lstate, "vim.schedule: expected function");} |
|
|
|
|
{2: return lua_error(lstate);} |
|
|
|
|
{2:*/} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, {11:nlua_schedule_event}, |
|
|
|
|
{5:1}, ({3:void} *)({3:ptrdiff_t})cb); |
|
|
|
|
{4:return} {5:0}; |
|
|
|
|
{8:}} |
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
feed("gg$")
|
|
|
|
feed("~")
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{2:/// Schedule Lua callback on main loop's event queu^E} |
|
|
|
|
{3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) |
|
|
|
|
{ |
|
|
|
|
{2:/*} |
|
|
|
|
{2: if (lua_type(lstate, 1) != LUA_TFUNCTION} |
|
|
|
|
{2: || lstate != lstate) {} |
|
|
|
|
{2: lua_pushliteral(lstate, "vim.schedule: expected function");} |
|
|
|
|
{2: return lua_error(lstate);} |
|
|
|
|
{2:*/} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, {11:nlua_schedule_event}, |
|
|
|
|
{5:1}, ({3:void} *)({3:ptrdiff_t})cb); |
|
|
|
|
{4:return} {5:0}; |
|
|
|
|
{8:}} |
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
|
|
|
|
feed("re")
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{2:/// Schedule Lua callback on main loop's event queu^e} |
|
|
|
|
{3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) |
|
|
|
|
{ |
|
|
|
|
{2:/*} |
|
|
|
|
{2: if (lua_type(lstate, 1) != LUA_TFUNCTION} |
|
|
|
|
{2: || lstate != lstate) {} |
|
|
|
|
{2: lua_pushliteral(lstate, "vim.schedule: expected function");} |
|
|
|
|
{2: return lua_error(lstate);} |
|
|
|
|
{2:*/} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, {11:nlua_schedule_event}, |
|
|
|
|
{5:1}, ({3:void} *)({3:ptrdiff_t})cb); |
|
|
|
|
{4:return} {5:0}; |
|
|
|
|
{8:}} |
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
end)
|
|
|
|
|
2021-01-02 03:05:30 -07:00
|
|
|
it('is updated with :sort', function()
|
2023-04-30 08:11:38 -07:00
|
|
|
insert(test_text_c)
|
2021-01-02 03:05:30 -07:00
|
|
|
exec_lua [[
|
|
|
|
local parser = vim.treesitter.get_parser(0, "c")
|
|
|
|
test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query}})
|
|
|
|
]]
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{3:int} width = {5:INT_MAX}, height = {5:INT_MAX}; |
|
|
|
|
{3:bool} ext_widgets[kUIExtCount]; |
|
|
|
|
{4:for} ({3:UIExtension} i = {5:0}; ({3:int})i < kUIExtCount; i++) { |
|
|
|
|
ext_widgets[i] = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{3:bool} inclusive = ui_override(); |
|
|
|
|
{4:for} ({3:size_t} i = {5:0}; i < ui_count; i++) { |
|
|
|
|
{3:UI} *ui = uis[i]; |
|
|
|
|
width = {5:MIN}(ui->width, width); |
|
|
|
|
height = {5:MIN}(ui->height, height); |
|
|
|
|
foo = {5:BAR}(ui->bazaar, bazaar); |
|
|
|
|
{4:for} ({3:UIExtension} j = {5:0}; ({3:int})j < kUIExtCount; j++) { |
|
|
|
|
ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
^} |
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
feed ":sort<cr>"
|
|
|
|
screen:expect{grid=[[
|
|
|
|
^ |
|
|
|
|
ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
|
|
|
|
{3:UI} *ui = uis[i]; |
|
|
|
|
ext_widgets[i] = true; |
|
|
|
|
foo = {5:BAR}(ui->bazaar, bazaar); |
|
|
|
|
{4:for} ({3:UIExtension} j = {5:0}; ({3:int})j < kUIExtCount; j++) { |
|
|
|
|
height = {5:MIN}(ui->height, height); |
|
|
|
|
width = {5:MIN}(ui->width, width); |
|
|
|
|
} |
|
|
|
|
{3:bool} ext_widgets[kUIExtCount]; |
|
|
|
|
{3:bool} inclusive = ui_override(); |
|
|
|
|
{4:for} ({3:UIExtension} i = {5:0}; ({3:int})i < kUIExtCount; i++) { |
|
|
|
|
{4:for} ({3:size_t} i = {5:0}; i < ui_count; i++) { |
|
|
|
|
{3:int} width = {5:INT_MAX}, height = {5:INT_MAX}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
{3:void} ui_refresh({3:void}) |
|
|
|
|
:sort |
|
|
|
|
]]}
|
|
|
|
|
|
|
|
feed "u"
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{3:int} width = {5:INT_MAX}, height = {5:INT_MAX}; |
|
|
|
|
{3:bool} ext_widgets[kUIExtCount]; |
|
|
|
|
{4:for} ({3:UIExtension} i = {5:0}; ({3:int})i < kUIExtCount; i++) { |
|
|
|
|
ext_widgets[i] = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{3:bool} inclusive = ui_override(); |
|
|
|
|
{4:for} ({3:size_t} i = {5:0}; i < ui_count; i++) { |
|
|
|
|
{3:UI} *ui = uis[i]; |
|
|
|
|
width = {5:MIN}(ui->width, width); |
|
|
|
|
height = {5:MIN}(ui->height, height); |
|
|
|
|
foo = {5:BAR}(ui->bazaar, bazaar); |
|
|
|
|
{4:for} ({3:UIExtension} j = {5:0}; ({3:int})j < kUIExtCount; j++) { |
|
|
|
|
ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
^} |
|
|
|
|
19 changes; before #2 {MATCH:.*}|
|
|
|
|
]]}
|
|
|
|
end)
|
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
it("supports with custom parser", function()
|
|
|
|
screen:set_default_attr_ids {
|
|
|
|
[1] = {bold = true, foreground = Screen.colors.SeaGreen4};
|
|
|
|
}
|
|
|
|
|
2023-04-30 08:11:38 -07:00
|
|
|
insert(test_text_c)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
screen:expect{ grid= [[
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
^} |
|
|
|
|
|
|
|
|
|
]] }
|
|
|
|
|
|
|
|
exec_lua [[
|
|
|
|
parser = vim.treesitter.get_parser(0, "c")
|
2023-03-24 07:43:14 -07:00
|
|
|
query = vim.treesitter.query.parse("c", "(declaration) @decl")
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
local nodes = {}
|
|
|
|
for _, node in query:iter_captures(parser:parse()[1]:root(), 0, 0, 19) do
|
|
|
|
table.insert(nodes, node)
|
|
|
|
end
|
|
|
|
|
|
|
|
parser:set_included_regions({nodes})
|
|
|
|
|
|
|
|
local hl = vim.treesitter.highlighter.new(parser, {queries = {c = "(identifier) @type"}})
|
|
|
|
]]
|
|
|
|
|
|
|
|
screen:expect{ grid = [[
|
|
|
|
int {1:width} = {1:INT_MAX}, {1:height} = {1:INT_MAX}; |
|
|
|
|
bool {1:ext_widgets}[{1:kUIExtCount}]; |
|
|
|
|
for (UIExtension {1:i} = 0; (int)i < kUIExtCount; i++) { |
|
|
|
|
ext_widgets[i] = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool {1:inclusive} = {1:ui_override}(); |
|
|
|
|
for (size_t {1:i} = 0; i < ui_count; i++) { |
|
|
|
|
UI *{1:ui} = {1:uis}[{1:i}]; |
|
|
|
|
width = MIN(ui->width, width); |
|
|
|
|
height = MIN(ui->height, height); |
|
|
|
|
foo = BAR(ui->bazaar, bazaar); |
|
|
|
|
for (UIExtension {1:j} = 0; (int)j < kUIExtCount; j++) { |
|
|
|
|
ext_widgets[j] &= (ui->ui_ext[j] || inclusive); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
^} |
|
|
|
|
|
|
|
|
|
]] }
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("supports injected languages", function()
|
|
|
|
insert([[
|
|
|
|
int x = INT_MAX;
|
2023-04-02 01:11:42 -07:00
|
|
|
#define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
2021-01-03 06:38:12 -07:00
|
|
|
#define foo void main() { \
|
|
|
|
return 42; \
|
|
|
|
}
|
|
|
|
]])
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
int x = INT_MAX; |
|
2023-04-02 01:11:42 -07:00
|
|
|
#define READ_STRING(x, y) (char *)read_string((x), (size_t)(y)) |
|
2021-01-03 06:38:12 -07:00
|
|
|
#define foo void main() { \ |
|
|
|
|
return 42; \ |
|
|
|
|
} |
|
|
|
|
^ |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
exec_lua [[
|
|
|
|
local parser = vim.treesitter.get_parser(0, "c", {
|
2021-03-02 12:51:08 -07:00
|
|
|
injections = {c = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @c)"}
|
2021-01-03 06:38:12 -07:00
|
|
|
})
|
|
|
|
local highlighter = vim.treesitter.highlighter
|
|
|
|
test_hl = highlighter.new(parser, {queries = {c = hl_query}})
|
|
|
|
]]
|
|
|
|
|
2021-03-30 07:57:38 -07:00
|
|
|
screen:expect{grid=[[
|
|
|
|
{3:int} x = {5:INT_MAX}; |
|
2023-04-02 01:11:42 -07:00
|
|
|
#define {5:READ_STRING}(x, y) ({3:char} *)read_string((x), ({3:size_t})(y)) |
|
2021-03-30 07:57:38 -07:00
|
|
|
#define foo {3:void} main() { \ |
|
|
|
|
{4:return} {5:42}; \ |
|
|
|
|
} |
|
|
|
|
^ |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("supports overriding queries, like ", function()
|
|
|
|
insert([[
|
|
|
|
int x = INT_MAX;
|
2023-04-02 01:11:42 -07:00
|
|
|
#define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
2021-03-30 07:57:38 -07:00
|
|
|
#define foo void main() { \
|
|
|
|
return 42; \
|
|
|
|
}
|
|
|
|
]])
|
|
|
|
|
|
|
|
exec_lua [[
|
|
|
|
local injection_query = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @c)"
|
2023-03-24 07:43:14 -07:00
|
|
|
vim.treesitter.query.set("c", "highlights", hl_query)
|
|
|
|
vim.treesitter.query.set("c", "injections", injection_query)
|
2021-03-30 07:57:38 -07:00
|
|
|
|
|
|
|
vim.treesitter.highlighter.new(vim.treesitter.get_parser(0, "c"))
|
|
|
|
]]
|
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
screen:expect{grid=[[
|
|
|
|
{3:int} x = {5:INT_MAX}; |
|
2023-04-02 01:11:42 -07:00
|
|
|
#define {5:READ_STRING}(x, y) ({3:char} *)read_string((x), ({3:size_t})(y)) |
|
2021-01-03 06:38:12 -07:00
|
|
|
#define foo {3:void} main() { \ |
|
|
|
|
{4:return} {5:42}; \ |
|
|
|
|
} |
|
|
|
|
^ |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
end)
|
2021-03-29 13:07:21 -07:00
|
|
|
|
|
|
|
it("supports highlighting with custom highlight groups", function()
|
2023-04-30 08:11:38 -07:00
|
|
|
insert(hl_text_c)
|
2021-03-29 13:07:21 -07:00
|
|
|
|
|
|
|
exec_lua [[
|
|
|
|
local parser = vim.treesitter.get_parser(0, "c")
|
|
|
|
test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query}})
|
|
|
|
]]
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{2:/// Schedule Lua callback on main loop's event queue} |
|
|
|
|
{3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) |
|
|
|
|
{ |
|
|
|
|
{4:if} ({11:lua_type}(lstate, {5:1}) != {5:LUA_TFUNCTION} |
|
|
|
|
|| {6:lstate} != {6:lstate}) { |
|
|
|
|
{11:lua_pushliteral}(lstate, {5:"vim.schedule: expected function"}); |
|
|
|
|
{4:return} {11:lua_error}(lstate); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, {11:nlua_schedule_event}, |
|
|
|
|
{5:1}, ({3:void} *)({3:ptrdiff_t})cb); |
|
|
|
|
{4:return} {5:0}; |
|
|
|
|
^} |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
-- This will change ONLY the literal strings to look like comments
|
|
|
|
-- The only literal string is the "vim.schedule: expected function" in this test.
|
2022-08-24 14:48:52 -07:00
|
|
|
exec_lua [[vim.cmd("highlight link @string.nonexistent_specializer comment")]]
|
2021-03-29 13:07:21 -07:00
|
|
|
screen:expect{grid=[[
|
|
|
|
{2:/// Schedule Lua callback on main loop's event queue} |
|
|
|
|
{3:static} {3:int} {11:nlua_schedule}({3:lua_State} *{3:const} lstate) |
|
|
|
|
{ |
|
|
|
|
{4:if} ({11:lua_type}(lstate, {5:1}) != {5:LUA_TFUNCTION} |
|
|
|
|
|| {6:lstate} != {6:lstate}) { |
|
|
|
|
{11:lua_pushliteral}(lstate, {2:"vim.schedule: expected function"}); |
|
|
|
|
{4:return} {11:lua_error}(lstate); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{7:LuaRef} cb = {11:nlua_ref}(lstate, {5:1}); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, {11:nlua_schedule_event}, |
|
|
|
|
{5:1}, ({3:void} *)({3:ptrdiff_t})cb); |
|
|
|
|
{4:return} {5:0}; |
|
|
|
|
^} |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
screen:expect{ unchanged=true }
|
|
|
|
end)
|
2021-07-17 05:41:17 -07:00
|
|
|
|
|
|
|
it("supports highlighting with priority", function()
|
|
|
|
insert([[
|
|
|
|
int x = INT_MAX;
|
2023-04-02 01:11:42 -07:00
|
|
|
#define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
2021-07-17 05:41:17 -07:00
|
|
|
#define foo void main() { \
|
|
|
|
return 42; \
|
|
|
|
}
|
|
|
|
]])
|
|
|
|
|
|
|
|
exec_lua [[
|
|
|
|
local parser = vim.treesitter.get_parser(0, "c")
|
2023-04-01 03:11:24 -07:00
|
|
|
test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = hl_query..'\n((translation_unit) @constant (#set! "priority" 101))\n'}})
|
2021-07-17 05:41:17 -07:00
|
|
|
]]
|
2023-04-01 03:11:24 -07:00
|
|
|
-- expect everything to have Constant highlight
|
2021-07-17 05:41:17 -07:00
|
|
|
screen:expect{grid=[[
|
|
|
|
{12:int}{8: x = INT_MAX;} |
|
2023-08-12 09:12:49 -07:00
|
|
|
{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; \} |
|
2021-07-17 05:41:17 -07:00
|
|
|
{8: }} |
|
|
|
|
^ |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]], attr_ids={
|
|
|
|
[1] = {bold = true, foreground = Screen.colors.Blue1};
|
2023-04-01 03:11:24 -07:00
|
|
|
[8] = {foreground = Screen.colors.Magenta1};
|
2021-07-17 05:41:17 -07:00
|
|
|
-- bold will not be overwritten at the moment
|
2023-04-01 03:11:24 -07:00
|
|
|
[12] = {bold = true, foreground = Screen.colors.Magenta1};
|
2021-07-17 05:41:17 -07:00
|
|
|
}}
|
2022-08-25 12:41:52 -07:00
|
|
|
|
|
|
|
eq({
|
2023-04-01 03:11:24 -07:00
|
|
|
{capture='constant', metadata = { priority='101' }, lang='c' };
|
2022-12-14 02:46:54 -07:00
|
|
|
{capture='type', metadata = { }, lang='c' };
|
2022-09-24 15:45:15 -07:00
|
|
|
}, exec_lua [[ return vim.treesitter.get_captures_at_pos(0, 0, 2) ]])
|
2021-07-17 05:41:17 -07:00
|
|
|
end)
|
2022-02-16 11:38:19 -07:00
|
|
|
|
|
|
|
it("allows to use captures with dots (don't use fallback when specialization of foo exists)", function()
|
|
|
|
insert([[
|
|
|
|
char* x = "Will somebody ever read this?";
|
|
|
|
]])
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
char* x = "Will somebody ever read this?"; |
|
|
|
|
^ |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
2022-08-24 14:48:52 -07:00
|
|
|
command [[
|
|
|
|
hi link @foo.bar Type
|
|
|
|
hi link @foo String
|
|
|
|
]]
|
2022-02-16 11:38:19 -07:00
|
|
|
exec_lua [[
|
|
|
|
local parser = vim.treesitter.get_parser(0, "c", {})
|
|
|
|
local highlighter = vim.treesitter.highlighter
|
|
|
|
test_hl = highlighter.new(parser, {queries = {c = "(primitive_type) @foo.bar (string_literal) @foo"}})
|
|
|
|
]]
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{3:char}* x = {5:"Will somebody ever read this?"}; |
|
|
|
|
^ |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
2022-03-19 05:48:03 -07:00
|
|
|
]]}
|
2022-08-24 14:48:52 -07:00
|
|
|
|
|
|
|
-- clearing specialization reactivates fallback
|
|
|
|
command [[ hi clear @foo.bar ]]
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{5:char}* x = {5:"Will somebody ever read this?"}; |
|
|
|
|
^ |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
]]}
|
2022-03-19 05:48:03 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("supports conceal attribute", function()
|
2023-04-30 08:11:38 -07:00
|
|
|
insert(hl_text_c)
|
2022-03-19 05:48:03 -07:00
|
|
|
|
|
|
|
-- conceal can be empty or a single cchar.
|
|
|
|
exec_lua [=[
|
|
|
|
vim.opt.cole = 2
|
|
|
|
local parser = vim.treesitter.get_parser(0, "c")
|
|
|
|
test_hl = vim.treesitter.highlighter.new(parser, {queries = {c = [[
|
|
|
|
("static" @keyword
|
|
|
|
(set! conceal "R"))
|
|
|
|
|
|
|
|
((identifier) @Identifier
|
|
|
|
(set! conceal "")
|
|
|
|
(eq? @Identifier "lstate"))
|
|
|
|
]]}})
|
|
|
|
]=]
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
/// Schedule Lua callback on main loop's event queue |
|
|
|
|
{4:R} int nlua_schedule(lua_State *const ) |
|
|
|
|
{ |
|
|
|
|
if (lua_type(, 1) != LUA_TFUNCTION |
|
|
|
|
|| != ) { |
|
|
|
|
lua_pushliteral(, "vim.schedule: expected function"); |
|
|
|
|
return lua_error(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
LuaRef cb = nlua_ref(, 1); |
|
|
|
|
|
|
|
|
|
multiqueue_put(main_loop.events, nlua_schedule_event, |
|
|
|
|
1, (void *)(ptrdiff_t)cb); |
|
|
|
|
return 0; |
|
|
|
|
^} |
|
|
|
|
{1:~ }|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
2022-02-16 11:38:19 -07:00
|
|
|
]]}
|
|
|
|
end)
|
|
|
|
|
2022-08-24 14:48:52 -07:00
|
|
|
it("@foo.bar groups has the correct fallback behavior", function()
|
|
|
|
local get_hl = function(name) return meths.get_hl_by_name(name,1).foreground end
|
|
|
|
meths.set_hl(0, "@foo", {fg = 1})
|
|
|
|
meths.set_hl(0, "@foo.bar", {fg = 2})
|
|
|
|
meths.set_hl(0, "@foo.bar.baz", {fg = 3})
|
|
|
|
|
|
|
|
eq(1, get_hl"@foo")
|
|
|
|
eq(1, get_hl"@foo.a.b.c.d")
|
|
|
|
eq(2, get_hl"@foo.bar")
|
|
|
|
eq(2, get_hl"@foo.bar.a.b.c.d")
|
|
|
|
eq(3, get_hl"@foo.bar.baz")
|
|
|
|
eq(3, get_hl"@foo.bar.baz.d")
|
|
|
|
|
|
|
|
-- lookup is case insensitive
|
|
|
|
eq(2, get_hl"@FOO.BAR.SPAM")
|
|
|
|
|
|
|
|
meths.set_hl(0, "@foo.missing.exists", {fg = 3})
|
|
|
|
eq(1, get_hl"@foo.missing")
|
|
|
|
eq(3, get_hl"@foo.missing.exists")
|
|
|
|
eq(3, get_hl"@foo.missing.exists.bar")
|
|
|
|
eq(nil, get_hl"@total.nonsense.but.a.lot.of.dots")
|
2022-02-16 11:38:19 -07:00
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
2023-04-30 08:11:38 -07:00
|
|
|
|
|
|
|
describe('treesitter highlighting (help)', 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("correctly redraws added/removed injections", function()
|
|
|
|
insert[[
|
|
|
|
>ruby
|
|
|
|
-- comment
|
|
|
|
local this_is = 'actually_lua'
|
|
|
|
<
|
|
|
|
]]
|
|
|
|
|
|
|
|
exec_lua [[
|
|
|
|
vim.bo.filetype = 'help'
|
|
|
|
vim.treesitter.start()
|
|
|
|
]]
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{1:>ruby} |
|
|
|
|
{1: -- comment} |
|
|
|
|
{1: local this_is = 'actually_lua'} |
|
|
|
|
< |
|
|
|
|
^ |
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
helpers.curbufmeths.set_text(0, 1, 0, 5, {'lua'})
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{1:>lua} |
|
|
|
|
{1: -- comment} |
|
|
|
|
{1: }{3:local}{1: }{4:this_is}{1: }{3:=}{1: }{5:'actually_lua'} |
|
|
|
|
< |
|
|
|
|
^ |
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
|
|
|
|
helpers.curbufmeths.set_text(0, 1, 0, 4, {'ruby'})
|
|
|
|
|
|
|
|
screen:expect{grid=[[
|
|
|
|
{1:>ruby} |
|
|
|
|
{1: -- comment} |
|
|
|
|
{1: local this_is = 'actually_lua'} |
|
|
|
|
< |
|
|
|
|
^ |
|
|
|
|
|
|
|
|
|
]]}
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|