refactor: use function in exec_lua

This commit is contained in:
vanaigr 2024-12-18 11:56:38 -06:00
parent fab5cbad00
commit b037af3f2c
3 changed files with 45 additions and 52 deletions

View File

@ -8,7 +8,7 @@ describe('decor perf', function()
it('can handle long lines', function() it('can handle long lines', function()
Screen.new(100, 101) Screen.new(100, 101)
local result = exec_lua [==[ local result = exec_lua(function()
local ephemeral_pattern = { local ephemeral_pattern = {
{ 0, 4, 'Comment', 11 }, { 0, 4, 'Comment', 11 },
{ 0, 3, 'Keyword', 12 }, { 0, 3, 'Keyword', 12 },
@ -61,7 +61,7 @@ describe('decor perf', function()
return true return true
end, end,
on_line = function() on_line = function()
add_pattern(ephemeral_pattern, true) add_pattern(ephemeral_pattern, true)
end, end,
}) })
@ -69,16 +69,16 @@ describe('decor perf', function()
local total = {} local total = {}
local provider = {} local provider = {}
for i = 1, 100 do for _ = 1, 100 do
local tic = vim.uv.hrtime() local tic = vim.uv.hrtime()
vim.cmd'redraw!' vim.cmd 'redraw!'
local toc = vim.uv.hrtime() local toc = vim.uv.hrtime()
table.insert(total, toc - tic) table.insert(total, toc - tic)
table.insert(provider, pe - ps) table.insert(provider, pe - ps)
end end
return { total, provider } return { total, provider }
]==] end)
local total, provider = unpack(result) local total, provider = unpack(result)
table.sort(total) table.sort(total)
@ -102,22 +102,22 @@ describe('decor perf', function()
it('can handle long lines with treesitter highlighting', function() it('can handle long lines with treesitter highlighting', function()
Screen.new(100, 51) Screen.new(100, 51)
local result = exec_lua [==[ local result = exec_lua(function()
local long_line = 'local a = { ' .. ('a = 5, '):rep(2000) .. '}' local long_line = 'local a = { ' .. ('a = 5, '):rep(2000) .. '}'
vim.api.nvim_buf_set_lines(0, 0, 0, false, { long_line }) vim.api.nvim_buf_set_lines(0, 0, 0, false, { long_line })
vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.api.nvim_win_set_cursor(0, { 1, 0 })
vim.treesitter.start(0, 'lua') vim.treesitter.start(0, 'lua')
local total = {} local total = {}
for i = 1, 50 do for _ = 1, 50 do
local tic = vim.uv.hrtime() local tic = vim.uv.hrtime()
vim.cmd'redraw!' vim.cmd 'redraw!'
local toc = vim.uv.hrtime() local toc = vim.uv.hrtime()
table.insert(total, toc - tic) table.insert(total, toc - tic)
end end
return { total } return { total }
]==] end)
local total = unpack(result) local total = unpack(result)
table.sort(total) table.sort(total)

View File

@ -49,33 +49,28 @@ describe('treesitter perf', function()
]] ]]
end) end)
local function test_long_line(pos, wrap, line, grid) local function test_long_line(_pos, _wrap, _line, grid)
local screen = Screen.new(20, 11) local screen = Screen.new(20, 11)
local result = exec_lua( local result = exec_lua(function(...)
[==[ local pos, wrap, line = ...
local pos, wrap, line = ...
vim.api.nvim_buf_set_lines(0, 0, 0, false, { line }) vim.api.nvim_buf_set_lines(0, 0, 0, false, { line })
vim.api.nvim_win_set_cursor(0, pos) vim.api.nvim_win_set_cursor(0, pos)
vim.api.nvim_set_option_value('wrap', wrap, { win = 0 }) vim.api.nvim_set_option_value('wrap', wrap, { win = 0 })
vim.treesitter.start(0, 'lua') vim.treesitter.start(0, 'lua')
local total = {} local total = {}
for i = 1, 100 do for _ = 1, 100 do
local tic = vim.uv.hrtime() local tic = vim.uv.hrtime()
vim.cmd'redraw!' vim.cmd 'redraw!'
local toc = vim.uv.hrtime() local toc = vim.uv.hrtime()
table.insert(total, toc - tic) table.insert(total, toc - tic)
end end
return { total } return { total }
]==], end, _pos, _wrap, _line)
pos,
wrap,
line
)
screen:expect({ grid = grid or '' }) screen:expect({ grid = grid or '' })

View File

@ -716,46 +716,44 @@ describe('decorations providers', function()
setup_screen(screen) setup_screen(screen)
local function record() local function record()
exec_lua [[ exec_lua(function()
p_min = { math.huge, math.huge } _G.p_min = { math.huge, math.huge }
p_max = { -math.huge, -math.huge } _G.p_max = { -math.huge, -math.huge }
function pos_gt(a, b) function _G.pos_gt(a, b)
return a[1] > b[1] or (a[1] == b[1] and a[2] > b[2]) return a[1] > b[1] or (a[1] == b[1] and a[2] > b[2])
end end
function pos_lt(a, b) function _G.pos_lt(a, b)
return a[1] < b[1] or (a[1] == b[1] and a[2] < b[2]) return a[1] < b[1] or (a[1] == b[1] and a[2] < b[2])
end end
]] end)
setup_provider [[ setup_provider [[
local function on_do(kind, _, bufnr, br, bc, er, ec) local function on_do(kind, _, bufnr, br, bc, er, ec)
if kind == 'range' then if kind == 'range' then
local b = { br, bc } local b = { br, bc }
local e = { er, ec } local e = { er, ec }
if pos_gt(p_min, b) then if _G.pos_gt(_G.p_min, b) then
p_min = b _G.p_min = b
end end
if pos_lt(p_max, e) then if _G.pos_lt(_G.p_max, e) then
p_max = e _G.p_max = e
end end
end end
end end
]] ]]
end end
local function check(min, max) local function check(min, max)
local function pos_gt(a, b) local p_min = exec_lua('return _G.p_min')
return a[1] > b[1] or (a[1] == b[1] and a[2] > b[2]) assert(
end p_min[1] < min[1] or (p_min[1] == min[1] and p_min[2] <= min[2]),
local function pos_lt(a, b)
return a[1] < b[1] or (a[1] == b[1] and a[2] < b[2])
end
local p_min = exec_lua('return p_min')
assert(not pos_gt(p_min, min),
"minimum position " .. vim.inspect(p_min) "minimum position " .. vim.inspect(p_min)
.. " should be before the first char") .. " should be before the first char"
local p_max = exec_lua('return p_max') )
assert(not pos_lt(p_max, max), local p_max = exec_lua('return _G.p_max')
assert(
p_max[1] > max[1] or (p_max[1] == max[1] and p_max[2] >= max[2]),
"maximum position " .. vim.inspect(p_max) "maximum position " .. vim.inspect(p_max)
.. " should be on or after the last char") .. " should be on or after the last char"
)
end end
-- Multiple lines. -- Multiple lines.