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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374local helpers = dofile('tests/helpers.lua')
local child = helpers.new_child_neovim()local expect, eq = helpers.expect, helpers.expect.equalitylocal new_set = MiniTest.new_set
-- Helpers with child processeslocal load_module = function(config) child.mini_load('pairs', config) endlocal unload_module = function() child.mini_unload('pairs') endlocal reload_module = function(config) child.mini_reload('pairs', config) endlocal set_cursor = function(...) return child.set_cursor(...) endlocal get_cursor = function(...) return child.get_cursor(...) endlocal set_lines = function(...) return child.set_lines(...) endlocal get_lines = function(...) return child.get_lines(...) endlocal type_keys = function(...) return child.type_keys(...) endlocal sleep = function(ms) helpers.sleep(ms, child) end
-- Make helperslocal get_term_channel = function() local term_chans = vim.tbl_filter(function(x) return x.mode == 'terminal' end, child.api.nvim_list_chans())[1]['id'] return term_chans[1]['id']end
-- Time constantslocal term_mode_wait = helpers.get_time_const(50)
--- Make simple test on empty entity---@privatelocal validate_action = function(mode, test) mode = mode or 'i'
-- Expects to start in Normal mode if mode == 'i' then -- Setup local lines, cursor = get_lines(), get_cursor() set_lines({}) child.cmd('startinsert')
-- Test test()
-- Cleanup type_keys('<Esc>') set_lines(lines) set_cursor(unpack(cursor)) elseif mode == 'c' then -- Setup type_keys(':')
-- Test test()
-- Cleanup type_keys('<Esc>') elseif mode == 't' then helpers.skip_on_windows('Terminal emulator testing is not robust/easy on Windows') helpers.skip_on_macos('Terminal emulator testing is not robust/easy on MacOS')
-- Setup child.cmd('terminal! bash --noprofile --norc') -- Wait for terminal to get active sleep(term_mode_wait) child.cmd('startinsert')
-- Test test()
-- Cleanup type_keys([[<C-\>]], '<C-n>') child.cmd('bwipeout!') endend
--- Validate that `key` on its own inserts `pair` (whatever it is: for smaller--- code, it might be single character to test in 'MiniPairs.close()').---@privatelocal validate_open = function(mode, key, pair) local test = ({ i = function() type_keys(key) eq(get_lines(), { pair }) eq(get_cursor(), { 1, 1 }) end, c = function() type_keys(key) eq(child.fn.getcmdline(), pair) eq(child.fn.getcmdpos(), 2) end, t = function() -- Need to wait after each keystroke to allow shell to process it local wait = 50 type_keys(wait, key)
local pair_pattern = vim.pesc(pair) .. '$' expect.match(get_lines()[1], pair_pattern) end, })[mode]
validate_action(mode, test)end
--- Validate that `key` jumps over right hand side of `pair` if it is next.---@privatelocal validate_close = function(mode, key, pair) local test = ({ i = function() set_lines({ pair }) set_cursor(1, 1) type_keys(key) eq(get_lines(), { pair }) eq(get_cursor(), { 1, 2 }) end, c = function() type_keys('<C-v>', pair:sub(1, 1), '<C-v>', pair:sub(2, 2), '<Left>') type_keys(key) eq(child.fn.getcmdline(), pair) eq(child.fn.getcmdpos(), 3) end, t = function() local term_channel = get_term_channel()
-- Jumps over right hand side of `pair` if it is next child.fn.chansend(term_channel, pair) -- Need to wait after each keystroke to allow shell to process it sleep(term_mode_wait) type_keys(term_mode_wait, '<Left>') type_keys(term_mode_wait, key)
local pair_pattern = vim.pesc(pair) .. '$' expect.match(get_lines()[1], pair_pattern) end, })[mode]
validate_action(mode, test)end
--- Validate that `<BS>` deletes whole pair if inside of it.---@privatelocal validate_bs = function(mode, pair) local test = ({ i = function() set_lines({ pair }) set_cursor(1, 1) type_keys('<BS>') eq(get_lines(), { '' }) end, c = function() type_keys('<C-v>', pair:sub(1, 1), '<C-v>', pair:sub(2, 2), '<Left>') type_keys('<BS>') eq(child.fn.getcmdline(), '') eq(child.fn.getcmdpos(), 1) end, t = function() local term_channel = get_term_channel()
child.fn.chansend(term_channel, pair) -- Need to wait after each keystroke to allow shell to process it sleep(term_mode_wait) type_keys(term_mode_wait, '<Left>') type_keys(term_mode_wait, '<BS>')
local pair_pattern = vim.pesc(pair) .. '$' expect.no_match(get_lines()[1], pair_pattern) end, })[mode]
validate_action(mode, test)end
--- Validate that `<CR>` acts like `<CR><C-o>O` in Insert mode if inside pair.---@privatelocal validate_cr = function(pair) local test = function() -- Reference pass to get baseline data for default behavior set_lines({ pair }) set_cursor(1, 0) local keys = child.api.nvim_replace_termcodes('<Esc>a<CR><C-o>O', true, true, true) child.api.nvim_feedkeys(keys, 'nx', false)
local ref_lines, ref_cursor = get_lines(), get_cursor()
-- Test pass set_lines({ pair }) set_cursor(1, 0) type_keys('<Esc>', 'a', '<CR>')
eq(get_lines(), ref_lines) eq(get_cursor(), ref_cursor) eq(child.fn.mode(), 'i') end
validate_action('i', test)end
local validate_neigh_disable = function(neigh, key) set_lines({ neigh }) set_cursor(1, 0) type_keys('a')
type_keys(key) eq(get_lines(), { neigh:sub(1, 1) .. key .. neigh:sub(2, 2) })
child.ensure_normal_mode()end
local validate_slash = function(key) validate_neigh_disable([[\]], key) end
local validate_no = function(action, ...) local val_f = ({ open = validate_open, close = validate_close, bs = validate_bs, cr = validate_cr, neigh_disable = validate_neigh_disable, })[action]
-- Validation function `val_f` should throw error local ok, _ = pcall(val_f, ...) child.ensure_normal_mode() eq(ok, false)end
local validate_disable = function(var_type, key) child[var_type].minipairs_disable = true set_lines({}) child.cmd('startinsert') type_keys(key) eq(get_lines(), { key })
child[var_type].minipairs_disable = nilend
local validate_cmdline = function(line, pos) eq(child.fn.mode(), 'c') eq(child.fn.getcmdline(), line) eq(child.fn.getcmdpos(), pos or line:len() + 1)end
local apply_map = function(fun_name, args_string) -- If testing `MiniPairs.map_buf()`, apply it in current buffer local is_buf_local = fun_name == 'map_buf' or fun_name == 'unmap_buf'
-- Apply mapping function of same scope local fun_name_actual = ({ map = 'map', unmap = 'map', map_buf = 'map_buf', unmap_buf = 'map_buf' })[fun_name]
local command = ('MiniPairs.%s(%s%s)'):format(fun_name_actual, is_buf_local and '0, ' or '', args_string) child.lua(command)end
local apply_unmap = function(fun_name, args_string) -- If testing `MiniPairs.map_buf()`, apply it in current buffer local is_buf_local = fun_name == 'map_buf' or fun_name == 'unmap_buf'
-- Apply mapping function of same scope local fun_name_actual = ({ map = 'unmap', unmap = 'unmap', map_buf = 'unmap_buf', unmap_buf = 'unmap_buf' })[fun_name]
local command = ('MiniPairs.%s(%s%s)'):format(fun_name_actual, is_buf_local and '0, ' or '', args_string) child.lua(command)end
-- Output test set ============================================================local T = new_set({ hooks = { pre_case = function() child.setup() load_module() end, post_once = child.stop, }, n_retry = helpers.get_n_retry(1),})
-- Unit tests =================================================================T['setup()'] = new_set()
T['setup()']['creates side effects'] = function() -- Global variable eq(child.lua_get('type(_G.MiniPairs)'), 'table')
-- Autocommand group eq(child.fn.exists('#MiniPairs'), 1)end
T['setup()']['creates `config` field'] = function() eq(child.lua_get('type(_G.MiniPairs.config)'), 'table')
-- Check default values local expect_config = function(field, value) eq(child.lua_get('MiniPairs.config.' .. field), value) end
-- Check default values expect_config('modes', { insert = true, command = false, terminal = false }) expect_config("mappings['(']", { action = 'open', pair = '()', neigh_pattern = '^[^\\]' }) expect_config("mappings['[']", { action = 'open', pair = '[]', neigh_pattern = '^[^\\]' }) expect_config("mappings['{']", { action = 'open', pair = '{}', neigh_pattern = '^[^\\]' }) expect_config("mappings[')']", { action = 'close', pair = '()', neigh_pattern = '^[^\\]' }) expect_config("mappings[']']", { action = 'close', pair = '[]', neigh_pattern = '^[^\\]' }) expect_config("mappings['}']", { action = 'close', pair = '{}', neigh_pattern = '^[^\\]' }) local quote_config = { action = 'closeopen', pair = '""', neigh_pattern = '^[^\\]', register = { cr = false } } expect_config("mappings['\"']", quote_config) quote_config.pair, quote_config.neigh_pattern = "''", '^[^%a\\]' expect_config('mappings["\'"]', quote_config) quote_config.pair, quote_config.neigh_pattern = '``', '^[^\\]' expect_config("mappings['`']", quote_config)end
T['setup()']['respects `config` argument'] = function() unload_module() load_module({ modes = { command = true } }) eq(child.lua_get('MiniPairs.config.modes.command'), true)end
T['setup()']['validates `config` argument'] = function() unload_module()
local expect_config_error = function(config, name, target_type) expect.error(function() load_module(config) end, vim.pesc(name) .. '.*' .. vim.pesc(target_type)) end
expect_config_error('a', 'config', 'table') expect_config_error({ modes = 'a' }, 'modes', 'table') expect_config_error({ modes = { insert = 'a' } }, 'modes.insert', 'boolean') expect_config_error({ modes = { command = 'a' } }, 'modes.command', 'boolean') expect_config_error({ modes = { terminal = 'a' } }, 'modes.terminal', 'boolean')
local expect_mapping_pair_info = function(key) local quote = key == "'" and '"' or "'" local prefix = ('mappings[%s%s%s]'):format(quote, key, quote) expect_config_error({ mappings = { [key] = { action = 1 } } }, prefix .. '.action', 'string') expect_config_error({ mappings = { [key] = { action = 1 } } }, prefix .. '.action', 'string') expect_config_error({ mappings = { [key] = { pair = 1 } } }, prefix .. '.pair', 'string') expect_config_error({ mappings = { [key] = { neigh_pattern = 1 } } }, prefix .. '.neigh_pattern', 'string') expect_config_error({ mappings = { [key] = { register = 'a' } } }, prefix .. '.register', 'table') expect_config_error({ mappings = { [key] = { register = { bs = 1 } } } }, prefix .. '.register.bs', 'boolean') expect_config_error({ mappings = { [key] = { register = { cr = 1 } } } }, prefix .. '.register.cr', 'boolean') end
expect_config_error({ mappings = 'a' }, 'mappings', 'table') expect_mapping_pair_info('(') expect_mapping_pair_info('[') expect_mapping_pair_info('{') expect_mapping_pair_info(')') expect_mapping_pair_info(']') expect_mapping_pair_info('}') expect_mapping_pair_info('"') expect_mapping_pair_info("'") expect_mapping_pair_info('`')end
local has_map = function(lhs, rhs, mode) mode = mode or 'i' local map_capture = child.cmd_capture(mode .. 'map ' .. lhs) return map_capture:find(vim.pesc(rhs)) ~= nilend
T['setup()']['makes default `config.mappings`'] = function() eq(has_map('(', [[v:lua.MiniPairs.open("()", "^[^\\]")]]), true) eq(has_map('[', [[v:lua.MiniPairs.open("[]", "^[^\\]")]]), true) eq(has_map('{', [[v:lua.MiniPairs.open("{}", "^[^\\]")]]), true) eq(has_map(')', [[v:lua.MiniPairs.close("()", "^[^\\]")]]), true) eq(has_map(']', [[v:lua.MiniPairs.close("[]", "^[^\\]")]]), true) eq(has_map('}', [[v:lua.MiniPairs.close("{}", "^[^\\]")]]), true) eq(has_map('"', [[v:lua.MiniPairs.closeopen('""', "^[^\\]")]]), true) eq(has_map("'", [[v:lua.MiniPairs.closeopen("''", "^[^%a\\]")]]), true) eq(has_map('`', [[v:lua.MiniPairs.closeopen("``", "^[^\\]")]]), true)
eq(has_map('<CR>', 'v:lua.MiniPairs.cr()'), true) eq(has_map('<BS>', 'v:lua.MiniPairs.bs()'), true)end
T['setup()']['makes custom `config.mappings`'] = function() reload_module({ mappings = { ['('] = { pair = '[]', action = 'close' } } }) eq(has_map('(', [[v:lua.MiniPairs.close("[]", "^[^\\]")]]), true)
reload_module({ mappings = { ['*'] = { pair = '**', action = 'closeopen' } } }) eq(has_map('*', 'v:lua.MiniPairs.closeopen("**", "..")'), true)end
T['setup()']['makes mappings in supplied modes'] = function() child.api.nvim_del_keymap('i', '(') reload_module({ modes = { insert = false, command = true, terminal = false } })
eq(has_map('(', [[v:lua.MiniPairs.open("()", "^[^\\]")]]), false) eq(has_map('(', [[v:lua.MiniPairs.open("()", "^[^\\]")]], 'c'), true)end
T['setup()']['allows `false` as `mappings` entry to not create mapping'] = function() eq(has_map('(', [[v:lua.MiniPairs.open("()", "^[^\\]")]]), true) child.api.nvim_del_keymap('i', '(')
reload_module({ mappings = { ['('] = false } }) eq(has_map('(', [[v:lua.MiniPairs.open("()", "^[^\\]")]]), false)end
T['setup()']['has default neighborhood patterns working with multibyte characters'] = function() local validate = function(line_before, col_before, key, line_after) set_lines({ line_before }) set_cursor(1, col_before) type_keys('i', '\\', key)
eq(get_lines(), { line_after })
child.ensure_normal_mode() end
validate('є', 0, '(', '\\(є') validate('є', 0, '[', '\\[є') validate('є', 0, '{', '\\{є')
validate('є)', 2, ')', 'є\\))') validate('є]', 2, ']', 'є\\]]') validate('є}', 2, '}', 'є\\}}')
validate('є', 0, '"', '\\"є') validate('є', 0, "'", "\\'є") validate('є', 0, '`', '\\`є')end
T['map()/map_buf()'] = new_set({ hooks = { pre_case = function() -- Allow switching between buffers with unsaved changes child.o.hidden = true end, }, parametrize = { { 'map' }, { 'map_buf' } },})
T['map()/map_buf()']['work'] = function(fun_name) validate_no('open', 'i', '<', '<>') validate_no('bs', 'i', '<>') validate_no('cr', '<>')
apply_map(fun_name, [['i', '<', { action = 'open', pair = '<>' }]])
validate_open('i', '<', '<>') validate_bs('i', '<>') validate_cr('<>')end
T['map()/map_buf()']['create mapping properly'] = function(fun_name) local has_open = function() validate_open('i', '<', '<>') validate_bs('i', '<>') validate_cr('<>') end local no_open = function() validate_no('open', 'i', '<', '<>') validate_no('bs', 'i', '<>') validate_no('cr', '<>') end
-- Set up two buffers local buffers = { cur = child.api.nvim_get_current_buf(), new = child.api.nvim_create_buf(true, false) }
child.api.nvim_set_current_buf(buffers.cur) no_open() child.api.nvim_set_current_buf(buffers.new) no_open()
child.api.nvim_set_current_buf(buffers.cur) apply_map(fun_name, [['i', '<', { action = 'open', pair = '<>' }]]) has_open()
child.api.nvim_set_current_buf(buffers.new) if fun_name == 'map_buf' then no_open() else has_open() endend
T['map()/map_buf()']['respect `mode` argument'] = function(fun_name) local validate = function(mode) local command = ([['%s', '<', { action = 'open', pair = '<>' }]]):format(mode) apply_map(fun_name, command) validate_open(mode, '<', '<>') end
validate('i') -- If making global mapping, also test in other modes if fun_name == 'map' then validate('c') validate('t') endend
T['map()/map_buf()']['respect `lhs` argument'] = function(fun_name) apply_map(fun_name, [['i', '<', { action = 'open', pair = '<>' }]]) validate_open('i', '<', '<>')
apply_map(fun_name, [['i', '$', { action = 'open', pair = '$%' }]]) validate_open('i', '$', '$%')end
T['map()/map_buf()']['respect `action` and `pair` of `pair_info` argument'] = function(fun_name) apply_map(fun_name, [['i', '>', { action = 'close', pair = '<>' }]]) validate_close('i', '>', '<>')end
T['map()/map_buf()']['respect `neigh_pattern` of `pair_info` argument'] = function(fun_name) -- It should insert pair only when cursor after whitespace and before -- letter 'a' apply_map(fun_name, [['i', '<', { action = 'open', pair = '<>', neigh_pattern = '%sa' }]])
local test = function() -- Use typing delay to poke eventloop and enable correct neighbor pattern -- checks local wait = 1
-- Shouldn't work in general type_keys(wait, 'a', '<') eq(get_lines(), { 'a<' })
-- Should work only within specified pattern set_lines({}) type_keys(wait, ' a', '<Left>', '<') eq(get_lines(), { ' <>a' }) end
validate_action('i', test)end
T['map()/map_buf()']['respect `register` of `pair_info` argument'] = function(fun_name) apply_map(fun_name, [['i', '<', { action = 'open', pair = '<>', register = { bs = true, cr = false } }]])
validate_bs('i', '<>') validate_no('cr', '<>')end
T['map()/map_buf()']['respect `opts` or `pair_info` argument'] = function(fun_name) -- Throws error because mapping `(` should already exist expect.error( function() apply_map(fun_name, [['i', '(', { action = 'open', pair = '()' }, { unique = true }]]) end, '[Mm]apping.*exists' )end
T['map()/map_buf()']['create mappings for `<BS>` in new mode'] = function(fun_name) -- Should not override existing one child.cmd('cnoremap <BS> <Cmd>echo "Hello"<CR>') apply_map(fun_name, '"c", "<", { action = "open", pair = "<>" }') expect.match(child.cmd_capture('cmap <BS>'), 'echo "Hello"') child.cmd('cunmap <BS>')
apply_map(fun_name, '"c", "<", { action = "open", pair = "<>" }') expect.match(child.cmd_capture('cmap <BS>'), 'MiniPairs%.bs') validate_bs('c', '<>')end
T['map()/map_buf()']['create mappings for `<CR>` in new mode'] = function(fun_name) -- Should not override existing one child.cmd('inoremap <CR> <Cmd>echo "Hello"<CR>') apply_map(fun_name, '"i", "<", { action = "open", pair = "<>" }') expect.match(child.cmd_capture('imap <CR>'), 'echo "Hello"') child.cmd('iunmap <CR>')
apply_map(fun_name, '"i", "<", { action = "open", pair = "<>" }') expect.match(child.cmd_capture('imap <CR>'), 'MiniPairs%.cr') validate_cr('<>')end
local make_test_map = function(fun_name) apply_map(fun_name, [['i', '<', { action = 'open', pair = '<>' }]]) end
local has_test_map = function() validate_open('i', '<', '<>') validate_bs('i', '<>') validate_cr('<>')end
local no_test_map = function() validate_no('open', 'i', '<', '<>') validate_no('bs', 'i', '<>') validate_no('cr', '<>')end
T['unmap()/unmap_buf()'] = new_set({ hooks = { pre_case = function() -- Allow switching between buffers with unsaved changes child.o.hidden = true end, }, parametrize = { { 'unmap' }, { 'unmap_buf' } },})
T['unmap()/unmap_buf()']['work'] = function(fun_name) make_test_map(fun_name) has_test_map()
apply_unmap(fun_name, [['i', '<', '<>']]) no_test_map()end
T['unmap()/unmap_buf()']['delete mapping properly'] = function(fun_name) -- Set up two buffers local buffers = { cur = child.api.nvim_get_current_buf(), new = child.api.nvim_create_buf(true, false) }
child.api.nvim_set_current_buf(buffers.cur) make_test_map(fun_name) has_test_map() child.api.nvim_set_current_buf(buffers.new) make_test_map(fun_name) has_test_map()
child.api.nvim_set_current_buf(buffers.cur) apply_unmap(fun_name, [['i', '<', '<>']]) no_test_map()
child.api.nvim_set_current_buf(buffers.new) if fun_name == 'unmap_buf' then has_test_map() else no_test_map() endend
T['unmap()/unmap_buf()']['respect `mode` argument'] = function(fun_name) apply_map(fun_name, [['c', '<', { action = 'open', pair = '<>' }]]) validate_open('c', '<', '<>') validate_bs('c', '<>')
apply_unmap(fun_name, [['c', '<', '<>']]) validate_no('open', 'c', '<', '<>') validate_no('bs', 'c', '<>')end
T['unmap()/unmap_buf()']['require explicit `pair` argument'] = function(fun_name) expect.error(function() apply_unmap(fun_name, [['i', '(']]) end, 'pair.*string')end
T['unmap()/unmap_buf()']['allow empty string for `pair` argument to not unregister pair'] = function(fun_name) make_test_map(fun_name) has_test_map()
apply_unmap(fun_name, [['i', '<', '']]) validate_no('open', 'i', '<', '<>') validate_bs('i', '<>') validate_cr('<>')end
T['unmap()/unmap_buf()']['work for already missing mapping'] = function(fun_name) expect.no_error(function() apply_unmap(fun_name, [['c', '%', '%%']]) end)end
-- Integration tests ==========================================================T['Open action'] = new_set()
T['Open action']['works'] = function() validate_open('i', '(', '()') validate_open('i', '[', '[]') validate_open('i', '{', '{}')
-- There should be no side effects eq(child.o.lazyredraw, false)end
T['Open action']['works with multibyte characters'] = function() -- Insert mode child.lua('MiniPairs.map("i", "ы", { action = "open", pair = "ы」" } )') type_keys('i', 'ы') eq(get_lines(), { 'ы」' }) eq(get_cursor(), { 1, 2 }) type_keys('ы') eq(get_lines(), { 'ыы」」' }) eq(get_cursor(), { 1, 4 })
-- - Should also work for `<BS>` type_keys('<BS>') eq(get_lines(), { 'ы」' }) eq(get_cursor(), { 1, 2 })
-- - Should also work for `<CR>` type_keys('<CR>') eq(get_lines(), { 'ы', '', '」' }) eq(get_cursor(), { 2, 0 })
-- Command-line mode child.ensure_normal_mode() child.lua('MiniPairs.map("c", "ы", { action = "open", pair = "ы」" } )') type_keys(':', 'ы') eq(child.fn.getcmdline(), 'ы」') eq(child.fn.getcmdpos(), 3) type_keys('ы') eq(child.fn.getcmdline(), 'ыы」」') eq(child.fn.getcmdpos(), 5)
-- - Should also work for `<BS>` type_keys('<BS>') eq(child.fn.getcmdline(), 'ы」') eq(child.fn.getcmdpos(), 3)
-- Omit testing Terminal mode in the hope that it is the sameend
T['Open action']['works in special lines'] = function() -- With null character -- - Insert mode set_lines({ '\0null byte' }) type_keys('A', '(') eq(get_lines(), { '\0null byte()' }) child.ensure_normal_mode()
-- - Command-line mode child.lua('MiniPairs.map("c", "(", { action = "open", pair = "()" } )') type_keys(':', '<C-v>000', 'null byte', '(') eq(child.fn.getcmdline(), '\nnull byte()')end
T['Open action']['works with multicursor'] = function() if child.fn.has('nvim-0.13') == 0 then MiniTest.skip('Multicursor is available in Neovim>=0.13') end
local keys = { 'Qjq=', 'i', '(', '[', '{', '<C-v>(' } child.validate_mc_edit({ '', '' }, { 1, 0 }, keys, { '([{(}])', '([{(}])' }, { 2, 4 }, { { 1, 4 } })end
T['Open action']['does not break undo sequence in Insert mode'] = function() type_keys('i', '((', '<Esc>') eq(get_lines(), { '(())' }) eq(get_cursor(), { 1, 1 })
type_keys('u') eq(get_lines(), { '' })end
T['Open action']['works with visible wildmenu'] = function() apply_map('map', '"c", "<", { action = "open", pair = "<>" }') type_keys(':', '<Tab>', '<') validate_cmdline('!<>', 3)end
T['Open action']['respects neighbor pattern'] = function() validate_slash('(') validate_slash('[') validate_slash('{')end
T['Open action']['is correctly initiated in `config.mappings`'] = function() child.api.nvim_del_keymap('i', '(') reload_module({ mappings = { ['('] = { action = 'open', pair = '()', neigh_pattern = '..' } } }) validate_no('neigh_disable', [[\]], '(')end
T['Open action']['works with `nvim_feedkeys()`'] = function() child.cmd('inoremap <C-j> <Cmd>call nvim_feedkeys("((", "", v:true)<CR>') type_keys('i', '<C-j>') eq(child.o.lazyredraw, false)end
T['Open action']['respects `vim.{g,b}.minipairs_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } },}, { test = function(var_type) validate_disable(var_type, '(') validate_disable(var_type, '[') validate_disable(var_type, '{') end,})
T['Close action'] = new_set()
T['Close action']['works'] = function() validate_open('i', ')', ')') validate_open('i', ']', ']') validate_open('i', '}', '}') validate_close('i', ')', '()') validate_close('i', ']', '[]') validate_close('i', '}', '{}')end
T['Close action']['works with multibyte characters'] = function() -- Insert mode child.lua('MiniPairs.map("i", "」", { action = "close", pair = "ы」" } )') type_keys('i', 'ы', '」', '<Left>') eq(get_lines(), { 'ы」' }) eq(get_cursor(), { 1, 2 }) type_keys('」') eq(get_lines(), { 'ы」' }) eq(get_cursor(), { 1, 5 }) type_keys('」') eq(get_lines(), { 'ы」」' }) eq(get_cursor(), { 1, 8 })
-- - Should also work for `<BS>` type_keys('<Left>', '<Left>', '<BS>') eq(get_lines(), { '」' }) eq(get_cursor(), { 1, 0 })
-- - Should also work for `<CR>` type_keys('<C-v>', 'ы', '<CR>') eq(get_lines(), { 'ы', '', '」' }) eq(get_cursor(), { 2, 0 })
-- Command-line mode child.ensure_normal_mode() child.lua('MiniPairs.map("c", "」", { action = "close", pair = "ы」" } )') type_keys(':', 'ы', '」', '<Left>') eq(child.fn.getcmdline(), 'ы」') eq(child.fn.getcmdpos(), 3) type_keys('」') eq(child.fn.getcmdline(), 'ы」') eq(child.fn.getcmdpos(), 6) type_keys('」') eq(child.fn.getcmdline(), 'ы」」') eq(child.fn.getcmdpos(), 9)
-- - Should also work for `<BS>` type_keys('<Left>', '<Left>', '<BS>') eq(child.fn.getcmdline(), '」') eq(child.fn.getcmdpos(), 1)
-- Omit testing Terminal mode in the hope that it is the sameend
T['Close action']['works in special lines'] = function() -- With null character -- - Insert mode set_lines({ '\0null byte()' }) set_cursor(1, 11) type_keys('i', ')') eq(get_lines(), { '\0null byte()' }) eq(get_cursor(), { 1, 12 }) child.ensure_normal_mode()
-- - Command-line mode child.lua('MiniPairs.map("c", ")", { action = "close", pair = "()" } )') type_keys(':', '<C-v>000', 'null byte()<Left>', ')') eq(child.fn.getcmdline(), '\nnull byte()') eq(child.fn.getcmdpos(), 13)end
T['Close action']['works with multicursor'] = function() if child.fn.has('nvim-0.13') == 0 then MiniTest.skip('Multicursor is available in Neovim>=0.13') end
local keys = { 'Qjq=', 'i', ')', ']', '}', '<C-v>)' } child.validate_mc_edit({ ')]})', ')]})' }, { 1, 0 }, keys, { ')]}))', ')]}))' }, { 2, 4 }, { { 1, 4 } })end
T['Close action']['does not break undo sequence in Insert mode'] = function() set_lines({ '(())' }) set_cursor(1, 2)
type_keys('i', ')) ', '<Esc>') type_keys('u') eq(get_lines(), { '(())' })end
T['Close action']['works with visible wildmenu'] = function() apply_map('map', '"c", "(", { action = "open", pair = "()" }') apply_map('map', '"c", ")", { action = "close", pair = "()" }') child.lua('_G.AAA = 1') type_keys(':', 'lua print(1 + ', '<Tab>') validate_cmdline('lua print(1 + AAA)', 18)
type_keys(')') validate_cmdline('lua print(1 + AAA)', 19)end
T['Close action']['works with inline virtual text'] = function() set_lines({ '()' }) local ns_id = child.api.nvim_create_namespace('Test') child.api.nvim_buf_set_extmark(0, ns_id, 0, 1, { virt_text = { { 'Virt', 'String' } }, virt_text_pos = 'inline' })
local validate = function(virtualedit) child.o.virtualedit = virtualedit set_cursor(1, 0)
type_keys('a', ')') eq(child.fn.getcurpos(), { 0, 1, 3, 0, 7 }) -- Should have no side effects eq(child.o.virtualedit, virtualedit)
type_keys('<Esc>') end
validate('all') validate('none') validate('block') validate('onemore')end
local validate_slash_close = function(key, pair) set_lines({ pair }) set_cursor(1, 1) child.cmd('startinsert')
-- Wait to poke eventloop to enable pattern check type_keys(1, [[\]], key) eq(get_lines(), { pair:sub(1, 1) .. [[\]] .. key .. pair:sub(2, 2) })end
T['Close action']['respects neighbor pattern'] = function() validate_slash_close(')', '()') validate_slash_close(']', '[]') validate_slash_close('}', '{}')end
T['Close action']['is correctly initiated in `config.mappings`'] = function() child.api.nvim_del_keymap('i', ')') reload_module({ mappings = { [')'] = { action = 'close', pair = '()', neigh_pattern = '..' } } }) expect.error(function() validate_slash_close(')', '()') end)end
T['Close action']['respects `vim.{g,b}.minipairs_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } },}, { test = function(var_type) validate_disable(var_type, ')') validate_disable(var_type, ']') validate_disable(var_type, '}') end,})
T['Closeopen action'] = new_set()
T['Closeopen action']['works'] = function() validate_open('i', '"', '""') validate_open('i', "'", "''") validate_open('i', '`', '``') validate_close('i', '"', '""') validate_close('i', "'", "''") validate_close('i', '`', '``')end
T['Closeopen action']['works with multibyte characters'] = function() -- Insert mode child.lua('MiniPairs.map("i", "」", { action = "closeopen", pair = "」」" } )') type_keys('i', '」') eq(get_lines(), { '」」' }) eq(get_cursor(), { 1, 3 }) type_keys('」') eq(get_lines(), { '」」' }) eq(get_cursor(), { 1, 6 }) type_keys('」') eq(get_lines(), { '」」」」' }) eq(get_cursor(), { 1, 9 })
-- - Should also work for `<BS>` type_keys('<BS>') eq(get_lines(), { '」」' }) eq(get_cursor(), { 1, 6 })
-- - Should also work for `<CR>` type_keys('<Left>', '<CR>') eq(get_lines(), { '」', '', '」' }) eq(get_cursor(), { 2, 0 })
-- Command-line mode child.ensure_normal_mode() child.lua('MiniPairs.map("c", "」", { action = "closeopen", pair = "」」" } )') type_keys(':', '」') eq(child.fn.getcmdline(), '」」') eq(child.fn.getcmdpos(), 4) type_keys('」') eq(child.fn.getcmdline(), '」」') eq(child.fn.getcmdpos(), 7) type_keys('」') eq(child.fn.getcmdline(), '」」」」') eq(child.fn.getcmdpos(), 10)
-- - Should also work for `<BS>` type_keys('<BS>') eq(child.fn.getcmdline(), '」」') eq(child.fn.getcmdpos(), 7)
-- Omit testing Terminal mode in the hope that it is the sameend
T['Closeopen action']['works in special lines'] = function() -- With null character -- - Insert mode set_lines({ '\0null byte' }) type_keys('A', '"') eq(get_lines(), { '\0null byte""' }) eq(get_cursor(), { 1, 11 }) type_keys('"') eq(get_lines(), { '\0null byte""' }) eq(get_cursor(), { 1, 12 }) child.ensure_normal_mode()
-- - Command-line mode child.lua([[MiniPairs.map('c', '"', { action = 'closeopen', pair = '""' } )]]) type_keys(':', '<C-v>000', 'null byte', '"') eq(child.fn.getcmdline(), '\nnull byte""') eq(child.fn.getcmdpos(), 12) type_keys('"') eq(child.fn.getcmdline(), '\nnull byte""') eq(child.fn.getcmdpos(), 13)end
T['Closeopen action']['works with multicursor'] = function() if child.fn.has('nvim-0.13') == 0 then MiniTest.skip('Multicursor is available in Neovim>=0.13') end
local validate = function(char) local keys = { 'Qjq=', 'i', char, char, '<C-v>', char, '<Left>', '<C-v>', char } local lines_after = { string.rep(char, 4), string.rep(char, 4) } child.validate_mc_edit({ '', '' }, { 1, 0 }, keys, lines_after, { 2, 3 }, { { 1, 3 } }) end
validate('"') validate("'") validate('`')end
T['Closeopen action']['does not break undo sequence in Insert mode'] = function() -- Open set_lines({})
type_keys('i', '""', '<Esc>') type_keys('u') eq(get_lines(), { '' })
-- Close set_lines({ '""""' }) set_cursor(1, 2)
type_keys('i', '"" ', '<Esc>') type_keys('u') eq(get_lines(), { '""""' })end
T['Closeopen action']['works with visible wildmenu'] = function() apply_map('map', '"c", "`", { action = "closeopen", pair = "``" }') child.lua('_G.AAA = 1') type_keys(':', 'lua print`1 + ', '<Tab>') validate_cmdline('lua print`1 + AAA`', 18)
type_keys('`') validate_cmdline('lua print`1 + AAA`', 19)end
T['Closeopen action']['works with inline virtual text'] = function() set_lines({ '""' }) local ns_id = child.api.nvim_create_namespace('Test') child.api.nvim_buf_set_extmark(0, ns_id, 0, 1, { virt_text = { { 'Virt', 'String' } }, virt_text_pos = 'inline' })
local validate = function(virtualedit) child.o.virtualedit = virtualedit set_cursor(1, 0)
type_keys('a', '"') eq(child.fn.getcurpos(), { 0, 1, 3, 0, 7 }) -- Should have no side effects eq(child.o.virtualedit, virtualedit)
type_keys('<Esc>') end
validate('all') validate('none') validate('block') validate('onemore')end
T['Closeopen action']['respects neighbor pattern'] = function() validate_slash('"') validate_slash("'") validate_slash('`')
validate_neigh_disable('a ', "'")
validate_no('neigh_disable', '__', '"') validate_no('neigh_disable', '__', "'") validate_no('neigh_disable', '__', '`')end
T['Closeopen action']['is correctly initiated in `config.mappings`'] = function() child.api.nvim_del_keymap('i', '"') reload_module({ mappings = { ['"'] = { action = 'closeopen', pair = '""', neigh_pattern = '..' } } }) validate_no('neigh_disable', [[\]], '"')end
T['Closeopen action']['respects `vim.{g,b}.minipairs_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } },}, { test = function(var_type) validate_disable(var_type, '"') validate_disable(var_type, "'") validate_disable(var_type, '`') end,})
T['<BS> action'] = new_set()
T['<BS> action']['works'] = function() validate_bs('i', '()') validate_bs('i', '[]') validate_bs('i', '{}') validate_bs('i', '""') validate_bs('i', "''") validate_bs('i', '``')
-- Should work in Command-line with redraw child.set_size(10, 12) reload_module({ modes = { command = true } }) type_keys(':aa()bb', '<Left>', '<Left>', '<Left>') type_keys('<BS>') child.expect_screenshot()end
T['<BS> action']['works in special lines'] = function() -- With null character -- - Insert mode set_lines({ '\0null byte()' }) set_cursor(1, 11) type_keys('i', '<BS>') eq(get_lines(), { '\0null byte' }) eq(get_cursor(), { 1, 10 }) child.ensure_normal_mode()
-- - Command-line mode reload_module({ modes = { command = true } }) type_keys(':', '<C-v>000', 'null byte()<Left>', '<BS>') eq(child.fn.getcmdline(), '\nnull byte') eq(child.fn.getcmdpos(), 11)end
T['<BS> action']['works with multicursor'] = function() if child.fn.has('nvim-0.13') == 0 then MiniTest.skip('Multicursor is available in Neovim>=0.13') end
local lines_before = { '([{"\'``\'"}])', '([{"\'``\'"}])' } -- NOTE: Exit from Insert mode to have up to date text at extra cursor local keys = { 'Qjq=', 'i', '<BS>', '<BS>', '<BS>', '<BS>', '<BS>', '<BS>', '<Esc>' } child.validate_mc_edit(lines_before, { 1, 6 }, keys, { '', '' }, { 2, 0 }, { { 1, 0 } })end
T['<BS> action']['respects `key` argument'] = function() child.lua([[ local map_bs = function(lhs, rhs) vim.keymap.set('i', lhs, rhs, { expr = true, replace_keycodes = false }) end
map_bs('<C-h>', 'v:lua.MiniPairs.bs()') map_bs('<C-w>', 'v:lua.MiniPairs.bs("\23")') map_bs('<C-u>', 'v:lua.MiniPairs.bs("\21")') ]]) local validate_key = function(key, line_before, col_before, line_after, col_after) child.ensure_normal_mode() set_lines({ line_before }) set_cursor(1, col_before) type_keys('a')
type_keys(key)
eq(child.api.nvim_get_mode().mode, 'i') eq(get_lines(), { line_after }) eq(get_cursor(), { 1, col_after }) end
-- `<C-h>` validate_key('<C-h>', 'aa(b)', 3, 'aa()', 3) validate_key('<C-h>', 'aa()', 2, 'aa', 2) validate_key('<C-h>', 'aa', 1, 'a', 1)
-- `<C-w>` validate_key('<C-w>', 'aa(bb)', 4, 'aa()', 3) validate_key('<C-w>', 'aa()', 2, 'aa', 2) validate_key('<C-w>', 'aa', 1, '', 0)
-- `<C-u>` validate_key('<C-u>', 'aa()cc', 1, '()cc', 0) validate_key('<C-u>', 'aa()cc', 2, 'cc', 0) validate_key('<C-u>', 'aa()cc', 5, '', 0)end
T['<BS> action']['does not break undo sequence in Insert mode'] = function() set_lines({ 'a()' }) set_cursor(1, 2) child.cmd('startinsert')
type_keys('<BS><BS>', '<Esc>') eq(get_lines(), { '' }) type_keys('u') eq(get_lines(), { 'a()' })end
local reload_unregister_bs = function() child.api.nvim_del_keymap('i', '<BS>') reload_module({ mappings = { ['('] = { register = { bs = false } }, ['['] = { register = { bs = false } }, ['{'] = { register = { bs = false } }, [')'] = { register = { bs = false } }, [']'] = { register = { bs = false } }, ['}'] = { register = { bs = false } }, ['"'] = { register = { bs = false } }, ["'"] = { register = { bs = false } }, ['`'] = { register = { bs = false } }, }, })end
T['<BS> action']['does not create mapping if nothing is registered in `config.mappings`'] = function() expect.match(child.cmd_capture('imap <BS>'), 'MiniPairs%.bs') reload_unregister_bs() expect.match(child.cmd_capture('imap <BS>'), 'No mapping found')end
T['<BS> action']['works as normal if nothing is registered'] = function() reload_unregister_bs()
set_lines({ '()' }) set_cursor(1, 1) child.cmd('startinsert')
type_keys('<BS>') eq(get_lines(), { ')' }) eq(get_cursor(), { 1, 0 })end
T['<BS> action']['respects `vim.{g,b}.minipairs_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } },}, { test = function(var_type) child[var_type].minipairs_disable = true set_lines({ '()' }) set_cursor(1, 1) child.cmd('startinsert') type_keys('<BS>') eq(get_lines(), { ')' }) end,})
T['<CR> action'] = new_set()
T['<CR> action']['works'] = function() validate_cr('()') validate_cr('[]') validate_cr('{}') validate_no('cr', '""') validate_no('cr', "''") validate_no('cr', '``')
-- There should be no side effects eq(child.o.eventignore, '') eq(child.o.lazyredraw, false)end
T['<CR> action']['works in special lines'] = function() -- With null character -- - Insert mode set_lines({ '\0null byte()' }) set_cursor(1, 11) type_keys('i', '<CR>') eq(get_lines(), { '\0null byte(', '', ')' }) eq(get_cursor(), { 2, 0 })end
T['<CR> action']['works with multicursor'] = function() if child.fn.has('nvim-0.13') == 0 then MiniTest.skip('Multicursor is available in Neovim>=0.13') end
local validate = function(left, right) local lines_before = { left .. right, left .. right } local lines_after = { left, '', right, left, '', right } -- NOTE: Exit from Insert mode to have up to date text at extra cursor local keys = { 'Qj', 'i', '<CR>', '<Esc>' } child.validate_mc_edit(lines_before, { 1, 1 }, keys, lines_after, { 5, 0 }, { { 2, 0 } }) end
validate('(', ')') validate('[', ']') validate('{', '}')end
T['<CR> action']['respects `key` argument'] = function() child.api.nvim_set_keymap('i', '<C-u>', 'v:lua.MiniPairs.cr("\21")', { noremap = true, expr = true })
local validate_key = function(key, lines_before, cursor_before, lines_after, cursor_after) child.ensure_normal_mode() set_lines(lines_before) set_cursor(unpack(cursor_before)) type_keys('a')
type_keys(key)
eq(child.api.nvim_get_mode().mode, 'i') eq(get_lines(), lines_after) eq(get_cursor(), cursor_after) end
-- `<C-u>` validate_key('<C-u>', { 'aa()cc' }, { 1, 1 }, { '()cc' }, { 1, 0 }) validate_key('<C-u>', { 'aa()cc' }, { 1, 2 }, { '', ')cc' }, { 1, 0 }) validate_key('<C-u>', { 'aa()cc' }, { 1, 5 }, { '' }, { 1, 0 })end
T['<CR> action']['does not break undo sequence in Insert mode'] = function() set_lines({ '()' }) set_cursor(1, 1) child.cmd('startinsert')
type_keys('<CR>', 'a', '<Esc>') type_keys('u') eq(get_lines(), { '()' })end
T['<CR> action']['reindents the line'] = function() child.bo.cindent = true set_lines({ '{}' }) set_cursor(1, 1) child.cmd('startinsert')
type_keys('<CR>') eq(get_lines(), { '{', '\t', '}' })end
T['<CR> action']['does not trigger mode change'] = function() child.o.eventignore = 'CursorHold' set_lines({ '{}' }) set_cursor(1, 1) child.cmd('startinsert') child.cmd('au InsertLeave,InsertLeavePre,InsertEnter,TextChanged,ModeChanged * lua _G.n = (_G.n or 0) + 1') type_keys('<CR>') eq(child.lua_get('_G.n'), vim.NIL) eq(child.o.eventignore, 'CursorHold')end
local reload_unregister_cr = function() child.api.nvim_del_keymap('i', '<CR>') reload_module({ mappings = { ['('] = { register = { cr = false } }, ['['] = { register = { cr = false } }, ['{'] = { register = { cr = false } }, [')'] = { register = { cr = false } }, [']'] = { register = { cr = false } }, ['}'] = { register = { cr = false } }, ['"'] = { register = { cr = false } }, ["'"] = { register = { cr = false } }, ['`'] = { register = { cr = false } }, }, })end
T['<CR> action']['does not create mapping if nothing is registered in `config.mappings`'] = function() expect.match(child.cmd_capture('imap <CR>'), 'MiniPairs%.cr') reload_unregister_cr() expect.match(child.cmd_capture('imap <CR>'), 'No mapping found')end
T['<CR> action']['works as normal if nothing is registered'] = function() reload_unregister_cr()
set_lines({ '()' }) set_cursor(1, 1) child.cmd('startinsert')
type_keys('<CR>') eq(get_lines(), { '(', ')' }) eq(get_cursor(), { 2, 0 })end
T['<CR> action']['works with `feedkeys()`'] = function() child.cmd('inoremap <C-j> <Cmd>call nvim_feedkeys("(\\r(\\r", "", v:true)<CR>') type_keys('i', '<C-j>') eq(child.o.eventignore, '') eq(child.o.lazyredraw, false)end
T['<CR> action']['respects `vim.{g,b}.minipairs_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } },}, { test = function(var_type) child[var_type].minipairs_disable = true set_lines({ '()' }) set_cursor(1, 1) child.cmd('startinsert') type_keys('<CR>') eq(get_lines(), { '(', ')' }) end,})
return T