From f8b50bf3b0f68af98f5012fa73f7d2425137310b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 4 Oct 2025 20:08:28 +0800 Subject: [PATCH] vim-patch:9.1.1825: completion: flicker when LSP server is slow (#36020) Problem: completion: flicker when LSP server is slow Solution: reinsert leader text before invoking user function (Girish Palya) Reference: https://github.com/girishji/vimcomplete/issues/101#issuecomment-3343063245 In insert-mode completion, the leader text is temporarily removed while searching for candidates. When the LSP server responds slowly, the client may call `:sleep` to wait, which triggers `out_flush()`. This causes the deleted text to disappear briefly before being redrawn, resulting in visible flicker. This commit reinserts the leader text before invoking the user function, and removes it again afterward to eliminate flicker. closes: vim/vim#18468 https://github.com/vim/vim/commit/c51d1cc578da32939ee458e98d78b180e1235b4b Co-authored-by: Girish Palya --- src/nvim/insexpand.c | 17 ++++++++++++++-- test/old/testdir/test_ins_complete.vim | 27 +++++++++++++------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/nvim/insexpand.c b/src/nvim/insexpand.c index cd79ca0119..646b7dbf21 100644 --- a/src/nvim/insexpand.c +++ b/src/nvim/insexpand.c @@ -6544,16 +6544,29 @@ static void remove_old_matches(void) /// 'refresh:always' flag. static void get_cpt_func_completion_matches(Callback *cb) { - int startcol = cpt_sources_array[cpt_sources_index].cs_startcol; + cpt_source_T *cpt_src = &cpt_sources_array[cpt_sources_index]; + int startcol = cpt_src->cs_startcol; if (startcol == -2 || startcol == -3) { return; } set_compl_globals(startcol, curwin->w_cursor.col, true); + + // Insert the leader string (previously removed) before expansion. + // This prevents flicker when `func` (e.g. an LSP client) is slow and + // calls 'sleep', which triggers ui_flush(). + if (!cpt_src->cs_refresh_always) { + ins_compl_insert_bytes(ins_compl_leader(), -1); + } + expand_by_function(0, cpt_compl_pattern.data, cb); - cpt_sources_array[cpt_sources_index].cs_refresh_always = compl_opt_refresh_always; + if (!cpt_src->cs_refresh_always) { + ins_compl_delete(false); + } + + cpt_src->cs_refresh_always = compl_opt_refresh_always; compl_opt_refresh_always = false; } diff --git a/test/old/testdir/test_ins_complete.vim b/test/old/testdir/test_ins_complete.vim index 2292a0a26d..4125ffaf13 100644 --- a/test/old/testdir/test_ins_complete.vim +++ b/test/old/testdir/test_ins_complete.vim @@ -591,15 +591,19 @@ func Test_completefunc_info() set completefunc& endfunc -func Test_cpt_func_cursorcol() +" For ^N completion, `completefunc` receives the same leader string in both the +" 'info' and 'expansion' phases (the leader is not removed before expansion). +" This avoids flicker when `completefunc` (e.g. an LSP client) is slow and calls +" 'sleep', which triggers out_flush(). +func Test_completefunc_leader() func CptColTest(findstart, query) if a:findstart - call assert_equal(b:info_compl_line, getline(1)) - call assert_equal(b:info_cursor_col, col('.')) + call assert_equal(b:compl_line, getline(1)) + call assert_equal(b:cursor_col, col('.')) return col('.') endif - call assert_equal(b:expn_compl_line, getline(1)) - call assert_equal(b:expn_cursor_col, col('.')) + call assert_equal(b:compl_line, getline(1)) + call assert_equal(b:cursor_col, col('.')) " return v:none return [] endfunc @@ -608,17 +612,13 @@ func Test_cpt_func_cursorcol() new " Replace mode - let b:info_compl_line = "foo barxyz" - let b:expn_compl_line = "foo barbaz" - let b:info_cursor_col = 10 - let b:expn_cursor_col = 5 + let b:compl_line = "foo barxyz" + let b:cursor_col = 10 call feedkeys("ifoo barbaz\2hRxy\", "tx") " Insert mode - let b:info_compl_line = "foo bar" - let b:expn_compl_line = "foo " - let b:info_cursor_col = 8 - let b:expn_cursor_col = 5 + let b:compl_line = "foo bar" + let b:cursor_col = 8 call feedkeys("Sfoo bar\", "tx") set completeopt=longest @@ -4239,7 +4239,6 @@ func Test_autocomplete_completeopt_preinsert() endfunc set omnifunc=Omni_test complete+=o set completeopt=preinsert autocomplete - " set completeopt=preinsert,menuone autocomplete func GetLine() let g:line = getline('.') let g:col = col('.')