fix(api): nvim_exec and nvim_cmd restore msg_col when capturing output (#19789)

This matches the code in execute_common(), preventing messages after the
API call from being printed at the wrong column.
This commit is contained in:
zeertzjq
2022-08-16 17:30:39 +08:00
committed by GitHub
parent dee96f4725
commit 03fddfd928
3 changed files with 54 additions and 4 deletions

View File

@@ -626,6 +626,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
garray_T capture_local; garray_T capture_local;
const int save_msg_silent = msg_silent; const int save_msg_silent = msg_silent;
garray_T * const save_capture_ga = capture_ga; garray_T * const save_capture_ga = capture_ga;
const int save_msg_col = msg_col;
if (output) { if (output) {
ga_init(&capture_local, 1, 80); ga_init(&capture_local, 1, 80);
@@ -636,6 +637,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
try_start(); try_start();
if (output) { if (output) {
msg_silent++; msg_silent++;
msg_col = 0; // prevent leading spaces
} }
WITH_SCRIPT_CONTEXT(channel_id, { WITH_SCRIPT_CONTEXT(channel_id, {
@@ -645,6 +647,8 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
if (output) { if (output) {
capture_ga = save_capture_ga; capture_ga = save_capture_ga;
msg_silent = save_msg_silent; msg_silent = save_msg_silent;
// Put msg_col back where it was, since nothing should have been written.
msg_col = save_msg_col;
} }
try_end(err); try_end(err);

View File

@@ -49,6 +49,7 @@ String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err)
{ {
const int save_msg_silent = msg_silent; const int save_msg_silent = msg_silent;
garray_T *const save_capture_ga = capture_ga; garray_T *const save_capture_ga = capture_ga;
const int save_msg_col = msg_col;
garray_T capture_local; garray_T capture_local;
if (output) { if (output) {
ga_init(&capture_local, 1, 80); ga_init(&capture_local, 1, 80);
@@ -58,6 +59,7 @@ String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err)
try_start(); try_start();
if (output) { if (output) {
msg_silent++; msg_silent++;
msg_col = 0; // prevent leading spaces
} }
const sctx_T save_current_sctx = api_set_sctx(channel_id); const sctx_T save_current_sctx = api_set_sctx(channel_id);
@@ -66,6 +68,8 @@ String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err)
if (output) { if (output) {
capture_ga = save_capture_ga; capture_ga = save_capture_ga;
msg_silent = save_msg_silent; msg_silent = save_msg_silent;
// Put msg_col back where it was, since nothing should have been written.
msg_col = save_msg_col;
} }
current_sctx = save_current_sctx; current_sctx = save_current_sctx;

View File

@@ -281,8 +281,8 @@ describe('API', function()
]]} ]]}
end) end)
it('does\'t display messages when output=true', function() it('doesn\'t display messages when output=true', function()
local screen = Screen.new(40, 8) local screen = Screen.new(40, 6)
screen:attach() screen:attach()
screen:set_default_attr_ids({ screen:set_default_attr_ids({
[0] = {bold=true, foreground=Screen.colors.Blue}, [0] = {bold=true, foreground=Screen.colors.Blue},
@@ -293,11 +293,23 @@ describe('API', function()
{0:~ }| {0:~ }|
{0:~ }| {0:~ }|
{0:~ }| {0:~ }|
{0:~ }|
{0:~ }|
{0:~ }| {0:~ }|
| |
]]} ]]}
exec([[
func Print()
call nvim_exec('echo "hello"', v:true)
endfunc
]])
feed([[:echon 1 | call Print() | echon 5<CR>]])
screen:expect{grid=[[
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
15 |
]]}
end) end)
end) end)
@@ -3836,5 +3848,35 @@ describe('API', function()
meths.cmd({ cmd = 'make', args = { 'foo', 'bar' } }, {}) meths.cmd({ cmd = 'make', args = { 'foo', 'bar' } }, {})
assert_alive() assert_alive()
end) end)
it('doesn\'t display messages when output=true', function()
local screen = Screen.new(40, 6)
screen:attach()
screen:set_default_attr_ids({
[0] = {bold=true, foreground=Screen.colors.Blue},
})
meths.cmd({cmd = 'echo', args = {[['hello']]}}, {output = true})
screen:expect{grid=[[
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]]}
exec([[
func Print()
call nvim_cmd(#{cmd: 'echo', args: ['"hello"']}, #{output: v:true})
endfunc
]])
feed([[:echon 1 | call Print() | echon 5<CR>]])
screen:expect{grid=[[
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
15 |
]]}
end)
end) end)
end) end)