mirror of
https://github.com/neovim/neovim
synced 2025-07-18 18:21:46 +00:00
fix(api): load buffer first on nvim_buf_set_lines (#25823)
Fix #22670 Fix #8659
This commit is contained in:
@ -359,6 +359,14 @@ void nvim_buf_set_lines(uint64_t channel_id, Buffer buffer, Integer start, Integ
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loaded buffer first if it's not loaded
|
||||||
|
if (buf->b_ml.ml_mfp == NULL) {
|
||||||
|
if (!buf_ensure_loaded(buf)) {
|
||||||
|
api_set_error(err, kErrorTypeException, "Failed to load buffer");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool oob = false;
|
bool oob = false;
|
||||||
start = normalize_index(buf, start, true, &oob);
|
start = normalize_index(buf, start, true, &oob);
|
||||||
end = normalize_index(buf, end, true, &oob);
|
end = normalize_index(buf, end, true, &oob);
|
||||||
@ -396,10 +404,6 @@ void nvim_buf_set_lines(uint64_t channel_id, Buffer buffer, Integer start, Integ
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!buf_ensure_loaded(buf)) {
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (u_save_buf(buf, (linenr_T)(start - 1), (linenr_T)end) == FAIL) {
|
if (u_save_buf(buf, (linenr_T)(start - 1), (linenr_T)end) == 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;
|
||||||
@ -537,6 +541,14 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loaded buffer first if it's not loaded
|
||||||
|
if (buf->b_ml.ml_mfp == NULL) {
|
||||||
|
if (!buf_ensure_loaded(buf)) {
|
||||||
|
api_set_error(err, kErrorTypeException, "Failed to load buffer");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool oob = false;
|
bool oob = false;
|
||||||
|
|
||||||
// check range is ordered and everything!
|
// check range is ordered and everything!
|
||||||
@ -645,10 +657,6 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!buf_ensure_loaded(buf)) {
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Small note about undo states: unlike set_lines, we want to save the
|
// 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.
|
// undo state of one past the end_row, since end_row is inclusive.
|
||||||
if (u_save_buf(buf, (linenr_T)start_row - 1, (linenr_T)end_row + 1) == FAIL) {
|
if (u_save_buf(buf, (linenr_T)start_row - 1, (linenr_T)end_row + 1) == FAIL) {
|
||||||
|
@ -713,6 +713,21 @@ describe('api/buf', function()
|
|||||||
feed('<c-w>p')
|
feed('<c-w>p')
|
||||||
eq(3, funcs.winnr())
|
eq(3, funcs.winnr())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('set_lines on unloaded buffer #8659 #22670', function()
|
||||||
|
local bufnr = curbuf('get_number')
|
||||||
|
meths.buf_set_lines(bufnr, 0, -1, false, {'a', 'b', 'c'})
|
||||||
|
meths.buf_set_name(bufnr, 'set_lines')
|
||||||
|
finally(function()
|
||||||
|
os.remove('set_lines')
|
||||||
|
end)
|
||||||
|
command('write!')
|
||||||
|
command('new')
|
||||||
|
command('bunload! '..bufnr)
|
||||||
|
local new_bufnr = funcs.bufnr('set_lines', true)
|
||||||
|
meths.buf_set_lines(new_bufnr, 0, -1, false, {})
|
||||||
|
eq({''}, meths.buf_get_lines(new_bufnr, 0, -1, false))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('nvim_buf_set_text', function()
|
describe('nvim_buf_set_text', function()
|
||||||
|
Reference in New Issue
Block a user