Something went wrong. Try again.
Library of 45+ independent Lua modules improving Neovim experience with minimal effort nvim-mini.org/mini.nvim
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333local Helpers = {}
-- Add extra expectationsHelpers.expect = vim.deepcopy(MiniTest.expect)
Helpers.expect.match = MiniTest.new_expectation( 'string matching', function(str, pattern) return str:find(pattern) ~= nil end, function(str, pattern) return string.format('Pattern: %s\nObserved string: %s', vim.inspect(pattern), str) end)
Helpers.expect.no_match = MiniTest.new_expectation( 'no string matching', function(str, pattern) return str:find(pattern) == nil end, function(str, pattern) return string.format('Pattern: %s\nObserved string: %s', vim.inspect(pattern), str) end)
Helpers.expect.equality_approx = MiniTest.new_expectation( 'approximate equality', function(x, y, tol) if type(x) ~= type(y) then return false end if type(x) == 'number' then return math.abs(x - y) <= tol end if type(x) ~= 'table' then return vim.deep_equal(x, y) end
local x_keys, y_keys = vim.tbl_keys(x), vim.tbl_keys(y) table.sort(x_keys) table.sort(y_keys) if not vim.deep_equal(x_keys, y_keys) then return false end for _, key in ipairs(x_keys) do if math.abs(x[key] - y[key]) > tol then return false end end
return true end, function(x, y, tol) return string.format('Left: %s\nRight: %s\nTolerance: %s', vim.inspect(x), vim.inspect(y), tol) end)
Helpers.make_partial_tbl = function(tbl, ref) local res = {} for k, v in pairs(ref) do res[k] = (type(tbl[k]) == 'table' and type(v) == 'table') and Helpers.make_partial_tbl(tbl[k], v) or tbl[k] end for i = 1, #tbl do if ref[i] == nil then res[i] = tbl[i] end end return resend
Helpers.expect.equality_partial_tbl = MiniTest.new_expectation( 'equality of tables only in reference fields', function(x, y) if type(x) == 'table' and type(y) == 'table' then x = Helpers.make_partial_tbl(x, y, {}) end return vim.deep_equal(x, y) end, function(x, y) return string.format('Left: %s\nRight: %s', vim.inspect(Helpers.make_partial_tbl(x, y, {})), vim.inspect(y)) end)
-- Monkey-patch `MiniTest.new_child_neovim` with helpful wrappersHelpers.new_child_neovim = function() local child = MiniTest.new_child_neovim()
local prevent_hanging = function(method) if not child.is_blocked() then return end
local msg = string.format('Can not use `child.%s` because child process is blocked.', method) error(msg) end
child.setup = function() child.restart({ '-u', 'scripts/minimal_init.lua' })
-- Change initial buffer to be readonly. This not only increases execution -- speed, but more closely resembles manually opened Neovim. child.bo.readonly = false end
child.set_lines = function(arr, start, finish) prevent_hanging('set_lines')
if type(arr) == 'string' then arr = vim.split(arr, '\n') end
child.api.nvim_buf_set_lines(0, start or 0, finish or -1, false, arr) end
child.get_lines = function(start, finish) prevent_hanging('get_lines')
return child.api.nvim_buf_get_lines(0, start or 0, finish or -1, false) end
child.set_cursor = function(line, column, win_id) prevent_hanging('set_cursor')
child.api.nvim_win_set_cursor(win_id or 0, { line, column }) end
child.get_cursor = function(win_id) prevent_hanging('get_cursor')
return child.api.nvim_win_get_cursor(win_id or 0) end
child.get_multicursor = function(buf_id) if buf_id == nil or buf_id == 0 then buf_id = child.api.nvim_get_current_buf() end local ns_id = child.api.nvim_get_namespaces()['nvim.multicursor'] local mc_extmarks = child.api.nvim_buf_get_extmarks(buf_id, ns_id, 0, -1, {}) local res = {} for _, data in ipairs(mc_extmarks) do -- NOTE: use 1-based row and 0-based col, like `nvim_win_get_cursor()` table.insert(res, { data[2] + 1, data[3] }) end -- Ensure top-down order table.sort(res, function(a, b) return a[1] < b[1] or (a[1] == b[1] and a[2] < b[2]) end) return res end
child.set_multicursor = function(buf_id, pos_array) if buf_id == nil or buf_id == 0 then buf_id = child.api.nvim_get_current_buf() end local ns_id = child.api.nvim_get_namespaces()['nvim.multicursor'] for _, pos in ipairs(pos_array) do -- NOTE: use 1-based row and 0-based col, like `nvim_win_get_cursor()` child.api.nvim_buf_set_extmark(buf_id, ns_id, pos[1] - 1, pos[2], {}) end end
child.clear_multicursor = function(buf_id) if buf_id == nil or buf_id == 0 then buf_id = child.api.nvim_get_current_buf() end local ns_id = child.api.nvim_get_namespaces()['nvim.multicursor'] child.api.nvim_buf_clear_namespace(buf_id, ns_id, 0, -1) end
child.set_size = function(lines, columns) prevent_hanging('set_size')
if type(lines) == 'number' then child.o.lines = lines end
if type(columns) == 'number' then child.o.columns = columns end end
child.get_size = function() prevent_hanging('get_size')
return { child.o.lines, child.o.columns } end
--- Assert visual marks --- --- Useful to validate visual selection --- ---@param first number|table Table with start position or number to check linewise. ---@param last number|table Table with finish position or number to check linewise. ---@private child.expect_visual_marks = function(first, last) child.ensure_normal_mode()
first = type(first) == 'number' and { first, 0 } or first last = type(last) == 'number' and { last, 2147483647 } or last
MiniTest.expect.equality(child.api.nvim_buf_get_mark(0, '<'), first) MiniTest.expect.equality(child.api.nvim_buf_get_mark(0, '>'), last) end
child.validate_edit = function(lines_before, cursor_before, keys, lines_after, cursor_after) child.ensure_normal_mode()
child.set_lines(lines_before) child.set_cursor(cursor_before[1], cursor_before[2])
if vim.is_callable(keys) then keys() end if not vim.is_callable(keys) then child.type_keys(keys) end
Helpers.expect.equality(child.get_lines(), lines_after) Helpers.expect.equality(child.get_cursor(), cursor_after) end
child.validate_edit1d = function(line_before, col_before, keys, line_after, col_after) child.validate_edit({ line_before }, { 1, col_before }, keys, { line_after }, { 1, col_after }) end
child.validate_mc_edit = function(lines_before, cursor_before, keys, lines_after, cursor_after, multicursor_after) child.validate_edit(lines_before, cursor_before, keys, lines_after, cursor_after) Helpers.expect.equality(child.get_multicursor(), multicursor_after) child.clear_multicursor() end
-- Work with 'mini.nvim': -- - `mini_load` - load with "normal" table config -- - `mini_load_strconfig` - load with "string" config, which is still a -- table but with string values. Final loading is done by constructing -- final string table. Needed to be used if one of the config entries is a -- function (as currently there is no way to communicate a function object -- through RPC). -- - `mini_unload` - unload module and revert common side effects. child.mini_load = function(name, config) local lua_cmd = ([[require('mini.%s').setup(...)]]):format(name) child.lua(lua_cmd, { config }) end
child.mini_load_strconfig = function(name, strconfig) local t = {} for key, val in pairs(strconfig) do table.insert(t, key .. ' = ' .. val) end local str = string.format('{ %s }', table.concat(t, ', '))
local command = ([[require('mini.%s').setup(%s)]]):format(name, str) child.lua(command) end
child.mini_unload = function(name) local module_name = 'mini.' .. name local tbl_name = 'Mini' .. name:sub(1, 1):upper() .. name:sub(2)
-- Unload Lua module child.lua(([[package.loaded['%s'] = nil]]):format(module_name))
-- Remove global table child.lua(('_G["%s"] = nil'):format(tbl_name))
-- Remove autocmd group if child.fn.exists('#' .. tbl_name) == 1 then child.api.nvim_del_augroup_by_name(tbl_name) end end
child.mini_reload = function(name, config) child.mini_unload(name) child.mini_load(name, config) end
child.mini_reload_strconfig = function(name, strconfig) child.mini_unload(name) child.mini_load_strconfig(name, strconfig) end
child.tweak_visual_guicursor = function() -- After https://github.com/neovim/neovim/pull/42105 there are differences -- in how cursor is highlighted in screenshots with active Visual mode and -- default 'guicursor', This allows to pass same screenshots on different -- Neovim versions. -- TODO: Remove the helper after compatibility with Neovim=0.12 is dropped if vim.fn.has('nvim-0.13') == 0 then child.o.guicursor = 'v:ver25' end end
child.expect_screenshot = function(opts, path) opts = opts or {} local screenshot_opts = { redraw = opts.redraw } opts.redraw = nil if opts.ignore_cmdline and child.o.cmdheight > 0 then local ignore_text, ignore_attr = opts.ignore_text or {}, opts.ignore_attr or {} local height = child.o.lines for i = 1, child.o.cmdheight do local lnum = height - i + 1 if not vim.tbl_contains(ignore_text, lnum) then table.insert(ignore_text, lnum) end if not vim.tbl_contains(ignore_attr, lnum) then table.insert(ignore_attr, lnum) end end opts.ignore_text = ignore_text opts.ignore_attr = ignore_attr end MiniTest.expect.reference_screenshot(child.get_screenshot(screenshot_opts), path, opts) end
-- Poke child's event loop to make it up to date child.poke_eventloop = function() child.api.nvim_eval('1') end
return childend
-- Create a function that forwards its arguments to a global function-- inside a child process and returns the outputHelpers.forward_lua = function(child, fun_str) return function(...) return child.lua_get(fun_str .. '(...)', { ... }) endend
-- Detect CIHelpers.is_ci = function() return os.getenv('CI') ~= nil endHelpers.skip_in_ci = function(msg) if Helpers.is_ci() then MiniTest.skip(msg or 'Does not test properly in CI') endend
-- Detect OSHelpers.is_windows = function() return vim.fn.has('win32') == 1 endHelpers.skip_on_windows = function(msg) if Helpers.is_windows() then MiniTest.skip(msg or 'Does not test properly on Windows') endend
Helpers.is_macos = function() return vim.fn.has('mac') == 1 endHelpers.skip_on_macos = function(msg) if Helpers.is_macos() then MiniTest.skip(msg or 'Does not test properly on MacOS') endend
Helpers.is_linux = function() return vim.fn.has('linux') == 1 endHelpers.skip_on_linux = function(msg) if Helpers.is_linux() then MiniTest.skip(msg or 'Does not test properly on Linux') endend
-- Standardized way of dealing with timeHelpers.is_slow = function() return Helpers.is_ci() and (Helpers.is_windows() or Helpers.is_macos()) endHelpers.skip_if_slow = function(msg) if Helpers.is_slow() then MiniTest.skip(msg or 'Does not test properly in slow context') endend
Helpers.get_time_const = function(delay) local coef = 1 if Helpers.is_ci() then if Helpers.is_linux() then coef = 2 end if Helpers.is_windows() then coef = 5 end if Helpers.is_macos() then coef = 15 end end return coef * delayend
Helpers.sleep = function(ms, child, skip_slow) if skip_slow then Helpers.skip_if_slow('Skip because state checks after sleep are hard to make robust in slow context') end vim.uv.sleep(math.max(ms, 1)) if child ~= nil then child.poke_eventloop() endend
-- Standardized way of setting number of retriesHelpers.get_n_retry = function(n) local coef = 1 if Helpers.is_ci() then if Helpers.is_linux() then coef = 2 end if Helpers.is_windows() then coef = 3 end if Helpers.is_macos() then coef = 4 end end return coef * nend
return Helpers