refactor(change): do API changes to buffer without curbuf switch

Most of the messy things when changing a non-current buffer is
not about the buffer, it is about windows. In particular, it is about
`curwin`.

When editing a non-current buffer which is displayed in some other
window in the current tabpage, one such window will be "borrowed" as the
curwin. But this means if two or more non-current windows displayed the buffers,
one of them will be treated differenty. this is not desirable.

In particular, with nvim_buf_set_text, cursor _column_ position was only
corrected for one single window. Two new tests are added: the test
with just one non-current window passes, but the one with two didn't.

Two corresponding such tests were also added for nvim_buf_set_lines.
This already worked correctly on master, but make sure this is
well-tested for future refactors.

Also, nvim_create_buf no longer invokes autocmds just because you happened
to use `scratch=true`. No option value was changed, therefore OptionSet
must not be fired.
This commit is contained in:
bfredl
2023-08-21 14:52:17 +02:00
parent 1635c9e75e
commit 0081549547
33 changed files with 737 additions and 548 deletions

View File

@@ -76,6 +76,38 @@ describe('api/buf', function()
eq({4, 2}, curwin('get_cursor'))
end)
it('cursor position is maintained in non-current window', function()
meths.buf_set_lines(0, 0, -1, 1, {"line1", "line2", "line3", "line4"})
meths.win_set_cursor(0, {3, 2})
local win = meths.get_current_win()
local buf = meths.get_current_buf()
command('new')
meths.buf_set_lines(buf, 1, 2, 1, {"line5", "line6"})
eq({"line1", "line5", "line6", "line3", "line4"}, meths.buf_get_lines(buf, 0, -1, true))
eq({4, 2}, meths.win_get_cursor(win))
end)
it('cursor position is maintained in TWO non-current windows', function()
meths.buf_set_lines(0, 0, -1, 1, {"line1", "line2", "line3", "line4"})
meths.win_set_cursor(0, {3, 2})
local win = meths.get_current_win()
local buf = meths.get_current_buf()
command('split')
meths.win_set_cursor(0, {4, 2})
local win2 = meths.get_current_win()
-- set current window to third one with another buffer
command("new")
meths.buf_set_lines(buf, 1, 2, 1, {"line5", "line6"})
eq({"line1", "line5", "line6", "line3", "line4"}, meths.buf_get_lines(buf, 0, -1, true))
eq({4, 2}, meths.win_get_cursor(win))
eq({5, 2}, meths.win_get_cursor(win2))
end)
it('line_count has defined behaviour for unloaded buffers', function()
-- we'll need to know our bufnr for when it gets unloaded
local bufnr = curbuf('get_number')
@@ -484,6 +516,50 @@ describe('api/buf', function()
eq({1, 9}, curwin('get_cursor'))
end)
it('updates the cursor position in non-current window', function()
insert([[
hello world!]])
-- position the cursor on `!`
meths.win_set_cursor(0, {1, 11})
local win = meths.get_current_win()
local buf = meths.get_current_buf()
command("new")
-- replace 'world' with 'foo'
meths.buf_set_text(buf, 0, 6, 0, 11, {'foo'})
eq({'hello foo!'}, meths.buf_get_lines(buf, 0, -1, true))
-- cursor should be moved left by two columns (replacement is shorter by 2 chars)
eq({1, 9}, meths.win_get_cursor(win))
end)
it('updates the cursor position in TWO non-current windows', function()
insert([[
hello world!]])
-- position the cursor on `!`
meths.win_set_cursor(0, {1, 11})
local win = meths.get_current_win()
local buf = meths.get_current_buf()
command("split")
local win2 = meths.get_current_win()
-- position the cursor on `w`
meths.win_set_cursor(0, {1, 6})
command("new")
-- replace 'hello' with 'foo'
meths.buf_set_text(buf, 0, 0, 0, 5, {'foo'})
eq({'foo world!'}, meths.buf_get_lines(buf, 0, -1, true))
-- both cursors should be moved left by two columns (replacement is shorter by 2 chars)
eq({1, 9}, meths.win_get_cursor(win))
eq({1, 4}, meths.win_get_cursor(win2))
end)
it('can handle NULs', function()
set_text(0, 0, 0, 0, {'ab\0cd'})
eq('ab\0cd', curbuf_depr('get_line', 0))