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

View File

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

View File

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