From 602cbbe1d93095c204b7858d535c459e09298953 Mon Sep 17 00:00:00 2001 From: Sean Dewar <6256228+seandewar@users.noreply.github.com> Date: Tue, 17 Feb 2026 12:48:19 +0000 Subject: [PATCH] fix(prompt): prompt_setprompt cursor col adjustment Problem: prompt_setprompt adjusted cursor col may be negative (<=0 when 1-based), and doesn't check the col of ': Solution: avoid negative col and adjust correctly if ': col differs from old prompt's length. --- src/nvim/eval/buffer.c | 3 +- test/functional/legacy/prompt_buffer_spec.lua | 29 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/nvim/eval/buffer.c b/src/nvim/eval/buffer.c index c550b6b743..f5b43fc4db 100644 --- a/src/nvim/eval/buffer.c +++ b/src/nvim/eval/buffer.c @@ -802,11 +802,12 @@ void f_prompt_setprompt(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } extmark_splice_cols(buf, prompt_lno - 1, 0, buf->b_prompt_start.mark.col, new_prompt_len, kExtmarkUndo); - cursor_col += new_prompt_len - old_prompt_len; + cursor_col += new_prompt_len - buf->b_prompt_start.mark.col; } if (curwin->w_buffer == buf && curwin->w_cursor.lnum == prompt_lno) { curwin->w_cursor.col = cursor_col; + check_cursor_col(curwin); } changed_lines(buf, prompt_lno, 0, prompt_lno + 1, 0, true); } diff --git a/test/functional/legacy/prompt_buffer_spec.lua b/test/functional/legacy/prompt_buffer_spec.lua index 0e6b665310..9ac6ae6ac9 100644 --- a/test/functional/legacy/prompt_buffer_spec.lua +++ b/test/functional/legacy/prompt_buffer_spec.lua @@ -931,9 +931,34 @@ describe('prompt buffer', function() {1:~ }|*7 {5:-- INSERT --} | ]]) + -- Minimum col should be 1. Same event to avoid corrections from the state loop. + feed('0') + local colnr = exec_lua(function() + vim.fn.prompt_setprompt('', '') + return vim.fn.col('.') + end) + eq(1, colnr) + -- Correct cursor adjustment when old ': col and old prompt length differs. + set_prompt('foo > ') + fn('setpos', "':", { 0, fn('line', '.'), 10, 0 }) + fn('setline', '.', ' foo > hello') + feed('fh') + screen:expect([[ + new-prompt > user input | + foo > ^hello | + {1:~ }|*7 + | + ]]) + set_prompt('bar > ') + screen:expect([[ + new-prompt > user input | + bar > ^hello | + {1:~ }|*7 + | + ]]) -- No crash when setting shorter prompt than curbuf's in other buffer. - feed('zt') + feed('ztA') command('set virtualedit& | new | setlocal buftype=prompt') set_prompt('looooooooooooooooooooooooooooooooooooooooooooong > ', '') -- curbuf set_prompt('foo > ') @@ -943,7 +968,7 @@ describe('prompt buffer', function() ^ | {1:~ }| {3:[Prompt] [+] }| - foo > a b | + foo > hello | {1:~ }|*3 {5:-- INSERT --} | ]])