fix(extmarks): splice earlier when opening new line (#28108)

Related #26364 #26499 #26501
Fix #28107
This commit is contained in:
zeertzjq 2024-03-30 08:05:36 +08:00 committed by GitHub
parent 2424c3e696
commit 19d63563e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 61 additions and 19 deletions

View File

@ -1838,6 +1838,13 @@ bool open_line(int dir, int flags, int second_line_indent, bool *did_do_comment)
saved_line = NULL;
if (did_append) {
// Always move extmarks - Here we move only the line where the
// cursor is, the later mark_adjust takes care of the lines after.
int cols_added = mincol - 1 + less_cols_off - less_cols;
extmark_splice(curbuf, (int)lnum - 1, mincol - 1 - cols_spliced,
0, less_cols_off, less_cols_off,
1, cols_added, 1 + cols_added, kExtmarkUndo);
changed_lines(curbuf, curwin->w_cursor.lnum, curwin->w_cursor.col,
curwin->w_cursor.lnum + 1, 1, true);
did_append = false;
@ -1848,12 +1855,6 @@ bool open_line(int dir, int flags, int second_line_indent, bool *did_do_comment)
curwin->w_cursor.col + less_cols_off,
1, -less_cols, 0);
}
// Always move extmarks - Here we move only the line where the
// cursor is, the previous mark_adjust takes care of the lines after
int cols_added = mincol - 1 + less_cols_off - less_cols;
extmark_splice(curbuf, (int)lnum - 1, mincol - 1 - cols_spliced,
0, less_cols_off, less_cols_off,
1, cols_added, 1 + cols_added, kExtmarkUndo);
} else {
changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
}

View File

@ -1,5 +1,6 @@
-- Test suite for testing interactions with API bindings
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local command = helpers.command
local api = helpers.api
@ -323,20 +324,60 @@ describe('lua buffer event callbacks: on_lines', function()
eq({ 'lines', 1, 6, 0, 3, 3, 9 }, api.nvim_get_var('linesev'))
end)
it(
'calling nvim_buf_call() from callback does not cause Normal mode CTRL-A to misbehave #16729',
function()
exec_lua([[
it('nvim_buf_call() from callback does not cause wrong Normal mode CTRL-A #16729', function()
exec_lua([[
vim.api.nvim_buf_attach(0, false, {
on_lines = function(...)
vim.api.nvim_buf_call(0, function() end)
end,
})
]])
feed('itest123<Esc><C-A>')
eq('test124', api.nvim_get_current_line())
end
)
feed('itest123<Esc><C-A>')
eq('test124', api.nvim_get_current_line())
end)
it('setting extmark in on_lines callback works', function()
local screen = Screen.new(40, 6)
screen:attach()
api.nvim_buf_set_lines(0, 0, -1, true, { 'aaa', 'bbb', 'ccc' })
exec_lua([[
local ns = vim.api.nvim_create_namespace('')
vim.api.nvim_buf_attach(0, false, {
on_lines = function(_, _, _, row, _, end_row)
vim.api.nvim_buf_clear_namespace(0, ns, row, end_row)
for i = row, end_row - 1 do
local id = vim.api.nvim_buf_set_extmark(0, ns, i, 0, {
virt_text = {{ 'NEW' .. tostring(i), 'WarningMsg' }},
})
end
end,
})
]])
feed('o')
screen:expect({
grid = [[
aaa |
^ {19:NEW1} |
bbb |
ccc |
{1:~ }|
{5:-- INSERT --} |
]],
})
feed('<CR>')
screen:expect({
grid = [[
aaa |
{19:NEW1} |
^ {19:NEW2} |
bbb |
ccc |
{5:-- INSERT --} |
]],
})
end)
end)
describe('lua: nvim_buf_attach on_bytes', function()
@ -426,14 +467,14 @@ describe('lua: nvim_buf_attach on_bytes', function()
it('opening lines', function()
local check_events = setup_eventcheck(verify, origlines)
-- api.nvim_set_option_value('autoindent', true, {})
api.nvim_set_option_value('autoindent', false, {})
feed 'Go'
check_events {
{ 'test1', 'bytes', 1, 3, 7, 0, 114, 0, 0, 0, 1, 0, 1 },
}
feed '<cr>'
check_events {
{ 'test1', 'bytes', 1, 5, 7, 0, 114, 0, 0, 0, 1, 0, 1 },
{ 'test1', 'bytes', 1, 4, 7, 0, 114, 0, 0, 0, 1, 0, 1 },
}
end)
@ -447,7 +488,7 @@ describe('lua: nvim_buf_attach on_bytes', function()
feed '<cr>'
check_events {
{ 'test1', 'bytes', 1, 4, 7, 0, 114, 0, 4, 4, 0, 0, 0 },
{ 'test1', 'bytes', 1, 5, 7, 0, 114, 0, 0, 0, 1, 4, 5 },
{ 'test1', 'bytes', 1, 4, 7, 0, 114, 0, 0, 0, 1, 4, 5 },
}
end)
@ -477,7 +518,7 @@ describe('lua: nvim_buf_attach on_bytes', function()
api.nvim_set_option_value('filetype', 'c', {})
feed 'A<CR>'
check_events {
{ 'test1', 'bytes', 1, 4, 0, 10, 10, 0, 0, 0, 1, 3, 4 },
{ 'test1', 'bytes', 1, 3, 0, 10, 10, 0, 0, 0, 1, 3, 4 },
}
feed '<ESC>'
@ -493,7 +534,7 @@ describe('lua: nvim_buf_attach on_bytes', function()
feed '<CR>'
check_events {
{ 'test1', 'bytes', 1, 6, 1, 2, 13, 0, 1, 1, 0, 0, 0 },
{ 'test1', 'bytes', 1, 7, 1, 2, 13, 0, 0, 0, 1, 3, 4 },
{ 'test1', 'bytes', 1, 6, 1, 2, 13, 0, 0, 0, 1, 3, 4 },
}
end)