local helpers = dofile('tests/helpers.lua') local child = helpers.new_child_neovim() local expect, eq = helpers.expect, helpers.expect.equality local new_set = MiniTest.new_set -- Helpers with child processes local load_module = function(config) child.mini_load('cmdline', config) end local unload_module = function(config) child.mini_unload('cmdline', config) end local set_cursor = function(...) return child.set_cursor(...) end local get_cursor = function(...) return child.get_cursor(...) end local set_lines = function(...) return child.set_lines(...) end local get_lines = function(...) return child.get_lines(...) end local type_keys = function(...) return child.type_keys(...) end local sleep = function(ms) helpers.sleep(ms, child) end local forward_lua = function(fun_str) return helpers.forward_lua(child, fun_str) end -- Create a helper for mock-typing every key separately because state tracking -- can depend on emulating one-by-one key presses (mostly for autocorrect). local slice_keys = function(keys) local res, cur_key = {}, '' for i = 1, vim.fn.strchars(keys) do cur_key = cur_key .. vim.fn.strcharpart(keys, i - 1, 1) if cur_key:sub(1, 1) ~= '<' or cur_key:sub(-1) == '>' then table.insert(res, cur_key) cur_key = '' end end return res end local type_every_key = function(...) ---@diagnostic disable-next-line: undefined-field local wait, keys = nil, vim.iter({ ... }):flatten(math.huge):totable() if type(keys[1]) == 'number' then wait, keys = keys[1], vim.list_slice(keys, 2) end local args = slice_keys(table.concat(keys, '')) if wait ~= nil then table.insert(args, 1, wait) end -- Always type keys one-by-one as this is needed to mock actual typing. -- Otherwise state tracking is not emulated properly child.type_keys(unpack(args)) end local test_dir = 'tests/dir-cmdline' -- Common test wrappers 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 expect_screenshot_after_keys = function(keys) type_keys(keys) child.expect_screenshot() end -- Data ======================================================================= local lines_101 = {} for i = 1, 101 do lines_101[i] = 'Line ' .. i end -- Time constants local small_time = helpers.get_time_const(10) -- Output test set ============================================================ local T = new_set({ hooks = { pre_case = function() -- Use `--noplugin` flag for easier testing without built-in plugins child.restart({ '--noplugin', '-u', 'scripts/minimal_init.lua' }) child.bo.readonly = false end, post_once = child.stop, n_retry = helpers.get_n_retry(2), }, }) -- Unit tests ================================================================= T['setup()'] = new_set() T['setup()']['creates side effects'] = function() load_module() -- Global variable eq(child.lua_get('type(_G.MiniCmdline)'), 'table') -- Autocommand group eq(child.fn.exists('#MiniCmdline'), 1) -- Highlight groups child.cmd('hi clear') load_module() local has_highlight = function(group, value) expect.match(child.cmd_capture('hi ' .. group), value) end has_highlight('MiniCmdlinePeekBorder', 'links to FloatBorder') has_highlight('MiniCmdlinePeekLineNr', 'links to DiagnosticSignWarn') has_highlight('MiniCmdlinePeekNormal', 'links to NormalFloat') has_highlight('MiniCmdlinePeekSign', 'links to DiagnosticSignHint') has_highlight('MiniCmdlinePeekSep', 'links to SignColumn') has_highlight('MiniCmdlinePeekTitle', 'links to FloatTitle') end T['setup()']['creates `config` field'] = function() load_module() eq(child.lua_get('type(_G.MiniCmdline.config)'), 'table') -- Check default values local expect_config = function(field, value) eq(child.lua_get('MiniCmdline.config.' .. field), value) end expect_config('autocomplete.enable', true) expect_config('autocomplete.delay', 0) expect_config('autocomplete.predicate', vim.NIL) expect_config('autocomplete.map_arrows', true) expect_config('autocorrect.enable', true) expect_config('autocorrect.func', vim.NIL) expect_config('autopeek.enable', true) expect_config('autopeek.n_context', 1) expect_config('autopeek.predicate', vim.NIL) expect_config('autopeek.window.config', {}) expect_config('autopeek.window.statuscolumn', vim.NIL) end T['setup()']['validates `config` argument'] = function() 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({ autocomplete = 1 }, 'autocomplete', 'table') expect_config_error({ autocomplete = { enable = 1 } }, 'autocomplete.enable', 'boolean') expect_config_error({ autocomplete = { predicate = 1 } }, 'autocomplete.predicate', 'function') expect_config_error({ autocomplete = { map_arrows = 1 } }, 'autocomplete.map_arrows', 'boolean') expect_config_error({ autocorrect = 1 }, 'autocorrect', 'table') expect_config_error({ autocorrect = { enable = 1 } }, 'autocorrect.enable', 'boolean') expect_config_error({ autocorrect = { func = 1 } }, 'autocorrect.func', 'function') expect_config_error({ autopeek = 1 }, 'autopeek', 'table') expect_config_error({ autopeek = { enable = 1 } }, 'autopeek.enable', 'boolean') expect_config_error({ autopeek = { window = 1 } }, 'autopeek.window', 'table') expect_config_error({ autopeek = { n_context = 'a' } }, 'autopeek.n_context', 'number') expect_config_error({ autopeek = { predicate = 1 } }, 'autopeek.predicate', 'callable') expect_config_error({ autopeek = { window = { config = 1 } } }, 'autopeek.window.config', 'table or callab') expect_config_error({ autopeek = { window = { statuscolumn = 1 } } }, 'autopeek.window.statuscolumn', 'callable') end T['setup()']['ensures colors'] = function() load_module() child.cmd('colorscheme default') expect.match(child.cmd_capture('hi MiniCmdlinePeekBorder'), 'links to FloatBorder') end T['setup()']['sets recommended option values'] = function() local check_wildmode = child.fn.has('nvim-0.11') == 1 load_module() eq(child.o.wildoptions, 'pum,fuzzy') if check_wildmode then eq(child.o.wildmode, 'noselect,full') end -- Should not set if was previously set child.o.wildoptions = 'pum' if check_wildmode then child.o.wildmode = 'full' end load_module() eq(child.o.wildoptions, 'pum') if check_wildmode then eq(child.o.wildmode, 'full') end -- Should only set 'wildmode' if autocomplete is enabled if check_wildmode then child.restart({ '--noplugin', '-u', 'scripts/minimal_init.lua' }) load_module({ autocomplete = { enable = false } }) eq(child.o.wildmode, 'full') end end T['default_autocomplete_predicate()'] = new_set() local default_autocomplete_predicate = forward_lua('MiniCmdline.default_autocomplete_predicate') T['default_autocomplete_predicate()']['works'] = function() load_module({ autocorrect = { enable = false }, autopeek = { enable = false } }) local line, pos, line_prev, pos_prev = '', 1, '', 1 eq(default_autocomplete_predicate({ line = line, pos = pos, line_prev = line_prev, pos_prev = pos_prev }), false) type_keys(':') local validate = function(key, ref) type_keys(key) line_prev, pos_prev = line, pos line, pos = child.fn.getcmdline(), child.fn.getcmdpos() eq(default_autocomplete_predicate({ line = line, pos = pos, line_prev = line_prev, pos_prev = pos_prev }), ref) end -- Should return `true` only if there is alphabetic character validate('1', false) validate(',', false) validate('2', false) validate(' ', false) validate('h', true) validate(' ', true) validate("'", true) type_keys('') line, pos, line_prev, pos_prev = '', 1, '', 1 type_keys(':') validate(' ', false) validate('h', true) validate(' ', true) validate("'", true) type_keys('') end T['default_autocorrect_func()'] = new_set({ hooks = { pre_case = load_module } }) local default_autocorrect_func = forward_lua('MiniCmdline.default_autocorrect_func') T['default_autocorrect_func()']['works'] = function() -- Most of the testing is done in 'Autocorrect' test case local validate = function(data, ref) eq(default_autocorrect_func(data), ref) end -- General validate({ word = 'ste', type = 'command' }, 'set') validate({ word = 'set', type = 'command' }, 'set') -- Strict type validate({ word = 'rndomhue', type = 'color' }, 'randomhue') -- Not strict type validate({ word = 'MniCmdline', type = 'augroup' }, 'MniCmdline') -- Empty string validate({ word = 'xxx', type = '' }, 'xxx') validate({ word = '', type = 'command' }, '') validate({ word = '', type = '' }, '') end T['default_autocorrect_func()']['respects `opts.strict_type`'] = function() local validate = function(data, ref) eq(default_autocorrect_func(data, { strict_type = false }), ref) end validate({ word = 'MniCmdline', type = 'augroup' }, 'MiniCmdline') validate({ word = 'Nrmal', type = 'highlight' }, 'Normal') end T['default_autocorrect_func()']['respects `opts.get_candidates`'] = function() local out = child.lua([[ _G.log = {} local get_cand = function(...) table.insert(_G.log, { ... }); return { 'xxx' } end return MiniCmdline.default_autocorrect_func({ word = 'ste', type = 'color' }, { get_candidates = get_cand }) ]]) eq(out, 'xxx') eq(child.lua_get('_G.log'), { { { type = 'color', word = 'ste' } } }) end T['default_autocorrect_func()']['validates arguments'] = function() expect.error(function() default_autocorrect_func(1) end, '`data`.*table') expect.error(function() default_autocorrect_func({ word = 1, type = 'command' }) end, '`data.word`.*string') expect.error(function() default_autocorrect_func({ word = 'ste', type = 1 }) end, '`data.type`.*string') end T['default_autopeek_predicate()'] = new_set() local default_autopeek_predicate = forward_lua('MiniCmdline.default_autopeek_predicate') T['default_autopeek_predicate()']['works'] = function() load_module() local validate = function(data, ref) eq(default_autopeek_predicate(data), ref) end -- Should mostly return `true` validate({ left = 1, right = 1, cmd = '' }, true) validate({ left = 10, right = 1, cmd = '' }, true) validate({ left = 1, right = 1, cmd = 'delete' }, true) validate({ left = 1, right = 1, cmd = 'sort' }, true) -- Should return `false` for (full) name of a command with preview -- - Built-in commands validate({ left = 1, right = 1, cmd = 'substitute' }, false) validate({ left = 1, right = 1, cmd = 'smagic' }, false) validate({ left = 1, right = 1, cmd = 'snomagic' }, false) -- - User commands child.lua([[ local opts = { preview = function() end, addr = 'lines', range = true } vim.api.nvim_create_user_command('WithPreview', function() end, opts) vim.api.nvim_buf_create_user_command(0, 'BufWithPreview', function() end, opts) ]]) validate({ left = 1, right = 1, cmd = 'WithPreview' }, false) validate({ left = 1, right = 1, cmd = 'BufWithPreview' }, false) end T['default_autopeek_statuscolumn()'] = new_set({ hooks = { pre_case = function() child.set_size(7, 15) set_lines(lines_101) load_module() child.o.showtabline, child.o.laststatus = 0, 0 end, }, }) local setup_statuscolumn = function(data, opts) child.lua('_G.data = ' .. vim.inspect(data)) child.lua('_G.opts = ' .. vim.inspect(opts)) child.lua([[ _G.test_statuscolumn = function() _G.res = MiniCmdline.default_autopeek_statuscolumn(_G.data, _G.opts) return _G.res end vim.wo.statuscolumn = '%{%v:lua.test_statuscolumn()%}' vim.cmd('redraw') ]]) -- Ensure more informative screenshots child.cmd('hi MiniCmdlinePeekLineNr guibg=Red ctermbg=Red') child.cmd('hi MiniCmdlinePeekSign guibg=Green ctermbg=Green') child.cmd('hi MiniCmdlinePeekSep guibg=Yellow ctermbg=Yellow') end local validate_statuscolumn = function(data, opts) setup_statuscolumn(data, opts) local last_line = child.o.lines child.expect_screenshot({ ignore_text = { last_line }, ignore_attr = { last_line } }) end T['default_autopeek_statuscolumn()']['works'] = function() -- Different type of ranges validate_statuscolumn({ left = 2, right = 5 }) validate_statuscolumn({ left = 4, right = 5 }) validate_statuscolumn({ left = 4, right = 4 }) validate_statuscolumn({ left = 5, right = 4 }) validate_statuscolumn({ left = 5, right = 2 }) -- Actually uses expected highlight groups local ref_pat = '^%%#MiniCmdlinePeekSign#.*%%#MiniCmdlinePeekLineNr#.*%%#MiniCmdlinePeekSep#│$' expect.match(child.lua_get('_G.res'), ref_pat) -- Line numbers should be aligned to the right, but their highlighting should -- start right after the sign (matters if different background). set_cursor(7, 0) child.cmd('normal! zt') validate_statuscolumn({ left = 8, right = 10 }) end T['default_autopeek_statuscolumn()']['works with wrapped and virtual lines'] = function() child.set_size(10, 15) set_lines({ '', 'Very big line number one', 'Very big line number two', '' }) local ns_id = child.api.nvim_create_namespace('Test') child.api.nvim_buf_set_extmark(0, ns_id, 1, 0, { virt_lines = { { { 'Virt' } } } }) child.api.nvim_buf_set_extmark(0, ns_id, 3, 0, { virt_lines = { { { 'Virt above' } } }, virt_lines_above = true }) validate_statuscolumn({ left = 2, right = 3 }) end T['default_autopeek_statuscolumn()']['respects `opts`'] = function() local opts = { signs = { same = '=', left = '<', mid = '-', right = '>', out = '*', virt = '$', wrap = '!' }, sep = '#', } -- All usages of `%` should be escaped as `%%` local opts_percent = { signs = { same = '%%', left = '%%', mid = '%%', right = '%%', out = '%%', virt = '%%', wrap = '%%' }, sep = '%%', } validate_statuscolumn({ left = 2, right = 5 }, opts) validate_statuscolumn({ left = 2, right = 5 }, opts_percent) set_lines({ '', 'Very big line number one', 'Very big line number two', '' }) local ns_id = child.api.nvim_create_namespace('Test') child.api.nvim_buf_set_extmark(0, ns_id, 1, 0, { virt_lines = { { { 'Virt' } } } }) validate_statuscolumn({ left = 2, right = 2 }, opts) validate_statuscolumn({ left = 2, right = 2 }, opts_percent) end -- Integration tests ========================================================== T['Autocomplete'] = new_set({ hooks = { pre_case = function() if child.fn.has('nvim-0.11') == 0 then MiniTest.skip('Autocompletion is available only on Neovim>=0.11') end load_module({ autocorrect = { enable = false }, autopeek = { enable = false } }) child.set_size(10, 20) child.o.pumheight, child.o.pumwidth = 5, 16 child.o.showtabline, child.o.laststatus = 0, 0 end, }, }) local has_pum = function() return child.fn.wildmenumode() == 1 end T['Autocomplete']['works'] = function() type_keys(':') -- Should be no completion at the start child.expect_screenshot() -- Should trigger after the first character expect_screenshot_after_keys('c') -- Should respect `wildmenu=pum,fuzzy`, which is set by default expect_screenshot_after_keys('m') -- Should react to text deletion expect_screenshot_after_keys('') expect_screenshot_after_keys('') end T['Autocomplete']["works with different 'wildchar'"] = function() child.cmd('set wildchar=') type_keys(':', 'b') eq(has_pum(), true) validate_cmdline('b') end T['Autocomplete']['works with different completion types'] = function() child.set_size(10, 30) child.fn.chdir(test_dir) child.o.wildoptions = 'pum' type_keys(':', 'set ig') child.expect_screenshot() type_keys('') type_keys(':', 'edit ', 'f') child.expect_screenshot() type_keys('') type_keys(':', 'grep ', 'f') child.expect_screenshot() type_keys('') end T['Autocomplete']['respects mappings'] = function() -- Should work when mapping enters command line mode local validate_enter = function(keys) type_keys(keys) validate_cmdline('a', 2) -- NOTE: would be good to make it work immediately, but not a big issue eq(has_pum(), false) type_keys('b') eq(has_pum(), true) type_keys('') -- Should work again later type_keys(':', 'a') eq(has_pum(), true) type_keys('') end child.cmd('nnoremap :a') validate_enter('') child.cmd('xnoremap :a') validate_enter('v') child.cmd('nnoremap :="a"') validate_enter('') child.cmd('xnoremap :="a"') validate_enter('v') -- Should not work if Command-line mode is both entered and exited child.cmd('nnoremap :sort') type_keys('') eq(has_pum(), false) eq(get_lines(), { '' }) end T['Autocomplete']['works in edge cases'] = function() -- After "!" type_keys(':', 'q', '!') validate_cmdline('q!') eq(has_pum(), false) type_keys('') -- With special commands local validate_special_cmd = function(key) set_lines({ 'Line 1', 'Line 2' }) type_keys(':', key) child.api.nvim_input('/') child.api.nvim_input('L') -- Completion in thiese special cases is available only on Neovim>=0.12 if child.fn.has('nvim-0.12') == 1 then child.expect_screenshot() else eq(has_pum(), false) end type_keys('') end -- :substitute validate_special_cmd('s') -- :global validate_special_cmd('g') -- :vimgrep validate_special_cmd('v') -- After mapping with nested command line mode child.cmd('nnoremap :=1') type_keys('') validate_cmdline('1', 1) type_keys('') type_keys(':', 'q') eq(has_pum(), true) end T['Autocomplete']['does not throw completion related errors'] = function() type_keys(':tag') -- NOTE: Use `nvim_input` instead of `type_keys()` because the latter checks -- `v:errmsg` to propagate the error. However, here on Neovim>=0.12 usage of -- `vim.fn.wildtrigger()` *does* set `errmsg` but no error is actual shown. child.api.nvim_input(' ') -- What actually should be tested is that there is no errors *shown*, but it -- doesn't look easy, as behavior in child process and manual testing differ. eq(child.cmd('messages'), '') expect.no_match(child.v.errmsg, 'cmdline%.lua') end T['Autocomplete']['is not triggered when wildmenu is visible'] = function() type_keys(':', 'c') eq(has_pum(), true) expect_screenshot_after_keys('') type_keys('') -- In combination with autocorrection child.lua('MiniCmdline.config.autocorrect.enable = true') child.fn.chdir(test_dir) child.cmd('argadd fileA fileB') type_keys(':', 'argdel fla', ' ') validate_cmdline('argdel fileA ') eq(has_pum(), true) end T['Autocomplete']['works in different command types'] = function() set_lines({ 'aa', 'aaa', 'aaaa' }) local validate = function(keys) eq(child.fn.mode(), 'n') type_every_key(keys) if child.fn.has('nvim-0.12') == 1 then child.expect_screenshot({ ignore_attr = true }) else eq(has_pum(), false) end type_keys('', '') end -- `/` and `?` can complete on Neovim>=0.12 validate({ '/', 'a' }) validate({ '?', 'a' }) -- Others command types - no completion validate({ 'i', '=', 'a' }) validate({ ':call input("")', 'a' }) end T['Autocomplete']['works with default predicate'] = function() type_keys(':', '1') eq(has_pum(), false) type_keys('s') eq(has_pum(), true) type_keys('') eq(has_pum(), false) end T['Autocomplete']['respects `config.autocomplete.delay`'] = function() local delay = 10 * small_time child.lua('MiniCmdline.config.autocomplete.delay = ' .. delay) type_keys(':', 'b') eq(has_pum(), false) sleep(delay - 2 * small_time) eq(has_pum(), false) -- Should implement debounce-style delay type_keys('u') sleep(delay - 2 * small_time) eq(has_pum(), false) sleep(4 * small_time) eq(has_pum(), true) -- Should work when pum is visible type_keys('f') sleep(delay - 2 * small_time) -- NOTE: Although no pum is present, on Neovim>=0.12 it is still drawn since -- that is how `wildtrigger()` works. This results in less flickering. eq(has_pum(), false) sleep(4 * small_time) eq(has_pum(), true) -- Should work when deleting text type_keys('') sleep(delay - 2 * small_time) eq(has_pum(), false) sleep(4 * small_time) eq(has_pum(), true) end T['Autocomplete']['should be triggered only during Command-line mode'] = function() local delay = 10 * small_time child.lua('MiniCmdline.config.autocomplete.delay = ' .. delay) set_lines({ 'Line 1', 'Line 2' }) set_cursor(1, 0) child.cmd('set wildchar=') type_keys(':', 'b') sleep(small_time) type_keys('') sleep(delay + small_time) eq(get_lines(), { 'Line 1', 'Line 2' }) eq(get_cursor(), { 1, 0 }) end T['Autocomplete']['respects `config.autocomplete.predicate`'] = function() child.lua([[ _G.res = true _G.predicate_log = {} MiniCmdline.config.autocomplete.predicate = function(...) table.insert(_G.predicate_log, { ... }) return _G.res end ]]) local validate_log = function(ref) eq(child.lua_get('_G.predicate_log'), ref) child.lua('_G.predicate_log = {}') end local has_012 = child.fn.has('nvim-0.12') == 1 -- Should be called with proper data type_keys(':') eq(has_pum(), false) validate_log({}) type_keys('1') validate_log({ { { line = '1', pos = 2, line_prev = '', pos_prev = 1 } } }) eq(has_pum(), not has_012) type_keys('s') validate_log({ { { line = '1s', pos = 3, line_prev = '1', pos_prev = 2 } } }) eq(has_pum(), true) type_keys('') validate_log({ { { line = '', pos = 1, line_prev = '1s', pos_prev = 3 } } }) eq(has_pum(), not has_012) type_keys('') -- Should not show if `false` child.lua('_G.res = false') type_keys(':') type_keys('s') eq(has_pum(), false) end T['Autocomplete']['respects `vim.{g,b}.minicmdline_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } }, }, { test = function(var_type) child[var_type].minicmdline_disable = true type_keys(':', 'b') eq(has_pum(), false) type_keys('') child[var_type].minicmdline_disable = false type_keys(':', 'b') eq(has_pum(), true) end, }) T['Autocorrect'] = new_set({ hooks = { pre_case = function() load_module({ autocomplete = { enable = false }, autopeek = { enable = false } }) end, }, }) T['Autocorrect']['works for commands'] = function() local validate = function(bad_word, ref_word) type_every_key(':', bad_word, ' ') validate_cmdline(ref_word .. ' ') type_keys('') end child.cmd('command MyCommand echo "Hello"') child.cmd('command Git echo "World"') -- Should correct for common edit types validate('bbuffer', 'buffer') -- Deletion --typos: ignore validate('uffer', 'buffer') -- Insertion validate('xuffer', 'buffer') -- Substitution validate('ste', 'set') -- Transposition -- Should respect abbreviations, even for user commands validate('nmor', 'nnor') validate('nmore', 'nnore') validate('MxC', 'MyC') validate('W', 'w') validate('Q', 'q') -- Should respect "ignore case" matches validate('WrItE', 'write') validate('W', 'w') validate('NNO', 'nno') validate('myc', 'MyC') -- Should prefer "respect case" abbreviation over "ignore case" validate('%g', '%g') -- Should prefer transposition validate('ua', 'au') -- Should work with bang type_keys(':', 'Q', '!') validate_cmdline('q!') type_keys('') -- Should work with range type_keys(':', "'a") validate_cmdline("'a") type_keys(',') validate_cmdline("'a,") type_keys("'b") validate_cmdline("'a,'b") type_keys('') -- Should work with command modifiers validate('abvelfet', 'aboveleft') -- Should not correct `:=` command -- validate('=123', '=123') validate('=_G', '=_G') end T['Autocorrect']['works for options'] = function() local validate_option = function(input, ref) -- In progress type_every_key(':', input, ' ') validate_cmdline(ref .. ' ') type_keys('') -- Final type_every_key(':', input, '') eq(child.fn.histget('cmd', -1), ref) end validate_option('set expndtb', 'set expandtab') validate_option('set noexpndtb', 'set noexpandtab') validate_option('set invexpndtb', 'set invexpandtab') validate_option('set ET', 'set et') --typos: ignore validate_option('set noET', 'set noet') validate_option('set invET', 'set invet') validate_option('setlocal expndtb', 'setlocal expandtab') -- Should work multiple times type_every_key(':set ET inorecase nowrtebakp invmgic ') validate_cmdline('set et ignorecase nowritebackup invmagic ') type_keys('') -- Correction when option is followed by not space local validate_option_char = function(char) type_every_key(':', 'set lstchars', char) validate_cmdline('set listchars' .. char) type_keys('') type_every_key(':', 'set LCS', char) validate_cmdline('set lcs' .. char) type_keys('') end validate_option_char('=') validate_option_char('?') validate_option_char('!') validate_option_char('&') validate_option_char('^') validate_option_char('+') validate_option_char('-') end T['Autocorrect']['works for other types'] = function() local validate_inprogress = function(input, ref) type_every_key(':', input, ' ') validate_cmdline(ref .. ' ') type_keys('') end local validate_final = function(input, ref) type_every_key(':', input, '') eq(child.fn.histget('cmd', -1), ref) end child.fn.chdir(test_dir) child.cmd([[let &runtimepath.=','.getcwd()]]) child.cmd([[let &packpath.=','.getcwd()]]) -- arglist child.cmd('argadd fileA fileB') validate_inprogress('argdel fla', 'argdel fileA') validate_final('argdel flb', 'argdel fileB') -- color -- - No in-progress check since `:colorscheme` expects single argument --typos: ignore validate_final('colorscheme dfault', 'colorscheme default') -- compiler -- - No in-progress check since `:compiler` expects single argument validate_final('compiler tstcompiler', 'compiler testcompiler') -- diff_buffer child.cmd('edit fileA') child.cmd('diffsplit fileB') -- - No in-progress check since `:diffput` expects single argument validate_final('diffput flA', 'diffput fileA') child.cmd('%bwipeout') child.cmd('only') -- event validate_inprogress('au Cmdlinelve', 'au CmdlineLeave') validate_final('au BfEnter', 'au BufEnter') type_keys('') -- filetype -- - No in-progress check since `:setfiletype` expects single argument --typos: ignore validate_final('setfiletype pthon', 'setfiletype python') -- history -- - No in-progress check since `:history` expects single argument validate_final('history srch', 'history search') -- keymap validate_inprogress('set keymap=tstkeymap', 'set keymap=testkeymap') validate_final('set keymap=tstkeymap', 'set keymap=testkeymap') -- locale -- - No in-progress check since `:language time` expects single argument -- No easy way to test locales on non-Linux if child.fn.has('linux') == 1 then validate_final('language time PSX', 'language time POSIX') end -- mapclear -- Neovim<0.11 has wrong `complpat` computation in this case if child.fn.has('nvim-0.11') == 1 then -- - No in-progress check since `:mapclear` expects single argument validate_final('mapclear bfr>', 'mapclear ') end -- messages -- - No in-progress check since `:messages` expects single argument validate_final('messages clr', 'messages clear') -- packadd -- - No in-progress check since `:packadd` expects single argument validate_final('packadd tstplugin', 'packadd testplugin') -- sign --typos: ignore validate_inprogress('sign dfine', 'sign define') validate_final('sign lst', 'sign list') -- syntax validate_inprogress('set syntax=LUA', 'set syntax=lua') validate_final('set syntax=LUA', 'set syntax=lua') -- syntime -- - No in-progress check since `:syntime` expects single argument validate_final('syntime clr', 'syntime clear') end T['Autocorrect']['works multiple times'] = function() type_every_key(':', 'srot', ' ') validate_cmdline('sort ') -- After delete type_every_key('', 'abvoelfet', ' ') validate_cmdline('aboveleft ') -- After another typed autocorrection type_every_key('noutocmd', ' ') validate_cmdline('aboveleft noautocmd ') type_every_key('sort', ' ') validate_cmdline('aboveleft noautocmd sort ') end T['Autocorrect']['works only in `:` command type'] = function() local validate = function(keys, ref) eq(child.fn.mode(), 'n') type_every_key(keys) validate_cmdline(ref) type_keys('', '') end validate('/srot ', 'srot ') validate('?srot ', 'srot ') validate('i=srot ', 'srot ') end T['Autocorrect']['respects mappings'] = function() -- Should work when mapping enters command line mode local validate_enter = function(keys) type_keys(keys) validate_cmdline('ehco', 5) type_keys(' ') validate_cmdline('echo ') type_keys('') -- Should work again later type_keys(':', 'ehco', ' ') validate_cmdline('echo ') type_keys('') end child.cmd('nnoremap :ehco') validate_enter('') child.cmd('xnoremap :ehco') validate_enter('v') child.cmd('nnoremap :="ehco"') validate_enter('') child.cmd('xnoremap :="ehco"') validate_enter('v') -- Should not work if Command-line mode is both entered and exited child.cmd('nnoremap :ehco') local ok, err = pcall(type_keys, '') eq(ok, false) eq(err, 'E492: Not an editor command: ehco') -- Should act if text increases latest word by a single character child.cmd('cnoremap w') type_keys(':', 'XX', '') validate_cmdline('XXw') type_keys(' ') validate_cmdline('bw ') type_keys('') -- Should act if text finishes the last word child.cmd('cnoremap ') type_keys(':', 'XX', '') validate_cmdline('ex ') type_keys('') end T['Autocorrect']['respects abbreviations'] = function() child.lua([[ _G.log = {} local orig = MiniCmdline.default_autocorrect_func MiniCmdline.default_autocorrect_func = function(...) table.insert(_G.log, vim.deepcopy({ ... })) return orig(...) end ]]) child.cmd('cabbrev Y wincmd') child.cmd('cnoreabbrev D bwipeout') type_keys(':', 'Y', ' ') validate_cmdline('wincmd ') type_keys('') type_keys(':', 'D', '') eq(child.fn.histget('cmd', -1), 'bwipeout') -- Should not call `config.autocorrect.func` for abbreviations eq(child.lua_get('_G.log'), {}) end T['Autocorrect']['ignores editing previous word'] = function() type_keys(':', 'set ', '') type_keys('') validate_cmdline('se ', 3) type_keys('T') validate_cmdline('seT ', 4) type_keys(' ') validate_cmdline('seT ', 5) type_keys('') end T['Autocorrect']['respects editing current word'] = function() if child.fn.has('nvim-0.11') == 0 then MiniTest.skip('Requires `CursorMovedC` event from Neovim>=0.11') end type_keys(':', 'te', '') validate_cmdline('te', 1) type_keys('s') validate_cmdline('ste', 2) type_keys('') validate_cmdline('ste', 4) type_keys(' ') validate_cmdline('set ', 5) end T['Autocorrect']['ignores navigating through history'] = function() child.cmd('command Icommand echo "Hello"') type_keys(':', 'Icommand ', '') -- Should not autocorrect `I` to `i` (as it happens in case of `:I`) type_keys(':', 'I', '') validate_cmdline('Icommand ') end T['Autocorrect']['correctly computes word to autocorrect'] = function() if child.fn.has('nvim-0.11') == 0 then MiniTest.skip('Requires `getcmdcomplpat()`, present in Neovim>=0.11') end local validate = function(text, final_key, ref) type_keys(':', text, final_key) validate_cmdline(ref) type_keys('') end validate("'a,'bsrot", ' ', "'a,'bsort ") validate("'a,'bsrot", '!', "'a,'bsort!") validate("'a,''srot", ' ', "'a,''sort ") end T['Autocorrect']['does not correct valid words'] = function() child.cmd('au CmdlineChanged * lua _G.n = (_G.n or 0) + 1') local validate = function(word) type_keys(':', word) child.lua('_G.n = 0') type_keys(' ') validate_cmdline(word .. ' ') eq(child.lua_get('_G.n'), 1) type_keys('') end validate('cnoremap') validate('cnor') child.cmd('command MyCommand echo "Hello"') validate('MyCommand') validate('MyC') child.cmd('vsplit') local win_id = child.api.nvim_get_current_win() type_keys(':', 'q', '!', '') eq(child.api.nvim_win_is_valid(win_id), false) end T['Autocorrect']['suggests only valid abbreviations'] = function() -- Should uniquely identify user commands child.cmd('command MyCommandA echo "Hello"') child.cmd('command MyCommandB echo "World"') --typos: ignore type_keys(':', 'MyComman', ' ') validate_cmdline('MyCommandA ') type_keys('') -- `:def` is not a valid abbreviation type_keys(':', 'def', ' ') validate_cmdline('de ') type_keys('') end T['Autocorrect']['respects `config.autocorrect.func`'] = function() child.lua([[ _G.log = {} MiniCmdline.config.autocorrect.func = function(data) table.insert(_G.log, vim.deepcopy(data)) return _G.correction end ]]) child.lua('_G.correction = "xxxx"') type_keys(':', 'srot') eq(child.lua_get('_G.log'), {}) type_keys(' ') -- NOTE: Should not autocorrect output validate_cmdline('xxxx ') eq(child.lua_get('_G.log'), { { word = 'srot', type = 'command' } }) child.lua('_G.log = {}') type_keys('') -- Should not change command line if output is same word or `nil` child.cmd('au CmdlineChanged * lua _G.n = (_G.n or 0) + 1') local validate_no_change = function(correction) child.lua('_G.correction = ' .. vim.inspect(correction)) type_keys(':', 'srot') child.lua('_G.n = 0') type_keys(' ') validate_cmdline('srot ') eq(child.lua_get('_G.n'), 1) type_keys('') end validate_no_change('srot') validate_no_change(nil) -- Handles improper output child.lua([[ _G.notify_log = {} vim.notify = function(...) table.insert(_G.notify_log, { ... }) end MiniCmdline.config.autocorrect.func = function() return 1 end ]]) type_keys(':', 'srot', ' ') validate_cmdline('srot ') local warn_level = child.lua_get('vim.log.levels.WARN') eq(child.lua_get('_G.notify_log'), { { '(mini.cmdline) Can not autocorrect for "srot"', warn_level } }) end T['Autocorrect']['works just before final '] = function() local validate = function(bad_word, ref_word) type_every_key(':', bad_word, '') eq(child.fn.histget('cmd', -1), ref_word) eq(child.fn.mode(), 'n') end local buf_id_other = child.api.nvim_create_buf(true, false) validate('bnxxt', 'bnext') eq(child.api.nvim_get_current_buf(), buf_id_other) type_keys('') -- Should work with `!` child.cmd('vsplit') local win_id = child.api.nvim_get_current_win() validate({ 'Q', '!' }, 'q!') eq(child.api.nvim_win_is_valid(win_id), false) end T['Autocorrect']['is not applied when abandoning command line'] = function() local validate = function(key) type_keys(':', 'xxxx', key) eq(child.fn.histget('cmd', -1), 'xxxx') end validate('') validate('') end T['Autocorrect']['uses correct state before final '] = function() child.lua([[ _G.log = {} MiniCmdline.config.autocorrect.func = function(data) table.insert(_G.log, vim.deepcopy(data)) return data.word end ]]) -- Changing `type` just before `` type_every_key(':', 'cnoremap C-x> www', '') type_every_key(':', 'cnoremap C-x> ww', '') validate_cmdline('cnoremap www') child.lua('_G.log = {}') type_keys('') local ref_word = child.fn.has('nvim-0.11') == 1 and ' www' or 'www' eq(child.lua_get('_G.log'), { { word = ref_word, type = 'mapping' } }) -- Should include `!` in the word (when using `vim.fn.getcmdcomplpat()`) if child.fn.has('nvim-0.11') == 0 then return end child.cmd('split') child.lua('_G.log = {}') type_every_key(':', 'q!', '') eq(child.lua_get('_G.log'), { { word = 'q!', type = '' } }) end T['Autocorrect']['respects `vim.{g,b}.minicmdline_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } }, }, { test = function(var_type) child[var_type].minicmdline_disable = true type_keys(':', 'seT', ' ') validate_cmdline('seT ') end, }) T['Autopeek'] = new_set({ hooks = { pre_case = function() load_module({ autocomplete = { enable = false }, autocorrect = { enable = false } }) child.set_size(11, 20) set_lines(lines_101) child.o.tabline, child.o.statusline = 'My tabline', 'My statusline' child.o.showtabline, child.o.laststatus = 2, 2 -- Ensure more informative screenshots child.cmd('hi MiniCmdlinePeekLineNr guibg=Red ctermbg=Red') child.cmd('hi MiniCmdlinePeekSign guibg=Green ctermbg=Green') child.cmd('hi MiniCmdlinePeekSep guibg=Yellow ctermbg=Yellow') end, }, }) local get_peek_win_id = function() local buf_id = child.api.nvim_get_current_buf() for _, win_id in ipairs(child.api.nvim_list_wins()) do local is_float = child.api.nvim_win_get_config(win_id).relative ~= '' if is_float and child.api.nvim_win_get_buf(win_id) == buf_id then return win_id end end end local get_peek_winheight = function() local peek_win_id = get_peek_win_id() return peek_win_id == nil and 0 or child.api.nvim_win_get_height(peek_win_id) end local validate_no_peek = function() eq(get_peek_winheight() == 0, true) end local validate_peek = function() eq(get_peek_winheight() > 0, true) end T['Autopeek']['works'] = function() type_keys(':') expect_screenshot_after_keys('2') -- Empty range end means cursor line expect_screenshot_after_keys(',') -- Explicit same line range should be the same as one number range expect_screenshot_after_keys('2') expect_screenshot_after_keys('0') -- Should truncate to last available line expect_screenshot_after_keys('0') -- Should update after deleting text expect_screenshot_after_keys('') expect_screenshot_after_keys('') expect_screenshot_after_keys('') -- Should work with blank line expect_screenshot_after_keys(' ') -- Exiting Command-line mode should hide peek window type_keys('') validate_no_peek() end T['Autopeek']['persists for the entirety of command typing'] = function() type_keys(':') expect_screenshot_after_keys('2') expect_screenshot_after_keys('delete ') expect_screenshot_after_keys('_ 3') type_keys('') eq(child.api.nvim_buf_get_lines(0, 0, 2, false), { 'Line 1', 'Line 5' }) end T['Autopeek']['correctly computes lines to show'] = function() type_keys(':') -- Apart, near edges expect_screenshot_after_keys('1,101') expect_screenshot_after_keys('2,101') expect_screenshot_after_keys('1,100') expect_screenshot_after_keys('2,100') -- Apart, away from edges expect_screenshot_after_keys('3,99') -- One line in between contexts (no fold should be visible) expect_screenshot_after_keys('3,7') -- Touching contexts expect_screenshot_after_keys('3,6') -- Intersecting contexts expect_screenshot_after_keys('3,5') -- Touching range lines expect_screenshot_after_keys('3,4') end T['Autopeek']['works with inverted range'] = function() type_keys(':') expect_screenshot_after_keys('101,1') expect_screenshot_after_keys('101,2') expect_screenshot_after_keys('100,1') expect_screenshot_after_keys('100,2') expect_screenshot_after_keys('99,3') expect_screenshot_after_keys('7,3') expect_screenshot_after_keys('6,3') expect_screenshot_after_keys('5,3') expect_screenshot_after_keys('4,3') end T['Autopeek']['can be hidden and opened within same session'] = function() type_keys(':') validate_no_peek() type_keys('2') validate_peek() type_keys(',') validate_peek() type_keys('') validate_no_peek() type_keys('1') validate_peek() type_keys('') validate_no_peek() end T['Autopeek']["works with 'inccommand'"] = function() -- Explicitly enable autopeek, as by default it is not shown for commands -- with preview child.lua('MiniCmdline.config.autopeek.predicate = function() return true end') type_keys(':') local validate = function(keys) child.api.nvim_input(keys) child.expect_screenshot() end validate('2s') validate('/') validate('Li') validate('/') validate('XX') validate('/') validate_no_peek() eq(child.api.nvim_buf_get_lines(0, 0, 2, false), { 'Line 1', 'XXne 2' }) end T['Autopeek']['respects mappings'] = function() -- Should work when mapping enters command line mode child.cmd('nnoremap :1,2') expect_screenshot_after_keys('') type_keys('') child.cmd('xnoremap :3,4') expect_screenshot_after_keys('v') type_keys('') child.cmd('nnoremap :="5,6"') expect_screenshot_after_keys('') type_keys('') child.cmd('xnoremap :="7,8"') expect_screenshot_after_keys('v') type_keys('') -- Should not work if Command-line mode is both entered and exited child.cmd('nnoremap :2,3sort') type_keys('') validate_no_peek() end T['Autopeek']['works in edge cases'] = function() -- After mapping with nested command line mode child.cmd('nnoremap :=1') type_keys('') validate_cmdline('1', 1) type_keys('2,') child.expect_screenshot() type_keys('') expect_screenshot_after_keys(':3') end T['Autopeek']['uses correct highlight groups'] = function() type_keys(':', '2') local win_id = get_peek_win_id() local peek_winhl = child.api.nvim_get_option_value('winhighlight', { scope = 'local', win = win_id }) expect.match(peek_winhl, 'NormalFloat:MiniCmdlinePeekNormal') expect.match(peek_winhl, 'FloatBorder:MiniCmdlinePeekBorder') expect.match(peek_winhl, 'FloatTitle:MiniCmdlinePeekTitle') end T['Autopeek']["ignores 'wrap'"] = function() set_lines({ 'Very big line number one', '', '', 'Very big line number two' }) child.o.wrap = true type_keys(':') expect_screenshot_after_keys('2') expect_screenshot_after_keys('2,3') expect_screenshot_after_keys('1,4') type_keys('') -- Should also respect global 'list' and 'listchars' type_keys(':') child.o.list = true child.o.listchars = 'precedes:<,extends:>' expect_screenshot_after_keys('2,3') end T['Autopeek']['works with virtual lines'] = function() local ns_id = child.api.nvim_create_namespace('Test') child.api.nvim_buf_set_extmark(0, ns_id, 1, 0, { virt_lines = { { { 'Virt' } } } }) child.api.nvim_buf_set_extmark(0, ns_id, 3, 0, { virt_lines = { { { 'Virt above' } } }, virt_lines_above = true }) -- Currently context computation doesn't take virtual lines into account. -- Ideally their extmarks should not be shown, but doing that is difficult. -- It is much easier to show buffer as is and document issue with virtual -- lines as a known limitation. type_keys(':') expect_screenshot_after_keys('2') expect_screenshot_after_keys('4') expect_screenshot_after_keys('2,4') type_keys('') -- Zero context should work better child.lua('MiniCmdline.config.autopeek.n_context = 0') type_keys(':') expect_screenshot_after_keys('2') expect_screenshot_after_keys('4') expect_screenshot_after_keys('2,4') end T['Autopeek']['hides visual selection'] = function() child.set_size(15, 20) child.tweak_visual_guicursor() -- The range preview is already a preview of Visual selection set_cursor(2, 1) type_keys('v', '2j', ':') child.expect_screenshot() eq(get_cursor(), { 2, 1 }) type_keys('') -- Should be overridable via autocommand before `require('mini.cmdline')` unload_module() child.lua([[ local disable = vim.schedule_wrap(function() local is_from_visual = vim.startswith(vim.fn.getcmdline(), "'<,'>") MiniCmdline.config.autopeek.enable = not is_from_visual end) local reenable = function() MiniCmdline.config.autopeek.enable = true end vim.api.nvim_create_autocmd('CmdlineEnter', { callback = disable }) vim.api.nvim_create_autocmd('CmdlineLeave', { callback = reenable }) ]]) load_module() type_keys('v', '2j', ':') child.expect_screenshot({ redraw = false }) type_keys('') type_keys(':', '5') validate_peek() end T['Autopeek']['works with every kind of line range'] = function() set_cursor(3, 0) type_keys('Vj', '') set_cursor(10, 1) type_keys('ma') set_cursor(12, 0) type_keys('mb') set_cursor(2, 0) type_keys(':') -- `:h :range` expect_screenshot_after_keys('.,$') expect_screenshot_after_keys('%') expect_screenshot_after_keys('*') expect_screenshot_after_keys("','>") expect_screenshot_after_keys("'a,'b") expect_screenshot_after_keys("'b,'a") expect_screenshot_after_keys('/Line 5/,/Line 4/') expect_screenshot_after_keys('/Line 5/;/Line 4/') expect_screenshot_after_keys('?Line 5?,?Line 6?') expect_screenshot_after_keys('?Line 5?;?Line 6?') -- `:h range-offset` expect_screenshot_after_keys('++,-') expect_screenshot_after_keys("'+2,'a--") expect_screenshot_after_keys('/5/+100,/4/-100') end T['Autopeek']['is shown only for line range'] = function() type_keys(':') -- Should assume line range by default type_keys('%') validate_peek() -- `:bwipeout` doesn't treat range as lines, see `:h :command-addr` type_keys('bw') validate_no_peek() type_keys('') validate_peek() end T['Autopeek']['is not shown for commands with preview'] = function() local validate = function(command_keys) type_keys(':', '2') type_keys(command_keys) validate_no_peek() type_keys('') end -- Built-in commands validate('substitute') validate('s') validate('smagic') validate('sm') validate('snomagic') validate('sno') -- User commands child.lua([[ local opts = { preview = function() end, addr = 'lines', range = true } vim.api.nvim_create_user_command('WithPreview', function() end, opts) vim.api.nvim_buf_create_user_command(0, 'BufWithPreview', function() end, opts) ]]) validate('WithPreview') validate('WithP') validate('BufWithPreview') validate('BufWithP') end T['Autopeek']['works when regular command line parsing fails'] = function() type_keys('/', 'Line 10', '') child.cmd('nohlsearch') type_keys(':') -- Line with only range expect_screenshot_after_keys('%') -- Unfinished `/xxx/` range expect_screenshot_after_keys('/') expect_screenshot_after_keys('Li') expect_screenshot_after_keys('ne 4') expect_screenshot_after_keys('/,') expect_screenshot_after_keys('/Line 6') -- Unrecognized command expect_screenshot_after_keys('1xxx') expect_screenshot_after_keys('1,2xxx') expect_screenshot_after_keys('%xxx') end T['Autopeek']['reacts to command line height change during typing'] = function() child.set_size(10, 15) local long_line = string.rep('a', 16) type_keys(':', '2,5') -- Should reposition on top of new command line height expect_screenshot_after_keys(long_line) -- Should adjust height if not enough space expect_screenshot_after_keys(long_line) expect_screenshot_after_keys(long_line) end T['Autopeek']['reacts to `VimResized`'] = function() child.set_size(10, 20) type_keys(':') expect_screenshot_after_keys('2,5') child.set_size(10, 25) child.expect_screenshot() child.set_size(15, 15) child.expect_screenshot() child.set_size(6, 20) child.expect_screenshot() end T['Autopeek']["works with different 'cmdheight'"] = function() child.o.cmdheight = 0 expect_screenshot_after_keys(':2,5') type_keys('') child.o.cmdheight = 2 expect_screenshot_after_keys(':2,5') type_keys('') end T['Autopeek']['works only in `:` command type'] = function() local validate = function(keys) eq(child.fn.mode(), 'n') type_keys(keys) validate_no_peek() type_keys('', '') end validate({ '/', '2' }) validate({ '?', '2' }) validate({ 'i', '=', '2' }) validate({ 'call input("")', '2' }) end T['Autopeek']['is shown below pmenu and output'] = function() type_keys(':', '1,2sor') expect_screenshot_after_keys('') -- Testing via screenshot requires `{ redraw = false }`, but it displays peek -- window in wrong place (not like when testing interactively). -- So test indirectly via verifying `zindex` (should be below 200). local peek_win_id = get_peek_win_id() eq(child.api.nvim_win_get_config(peek_win_id).zindex, 199) end T['Autopeek']['respects `config.autopeek.window.config`'] = function() -- As table child.lua([[MiniCmdline.config.autopeek.window.config = { border = 'none', width = 15 }]]) expect_screenshot_after_keys({ ':', '2' }) type_keys('') -- As callable child.lua([[MiniCmdline.config.autopeek.window.config = function() return { border = 'double', width = 0.25 * vim.o.columns, height = 5, title = 'Custom title to check truncation' } end]]) expect_screenshot_after_keys({ ':', '2' }) type_keys('') -- Should properly truncate title child.lua([[ local title = 'abcdefgijklmnopqrstuvwxyz😈ツ' MiniCmdline.config.autopeek.window.config = { width = vim.o.columns, title = title } ]]) expect_screenshot_after_keys({ ':', '2' }) end T['Autopeek']['respects `config.autopeek.window.statuscolumn`'] = function() child.lua([[ MiniCmdline.config.autopeek.window.statuscolumn = function(...) _G.args = { ... } return '!' .. vim.v.lnum end ]]) set_cursor(3, 0) type_keys(':') local validate = function(key, ref_args) type_keys(key) eq(child.lua_get('_G.args'), ref_args) child.lua('_G.args = nil') end validate('2', { { left = 2, right = 2, cmd = '' } }) child.expect_screenshot() validate(',', { { left = 2, right = 3, cmd = '' } }) validate('4', { { left = 2, right = 4, cmd = '' } }) validate('d', { { left = 2, right = 4, cmd = 'delete' } }) validate('', { { left = 2, right = 4, cmd = '' } }) end T['Autopeek']["respects 'winborder'"] = function() if child.fn.has('nvim-0.11') == 0 then MiniTest.skip("'winborder' option is present on Neovim>=0.11") end local validate = function(winborder) child.o.winborder = winborder type_keys(':') expect_screenshot_after_keys(':2,100') type_keys('') end validate('rounded') -- Should prefer explicitly configured value over 'winborder' child.lua('MiniCmdline.config.autopeek.window.config = { border = "double" }') validate('rounded') -- Should work with "string array" 'winborder' if child.fn.has('nvim-0.12') == 0 then MiniTest.skip("String array 'winborder' is present on Neovim>=0.12") end child.lua('MiniCmdline.config.autopeek.window.config.border = nil') validate('+,-,+,|,+,-,+,|') end T['Autopeek']['correctly shows window without border'] = function() child.lua('MiniCmdline.config.autopeek.window.config = { border = "none" }') local validate = function(screen_lines, range) child.set_size(screen_lines, 20) type_keys(':') expect_screenshot_after_keys(range) type_keys('') end validate(10, '1,101') validate(10, '2,100') validate(10, '2,5') validate(10, '2,2') -- No context child.lua('MiniCmdline.config.autopeek.n_context = 0') validate(10, '2,101') -- Extreme available height validate(4, '2,4') end T['Autopeek']['respects `config.autopeek.n_context`'] = function() child.lua('MiniCmdline.config.autopeek.n_context = 0') type_keys(':') expect_screenshot_after_keys('1,101') expect_screenshot_after_keys('2,101') expect_screenshot_after_keys('1,100') expect_screenshot_after_keys('2,100') expect_screenshot_after_keys('2,4') expect_screenshot_after_keys('2,3') expect_screenshot_after_keys('2,2') end T['Autopeek']['respects `config.autopeek.predicate`'] = function() child.lua([[ _G.log = {} MiniCmdline.config.autopeek.predicate = function(data) table.insert(_G.log, vim.deepcopy(data)) return data.left > 9 and data.right > 9 and (data.cmd == '' or data.cmd == 'change') end ]]) local validate = function(key, is_shown, ref_log) child.lua('_G.log = {}') type_keys(key) if is_shown then return validate_peek() end if not is_shown then validate_no_peek() end eq(child.lua_get('_G.log'), ref_log) end set_cursor(9, 0) type_keys(':') validate('1', false, { { left = 1, right = 1, cmd = '' } }) validate('0', true, { { left = 10, right = 10, cmd = '' } }) validate(',', false, { { left = 10, right = 9, cmd = '' } }) validate('1', false, { { left = 10, right = 1, cmd = '' } }) validate('0', true, { { left = 10, right = 10, cmd = '' } }) validate('c', true, { { left = 10, right = 10, cmd = 'change' } }) validate('b', false, { { left = 10, right = 10, cmd = 'cbuffer' } }) -- Should be called when deleting text type_keys('') validate('', false, { { left = 1, right = 10, cmd = 'cbuffer' } }) type_keys('') -- Should be called only for valid buffer lines range type_keys(':') validate('1', false, { { left = 1, right = 1, cmd = '' } }) validate('d', false, { { left = 1, right = 1, cmd = 'delete' } }) validate('x', false, { { left = 1, right = 1, cmd = '' } }) type_keys('') type_keys(':') validate('%', false, { { left = 1, right = 101, cmd = '' } }) validate('b', false, {}) validate('w', false, {}) type_keys('') end T['Autopeek']['fits into available height'] = function() child.lua('MiniCmdline.config.autopeek.n_context = 2') local validate = function(screen_lines, range) child.set_size(screen_lines, 20) type_keys(':') expect_screenshot_after_keys(range) type_keys('') end validate(14, '3,10') -- Should first hide outer context equally from top and bottom validate(13, '3,10') validate(12, '3,10') validate(11, '3,10') validate(10, '3,10') -- Should then hide excsess under the fold validate(9, '3,10') validate(8, '3,10') validate(7, '3,10') validate(6, '3,10') -- Should work with extreme available height validate(5, '3,10') validate(4, '3,10') -- Should work with different kinds of context overlap -- - Touching validate(13, '3,8') validate(12, '3,8') validate(11, '3,8') validate(10, '3,8') validate(9, '3,8') validate(8, '3,8') validate(7, '3,8') validate(6, '3,8') validate(5, '3,8') -- - Inner intersecting validate(11, '3,6') validate(10, '3,6') validate(9, '3,6') validate(8, '3,6') validate(7, '3,6') validate(6, '3,6') validate(5, '3,6') -- - Outer intersecting validate(9, '3,4') validate(8, '3,4') validate(7, '3,4') validate(6, '3,4') validate(5, '3,4') -- Inverted range (only selective basic sanity checks) validate(12, '10,3') validate(9, '10,3') validate(12, '8,3') validate(8, '8,3') validate(8, '4,3') validate(5, '4,3') end T['Autopeek']['updates when navigating through history'] = function() type_keys(':', '2,5sort', '') type_keys(':', 'echo', '') type_keys(':') expect_screenshot_after_keys('10') expect_screenshot_after_keys('') expect_screenshot_after_keys('') expect_screenshot_after_keys('') end T['Autopeek']['works with command window'] = function() -- Command window seems to block child process, so use a workaround to test -- if autopeek was shown child.lua([[ _G.statuscolumn_was_used = false MiniCmdline.config.autopeek.window.statuscolumn = function(...) _G.statuscolumn_was_used = true return MiniCmdline.default_autopeek_statuscolumn(...) end ]]) -- Should not be shown if command is typed inside command window -- NOTE: It would be good to also show it, but it would require much more -- explicit work (`CmdlineChanged` is not triggered in cmdwin) type_keys('q:', '2', ',5', '') eq(child.lua_get('_G.statuscolumn_was_used'), false) -- Should not be shown for command window buffer. type_keys('q:') type_keys(':', '1', '') type_keys('') eq(child.lua_get('_G.statuscolumn_was_used'), false) -- Should hide already visible peek when entering command window type_keys(':', '2') validate_peek() child.lua('_G.statuscolumn_was_used = false') type_keys('') type_keys(',5', '') eq(child.lua_get('_G.statuscolumn_was_used'), false) end T['Autopeek']['works when using command-line expression register'] = function() type_keys(':', '2,', '=', '10+10') -- Should not react to "range like" content when entering register eq(get_peek_winheight(), 3) type_keys('') child.expect_screenshot() -- Should keep working as if the number was typed manually type_keys('+') child.expect_screenshot() type_keys('') validate_no_peek() end T['Autopeek']['respects `vim.{g,b}.minicmdline_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } }, }, { test = function(var_type) child[var_type].minicmdline_disable = true type_keys(':', '2') validate_no_peek() type_keys('') child[var_type].minicmdline_disable = false type_keys(':', '2') validate_peek() end, }) T['Arrow mappings'] = new_set() T['Arrow mappings']['work horizontally'] = function() load_module({ autocomplete = { enable = false } }) -- Left/Right - always move cursor regardless of pum type_keys(':', 'so', '') eq(has_pum(), true) type_keys('') validate_cmdline('sort', 4) eq(has_pum(), false) type_keys('') eq(has_pum(), true) type_keys('') validate_cmdline('sortt', 6) eq(has_pum(), false) end T['Arrow mappings']['work vertically'] = function() load_module({ autocomplete = { enable = false } }) -- Disable fuzzy matching for easier testing child.o.wildoptions = 'pum' -- Up/Down - navigate history regardless of pum type_keys(':', 'setglobal expandtab', '') type_keys(':', 'setglobal ignorecase', '') type_keys(':', 'set', '') eq(has_pum(), true) type_keys('') validate_cmdline('setglobal ignorecase') eq(has_pum(), false) type_keys('') validate_cmdline('setglobal expandtab') eq(has_pum(), false) type_keys('set', '') eq(has_pum(), true) type_keys('') validate_cmdline('setglobal ignorecase') eq(has_pum(), false) type_keys('') validate_cmdline('set') eq(has_pum(), false) end T['Arrow mappings']['respect `config.autocomplete.map_arrows`'] = function() load_module({ autocomplete = { map_arrows = false } }) local validate = function(key) expect.match(child.cmd_capture('cnoremap ' .. key), 'No mapping found') end validate('') validate('') validate('') validate('') end return T