From 31170af24a107d7f145a679ea624b0bba4b98d9b Mon Sep 17 00:00:00 2001 From: glepnir Date: Mon, 18 Aug 2025 21:14:48 +0200 Subject: [PATCH] patch 9.1.1650: popup: window may not properly resize Problem: After scrolling a popup and then using popup_settext() with fewer lines, the popup fails to resize properly because firstline points beyond the new buffer content (lifepillar) Solution: In popup_adjust_position(), validate that firstline doesn't exceed buffer line count and reset to 0 (auto-position) if it does (glepnir) fixes: #14745 closes: #18031 Signed-off-by: glepnir Signed-off-by: Christian Brabandt --- src/popupwin.c | 10 ++++- .../Test_popupwin_first_after_scroll.dump | 20 ++++++++++ src/testdir/test_popupwin.vim | 37 +++++++++++++++++++ src/version.c | 2 + 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/testdir/dumps/Test_popupwin_first_after_scroll.dump diff --git a/src/popupwin.c b/src/popupwin.c index 7b1237c5f5..2f956a3adb 100644 --- a/src/popupwin.c +++ b/src/popupwin.c @@ -1403,7 +1403,15 @@ popup_adjust_position(win_T *wp) // start at the desired first line if (wp->w_firstline > 0) - wp->w_topline = wp->w_firstline; + { + // If firstline is beyond the buffer content, reset it to auto-position. + // This can happen when the popup was scrolled and then the buffer + // content was changed to have fewer lines. + if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count) + wp->w_firstline = 0; + else + wp->w_topline = wp->w_firstline; + } if (wp->w_topline < 1) wp->w_topline = 1; else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count) diff --git a/src/testdir/dumps/Test_popupwin_first_after_scroll.dump b/src/testdir/dumps/Test_popupwin_first_after_scroll.dump new file mode 100644 index 0000000000..74591df79a --- /dev/null +++ b/src/testdir/dumps/Test_popupwin_first_after_scroll.dump @@ -0,0 +1,20 @@ +> +0&#ffffff0@74 +|~+0#4040ff13&| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~|╔+0#0000001#ffd7ff255|═@69|╗| +0#4040ff13#ffffff0@1 +|~|║+0#0000001#ffd7ff255|a| @68|║| +0#4040ff13#ffffff0@1 +|~|║+0#0000001#ffd7ff255|b| @68|║| +0#4040ff13#ffffff0@1 +|~|║+0#0000001#ffd7ff255|c| @68|║| +0#4040ff13#ffffff0@1 +|~|╚+0#0000001#ffd7ff255|═@69|╝| +0#4040ff13#ffffff0@1 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|:+0#0000000&|c|a|l@1| |p|o|p|u|p|_|s|e|t@1|e|x|t|(|1|0@1|1|,| |[|'|a|'|,| |'|b|'|,| |'|c|'|]|)| @14|0|,|0|-|1| @8|A|l@1| diff --git a/src/testdir/test_popupwin.vim b/src/testdir/test_popupwin.vim index ab99e5d7c5..ed903bdbb3 100644 --- a/src/testdir/test_popupwin.vim +++ b/src/testdir/test_popupwin.vim @@ -4533,4 +4533,41 @@ func Test_popupwin_closing_buffer() %bd! endfunc +func Test_popupwin_firstline_after_scroll() + CheckScreendump + + let lines =<< trim END + vim9script + + def Popup(): number + return popup_create([], { + border: [1, 1, 1, 1], + close: 'click', + minheight: 1, + maxheight: 10, + scrollbar: true, + minwidth: &columns - 5, + maxwidth: &columns - 5, + }) + enddef + + var id = Popup() + g:popup_id = id + popup_settext(id, repeat(['abcd'], 20)) + END + call writefile(lines, 'XtestPopupScroll_win', 'D') + let buf = RunVimInTerminal('-S XtestPopupScroll_win', {}) + call TermWait(buf, 50) + + call term_sendkeys(buf, ":call popup_setoptions(g:popup_id, {'firstline': 6})\") + call TermWait(buf, 50) + call term_sendkeys(buf, ":call popup_settext(g:popup_id, [])\") + call TermWait(buf, 50) + call term_sendkeys(buf, ":call popup_settext(1001, ['a', 'b', 'c'])\") + call TermWait(buf, 50) + call VerifyScreenDump(buf, 'Test_popupwin_first_after_scroll', {}) + + call StopVimInTerminal(buf) +endfunc + " vim: shiftwidth=2 sts=2 diff --git a/src/version.c b/src/version.c index 8f351ed07d..a50755f53f 100644 --- a/src/version.c +++ b/src/version.c @@ -719,6 +719,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 1650, /**/ 1649, /**/