mirror of
https://github.com/neovim/neovim.git
synced 2025-09-15 15:58:17 +00:00
API: nvim_put #6819
This commit is contained in:
@@ -1206,21 +1206,18 @@ Dictionary nvim_get_namespaces(void)
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @param lines contents. One empty line for no-op, zero lines to emulate error
|
/// Inserts text at cursor.
|
||||||
|
///
|
||||||
|
/// Compare |:put| and |p| which are always linewise.
|
||||||
|
///
|
||||||
|
/// @param lines contents
|
||||||
/// @param type type ("c", "l", "b") or empty to guess from contents
|
/// @param type type ("c", "l", "b") or empty to guess from contents
|
||||||
/// @param name if emulates put from a register, otherwise empty
|
/// @param direction behave like |P| instead of |p|
|
||||||
/// @param prev True to emulate "P" otherwise "p"
|
/// @param[out] err Error details, if any
|
||||||
/// @param count repeat count
|
void nvim_put(ArrayOf(String) lines, String type, Boolean direction,
|
||||||
/// @param[out] err details of an error that have occurred, if any.
|
Error *err)
|
||||||
void nvim_put(ArrayOf(String) lines, String type, String regname, Boolean prev, Integer count, Error *err)
|
|
||||||
FUNC_API_SINCE(6)
|
FUNC_API_SINCE(6)
|
||||||
{
|
{
|
||||||
if (regname.size > 1) {
|
|
||||||
api_set_error(err,
|
|
||||||
kErrorTypeValidation,
|
|
||||||
"regname must be a single ASCII char or the empty string");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
yankreg_T *reg = xcalloc(sizeof(yankreg_T), 1);
|
yankreg_T *reg = xcalloc(sizeof(yankreg_T), 1);
|
||||||
if (!prepare_yankreg_from_object(reg, type, lines.size)) {
|
if (!prepare_yankreg_from_object(reg, type, lines.size)) {
|
||||||
api_set_error(err,
|
api_set_error(err,
|
||||||
@@ -1229,6 +1226,9 @@ void nvim_put(ArrayOf(String) lines, String type, String regname, Boolean prev,
|
|||||||
type.data);
|
type.data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (lines.size == 0) {
|
||||||
|
goto cleanup; // Nothing to do.
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < lines.size; i++) {
|
for (size_t i = 0; i < lines.size; i++) {
|
||||||
if (lines.items[i].type != kObjectTypeString) {
|
if (lines.items[i].type != kObjectTypeString) {
|
||||||
@@ -1244,7 +1244,6 @@ void nvim_put(ArrayOf(String) lines, String type, String regname, Boolean prev,
|
|||||||
|
|
||||||
finish_yankreg_from_object(reg, false);
|
finish_yankreg_from_object(reg, false);
|
||||||
|
|
||||||
int name = regname.size ? regname.data[0] : NUL;
|
|
||||||
bool VIsual_was_active = VIsual_active;
|
bool VIsual_was_active = VIsual_active;
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
if (State & INSERT) {
|
if (State & INSERT) {
|
||||||
@@ -1253,13 +1252,12 @@ void nvim_put(ArrayOf(String) lines, String type, String regname, Boolean prev,
|
|||||||
// TODO: fix VIsual when cursor is before, or emulate the delete as well
|
// TODO: fix VIsual when cursor is before, or emulate the delete as well
|
||||||
flags |= lt(VIsual, curwin->w_cursor) ? PUT_CURSEND : 0;
|
flags |= lt(VIsual, curwin->w_cursor) ? PUT_CURSEND : 0;
|
||||||
}
|
}
|
||||||
do_put(name, reg, prev ? BACKWARD : FORWARD, (long)count, flags);
|
do_put(0, reg, direction ? BACKWARD : FORWARD, 1, flags);
|
||||||
VIsual_active = VIsual_was_active;
|
VIsual_active = VIsual_was_active;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
free_register(reg);
|
free_register(reg);
|
||||||
xfree(reg);
|
xfree(reg);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Subscribes to event broadcasts.
|
/// Subscribes to event broadcasts.
|
||||||
|
@@ -5,6 +5,7 @@ local NIL = helpers.NIL
|
|||||||
local clear, nvim, eq, neq = helpers.clear, helpers.nvim, helpers.eq, helpers.neq
|
local clear, nvim, eq, neq = helpers.clear, helpers.nvim, helpers.eq, helpers.neq
|
||||||
local command = helpers.command
|
local command = helpers.command
|
||||||
local eval = helpers.eval
|
local eval = helpers.eval
|
||||||
|
local expect = helpers.expect
|
||||||
local funcs = helpers.funcs
|
local funcs = helpers.funcs
|
||||||
local iswin = helpers.iswin
|
local iswin = helpers.iswin
|
||||||
local meth_pcall = helpers.meth_pcall
|
local meth_pcall = helpers.meth_pcall
|
||||||
@@ -365,6 +366,40 @@ describe('API', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('nvim_put', function()
|
||||||
|
it('inserts text', function()
|
||||||
|
-- linewise
|
||||||
|
nvim('put', {'line 1','line 2','line 3'}, 'l', false)
|
||||||
|
expect([[
|
||||||
|
|
||||||
|
line 1
|
||||||
|
line 2
|
||||||
|
line 3]])
|
||||||
|
command('%delete _')
|
||||||
|
-- charwise
|
||||||
|
nvim('put', {'line 1','line 2','line 3'}, 'c', false)
|
||||||
|
expect([[
|
||||||
|
line 1
|
||||||
|
line 2
|
||||||
|
line 3]])
|
||||||
|
-- blockwise
|
||||||
|
nvim('put', {'AA','BB'}, 'b', false)
|
||||||
|
expect([[
|
||||||
|
lAAine 1
|
||||||
|
lBBine 2
|
||||||
|
line 3]])
|
||||||
|
command('%delete _')
|
||||||
|
-- Empty lines list.
|
||||||
|
nvim('put', {}, 'c', false)
|
||||||
|
expect([[]])
|
||||||
|
-- Single empty line.
|
||||||
|
nvim('put', {''}, 'c', false)
|
||||||
|
expect([[
|
||||||
|
]])
|
||||||
|
eq('', nvim('eval', 'v:errmsg'))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe('nvim_strwidth', function()
|
describe('nvim_strwidth', function()
|
||||||
it('works', function()
|
it('works', function()
|
||||||
eq(3, nvim('strwidth', 'abc'))
|
eq(3, nvim('strwidth', 'abc'))
|
||||||
@@ -626,12 +661,12 @@ describe('API', function()
|
|||||||
-- Make any RPC request (can be non-async: op-pending does not block).
|
-- Make any RPC request (can be non-async: op-pending does not block).
|
||||||
nvim('get_current_buf')
|
nvim('get_current_buf')
|
||||||
-- Buffer should not change.
|
-- Buffer should not change.
|
||||||
helpers.expect([[
|
expect([[
|
||||||
FIRST LINE
|
FIRST LINE
|
||||||
SECOND LINE]])
|
SECOND LINE]])
|
||||||
-- Now send input to complete the operator.
|
-- Now send input to complete the operator.
|
||||||
nvim('input', 'j')
|
nvim('input', 'j')
|
||||||
helpers.expect([[
|
expect([[
|
||||||
first line
|
first line
|
||||||
second line]])
|
second line]])
|
||||||
end)
|
end)
|
||||||
@@ -664,7 +699,7 @@ describe('API', function()
|
|||||||
nvim('get_api_info')
|
nvim('get_api_info')
|
||||||
-- Send input to complete the mapping.
|
-- Send input to complete the mapping.
|
||||||
nvim('input', 'd')
|
nvim('input', 'd')
|
||||||
helpers.expect([[
|
expect([[
|
||||||
FIRST LINE
|
FIRST LINE
|
||||||
SECOND LINE]])
|
SECOND LINE]])
|
||||||
eq('it worked...', helpers.eval('g:foo'))
|
eq('it worked...', helpers.eval('g:foo'))
|
||||||
@@ -680,7 +715,7 @@ describe('API', function()
|
|||||||
nvim('get_api_info')
|
nvim('get_api_info')
|
||||||
-- Send input to complete the mapping.
|
-- Send input to complete the mapping.
|
||||||
nvim('input', 'x')
|
nvim('input', 'x')
|
||||||
helpers.expect([[
|
expect([[
|
||||||
FIRST LINE
|
FIRST LINE
|
||||||
SECOND LINfooE]])
|
SECOND LINfooE]])
|
||||||
end)
|
end)
|
||||||
|
Reference in New Issue
Block a user