fix(api)!: correctly deal with number before :tab

Now nvim_parse_cmd and nvim_create_user_command use a "tab" value which
is the same as the number passed before :tab modifier instead of the
number plus 1, and "tab" value is -1 if :tab modifier is not used.
This commit is contained in:
zeertzjq 2022-09-01 18:46:34 +08:00
parent 2afcdbd63a
commit 1ef7720567
6 changed files with 54 additions and 31 deletions

View File

@ -1846,7 +1846,7 @@ nvim_parse_cmd({str}, {opts}) *nvim_parse_cmd()*
• keeppatterns: (boolean) |:keeppatterns|.
• lockmarks: (boolean) |:lockmarks|.
• noswapfile: (boolean) |:noswapfile|.
• tab: (integer) |:tab|.
• tab: (integer) |:tab|. -1 when omitted.
• verbose: (integer) |:verbose|. -1 when omitted.
• vertical: (boolean) |:vertical|.
• split: (string) Split modifier string, is an empty string when

View File

@ -671,7 +671,7 @@ function M.open_page(count, smods, args)
local target = ('%s(%s)'):format(name, sect)
local ok, ret = pcall(function()
if not smods.tab and find_man() then
if smods.tab == -1 and find_man() then
vim.cmd.tag({ target, mods = { silent = true, keepalt = true } })
else
smods.silent = true

View File

@ -69,7 +69,7 @@
/// - keeppatterns: (boolean) |:keeppatterns|.
/// - lockmarks: (boolean) |:lockmarks|.
/// - noswapfile: (boolean) |:noswapfile|.
/// - tab: (integer) |:tab|.
/// - tab: (integer) |:tab|. -1 when omitted.
/// - verbose: (integer) |:verbose|. -1 when omitted.
/// - vertical: (boolean) |:vertical|.
/// - split: (string) Split modifier string, is an empty string when there's no split
@ -239,7 +239,7 @@ Dictionary nvim_parse_cmd(String str, Dictionary opts, Error *err)
PUT(mods, "unsilent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_UNSILENT));
PUT(mods, "sandbox", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SANDBOX));
PUT(mods, "noautocmd", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_NOAUTOCMD));
PUT(mods, "tab", INTEGER_OBJ(cmdinfo.cmdmod.cmod_tab));
PUT(mods, "tab", INTEGER_OBJ(cmdinfo.cmdmod.cmod_tab - 1));
PUT(mods, "verbose", INTEGER_OBJ(cmdinfo.cmdmod.cmod_verbose - 1));
PUT(mods, "browse", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_BROWSE));
PUT(mods, "confirm", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_CONFIRM));
@ -559,15 +559,17 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
}
if (HAS_KEY(mods.tab)) {
if (mods.tab.type != kObjectTypeInteger || mods.tab.data.integer < 0) {
VALIDATION_ERROR("'mods.tab' must be a non-negative Integer");
if (mods.tab.type != kObjectTypeInteger) {
VALIDATION_ERROR("'mods.tab' must be an Integer");
} else if ((int)mods.tab.data.integer >= 0) {
// Silently ignore negative integers to allow mods.tab to be set to -1.
cmdinfo.cmdmod.cmod_tab = (int)mods.tab.data.integer + 1;
}
cmdinfo.cmdmod.cmod_tab = (int)mods.tab.data.integer + 1;
}
if (HAS_KEY(mods.verbose)) {
if (mods.verbose.type != kObjectTypeInteger) {
VALIDATION_ERROR("'mods.verbose' must be a Integer");
VALIDATION_ERROR("'mods.verbose' must be an Integer");
} else if ((int)mods.verbose.data.integer >= 0) {
// Silently ignore negative integers to allow mods.verbose to be set to -1.
cmdinfo.cmdmod.cmod_verbose = (int)mods.verbose.data.integer + 1;

View File

@ -2082,7 +2082,7 @@ int nlua_do_ucmd(ucmd_T *cmd, exarg_T *eap, bool preview)
lua_newtable(lstate); // smods table
lua_pushinteger(lstate, cmdmod.cmod_tab);
lua_pushinteger(lstate, cmdmod.cmod_tab - 1);
lua_setfield(lstate, -2, "tab");
lua_pushinteger(lstate, cmdmod.cmod_verbose - 1);

View File

@ -136,7 +136,7 @@ describe('nvim_create_user_command', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -172,7 +172,7 @@ describe('nvim_create_user_command', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -208,7 +208,7 @@ describe('nvim_create_user_command', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -244,7 +244,7 @@ describe('nvim_create_user_command', function()
sandbox = false,
silent = false,
split = "botright",
tab = 0,
tab = -1,
unsilent = true,
verbose = -1,
vertical = false,
@ -280,7 +280,7 @@ describe('nvim_create_user_command', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -316,7 +316,7 @@ describe('nvim_create_user_command', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -364,7 +364,7 @@ describe('nvim_create_user_command', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -401,7 +401,7 @@ describe('nvim_create_user_command', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -448,7 +448,7 @@ describe('nvim_create_user_command', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -523,8 +523,29 @@ describe('nvim_create_user_command', function()
vim.api.nvim_cmd({ cmd = 'echo', args = { '&verbose' }, mods = opts.smods }, {})
end, {})
]]
eq("3", meths.cmd({ cmd = 'MyEcho', mods = { verbose = 3 } }, { output = true }))
eq(1, #meths.list_tabpages())
exec_lua[[
vim.api.nvim_create_user_command('MySplit', function(opts)
vim.api.nvim_cmd({ cmd = 'split', mods = opts.smods }, {})
end, {})
]]
meths.cmd({ cmd = 'MySplit' }, {})
eq(1, #meths.list_tabpages())
eq(2, #meths.list_wins())
meths.cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
eq(2, #meths.list_tabpages())
eq(2, funcs.tabpagenr())
meths.cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
eq(3, #meths.list_tabpages())
eq(2, funcs.tabpagenr())
meths.cmd({ cmd = 'MySplit', mods = { tab = 3 } }, {})
eq(4, #meths.list_tabpages())
eq(4, funcs.tabpagenr())
meths.cmd({ cmd = 'MySplit', mods = { tab = 0 } }, {})
eq(5, #meths.list_tabpages())
eq(1, funcs.tabpagenr())
end)
end)

View File

@ -3206,7 +3206,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -3248,7 +3248,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -3290,7 +3290,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -3332,7 +3332,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -3374,7 +3374,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -3416,7 +3416,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -3458,7 +3458,7 @@ describe('API', function()
sandbox = false,
silent = true,
split = "topleft",
tab = 2,
tab = 1,
unsilent = false,
verbose = 15,
vertical = false,
@ -3503,7 +3503,7 @@ describe('API', function()
verbose = 0,
vertical = false,
},
}, meths.parse_cmd('0verbose unsilent botright confirm filter! /foo/ split foo.txt', {}))
}, meths.parse_cmd('0verbose unsilent botright 0tab confirm filter! /foo/ split foo.txt', {}))
end)
it('works with user commands', function()
command('command -bang -nargs=+ -range -addr=lines MyCommand echo foo')
@ -3541,7 +3541,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -3583,7 +3583,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -3626,7 +3626,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
@ -3719,7 +3719,7 @@ describe('API', function()
sandbox = false,
silent = false,
split = "",
tab = 0,
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,