From 4374ec83cd49d148fc788bdb43f49d4b0365cf9f Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 2 Jun 2024 14:58:12 +0800 Subject: vim-patch:8.2.0083: text properties wrong when tabs and spaces are exchanged Problem: Text properties wrong when tabs and spaces are exchanged. Solution: Take text properties into account. (Nobuhiro Takasaki, closes vim/vim#5427) https://github.com/vim/vim/commit/5cb0b93d52fa5c12ca50a18509947ee6459bb7a8 Co-authored-by: Bram Moolenaar --- src/nvim/edit.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 595b4da589..436d43a4db 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -4421,18 +4421,21 @@ static bool ins_tab(void) // Delete following spaces. int i = cursor->col - fpos.col; if (i > 0) { - STRMOVE(ptr, ptr + i); + if (!(State & VREPLACE_FLAG)) { + memmove(ptr, ptr + i, (size_t)(curbuf->b_ml.ml_line_len - i + - (ptr - curbuf->b_ml.ml_line_ptr))); + curbuf->b_ml.ml_line_len -= i; + inserted_bytes(fpos.lnum, change_col, + cursor->col - change_col, fpos.col - change_col); + } else { + STRMOVE(ptr, ptr + i); + } // correct replace stack. if ((State & REPLACE_FLAG) && !(State & VREPLACE_FLAG)) { for (temp = i; --temp >= 0;) { replace_join(repl_off); } } - if (!(State & VREPLACE_FLAG)) { - curbuf->b_ml.ml_line_len -= i; - inserted_bytes(fpos.lnum, change_col, - cursor->col - change_col, fpos.col - change_col); - } } cursor->col -= i; -- cgit From 97d9d71bf3ccc156b94acf988f6b2f60fb706fc9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 2 Jun 2024 15:00:16 +0800 Subject: vim-patch:8.2.0109: corrupted text properties when expanding spaces Problem: Corrupted text properties when expanding spaces. Solution: Reallocate the line. (Nobuhiro Takasaki, closes vim/vim#5457) https://github.com/vim/vim/commit/ac15fd8c6761763c8feedef1a2fbd8309f0a823c Co-authored-by: Bram Moolenaar --- src/nvim/edit.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 436d43a4db..f6e2dbfa4e 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -4422,9 +4422,18 @@ static bool ins_tab(void) int i = cursor->col - fpos.col; if (i > 0) { if (!(State & VREPLACE_FLAG)) { - memmove(ptr, ptr + i, (size_t)(curbuf->b_ml.ml_line_len - i - - (ptr - curbuf->b_ml.ml_line_ptr))); + char *newp = xmalloc((size_t)(curbuf->b_ml.ml_line_len - i)); + ptrdiff_t col = ptr - curbuf->b_ml.ml_line_ptr; + if (col > 0) { + memmove(newp, ptr - col, (size_t)col); + } + memmove(newp + col, ptr + i, (size_t)(curbuf->b_ml.ml_line_len - col - i)); + if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED)) { + xfree(curbuf->b_ml.ml_line_ptr); + } + curbuf->b_ml.ml_line_ptr = newp; curbuf->b_ml.ml_line_len -= i; + curbuf->b_ml.ml_flags = (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY; inserted_bytes(fpos.lnum, change_col, cursor->col - change_col, fpos.col - change_col); } else { -- cgit From 56337310efc765a760b7a1ca0040c7c6ce23574e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 2 Jun 2024 15:18:30 +0800 Subject: test: add a test for #29119 --- test/functional/lua/buffer_updates_spec.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/functional/lua/buffer_updates_spec.lua b/test/functional/lua/buffer_updates_spec.lua index d4af7e4732..6b575ad0ef 100644 --- a/test/functional/lua/buffer_updates_spec.lua +++ b/test/functional/lua/buffer_updates_spec.lua @@ -379,6 +379,25 @@ describe('lua buffer event callbacks: on_lines', function() ]], }) end) + + it('line lengths are correct when pressing TAB with folding #29119', function() + api.nvim_buf_set_lines(0, 0, -1, true, { 'a', 'b' }) + + exec_lua([[ + _G.res = {} + vim.o.foldmethod = 'indent' + vim.o.softtabstop = -1 + vim.api.nvim_buf_attach(0, false, { + on_lines = function(_, bufnr, _, row, _, end_row) + local lines = vim.api.nvim_buf_get_lines(bufnr, row, end_row, true) + table.insert(_G.res, lines) + end + }) + ]]) + + feed('i') + eq({ '\ta' }, exec_lua('return _G.res[#_G.res]')) + end) end) describe('lua: nvim_buf_attach on_bytes', function() -- cgit