From 79c8df7e97bdc5513a2d645d03e58ee68792d09d Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 6 Jun 2023 09:46:04 +0800 Subject: [PATCH] fix(spell): splice extmarks on :spellrepall (cherry picked from commit 22241639eb532fc2ad2be6a56c7d5c5ab3334d58) --- src/nvim/spell.c | 17 ++++++++++------- test/functional/lua/buffer_updates_spec.lua | 7 ++++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/nvim/spell.c b/src/nvim/spell.c index 3ab4fa3479..1b61a88eef 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -2597,9 +2597,11 @@ void ex_spellrepall(exarg_T *eap) emsg(_("E752: No previous spell replacement")); return; } - int addlen = (int)(strlen(repl_to) - strlen(repl_from)); + const size_t repl_from_len = strlen(repl_from); + const size_t repl_to_len = strlen(repl_to); + int addlen = (int)(repl_to_len - repl_from_len); - size_t frompatlen = strlen(repl_from) + 7; + const size_t frompatlen = repl_from_len + 7; char *frompat = xmalloc(frompatlen); snprintf(frompat, frompatlen, "\\V\\<%s\\>", repl_from); p_ws = false; @@ -2616,14 +2618,15 @@ void ex_spellrepall(exarg_T *eap) // Only replace when the right word isn't there yet. This happens // when changing "etc" to "etc.". char *line = get_cursor_line_ptr(); - if (addlen <= 0 || strncmp(line + curwin->w_cursor.col, - repl_to, strlen(repl_to)) != 0) { + if (addlen <= 0 + || strncmp(line + curwin->w_cursor.col, repl_to, repl_to_len) != 0) { char *p = xmalloc(strlen(line) + (size_t)addlen + 1); memmove(p, line, (size_t)curwin->w_cursor.col); STRCPY(p + curwin->w_cursor.col, repl_to); - STRCAT(p, line + curwin->w_cursor.col + strlen(repl_from)); + STRCAT(p, line + curwin->w_cursor.col + repl_from_len); ml_replace(curwin->w_cursor.lnum, p, false); - changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); + inserted_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col, + (int)repl_from_len, (int)repl_to_len); if (curwin->w_cursor.lnum != prev_lnum) { sub_nlines++; @@ -2631,7 +2634,7 @@ void ex_spellrepall(exarg_T *eap) } sub_nsubs++; } - curwin->w_cursor.col += (colnr_T)strlen(repl_to); + curwin->w_cursor.col += (colnr_T)repl_to_len; } p_ws = save_ws; diff --git a/test/functional/lua/buffer_updates_spec.lua b/test/functional/lua/buffer_updates_spec.lua index 04f4f89472..a4f81ad814 100644 --- a/test/functional/lua/buffer_updates_spec.lua +++ b/test/functional/lua/buffer_updates_spec.lua @@ -1164,12 +1164,17 @@ describe('lua: nvim_buf_attach on_bytes', function() end) it("works with accepting spell suggestions", function() - local check_events = setup_eventcheck(verify, {"hallo"}) + local check_events = setup_eventcheck(verify, {"hallo world", "hallo world"}) feed("gg0z=4") -- accepts 'Hello' check_events { { "test1", "bytes", 1, 3, 0, 0, 0, 0, 2, 2, 0, 2, 2 }; } + + command("spellrepall") -- replaces whole words + check_events { + { "test1", "bytes", 1, 4, 1, 0, 12, 0, 5, 5, 0, 5, 5 }; + } end) it('works with :diffput and :diffget', function()