Merge #5822 'mouse.c: Fix mouse click on multibyte + concealed'

This commit is contained in:
Justin M. Keyes 2018-01-29 23:50:43 +01:00 committed by GitHub
commit 397ff2c35b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 388 additions and 176 deletions

View File

@ -608,8 +608,7 @@ bool mouse_scroll_horiz(int dir)
return leftcol_changed();
}
// Adjust the clicked column position if there are concealed characters
// before the current column. But only when it's absolutely necessary.
/// Adjusts the clicked column position when 'conceallevel' > 0
static int mouse_adjust_click(win_T *wp, int row, int col)
{
if (!(wp->w_p_cole > 0 && curbuf->b_p_smc > 0
@ -617,64 +616,102 @@ static int mouse_adjust_click(win_T *wp, int row, int col)
return col;
}
int end = (colnr_T)STRLEN(ml_get(wp->w_cursor.lnum));
int vend = getviscol2(end, 0);
// `col` is the position within the current line that is highlighted by the
// cursor without consideration for concealed characters. The current line is
// scanned *up to* `col`, nudging it left or right when concealed characters
// are encountered.
//
// chartabsize() is used to keep track of the virtual column position relative
// to the line's bytes. For example: if col == 9 and the line starts with a
// tab that's 8 columns wide, we would want the cursor to be highlighting the
// second byte, not the ninth.
if (col >= vend) {
return col;
}
int i = wp->w_leftcol;
linenr_T lnum = wp->w_cursor.lnum;
char_u *line = ml_get(lnum);
char_u *ptr = line;
char_u *ptr_end = line;
char_u *ptr_row_offset = line; // Where we begin adjusting `ptr_end`
// Find the offset where scanning should begin.
int offset = wp->w_leftcol;
if (row > 0) {
i += row * (wp->w_width - win_col_off(wp) - win_col_off2(wp)
- wp->w_leftcol) + wp->w_skipcol;
offset += row * (wp->w_width - win_col_off(wp) - win_col_off2(wp) -
wp->w_leftcol + wp->w_skipcol);
}
int vcol;
if (offset) {
// Skip everything up to an offset since nvim takes care of displaying the
// correct portion of the line when horizontally scrolling.
// When 'wrap' is enabled, only the row (of the wrapped line) needs to be
// checked for concealed characters.
vcol = 0;
while (vcol < offset && *ptr != NUL) {
vcol += chartabsize(ptr, vcol);
ptr += utfc_ptr2len(ptr);
}
ptr_row_offset = ptr;
}
// Align `ptr_end` with `col`
vcol = offset;
ptr_end = ptr_row_offset;
while (vcol < col && *ptr_end != NUL) {
vcol += chartabsize(ptr_end, vcol);
ptr_end += utfc_ptr2len(ptr_end);
}
int start_col = i;
int matchid;
int last_matchid;
int bcol = end - (vend - col);
int prev_matchid;
int nudge = 0;
int cwidth = 0;
while (i < bcol) {
matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, i);
vcol = offset;
#define incr() nudge++; ptr_end += utfc_ptr2len(ptr_end)
#define decr() nudge--; ptr_end -= utfc_ptr2len(ptr_end)
while (ptr < ptr_end && *ptr != NUL) {
cwidth = chartabsize(ptr, vcol);
vcol += cwidth;
if (cwidth > 1 && *ptr == '\t' && nudge > 0) {
// A tab will "absorb" any previous adjustments.
cwidth = MIN(cwidth, nudge);
while (cwidth > 0) {
decr();
cwidth--;
}
}
matchid = syn_get_concealed_id(wp, lnum, (colnr_T)(ptr - line));
if (matchid != 0) {
if (wp->w_p_cole == 3) {
bcol++;
incr();
} else {
if (row > 0 && i == start_col) {
// Check if the current concealed character is actually part of
// the previous wrapped row's conceal group.
last_matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum,
i - 1);
if (last_matchid == matchid) {
bcol++;
}
} else if (wp->w_p_cole == 1
|| (wp->w_p_cole == 2
&& (lcs_conceal != NUL
|| syn_get_sub_char() != NUL))) {
if (!(row > 0 && ptr == ptr_row_offset)
&& (wp->w_p_cole == 1 || (wp->w_p_cole == 2
&& (lcs_conceal != NUL
|| syn_get_sub_char() != NUL)))) {
// At least one placeholder character will be displayed.
bcol--;
decr();
}
last_matchid = matchid;
prev_matchid = matchid;
// Adjust for concealed text that spans more than one character.
do {
i++;
bcol++;
matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, i);
} while (last_matchid == matchid);
while (prev_matchid == matchid && *ptr != NUL) {
incr();
ptr += utfc_ptr2len(ptr);
matchid = syn_get_concealed_id(wp, lnum, (colnr_T)(ptr - line));
}
continue;
}
}
i++;
ptr += utfc_ptr2len(ptr);
}
return getviscol2(bcol, 0);
return col + nudge;
}

View File

@ -750,17 +750,19 @@ describe('ui/mouse/input', function()
feed_command('set concealcursor=n')
feed_command('set nowrap')
feed_command('syntax match NonText "\\<amet\\>" conceal')
feed_command('syntax match NonText "\\cs\\|g." conceal cchar=X')
feed_command('syntax match NonText "\\%(lo\\|cl\\)." conceal')
feed_command('syntax match NonText "Lo" conceal cchar=Y')
feed_command('set shiftwidth=2 tabstop=4 list listchars=tab:>-')
feed_command('syntax match NonText "\\*" conceal')
feed_command('syntax match NonText "cats" conceal cchar=X')
feed_command('syntax match NonText "x" conceal cchar=>')
-- First column is there to retain the tabs.
insert([[
Lorem ipsum dolor sit amet, consetetur sadipscing elitr.
Stet clita kasd gubergren, no sea takimata sanctus est.
|Section *t1*
| *t2* *t3* *t4*
|x *cats* 🐈
]])
feed('gg')
feed('gg<c-v>Gxgg')
end)
it('(level 1) click on non-wrapped lines', function()
@ -768,93 +770,138 @@ describe('ui/mouse/input', function()
feed('<esc><LeftMouse><0,0>')
screen:expect([[
{c:^Y}rem ip{c:X}um do{c: } {c:X}it {c: }, con|
{c:X}tet {c: }ta ka{c:X}d {c:X}ber{c:X}en, no|
^Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
{c:>} {0:>---}{c: X } {0:>}|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><1,0>')
screen:expect([[
{c:Y}^rem ip{c:X}um do{c: } {c:X}it {c: }, con|
{c:X}tet {c: }ta ka{c:X}d {c:X}ber{c:X}en, no|
S^ection{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
{c:>} {0:>---}{c: X } {0:>}|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><15,0>')
feed('<esc><LeftMouse><21,0>')
screen:expect([[
{c:Y}rem ip{c:X}um do{c: } {c:^X}it {c: }, con|
{c:X}tet {c: }ta ka{c:X}d {c:X}ber{c:X}en, no|
Section{0:>>--->--->---}{c: }^t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
{c:>} {0:>---}{c: X } {0:>}|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><15,1>')
feed('<esc><LeftMouse><21,1>')
screen:expect([[
{c:Y}rem ip{c:X}um do{c: } {c:X}it {c: }, con|
{c:X}tet {c: }ta ka{c:X}d {c:X}^ber{c:X}en, no|
Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t^3{c: } {c: }|
{c:>} {0:>---}{c: X } {0:>}|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><0,2>')
screen:expect([[
Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
{c:^>} {0:>---}{c: X } {0:>}|
|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><7,2>')
screen:expect([[
Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
{c:>} ^{0:>---}{c: X } {0:>}|
|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><21,2>')
screen:expect([[
Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
{c:>} {0:>---}{c: ^X } {0:>}|
|
{0:~ }|
{0:~ }|
|
]])
end) -- level 1 - non wrapped
it('(level 1) click on wrapped lines', function()
feed_command('let &conceallevel=1', 'let &wrap=1', 'echo')
feed('<esc><LeftMouse><0,0>')
feed('<esc><LeftMouse><24,1>')
screen:expect([[
{c:^Y}rem ip{c:X}um do{c: } {c:X}it {c: } |
, con{c:X}etetur {c:X}adip{c:X}cin{c:X} |
elitr. |
{c:X}tet {c: }ta ka{c:X}d {c:X}ber{c:X}en |
, no {c:X}ea takimata {c:X}anctu{c:X}|
e{c:X}t. |
Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c:^ }|
t4{c: } |
{c:>} {0:>---}{c: X} |
{c: } 🐈 |
|
|
]])
feed('<esc><LeftMouse><6,1>')
feed('<esc><LeftMouse><0,2>')
screen:expect([[
{c:Y}rem ip{c:X}um do{c: } {c:X}it {c: } |
, con{c:X}^etetur {c:X}adip{c:X}cin{c:X} |
elitr. |
{c:X}tet {c: }ta ka{c:X}d {c:X}ber{c:X}en |
, no {c:X}ea takimata {c:X}anctu{c:X}|
e{c:X}t. |
Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
^t4{c: } |
{c:>} {0:>---}{c: X} |
{c: } 🐈 |
|
|
]])
feed('<esc><LeftMouse><15,1>')
feed('<esc><LeftMouse><8,3>')
screen:expect([[
{c:Y}rem ip{c:X}um do{c: } {c:X}it {c: } |
, con{c:X}etetur {c:X}a^dip{c:X}cin{c:X} |
elitr. |
{c:X}tet {c: }ta ka{c:X}d {c:X}ber{c:X}en |
, no {c:X}ea takimata {c:X}anctu{c:X}|
e{c:X}t. |
Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
t4{c: } |
{c:>} ^{0:>---}{c: X} |
{c: } 🐈 |
|
|
]])
feed('<esc><LeftMouse><15,3>')
feed('<esc><LeftMouse><21,3>')
screen:expect([[
{c:Y}rem ip{c:X}um do{c: } {c:X}it {c: } |
, con{c:X}etetur {c:X}adip{c:X}cin{c:X} |
elitr. |
{c:X}tet {c: }ta ka{c:X}d {c:X}^ber{c:X}en |
, no {c:X}ea takimata {c:X}anctu{c:X}|
e{c:X}t. |
Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
t4{c: } |
{c:>} {0:>---}{c: ^X} |
{c: } 🐈 |
|
|
]])
feed('<esc><LeftMouse><4,4>')
screen:expect([[
Section{0:>>--->--->---}{c: }t1{c: } |
{0:>--->--->---} {c: }t2{c: } {c: }t3{c: } {c: }|
t4{c: } |
{c:>} {0:>---}{c: X} |
{c: } ^🐈 |
|
|
]])
end) -- level 1 - wrapped
@ -863,45 +910,67 @@ describe('ui/mouse/input', function()
it('(level 2) click on non-wrapped lines', function()
feed_command('let &conceallevel=2', 'echo')
feed('<esc><LeftMouse><0,0>')
feed('<esc><LeftMouse><20,0>')
screen:expect([[
{c:^Y}rem ip{c:X}um do {c:X}it , con{c:X}e|
{c:X}tet ta ka{c:X}d {c:X}ber{c:X}en, no |
Section{0:>>--->--->---}^t1 |
{0:>--->--->---} t2 t3 t4 |
{c:>} {0:>---}{c:X} {0:>}|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><1,0>')
feed('<esc><LeftMouse><14,1>')
screen:expect([[
{c:Y}^rem ip{c:X}um do {c:X}it , con{c:X}e|
{c:X}tet ta ka{c:X}d {c:X}ber{c:X}en, no |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} ^t2 t3 t4 |
{c:>} {0:>---}{c:X} {0:>}|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><15,0>')
feed('<esc><LeftMouse><18,1>')
screen:expect([[
{c:Y}rem ip{c:X}um do {c:X}^it , con{c:X}e|
{c:X}tet ta ka{c:X}d {c:X}ber{c:X}en, no |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t^3 t4 |
{c:>} {0:>---}{c:X} {0:>}|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><15,1>')
feed('<esc><LeftMouse><0,2>') -- Weirdness
screen:expect([[
{c:Y}rem ip{c:X}um do {c:X}it , con{c:X}e|
{c:X}tet ta ka{c:X}d {c:X}b^er{c:X}en, no |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 t4 |
{c:^>} {0:>---}{c:X} {0:>}|
|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><8,2>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 t4 |
{c:>} ^{0:>---}{c:X} {0:>}|
|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><20,2>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 t4 |
{c:>} {0:>---}{c:^X} {0:>}|
|
{0:~ }|
{0:~ }|
|
@ -911,47 +980,108 @@ describe('ui/mouse/input', function()
it('(level 2) click on wrapped lines', function()
feed_command('let &conceallevel=2', 'let &wrap=1', 'echo')
feed('<esc><LeftMouse><0,0>')
feed('<esc><LeftMouse><20,0>')
screen:expect([[
{c:^Y}rem ip{c:X}um do {c:X}it |
, con{c:X}etetur {c:X}adip{c:X}cin{c:X} |
elitr. |
{c:X}tet ta ka{c:X}d {c:X}ber{c:X}en |
, no {c:X}ea takimata {c:X}anctu{c:X}|
e{c:X}t. |
Section{0:>>--->--->---}^t1 |
{0:>--->--->---} t2 t3 |
t4 |
{c:>} {0:>---}{c:X} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><6,1>')
feed('<esc><LeftMouse><14,1>')
screen:expect([[
{c:Y}rem ip{c:X}um do {c:X}it |
, con{c:X}^etetur {c:X}adip{c:X}cin{c:X} |
elitr. |
{c:X}tet ta ka{c:X}d {c:X}ber{c:X}en |
, no {c:X}ea takimata {c:X}anctu{c:X}|
e{c:X}t. |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} ^t2 t3 |
t4 |
{c:>} {0:>---}{c:X} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><15,1>')
feed('<esc><LeftMouse><18,1>')
screen:expect([[
{c:Y}rem ip{c:X}um do {c:X}it |
, con{c:X}etetur {c:X}a^dip{c:X}cin{c:X} |
elitr. |
{c:X}tet ta ka{c:X}d {c:X}ber{c:X}en |
, no {c:X}ea takimata {c:X}anctu{c:X}|
e{c:X}t. |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t^3 |
t4 |
{c:>} {0:>---}{c:X} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><15,3>')
-- NOTE: The click would ideally be on the 't' in 't4', but wrapping
-- caused the invisible '*' right before 't4' to remain on the previous
-- screen line. This is being treated as expected because fixing this is
-- out of scope for mouse clicks. Should the wrapping behavior of
-- concealed characters change in the future, this case should be
-- reevaluated.
feed('<esc><LeftMouse><0,2>')
screen:expect([[
{c:Y}rem ip{c:X}um do {c:X}it |
, con{c:X}etetur {c:X}adip{c:X}cin{c:X} |
elitr. |
{c:X}tet ta ka{c:X}d {c:X}b^er{c:X}en |
, no {c:X}ea takimata {c:X}anctu{c:X}|
e{c:X}t. |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 ^ |
t4 |
{c:>} {0:>---}{c:X} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><1,2>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t^4 |
{c:>} {0:>---}{c:X} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><0,3>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t4 |
{c:^>} {0:>---}{c:X} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><20,3>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t4 |
{c:>} {0:>---}{c:^X} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><1,4>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t4 |
{c:>} {0:>---}{c:X} |
^🐈 |
|
|
]])
feed('<esc><LeftMouse><5,4>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t4 |
{c:>} {0:>---}{c:X} |
🐈^ |
|
|
]])
end) -- level 2 - wrapped
@ -960,46 +1090,46 @@ describe('ui/mouse/input', function()
it('(level 3) click on non-wrapped lines', function()
feed_command('let &conceallevel=3', 'echo')
feed('<esc><LeftMouse><0,0>')
feed('<esc><LeftMouse><0,2>')
screen:expect([[
^rem ipum do it , conetetu|
tet ta kad beren, no ea t|
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 t4 |
^ {0:>----} 🐈|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><1,0>')
feed('<esc><LeftMouse><1,2>')
screen:expect([[
r^em ipum do it , conetetu|
tet ta kad beren, no ea t|
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 t4 |
^{0:>----} 🐈|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><15,0>')
feed('<esc><LeftMouse><13,2>')
screen:expect([[
rem ipum do it ^, conetetu|
tet ta kad beren, no ea t|
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 t4 |
^{0:>----} 🐈|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
feed('<esc><LeftMouse><15,1>')
feed('<esc><LeftMouse><20,2>')
screen:expect([[
rem ipum do it , conetetu|
tet ta kad bere^n, no ea t|
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 t4 |
{0:>----}^ 🐈|
|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
@ -1008,49 +1138,94 @@ describe('ui/mouse/input', function()
it('(level 3) click on wrapped lines', function()
feed_command('let &conceallevel=3', 'let &wrap=1', 'echo')
feed('<esc><LeftMouse><0,0>')
feed('<esc><LeftMouse><14,1>')
screen:expect([[
^rem ipum do it |
, conetetur adipcin |
elitr. |
tet ta kad beren |
, no ea takimata anctu |
et. |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} ^t2 t3 |
t4 |
{0:>----} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><6,1>')
feed('<esc><LeftMouse><18,1>')
screen:expect([[
rem ipum do it |
, cone^tetur adipcin |
elitr. |
tet ta kad beren |
, no ea takimata anctu |
et. |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t^3 |
t4 |
{0:>----} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><15,1>')
feed('<esc><LeftMouse><1,2>')
screen:expect([[
rem ipum do it |
, conetetur adi^pcin |
elitr. |
tet ta kad beren |
, no ea takimata anctu |
et. |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t^4 |
{0:>----} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><15,3>')
feed('<esc><LeftMouse><0,3>')
screen:expect([[
rem ipum do it |
, conetetur adipcin |
elitr. |
tet ta kad bere^n |
, no ea takimata anctu |
et. |
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t4 |
^ {0:>----} |
🐈 |
|
|
]])
feed('<esc><LeftMouse><20,3>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t4 |
{0:>----}^ |
🐈 |
|
|
]])
feed('<esc><LeftMouse><1,4>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t4 |
{0:>----} |
^🐈 |
|
|
]])
feed('<esc><LeftMouse><3,4>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t4 |
{0:>----} |
^🐈 |
|
|
]])
feed('<esc><LeftMouse><5,4>')
screen:expect([[
Section{0:>>--->--->---}t1 |
{0:>--->--->---} t2 t3 |
t4 |
{0:>----} |
🐈^ |
|
|
]])
end) -- level 3 - wrapped
end)
end)