vim-patch:9.1.0180: Cursor pos wrong when double-width chars are concealed (#27862)

Problem:  Cursor pos wrong when double-width chars are concealed.
Solution: Advance one more virtual column for a double-width char.
          Run some tests with both 'wrap' and 'nowrap' (zeertzjq).

closes: vim/vim#14197

010e1539d6
This commit is contained in:
zeertzjq 2024-03-15 06:56:45 +08:00 committed by GitHub
parent ca7dd33fa7
commit 60491466f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 248 additions and 46 deletions

View File

@ -2403,6 +2403,13 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
} else {
mb_schar = schar_from_ascii(' ');
}
if (utf_char2cells(mb_c) > 1) {
// When the first char to be concealed is double-width,
// need to advance one more virtual column.
wlv.n_extra++;
}
mb_c = schar_get_first_codepoint(mb_schar);
prev_syntax_id = syntax_seqnr;
@ -2739,11 +2746,21 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
wlv.off++;
wlv.col++;
} else if (wp->w_p_cole > 0 && is_concealing) {
bool concealed_wide = utf_char2cells(mb_c) > 1;
wlv.skip_cells--;
wlv.vcol_off_co++;
if (concealed_wide) {
// When a double-width char is concealed,
// need to advance one more virtual column.
wlv.vcol++;
wlv.vcol_off_co++;
}
if (wlv.n_extra > 0) {
wlv.vcol_off_co += wlv.n_extra;
}
if (is_wrapped) {
// Special voodoo required if 'wrap' is on.
//
@ -2764,7 +2781,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
wlv.n_attr = 0;
}
if (utf_char2cells(mb_c) > 1) {
if (concealed_wide) {
// Need to fill two screen columns.
wlv.boguscols++;
wlv.col++;

View File

@ -4,6 +4,7 @@ local clear = helpers.clear
local command = helpers.command
local exec = helpers.exec
local feed = helpers.feed
local api = helpers.api
local expect_pos = function(row, col)
return helpers.eq({ row, col }, helpers.eval('[screenrow(), screencol()]'))
@ -633,13 +634,13 @@ describe('Conceal', function()
expect_pos(9, 26)
end)
-- oldtest: Test_conceal_virtualedit_after_eol()
it('cursor drawn at correct column with virtualedit', function()
local screen = Screen.new(75, 3)
local function test_conceal_virtualedit_after_eol(wrap)
local screen = Screen.new(60, 3)
screen:set_default_attr_ids({
[0] = { bold = true, foreground = Screen.colors.Blue }, -- NonText
})
screen:attach()
api.nvim_set_option_value('wrap', wrap, {})
exec([[
call setline(1, 'abcdefgh|hidden|ijklmnpop')
syntax match test /|hidden|/ conceal
@ -675,15 +676,25 @@ describe('Conceal', function()
{0:~ }|
|
]])
end
-- oldtest: Test_conceal_virtualedit_after_eol()
describe('cursor drawn at correct column with virtualedit', function()
it('with wrapping', function()
test_conceal_virtualedit_after_eol(true)
end)
it('without wrapping', function()
test_conceal_virtualedit_after_eol(false)
end)
end)
-- oldtest: Test_conceal_virtualedit_after_eol_rightleft()
it('cursor drawn correctly with virtualedit and rightleft', function()
local screen = Screen.new(75, 3)
local function test_conceal_virtualedit_after_eol_rightleft(wrap)
local screen = Screen.new(60, 3)
screen:set_default_attr_ids({
[0] = { bold = true, foreground = Screen.colors.Blue }, -- NonText
})
screen:attach()
api.nvim_set_option_value('wrap', wrap, {})
exec([[
call setline(1, 'abcdefgh|hidden|ijklmnpop')
syntax match test /|hidden|/ conceal
@ -719,5 +730,113 @@ describe('Conceal', function()
{0: ~}|
|
]])
end
-- oldtest: Test_conceal_virtualedit_after_eol_rightleft()
describe('cursor drawn correctly with virtualedit and rightleft', function()
it('with wrapping', function()
test_conceal_virtualedit_after_eol_rightleft(true)
end)
it('without wrapping', function()
test_conceal_virtualedit_after_eol_rightleft(false)
end)
end)
local function test_conceal_double_width(wrap)
local screen = Screen.new(60, 4)
screen:set_default_attr_ids({
[0] = { bold = true, foreground = Screen.colors.Blue },
[1] = { background = Screen.colors.DarkGrey, foreground = Screen.colors.LightGrey },
[2] = { background = Screen.colors.LightRed },
})
screen:attach()
api.nvim_set_option_value('wrap', wrap, {})
exec([[
call setline(1, ['aaaaa口=口bbbbb口=口ccccc', 'foobar'])
syntax match test /=/ conceal cchar=β
set conceallevel=2 concealcursor=n colorcolumn=30
normal! $
]])
screen:expect([[
aaaaa{1:β}bbbbb{1:β}cccc^c {2: } |
foobar {2: } |
{0:~ }|
|
]])
feed('gM')
screen:expect([[
aaaaa{1:β}bb^bbb{1:β}ccccc {2: } |
foobar {2: } |
{0:~ }|
|
]])
command('set conceallevel=3')
screen:expect([[
aaaaabb^bbbccccc {2: } |
foobar {2: } |
{0:~ }|
|
]])
feed('$')
screen:expect([[
aaaaabbbbbcccc^c {2: } |
foobar {2: } |
{0:~ }|
|
]])
end
-- oldtest: Test_conceal_double_width()
describe('cursor drawn correctly when double-width chars are concealed', function()
it('with wrapping', function()
test_conceal_double_width(true)
end)
it('without wrapping', function()
test_conceal_double_width(false)
end)
end)
-- oldtest: Test_conceal_double_width_wrap()
it('line wraps correctly when double-width chars are concealed', function()
local screen = Screen.new(20, 4)
screen:set_default_attr_ids({
[0] = { bold = true, foreground = Screen.colors.Blue },
[1] = { background = Screen.colors.DarkGrey, foreground = Screen.colors.LightGrey },
[2] = { background = Screen.colors.LightRed },
})
screen:attach()
exec([[
call setline(1, 'aaaaaaaaaa口=口bbbbbbbbbb口=口cccccccccc')
syntax match test /=/ conceal cchar=β
set conceallevel=2 concealcursor=n
normal! $
]])
screen:expect([[
aaaaaaaaaa{1:β}bbbbb |
bbbbb{1:β}ccccccccc^c |
{0:~ }|
|
]])
feed('gM')
screen:expect([[
aaaaaaaaaa{1:β}bbbbb |
^bbbbb{1:β}cccccccccc |
{0:~ }|
|
]])
command('set conceallevel=3')
screen:expect([[
aaaaaaaaaabbbbb |
^bbbbbcccccccccc |
{0:~ }|
|
]])
feed('$')
screen:expect([[
aaaaaaaaaabbbbb |
bbbbbccccccccc^c |
{0:~ }|
|
]])
end)
end)

View File

@ -439,7 +439,7 @@ func Test_conceal_mouse_click()
call Ntest_setmouse(1, 19)
call feedkeys("\<LeftMouse>", "tx")
call assert_equal([0, 1, 23, 0, 23], getcurpos())
" click after end of line puts cursor there without 'virtualedit'
" click after end of line puts cursor there with 'virtualedit'
call Ntest_setmouse(1, 20)
call feedkeys("\<LeftMouse>", "tx")
call assert_equal([0, 1, 24, 0, 24], getcurpos())
@ -462,10 +462,9 @@ endfunc
" Test that cursor is drawn at the correct column when it is after end of the
" line with 'virtualedit' and concealing.
func Test_conceal_virtualedit_after_eol()
CheckScreendump
let code =<< trim [CODE]
func Run_test_conceal_virtualedit_after_eol(wrap)
let code =<< trim eval [CODE]
let &wrap = {a:wrap}
call setline(1, 'abcdefgh|hidden|ijklmnpop')
syntax match test /|hidden|/ conceal
set conceallevel=2 concealcursor=n virtualedit=all
@ -487,12 +486,17 @@ func Test_conceal_virtualedit_after_eol()
call StopVimInTerminal(buf)
endfunc
" Same as Test_conceal_virtualedit_after_eol(), but with 'rightleft' set.
func Test_conceal_virtualedit_after_eol_rightleft()
CheckFeature rightleft
func Test_conceal_virtualedit_after_eol()
CheckScreendump
let code =<< trim [CODE]
call Run_test_conceal_virtualedit_after_eol(1)
call Run_test_conceal_virtualedit_after_eol(0)
endfunc
" Same as Run_test_conceal_virtualedit_after_eol(), but with 'rightleft'.
func Run_test_conceal_virtualedit_after_eol_rightleft(wrap)
let code =<< trim eval [CODE]
let &wrap = {a:wrap}
call setline(1, 'abcdefgh|hidden|ijklmnpop')
syntax match test /|hidden|/ conceal
set conceallevel=2 concealcursor=n virtualedit=all rightleft
@ -514,4 +518,66 @@ func Test_conceal_virtualedit_after_eol_rightleft()
call StopVimInTerminal(buf)
endfunc
func Test_conceal_virtualedit_after_eol_rightleft()
CheckFeature rightleft
CheckScreendump
call Run_test_conceal_virtualedit_after_eol_rightleft(1)
call Run_test_conceal_virtualedit_after_eol_rightleft(0)
endfunc
" Test that cursor position is correct when double-width chars are concealed.
func Run_test_conceal_double_width(wrap)
let code =<< trim eval [CODE]
let &wrap = {a:wrap}
call setline(1, ['aaaaa口=口bbbbb口=口ccccc', 'foobar'])
syntax match test /口=口/ conceal cchar=β
set conceallevel=2 concealcursor=n colorcolumn=30
normal! $
[CODE]
call writefile(code, 'XTest_conceal_double_width', 'D')
let buf = RunVimInTerminal('-S XTest_conceal_double_width', {'rows': 4})
call VerifyScreenDump(buf, 'Test_conceal_double_width_1', {})
call term_sendkeys(buf, "gM")
call VerifyScreenDump(buf, 'Test_conceal_double_width_2', {})
call term_sendkeys(buf, ":set conceallevel=3\<CR>")
call VerifyScreenDump(buf, 'Test_conceal_double_width_3', {})
call term_sendkeys(buf, "$")
call VerifyScreenDump(buf, 'Test_conceal_double_width_4', {})
" clean up
call StopVimInTerminal(buf)
endfunc
func Test_conceal_double_width()
CheckScreendump
call Run_test_conceal_double_width(1)
call Run_test_conceal_double_width(0)
endfunc
" Test that line wrapping is correct when double-width chars are concealed.
func Test_conceal_double_width_wrap()
CheckScreendump
let code =<< trim [CODE]
call setline(1, 'aaaaaaaaaa口=口bbbbbbbbbb口=口cccccccccc')
syntax match test /口=口/ conceal cchar=β
set conceallevel=2 concealcursor=n
normal! $
[CODE]
call writefile(code, 'XTest_conceal_double_width_wrap', 'D')
let buf = RunVimInTerminal('-S XTest_conceal_double_width_wrap', {'rows': 4, 'cols': 20})
call VerifyScreenDump(buf, 'Test_conceal_double_width_wrap_1', {})
call term_sendkeys(buf, "gM")
call VerifyScreenDump(buf, 'Test_conceal_double_width_wrap_2', {})
call term_sendkeys(buf, ":set conceallevel=3\<CR>")
call VerifyScreenDump(buf, 'Test_conceal_double_width_wrap_3', {})
call term_sendkeys(buf, "$")
call VerifyScreenDump(buf, 'Test_conceal_double_width_wrap_4', {})
" clean up
call StopVimInTerminal(buf)
endfunc
" vim: shiftwidth=2 sts=2 expandtab