local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
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 fn = n.fn
local eq = t.eq
local hl_query_c = [[
; query
(ERROR) @error
"if" @keyword
"else" @keyword
"for" @keyword
"return" @keyword
"const" @type
"static" @type
"struct" @type
"enum" @type
"extern" @type
; nonexistent specializer for string should fallback to string
(string_literal) @string.nonexistent_specializer
(number_literal) @number
(char_literal) @string
(type_identifier) @type
((type_identifier) @constant.builtin (#eq? @constant.builtin "LuaRef"))
(primitive_type) @type
(sized_type_specifier) @type
; Use lua regexes
((identifier) @function (#contains? @function "lua_"))
((identifier) @Constant (#lua-match? @Constant "^[A-Z_]+$"))
((identifier) @Normal (#vim-match? @Normal "^lstate$"))
((binary_expression left: (identifier) @warning.left right: (identifier) @warning.right) (#eq? @warning.left @warning.right))
(comment) @comment
]]
local hl_text_c = [[
/// 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;
}]]
local hl_grid_legacy_c = [[
{18:^/// Schedule Lua callback on main loop's event queue} |
{6:static} {6:int} nlua_schedule(lua_State *{6:const} lstate) |
{ |
{15:if} (lua_type(lstate, {26:1}) != LUA_TFUNCTION |
|| lstate != lstate) { |
lua_pushliteral(lstate, {26:"vim.schedule: expected function"}); |
{15:return} lua_error(lstate); |
} |
|
LuaRef cb = nlua_ref(lstate, {26:1}); |
|
multiqueue_put(main_loop.events, nlua_schedule_event, |
{26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
{15:return} {26:0}; |
} |
{1:~ }|*2
|
]]
local hl_grid_ts_c = [[
{18:^/// Schedule Lua callback on main loop's event queue} |
{6:static} {6:int} {25:nlua_schedule}({6:lua_State} *{6:const} lstate) |
{ |
{15:if} ({25:lua_type}(lstate, {26:1}) != {26:LUA_TFUNCTION} |
|| {19:lstate} != {19:lstate}) { |
{25:lua_pushliteral}(lstate, {26:"vim.schedule: expected function"}); |
{15:return} {25:lua_error}(lstate); |
} |
|
{29:LuaRef} cb = {25:nlua_ref}(lstate, {26:1}); |
|
multiqueue_put(main_loop.events, {25:nlua_schedule_event}, |
{26:1}, ({6:void} *)({6:ptrdiff_t})cb); |
{15:return} {26:0}; |
} |
{1:~ }|*2
|
]]
local test_text_c = [[
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 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:~ }|*11
|
]]
local injection_grid_expected_c = [[
{6:int} x = {26:INT_MAX}; |
#define {26:READ_STRING}(x, y) ({6:char} *)read_string((x), ({6:size_t})(y)) |
#define foo {6:void} main() { \ |
{15:return} {26:42}; \ |
} |
^ |
{1:~ }|*11
|
]]
describe('treesitter highlighting (C)', function()
local screen --- @type test.functional.ui.screen
before_each(function()
clear()
screen = Screen.new(65, 18)
command [[ hi link @error ErrorMsg ]]
command [[ hi link @warning WarningMsg ]]
end)
it('starting and stopping treesitter highlight works', function()
command('setfiletype c | syntax on')
fn.setreg('r', hl_text_c)
feed('i<C-R><C-O>r<Esc>gg')
-- legacy syntax highlighting is used by default
screen:expect(hl_grid_legacy_c)
exec_lua(function()
vim.treesitter.query.set('c', 'highlights', hl_query_c)
vim.treesitter.start()
end)
-- treesitter highlighting is used
screen:expect(hl_grid_ts_c)
exec_lua(function()
vim.treesitter.stop()
end)
-- legacy syntax highlighting is used
screen:expect(hl_grid_legacy_c)
exec_lua(function()
vim.treesitter.start
|