mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
api: set_text: more tests, and fixing lint
removing pending virtcol tests Allow passing in empty array as a shorthand for array with empty string; add more documentation add check for start_row as well
This commit is contained in:
parent
45b14f88db
commit
f7d01a65d5
@ -495,21 +495,24 @@ end:
|
|||||||
try_end(err);
|
try_end(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets (replaces) a range in the buffer.
|
/// Sets (replaces) a range in the buffer, retaining any extmarks
|
||||||
|
/// that may lie in that range.
|
||||||
///
|
///
|
||||||
/// Indexing is zero-based, end-exclusive.
|
/// Indexing is zero-based; end_row is inclusive, while end_col is
|
||||||
|
/// exclusive.
|
||||||
///
|
///
|
||||||
/// To insert text at a given index, set `start` and `end` ranges to the same
|
/// To insert text at a given index, set `start` and `end` ranges to the same
|
||||||
/// index. To delete a range, set `replacement` to an empty array.
|
/// index. To delete a range, set `replacement` to an array containing
|
||||||
|
/// an empty string, or simply an empty array.
|
||||||
///
|
///
|
||||||
/// Prefer nvim_buf_set_lines when modifying entire lines.
|
/// Prefer nvim_buf_set_lines when adding or deleting entire lines.
|
||||||
///
|
///
|
||||||
/// @param channel_id
|
/// @param channel_id
|
||||||
/// @param buffer Buffer handle, or 0 for current buffer
|
/// @param buffer Buffer handle, or 0 for current buffer
|
||||||
/// @param start_row First line index
|
/// @param start_row First line index
|
||||||
/// @param start_column Last column
|
/// @param start_column Last column
|
||||||
/// @param end_row Last line index (exclusive)
|
/// @param end_row Last line index (inclusive)
|
||||||
/// @param end_column Last column
|
/// @param end_column Last column (exclusive)
|
||||||
/// @param replacement Array of lines to use as replacement
|
/// @param replacement Array of lines to use as replacement
|
||||||
/// @param[out] err Error details, if any
|
/// @param[out] err Error details, if any
|
||||||
void nvim_buf_set_text(uint64_t channel_id,
|
void nvim_buf_set_text(uint64_t channel_id,
|
||||||
@ -522,8 +525,12 @@ void nvim_buf_set_text(uint64_t channel_id,
|
|||||||
Error *err)
|
Error *err)
|
||||||
FUNC_API_SINCE(7)
|
FUNC_API_SINCE(7)
|
||||||
{
|
{
|
||||||
// TODO: treat [] as [''] for convenience
|
if (replacement.size == 0) {
|
||||||
assert(replacement.size > 0);
|
String s = { .data = "", .size = 0 };
|
||||||
|
ADD(replacement, STRING_OBJ(s));
|
||||||
|
replacement.size = 1;
|
||||||
|
}
|
||||||
|
|
||||||
buf_T *buf = find_buffer_by_handle(buffer, err);
|
buf_T *buf = find_buffer_by_handle(buffer, err);
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
return;
|
return;
|
||||||
@ -534,13 +541,13 @@ void nvim_buf_set_text(uint64_t channel_id,
|
|||||||
// check range is ordered and everything!
|
// check range is ordered and everything!
|
||||||
// start_row, end_row within buffer len (except add text past the end?)
|
// start_row, end_row within buffer len (except add text past the end?)
|
||||||
start_row = normalize_index(buf, start_row, &oob);
|
start_row = normalize_index(buf, start_row, &oob);
|
||||||
if (oob) {
|
if (oob || start_row == buf->b_ml.ml_line_count + 1) {
|
||||||
api_set_error(err, kErrorTypeValidation, "start_row out of bounds");
|
api_set_error(err, kErrorTypeValidation, "start_row out of bounds");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
end_row = normalize_index(buf, end_row, &oob);
|
end_row = normalize_index(buf, end_row, &oob);
|
||||||
if (oob) {
|
if (oob || end_row == buf->b_ml.ml_line_count + 1) {
|
||||||
api_set_error(err, kErrorTypeValidation, "end_row out of bounds");
|
api_set_error(err, kErrorTypeValidation, "end_row out of bounds");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -568,6 +575,8 @@ void nvim_buf_set_text(uint64_t channel_id,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t new_len = replacement.size;
|
||||||
|
|
||||||
bcount_t new_byte = 0;
|
bcount_t new_byte = 0;
|
||||||
bcount_t old_byte = 0;
|
bcount_t old_byte = 0;
|
||||||
|
|
||||||
@ -575,29 +584,25 @@ void nvim_buf_set_text(uint64_t channel_id,
|
|||||||
if (start_row == end_row) {
|
if (start_row == end_row) {
|
||||||
old_byte = (bcount_t)end_col - start_col;
|
old_byte = (bcount_t)end_col - start_col;
|
||||||
} else {
|
} else {
|
||||||
char_u *line;
|
const char *bufline;
|
||||||
old_byte += (bcount_t)strlen(str_at_start) - start_col;
|
old_byte += (bcount_t)strlen(str_at_start) - start_col;
|
||||||
for (size_t i = 0; i < (bcount_t)end_row - start_row; i++){
|
for (int64_t i = 0; i < end_row - start_row; i++) {
|
||||||
int64_t lnum = start_row + (int64_t)i;
|
int64_t lnum = start_row + i;
|
||||||
|
|
||||||
if (lnum >= MAXLNUM) {
|
if (lnum >= MAXLNUM) {
|
||||||
api_set_error(err, kErrorTypeValidation, "Index value is too high");
|
api_set_error(err, kErrorTypeValidation, "Index value is too high");
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
line = ml_get_buf(buf, lnum, false);
|
bufline = (char *)ml_get_buf(buf, lnum, false);
|
||||||
old_byte += (bcount_t)(strlen((char *)line));
|
old_byte += (bcount_t)(strlen(bufline));
|
||||||
}
|
}
|
||||||
old_byte += end_col;
|
old_byte += (bcount_t)end_col;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t new_len = replacement.size;
|
|
||||||
|
|
||||||
String first_item = replacement.items[0].data.string;
|
String first_item = replacement.items[0].data.string;
|
||||||
String last_item = replacement.items[replacement.size-1].data.string;
|
String last_item = replacement.items[replacement.size-1].data.string;
|
||||||
|
|
||||||
new_byte += (bcount_t)(first_item.size);
|
|
||||||
|
|
||||||
size_t firstlen = (size_t)start_col+first_item.size;
|
size_t firstlen = (size_t)start_col+first_item.size;
|
||||||
size_t last_part_len = strlen(str_at_end) - (size_t)end_col;
|
size_t last_part_len = strlen(str_at_end) - (size_t)end_col;
|
||||||
if (replacement.size == 1) {
|
if (replacement.size == 1) {
|
||||||
@ -618,6 +623,7 @@ void nvim_buf_set_text(uint64_t channel_id,
|
|||||||
|
|
||||||
char **lines = (new_len != 0) ? xcalloc(new_len, sizeof(char *)) : NULL;
|
char **lines = (new_len != 0) ? xcalloc(new_len, sizeof(char *)) : NULL;
|
||||||
lines[0] = first;
|
lines[0] = first;
|
||||||
|
new_byte += (bcount_t)(first_item.size);
|
||||||
for (size_t i = 1; i < new_len-1; i++) {
|
for (size_t i = 1; i < new_len-1; i++) {
|
||||||
const String l = replacement.items[i].data.string;
|
const String l = replacement.items[i].data.string;
|
||||||
|
|
||||||
@ -641,6 +647,8 @@ void nvim_buf_set_text(uint64_t channel_id,
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Small note about undo states: unlike set_lines, we want to save the
|
||||||
|
// undo state of one past the end_row, since end_row is inclusive.
|
||||||
if (u_save((linenr_T)start_row - 1, (linenr_T)end_row + 1) == FAIL) {
|
if (u_save((linenr_T)start_row - 1, (linenr_T)end_row + 1) == FAIL) {
|
||||||
api_set_error(err, kErrorTypeException, "Failed to save undo information");
|
api_set_error(err, kErrorTypeException, "Failed to save undo information");
|
||||||
goto end;
|
goto end;
|
||||||
@ -718,8 +726,8 @@ void nvim_buf_set_text(uint64_t channel_id,
|
|||||||
colnr_T col_extent = (colnr_T)(end_col
|
colnr_T col_extent = (colnr_T)(end_col
|
||||||
- ((end_col > start_col) ? start_col : 0));
|
- ((end_col > start_col) ? start_col : 0));
|
||||||
extmark_splice(buf, (int)start_row-1, (colnr_T)start_col,
|
extmark_splice(buf, (int)start_row-1, (colnr_T)start_col,
|
||||||
(int)(end_row-start_row), col_extent, (bcount_t)(old_byte),
|
(int)(end_row-start_row), col_extent, old_byte,
|
||||||
(int)new_len-1, (colnr_T)last_item.size, (bcount_t)new_byte,
|
(int)new_len-1, (colnr_T)last_item.size, new_byte,
|
||||||
kExtmarkUndo);
|
kExtmarkUndo);
|
||||||
|
|
||||||
|
|
||||||
|
@ -394,7 +394,6 @@ describe('api/buf', function()
|
|||||||
|
|
||||||
describe('nvim_buf_get_lines, nvim_buf_set_text', function()
|
describe('nvim_buf_get_lines, nvim_buf_set_text', function()
|
||||||
local get_lines, set_text = curbufmeths.get_lines, curbufmeths.set_text
|
local get_lines, set_text = curbufmeths.get_lines, curbufmeths.set_text
|
||||||
local line_count = curbufmeths.line_count
|
|
||||||
|
|
||||||
it('works', function()
|
it('works', function()
|
||||||
insert([[
|
insert([[
|
||||||
@ -418,7 +417,7 @@ describe('api/buf', function()
|
|||||||
eq({'hello world!', 'text'}, get_lines(0, 2, true))
|
eq({'hello world!', 'text'}, get_lines(0, 2, true))
|
||||||
|
|
||||||
-- can replace with multiple lines
|
-- can replace with multiple lines
|
||||||
local err = set_text(0, 6, 0, 11, {'foo', 'wo', 'more'})
|
set_text(0, 6, 0, 11, {'foo', 'wo', 'more'})
|
||||||
eq({'hello foo', 'wo', 'more!', 'text'}, get_lines(0, 4, true))
|
eq({'hello foo', 'wo', 'more!', 'text'}, get_lines(0, 4, true))
|
||||||
|
|
||||||
-- will join multiple lines if needed
|
-- will join multiple lines if needed
|
||||||
@ -426,25 +425,10 @@ describe('api/buf', function()
|
|||||||
eq({'hello bar'}, get_lines(0, 1, true))
|
eq({'hello bar'}, get_lines(0, 1, true))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
pending('can handle multibyte characters', function()
|
|
||||||
insert([[
|
|
||||||
hellØ world!
|
|
||||||
]])
|
|
||||||
|
|
||||||
eq({'hellØ world!'}, get_lines(0, 1, true))
|
|
||||||
|
|
||||||
-- inserting multibyte
|
|
||||||
set_text(0, 11, 0, 11, {'Ø'})
|
|
||||||
eq({'hellØ worldØ!'}, get_lines(0, 1, true))
|
|
||||||
|
|
||||||
-- deleting multibyte
|
|
||||||
set_text(0, 0, 0, 6, {''})
|
|
||||||
eq({'worldØ!'}, get_lines(0, 1, true))
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('works with undo', function()
|
it('works with undo', function()
|
||||||
insert([[
|
insert([[
|
||||||
hello world!
|
hello world!
|
||||||
|
foo bar
|
||||||
]])
|
]])
|
||||||
|
|
||||||
-- setting text
|
-- setting text
|
||||||
@ -461,6 +445,11 @@ describe('api/buf', function()
|
|||||||
set_text(0, 0, 0, 0, {'hello', 'mr '})
|
set_text(0, 0, 0, 0, {'hello', 'mr '})
|
||||||
feed('u')
|
feed('u')
|
||||||
eq({'hello world!'}, get_lines(0, 1, true))
|
eq({'hello world!'}, get_lines(0, 1, true))
|
||||||
|
|
||||||
|
-- deleting newlines
|
||||||
|
set_text(0, 0, 1, 4, {'hello'})
|
||||||
|
feed('u')
|
||||||
|
eq({'hello world!'}, get_lines(0, 1, true))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('updates the cursor position', function()
|
it('updates the cursor position', function()
|
||||||
@ -493,30 +482,36 @@ describe('api/buf', function()
|
|||||||
local id3 = curbufmeths.set_extmark(ns, 1, 1, {})
|
local id3 = curbufmeths.set_extmark(ns, 1, 1, {})
|
||||||
set_text(0, 4, 0, 7, {"q"})
|
set_text(0, 4, 0, 7, {"q"})
|
||||||
|
|
||||||
-- TODO: if we set text at 0,3, what happens to the mark at 0,3
|
|
||||||
|
|
||||||
eq({'foo q', 'baz'}, get_lines(0, 2, true))
|
eq({'foo q', 'baz'}, get_lines(0, 2, true))
|
||||||
-- mark before replacement point is unaffected
|
-- mark before replacement point is unaffected
|
||||||
rv = curbufmeths.get_extmark_by_id(ns, id1, {})
|
eq({0, 1}, curbufmeths.get_extmark_by_id(ns, id1, {}))
|
||||||
eq({0, 1}, rv)
|
|
||||||
-- mark gets shifted back because the replacement was shorter
|
-- mark gets shifted back because the replacement was shorter
|
||||||
rv = curbufmeths.get_extmark_by_id(ns, id2, {})
|
eq({0, 5}, curbufmeths.get_extmark_by_id(ns, id2, {}))
|
||||||
eq({0, 5}, rv)
|
|
||||||
-- mark on the next line is unaffected
|
-- mark on the next line is unaffected
|
||||||
rv = curbufmeths.get_extmark_by_id(ns, id3, {})
|
eq({1, 1}, curbufmeths.get_extmark_by_id(ns, id3, {}))
|
||||||
eq({1, 1}, rv)
|
|
||||||
|
|
||||||
-- replacing the text spanning two lines will adjust the mark on the next line
|
-- replacing the text spanning two lines will adjust the mark on the next line
|
||||||
set_text(0, 3, 1, 3, {"qux"})
|
set_text(0, 3, 1, 3, {"qux"})
|
||||||
rv = curbufmeths.get_extmark_by_id(ns, id3, {})
|
|
||||||
eq({'fooqux', ''}, get_lines(0, 2, true))
|
eq({'fooqux', ''}, get_lines(0, 2, true))
|
||||||
eq({0, 6}, rv)
|
eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id3, {}))
|
||||||
-- but mark before replacement point is still unaffected
|
-- but mark before replacement point is still unaffected
|
||||||
rv = curbufmeths.get_extmark_by_id(ns, id1, {})
|
eq({0, 1}, curbufmeths.get_extmark_by_id(ns, id1, {}))
|
||||||
eq({0, 1}, rv)
|
|
||||||
-- and the mark in the middle was shifted to the end of the insertion
|
-- and the mark in the middle was shifted to the end of the insertion
|
||||||
rv = curbufmeths.get_extmark_by_id(ns, id2, {})
|
eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id2, {}))
|
||||||
eq({0, 6}, rv)
|
|
||||||
|
-- marks should be put back into the same place after undoing
|
||||||
|
set_text(0, 0, 0, 2, {''})
|
||||||
|
feed('u')
|
||||||
|
eq({0, 1}, curbufmeths.get_extmark_by_id(ns, id1, {}))
|
||||||
|
eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id2, {}))
|
||||||
|
eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id3, {}))
|
||||||
|
|
||||||
|
-- marks should be shifted over by the correct number of bytes for multibyte
|
||||||
|
-- chars
|
||||||
|
set_text(0, 0, 0, 0, {'Ø'})
|
||||||
|
eq({0, 3}, curbufmeths.get_extmark_by_id(ns, id1, {}))
|
||||||
|
eq({0, 8}, curbufmeths.get_extmark_by_id(ns, id2, {}))
|
||||||
|
eq({0, 8}, curbufmeths.get_extmark_by_id(ns, id3, {}))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user