aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/api/buffer.c14
-rw-r--r--src/nvim/buffer_updates.c4
-rw-r--r--test/functional/api/buffer_spec.lua16
3 files changed, 18 insertions, 16 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index 1101689391..0a31286df7 100644
--- a/src/nvim/api/buffer.c
+++ b/src/nvim/api/buffer.c
@@ -311,7 +311,7 @@ ArrayOf(String) nvim_buf_get_lines(uint64_t channel_id,
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)) {
goto end;
}
@@ -857,9 +857,8 @@ ArrayOf(String) nvim_buf_get_text(uint64_t channel_id, Buffer buffer,
}
if (size > 2) {
- Array tmp = ARRAY_DICT_INIT;
- tmp.items = &rv.items[1];
- if (!buf_collect_lines(buf, size - 2, (linenr_T)start_row + 1, replace_nl, &tmp, lstate, err)) {
+ if (!buf_collect_lines(buf, size - 2, (linenr_T)start_row + 1, 1, replace_nl, &rv, lstate,
+ err)) {
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 replace_nl Replace newlines ("\n") with NUL
/// @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] lstate If not NULL, Lines are pushed into a table onto the stack
/// @param err[out] Error, if any
/// @return true unless `err` was set
-bool buf_collect_lines(buf_T *buf, size_t n, linenr_T start, bool replace_nl, Array *l,
- lua_State *lstate, Error *err)
+bool buf_collect_lines(buf_T *buf, size_t n, linenr_T start, int start_idx, bool replace_nl,
+ Array *l, lua_State *lstate, Error *err)
{
for (size_t i = 0; i < n; 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);
- 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;
diff --git a/src/nvim/buffer_updates.c b/src/nvim/buffer_updates.c
index bee7db1e98..2c92fb26b2 100644
--- a/src/nvim/buffer_updates.c
+++ b/src/nvim/buffer_updates.c
@@ -81,7 +81,7 @@ bool buf_updates_register(buf_T *buf, uint64_t channel_id, BufUpdateCallbacks cb
linedata.size = line_count;
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);
@@ -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");
linedata.size = (size_t)num_added;
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);
}
args.items[4] = ARRAY_OBJ(linedata);
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua
index 5980d99f97..6b13729994 100644
--- a/test/functional/api/buffer_spec.lua
+++ b/test/functional/api/buffer_spec.lua
@@ -586,7 +586,8 @@ describe('api/buf', function()
before_each(function()
insert([[
hello foo!
- text]])
+ text
+ more]])
end)
it('works', function()
@@ -594,16 +595,17 @@ describe('api/buf', function()
eq({'hello foo!'}, get_text(0, 0, 0, 42, {}))
eq({'foo!'}, get_text(0, 6, 0, 10, {}))
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({'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)
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, -3, 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, 2, 0, 4, 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, 3, 0, {}))
+ eq('Index out of bounds', pcall_err(get_text, 0, 0, -4, 0, {}))
-- no ml_get errors should happen #19017
eq('', meths.get_vvar('errmsg'))
end)