vim-patch:9.0.0646: with 'smoothscroll' CTRL-E is wrong when 'foldmethod' set

Problem:    with 'smoothscroll' set CTRL-E does not work properly when
            'foldmethod' is set to "indent". (Yee Cheng Chin)
Solution:   Merge the code for scroling with folds and 'smoothscroll'.
            (closes vim/vim#11262)

6b2d4ff714

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
Luuk van Baal
2023-04-26 04:32:50 +02:00
parent 69af5e8782
commit d6050e9bda
3 changed files with 136 additions and 58 deletions

View File

@@ -707,8 +707,8 @@ int curwin_col_off(void)
} }
// Return the difference in column offset for the second screen line of a // Return the difference in column offset for the second screen line of a
// wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in // wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
// 'cpoptions'. // is in 'cpoptions'.
int win_col_off2(win_T *wp) int win_col_off2(win_T *wp)
{ {
if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL) { if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL) {
@@ -1097,7 +1097,7 @@ bool scrolldown(long line_count, int byfold)
if (curwin->w_p_wrap && curwin->w_p_sms) { if (curwin->w_p_wrap && curwin->w_p_sms) {
width1 = curwin->w_width - curwin_col_off(); width1 = curwin->w_width - curwin_col_off();
width2 = width1 - curwin_col_off2(); width2 = width1 + curwin_col_off2();
} }
// Make sure w_topline is at the first of a sequence of folded lines. // Make sure w_topline is at the first of a sequence of folded lines.
@@ -1209,55 +1209,66 @@ bool scrollup(long line_count, int byfold)
{ {
linenr_T topline = curwin->w_topline; linenr_T topline = curwin->w_topline;
linenr_T botline = curwin->w_botline; linenr_T botline = curwin->w_botline;
int do_smoothscroll = curwin->w_p_wrap && curwin->w_p_sms;
if ((byfold && hasAnyFolding(curwin)) if (do_smoothscroll || (byfold && hasAnyFolding(curwin)) || win_may_fill(curwin)) {
|| win_may_fill(curwin)) { int width1 = curwin->w_width - curwin_col_off();
// count each sequence of folded lines as one logical line int width2 = width1 + curwin_col_off2();
linenr_T lnum = curwin->w_topline; int size = 0;
while (line_count--) { linenr_T prev_topline = curwin->w_topline;
if (do_smoothscroll) {
size = (int)win_linetabsize(curwin, curwin->w_topline,
ml_get(curwin->w_topline), (colnr_T)MAXCOL);
}
// diff mode: first consume "topfill"
// 'smoothscroll': increase "w_skipcol" until it goes over the end of
// the line, then advance to the next line.
// folding: count each sequence of folded lines as one logical line.
for (long todo = line_count; todo > 0; todo--) {
if (curwin->w_topfill > 0) { if (curwin->w_topfill > 0) {
curwin->w_topfill--; curwin->w_topfill--;
} else { } else {
linenr_T lnum = curwin->w_topline;
if (byfold) { if (byfold) {
// for a closed fold: go to the last line in the fold
(void)hasFolding(lnum, NULL, &lnum); (void)hasFolding(lnum, NULL, &lnum);
} }
if (lnum == curwin->w_topline && curwin->w_p_wrap && curwin->w_p_sms) {
// 'smoothscroll': increase "w_skipcol" until it goes over
// the end of the line, then advance to the next line.
int add = curwin->w_skipcol > 0 ? width2 : width1;
curwin->w_skipcol += add;
if (curwin->w_skipcol >= size) {
if (lnum == curbuf->b_ml.ml_line_count) {
// at the last screen line, can't scroll further
curwin->w_skipcol -= add;
break;
}
lnum++;
}
} else {
if (lnum >= curbuf->b_ml.ml_line_count) { if (lnum >= curbuf->b_ml.ml_line_count) {
break; break;
} }
lnum++; lnum++;
curwin->w_topfill = win_get_fill(curwin, lnum);
}
} }
if (lnum > curwin->w_topline) {
// approximate w_botline // approximate w_botline
curwin->w_botline += lnum - curwin->w_topline; curwin->w_botline += lnum - curwin->w_topline;
curwin->w_topline = lnum; curwin->w_topline = lnum;
} else if (curwin->w_p_wrap && curwin->w_p_sms) { curwin->w_topfill = win_get_fill(curwin, lnum);
int off1 = curwin_col_off();
int off2 = off1 + curwin_col_off2();
int add;
int size = (int)win_linetabsize(curwin, curwin->w_topline,
ml_get(curwin->w_topline), (colnr_T)MAXCOL);
linenr_T prev_topline = curwin->w_topline;
// 'smoothscroll': increase "w_skipcol" until it goes over the end of
// the line, then advance to the next line.
for (long todo = line_count; todo > 0; todo--) {
add = curwin->w_width - (curwin->w_skipcol > 0 ? off2 : off1);
curwin->w_skipcol += add;
if (curwin->w_skipcol >= size) {
if (curwin->w_topline == curbuf->b_ml.ml_line_count) {
curwin->w_skipcol -= add;
break;
}
curwin->w_topline++;
curwin->w_botline++; // approximate w_botline
curwin->w_skipcol = 0; curwin->w_skipcol = 0;
if (todo > 1) { if (todo > 1 && do_smoothscroll) {
size = (int)win_linetabsize(curwin, curwin->w_topline, size = (int)win_linetabsize(curwin, curwin->w_topline,
ml_get(curwin->w_topline), (colnr_T)MAXCOL); ml_get(curwin->w_topline), (colnr_T)MAXCOL);
} }
} }
} }
}
if (curwin->w_topline == prev_topline) { if (curwin->w_topline == prev_topline) {
// need to redraw even though w_topline didn't change // need to redraw even though w_topline didn't change
redraw_later(curwin, UPD_NOT_VALID); redraw_later(curwin, UPD_NOT_VALID);

View File

@@ -31,23 +31,9 @@ describe('smoothscroll', function()
it('works with <C-E> and <C-E>', function() it('works with <C-E> and <C-E>', function()
exec([[ exec([[
call setline(1, [ 'line one', 'word '->repeat(20), 'line three', 'long word '->repeat(7), 'line', 'line', 'line', ]) call setline(1, [ 'line one', 'word '->repeat(20), 'line three', 'long word '->repeat(7), 'line', 'line', 'line', ])
set smoothscroll set smoothscroll scrolloff=5
:5 :5
]]) ]])
local s0 = [[
line one |
word word word word word word word word |
word word word word word word word word |
word word word word |
line three |
long word long word long word long word |
long word long word long word |
^line |
line |
line |
~ |
|
]]
local s1 = [[ local s1 = [[
word word word word word word word word | word word word word word word word word |
word word word word word word word word | word word word word word word word word |
@@ -94,13 +80,69 @@ describe('smoothscroll', function()
line three | line three |
long word long word long word long word | long word long word long word long word |
long word long word long word | long word long word long word |
line |
line |
^line | ^line |
~ |
~ |
~ |
~ |
~ |
|
]]
local s5 = [[
word word word word |
line three |
long word long word long word long word |
long word long word long word |
line | line |
line | line |
^line |
~ | ~ |
~ | ~ |
~ | ~ |
~ | ~ |
|
]]
local s6 = [[
word word word word word word word word |
word word word word |
line three |
long word long word long word long word |
long word long word long word |
line |
line |
^line |
~ |
~ |
~ |
|
]]
local s7 = [[
word word word word word word word word |
word word word word word word word word |
word word word word |
line three |
long word long word long word long word |
long word long word long word |
line |
line |
^line |
~ |
~ |
|
]]
local s8 = [[
line one |
word word word word word word word word |
word word word word word word word word |
word word word word |
line three |
long word long word long word long word |
long word long word long word |
line |
line |
^line |
~ | ~ |
| |
]] ]]
@@ -113,12 +155,22 @@ describe('smoothscroll', function()
feed('<C-E>') feed('<C-E>')
screen:expect(s4) screen:expect(s4)
feed('<C-Y>') feed('<C-Y>')
screen:expect(s3) screen:expect(s5)
feed('<C-Y>') feed('<C-Y>')
screen:expect(s2) screen:expect(s6)
feed('<C-Y>') feed('<C-Y>')
screen:expect(s7)
feed('<C-Y>')
screen:expect(s8)
exec('set foldmethod=indent')
-- move the cursor so we can reuse the same dumps
feed('5G<C-E>')
screen:expect(s1) screen:expect(s1)
feed('<C-E>')
screen:expect(s2)
feed('7G<C-Y>')
screen:expect(s7)
feed('<C-Y>') feed('<C-Y>')
screen:expect(s0) screen:expect(s8)
end) end)
end) end)

View File

@@ -105,6 +105,21 @@ func Test_smoothscroll_CtrlE_CtrlY()
call term_sendkeys(buf, "\<C-Y>") call term_sendkeys(buf, "\<C-Y>")
call VerifyScreenDump(buf, 'Test_smoothscroll_8', {}) call VerifyScreenDump(buf, 'Test_smoothscroll_8', {})
if has('folding')
call term_sendkeys(buf, ":set foldmethod=indent\<CR>")
" move the cursor so we can reuse the same dumps
call term_sendkeys(buf, "5G")
call term_sendkeys(buf, "\<C-E>")
call VerifyScreenDump(buf, 'Test_smoothscroll_1', {})
call term_sendkeys(buf, "\<C-E>")
call VerifyScreenDump(buf, 'Test_smoothscroll_2', {})
call term_sendkeys(buf, "7G")
call term_sendkeys(buf, "\<C-Y>")
call VerifyScreenDump(buf, 'Test_smoothscroll_7', {})
call term_sendkeys(buf, "\<C-Y>")
call VerifyScreenDump(buf, 'Test_smoothscroll_8', {})
endif
call StopVimInTerminal(buf) call StopVimInTerminal(buf)
endfunc endfunc