fix(api): nvim_buf_get_text regression (#21071)

This commit is contained in:
Lewis Russell 2022-11-15 21:27:42 +00:00 committed by GitHub
parent fd54194a4f
commit fa7e1e2601
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 16 deletions

View File

@ -311,7 +311,7 @@ ArrayOf(String) nvim_buf_get_lines(uint64_t channel_id,
init_line_array(lstate, &rv, size); init_line_array(lstate, &rv, size);
if (!buf_collect_lines(buf, size, (linenr_T)start, (channel_id != VIML_INTERNAL_CALL), &rv, if (!buf_collect_lines(buf, size, (linenr_T)start, 0, (channel_id != VIML_INTERNAL_CALL), &rv,
lstate, err)) { lstate, err)) {
goto end; goto end;
} }
@ -857,9 +857,8 @@ ArrayOf(String) nvim_buf_get_text(uint64_t channel_id, Buffer buffer,
} }
if (size > 2) { if (size > 2) {
Array tmp = ARRAY_DICT_INIT; if (!buf_collect_lines(buf, size - 2, (linenr_T)start_row + 1, 1, replace_nl, &rv, lstate,
tmp.items = &rv.items[1]; err)) {
if (!buf_collect_lines(buf, size - 2, (linenr_T)start_row + 1, replace_nl, &tmp, lstate, err)) {
goto end; goto end;
} }
} }
@ -1464,12 +1463,13 @@ static void push_linestr(lua_State *lstate, Array *a, const char *s, size_t len,
/// @param n Number of lines to collect /// @param n Number of lines to collect
/// @param replace_nl Replace newlines ("\n") with NUL /// @param replace_nl Replace newlines ("\n") with NUL
/// @param start Line number to start from /// @param start Line number to start from
/// @param start_idx First index to push to
/// @param[out] l If not NULL, Lines are copied here /// @param[out] l If not NULL, Lines are copied here
/// @param[out] lstate If not NULL, Lines are pushed into a table onto the stack /// @param[out] lstate If not NULL, Lines are pushed into a table onto the stack
/// @param err[out] Error, if any /// @param err[out] Error, if any
/// @return true unless `err` was set /// @return true unless `err` was set
bool buf_collect_lines(buf_T *buf, size_t n, linenr_T start, bool replace_nl, Array *l, bool buf_collect_lines(buf_T *buf, size_t n, linenr_T start, int start_idx, bool replace_nl,
lua_State *lstate, Error *err) Array *l, lua_State *lstate, Error *err)
{ {
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
linenr_T lnum = start + (linenr_T)i; linenr_T lnum = start + (linenr_T)i;
@ -1482,7 +1482,7 @@ bool buf_collect_lines(buf_T *buf, size_t n, linenr_T start, bool replace_nl, Ar
} }
char *bufstr = ml_get_buf(buf, lnum, false); char *bufstr = ml_get_buf(buf, lnum, false);
push_linestr(lstate, l, bufstr, strlen(bufstr), (int)i, replace_nl); push_linestr(lstate, l, bufstr, strlen(bufstr), start_idx + (int)i, replace_nl);
} }
return true; return true;

View File

@ -81,7 +81,7 @@ bool buf_updates_register(buf_T *buf, uint64_t channel_id, BufUpdateCallbacks cb
linedata.size = line_count; linedata.size = line_count;
linedata.items = xcalloc(line_count, sizeof(Object)); linedata.items = xcalloc(line_count, sizeof(Object));
buf_collect_lines(buf, line_count, 1, true, &linedata, NULL, NULL); buf_collect_lines(buf, line_count, 1, 0, true, &linedata, NULL, NULL);
} }
args.items[4] = ARRAY_OBJ(linedata); args.items[4] = ARRAY_OBJ(linedata);
@ -242,7 +242,7 @@ void buf_updates_send_changes(buf_T *buf, linenr_T firstline, int64_t num_added,
STATIC_ASSERT(SIZE_MAX >= MAXLNUM, "size_t smaller than MAXLNUM"); STATIC_ASSERT(SIZE_MAX >= MAXLNUM, "size_t smaller than MAXLNUM");
linedata.size = (size_t)num_added; linedata.size = (size_t)num_added;
linedata.items = xcalloc((size_t)num_added, sizeof(Object)); linedata.items = xcalloc((size_t)num_added, sizeof(Object));
buf_collect_lines(buf, (size_t)num_added, firstline, true, &linedata, buf_collect_lines(buf, (size_t)num_added, firstline, 0, true, &linedata,
NULL, NULL); NULL, NULL);
} }
args.items[4] = ARRAY_OBJ(linedata); args.items[4] = ARRAY_OBJ(linedata);

View File

@ -586,7 +586,8 @@ describe('api/buf', function()
before_each(function() before_each(function()
insert([[ insert([[
hello foo! hello foo!
text]]) text
more]])
end) end)
it('works', function() it('works', function()
@ -594,16 +595,17 @@ describe('api/buf', function()
eq({'hello foo!'}, get_text(0, 0, 0, 42, {})) eq({'hello foo!'}, get_text(0, 0, 0, 42, {}))
eq({'foo!'}, get_text(0, 6, 0, 10, {})) eq({'foo!'}, get_text(0, 6, 0, 10, {}))
eq({'foo!', 'tex'}, get_text(0, 6, 1, 3, {})) eq({'foo!', 'tex'}, get_text(0, 6, 1, 3, {}))
eq({'foo!', 'tex'}, get_text(-2, 6, -1, 3, {})) eq({'foo!', 'tex'}, get_text(-3, 6, -2, 3, {}))
eq({''}, get_text(0, 18, 0, 20, {})) eq({''}, get_text(0, 18, 0, 20, {}))
eq({'ext'}, get_text(-1, 1, -1, 4, {})) eq({'ext'}, get_text(-2, 1, -2, 4, {}))
eq({'hello foo!', 'text', 'm'}, get_text(0, 0, 2, 1, {}))
end) end)
it('errors on out-of-range', function() it('errors on out-of-range', function()
eq('Index out of bounds', pcall_err(get_text, 2, 0, 3, 0, {})) eq('Index out of bounds', pcall_err(get_text, 2, 0, 4, 0, {}))
eq('Index out of bounds', pcall_err(get_text, -3, 0, 0, 0, {})) eq('Index out of bounds', pcall_err(get_text, -4, 0, 0, 0, {}))
eq('Index out of bounds', pcall_err(get_text, 0, 0, 2, 0, {})) eq('Index out of bounds', pcall_err(get_text, 0, 0, 3, 0, {}))
eq('Index out of bounds', pcall_err(get_text, 0, 0, -3, 0, {})) eq('Index out of bounds', pcall_err(get_text, 0, 0, -4, 0, {}))
-- no ml_get errors should happen #19017 -- no ml_get errors should happen #19017
eq('', meths.get_vvar('errmsg')) eq('', meths.get_vvar('errmsg'))
end) end)