From 6e05f8afff39a83402784b92272c70361a144159 Mon Sep 17 00:00:00 2001 From: Jaehwang Jung Date: Tue, 5 Dec 2023 09:40:48 +0900 Subject: [PATCH] fix(change): update fold after on_bytes Problem: With vim.treesitter.foldexpr, `o`-ing two lines above a folded region opens the fold. This does not happen with legacy foldexprs. For example, make a markdown file with the following text (without indentation), enable treesitter fold, and follow the instruction in the text. put cursor on this line and type zoo initially folded, revealed by zo # then this fold will be opened initially folded, revealed by o Analysis: * `o` updates folds first (done in `changed_lines`), evaluating foldexpr, and then invokes `on_bytes` (done in `extmark_splice`). * Treesitter fold allocates the foldinfo for added lines (`add_range`) on `on_bytes`. * Therefore, when treesitter foldexpr is invoked while running `o`, it sees outdated foldinfo. Solution: `extmark_splice`, and then `changed_lines`. This seems to be the standard order in other places, e.g., `nvim_buf_set_lines`. --- src/nvim/change.c | 2 +- test/functional/lua/buffer_updates_spec.lua | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/nvim/change.c b/src/nvim/change.c index f3f685f1b1..0b87fdafd8 100644 --- a/src/nvim/change.c +++ b/src/nvim/change.c @@ -1814,11 +1814,11 @@ int open_line(int dir, int flags, int second_line_indent, bool *did_do_comment) curwin->w_cursor.lnum = old_cursor.lnum + 1; } if (did_append) { - changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L, true); // bail out and just get the final length of the line we just manipulated bcount_t extra = (bcount_t)strlen(ml_get(curwin->w_cursor.lnum)); extmark_splice(curbuf, (int)curwin->w_cursor.lnum - 1, 0, 0, 0, 0, 1, 0, 1 + extra, kExtmarkUndo); + changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L, true); } curbuf_splice_pending--; diff --git a/test/functional/lua/buffer_updates_spec.lua b/test/functional/lua/buffer_updates_spec.lua index 4938a42957..d6e93f1bf8 100644 --- a/test/functional/lua/buffer_updates_spec.lua +++ b/test/functional/lua/buffer_updates_spec.lua @@ -418,7 +418,7 @@ describe('lua: nvim_buf_attach on_bytes', function() -- meths.buf_set_option(0, 'autoindent', true) feed 'Go' check_events { - { "test1", "bytes", 1, 4, 7, 0, 114, 0, 0, 0, 1, 0, 1 }; + { "test1", "bytes", 1, 3, 7, 0, 114, 0, 0, 0, 1, 0, 1 }; } feed '' check_events { @@ -431,7 +431,7 @@ describe('lua: nvim_buf_attach on_bytes', function() meths.buf_set_option(0, 'autoindent', true) feed 'Go' check_events { - { "test1", "bytes", 1, 4, 7, 0, 114, 0, 0, 0, 1, 0, 5 }; + { "test1", "bytes", 1, 3, 7, 0, 114, 0, 0, 0, 1, 0, 5 }; } feed '' check_events { @@ -476,7 +476,7 @@ describe('lua: nvim_buf_attach on_bytes', function() feed 'ggo' -- goto first line to continue testing check_events { - { "test1", "bytes", 1, 6, 1, 0, 11, 0, 0, 0, 1, 0, 4 }; + { "test1", "bytes", 1, 5, 1, 0, 11, 0, 0, 0, 1, 0, 4 }; } feed ''