fix(mouse): click on empty line with 'foldcolumn'

This commit is contained in:
zeertzjq 2023-09-21 10:20:30 +08:00
parent acc32f20be
commit e25cf47ad3
2 changed files with 143 additions and 29 deletions

View File

@ -1861,44 +1861,47 @@ static void mouse_check_grid(colnr_T *vcolp, int *flagsp)
int start_row = 0; int start_row = 0;
int start_col = 0; int start_col = 0;
grid_adjust(&gp, &start_row, &start_col); grid_adjust(&gp, &start_row, &start_col);
if (gp->handle != click_grid) { if (gp->handle != click_grid || gp->chars == NULL) {
return; return;
} }
click_row += start_row; click_row += start_row;
click_col += start_col; click_col += start_col;
if (click_row < 0 || click_row >= gp->rows
|| click_col < 0 || click_col >= gp->cols) {
return;
}
colnr_T col_from_screen = -1; const size_t off = gp->line_offset[click_row] + (size_t)click_col;
colnr_T col_from_screen = gp->vcols[off];
if (gp->chars != NULL if (col_from_screen == MAXCOL) {
&& click_row >= 0 && click_row < gp->rows // When clicking after end of line, still need to set correct curswant
&& click_col >= 0 && click_col < gp->cols) { size_t off_l = gp->line_offset[click_row] + (size_t)start_col;
const size_t off = gp->line_offset[click_row] + (size_t)click_col; if (gp->vcols[off_l] < MAXCOL) {
col_from_screen = gp->vcols[off]; // Binary search to find last char in line
size_t off_r = off;
if (col_from_screen == MAXCOL) { while (off_l < off_r) {
// When clicking after end of line, still need to set correct curswant size_t off_m = (off_l + off_r + 1) / 2;
size_t off_l = gp->line_offset[click_row] + (size_t)start_col; if (gp->vcols[off_m] < MAXCOL) {
if (gp->vcols[off_l] < MAXCOL) { off_l = off_m;
// Binary search to find last char in line } else {
size_t off_r = off; off_r = off_m - 1;
while (off_l < off_r) {
size_t off_m = (off_l + off_r + 1) / 2;
if (gp->vcols[off_m] < MAXCOL) {
off_l = off_m;
} else {
off_r = off_m - 1;
}
} }
*vcolp = gp->vcols[off_r] + (int)(off - off_r);
} else {
// Clicking on an empty line
*vcolp = click_col - start_col;
} }
} else if (col_from_screen >= 0) { colnr_T eol_vcol = gp->vcols[off_r];
// Use the virtual column from vcols[], it is accurate also after assert(eol_vcol < MAXCOL);
// concealed characters. // This may be -2 or -3 with 'foldcolumn' and empty line.
*vcolp = col_from_screen; // In that case set it to -1 as it's just before start of line.
eol_vcol = MAX(eol_vcol, -1);
*vcolp = eol_vcol + (int)(off - off_r);
} else {
// Clicking on an empty line
*vcolp = click_col - start_col;
} }
} else if (col_from_screen >= 0) {
// Use the virtual column from vcols[], it is accurate also after
// concealed characters.
*vcolp = col_from_screen;
} }
if (col_from_screen == -2) { if (col_from_screen == -2) {

View File

@ -8831,6 +8831,117 @@ describe('float window', function()
meths.input_mouse('left', 'press', '', 0, 3, 15) meths.input_mouse('left', 'press', '', 0, 3, 15)
end end
eq({0, 3, 1, 0, 10}, funcs.getcurpos()) eq({0, 3, 1, 0, 10}, funcs.getcurpos())
command('setlocal foldcolumn=1')
feed('zfkgg')
if multigrid then
screen:expect{grid=[[
## grid 1
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[3:----------------------------------------]|
## grid 2
|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
## grid 3
|
## grid 4
{5:}|
{5:}{19: }{1:^ }{5:}|
{5:}{19:+}{28:+-- 2 lines: ·····}{5:}|
{5:}{2:~ }{5:}|
{5:}|
]], float_pos={
[4] = {{id = 1001}, "NW", 1, 0, 5, true, 50};
}, win_viewport={
[2] = {win = {id = 1000}, topline = 0, botline = 2, curline = 0, curcol = 0, linecount = 1, sum_scroll_delta = 0};
[4] = {win = {id = 1001}, topline = 0, botline = 4, curline = 0, curcol = 0, linecount = 3, sum_scroll_delta = 0};
}}
else
screen:expect{grid=[[
{5:} |
{0:~ }{5:}{19: }{1:^ }{5:}{0: }|
{0:~ }{5:}{19:+}{28:+-- 2 lines: ·····}{5:}{0: }|
{0:~ }{5:}{2:~ }{5:}{0: }|
{0:~ }{5:}{0: }|
{0:~ }|
|
]]}
end
if multigrid then
meths.input_mouse('left', 'press', '', 4, 2, 1)
screen:expect{grid=[[
## grid 1
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[3:----------------------------------------]|
## grid 2
|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
## grid 3
|
## grid 4
{5:}|
{5:}{19: }{1:^ }{5:}|
{5:}{19:-}{1: }{5:}|
{5:}{19:}{1: }{5:}|
{5:}|
]], float_pos={
[4] = {{id = 1001}, "NW", 1, 0, 5, true, 50};
}, win_viewport={
[2] = {win = {id = 1000}, topline = 0, botline = 2, curline = 0, curcol = 0, linecount = 1, sum_scroll_delta = 0};
[4] = {win = {id = 1001}, topline = 0, botline = 3, curline = 0, curcol = 0, linecount = 3, sum_scroll_delta = 0};
}}
else
meths.input_mouse('left', 'press', '', 0, 2, 6)
screen:expect{grid=[[
{5:} |
{0:~ }{5:}{19: }{1:^ }{5:}{0: }|
{0:~ }{5:}{19:-}{1: }{5:}{0: }|
{0:~ }{5:}{19:}{1: }{5:}{0: }|
{0:~ }{5:}{0: }|
{0:~ }|
|
]]}
end
if multigrid then
meths.input_mouse('left', 'press', '', 4, 2, 2)
else
meths.input_mouse('left', 'press', '', 0, 2, 7)
end
eq({0, 2, 1, 0, 1}, funcs.getcurpos())
if multigrid then
meths.input_mouse('left', 'press', '', 4, 2, 3)
else
meths.input_mouse('left', 'press', '', 0, 2, 8)
end
eq({0, 2, 1, 0, 2}, funcs.getcurpos())
if multigrid then
meths.input_mouse('left', 'press', '', 4, 2, 11)
else
meths.input_mouse('left', 'press', '', 0, 2, 16)
end
eq({0, 2, 1, 0, 10}, funcs.getcurpos())
end) end)
it("'winblend' option", function() it("'winblend' option", function()