vim-patch:9.1.0494: Wrong matched text highlighted in pum with 'rightleft' (#29371)

Problem:  Wrong matched text highlighted in pum with 'rightleft'.
Solution: Match using the original text instead of the reversed text.
          (zeertzjq)

closes: vim/vim#15020

63901e8963
This commit is contained in:
zeertzjq
2024-06-17 06:42:11 +08:00
committed by GitHub
parent 7746c54e10
commit 20a7eebec0
3 changed files with 156 additions and 63 deletions

View File

@@ -437,70 +437,81 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
pum_redraw();
}
/// Displays text on the popup menu with specific attributes.
static void pum_puts_with_attr(int col, char *text, hlf_T hlf)
/// Computes attributes of text on the popup menu.
/// Returns attributes for every cell, or NULL if all attributes are the same.
static int *pum_compute_text_attrs(char *text, hlf_T hlf)
{
char *leader = ins_compl_leader();
if (leader == NULL || *leader == NUL || (hlf != HLF_PSI && hlf != HLF_PNI)
|| (win_hl_attr(curwin, HLF_PMSI) == win_hl_attr(curwin, HLF_PSI)
&& win_hl_attr(curwin, HLF_PMNI) == win_hl_attr(curwin, HLF_PNI))) {
grid_line_puts(col, text, -1, win_hl_attr(curwin, (int)hlf));
return;
return NULL;
}
char *rt_leader = NULL;
if (pum_rl) {
rt_leader = reverse_text(leader);
}
char *match_leader = rt_leader != NULL ? rt_leader : leader;
size_t leader_len = strlen(match_leader);
int *attrs = xmalloc(sizeof(int) * (size_t)vim_strsize(text));
size_t leader_len = strlen(leader);
const bool in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
garray_T *ga = NULL;
bool matched_start = false;
if (in_fuzzy) {
ga = fuzzy_match_str_with_pos(text, match_leader);
ga = fuzzy_match_str_with_pos(text, leader);
} else {
matched_start = strncmp(text, leader, leader_len) == 0;
}
// Render text with proper attributes
const char *ptr = text;
int cell_idx = 0;
uint32_t char_pos = 0;
while (*ptr != NUL) {
int char_len = utfc_ptr2len(ptr);
int cells = utf_ptr2cells(ptr);
int new_attr = win_hl_attr(curwin, (int)hlf);
if (ga != NULL) {
// Handle fuzzy matching
for (int i = 0; i < ga->ga_len; i++) {
uint32_t *match_pos = ((uint32_t *)ga->ga_data) + i;
uint32_t actual_char_pos = 0;
const char *temp_ptr = text;
while (temp_ptr < ptr) {
temp_ptr += utfc_ptr2len(temp_ptr);
actual_char_pos++;
}
if (actual_char_pos == match_pos[0]) {
if (char_pos == ((uint32_t *)ga->ga_data)[i]) {
new_attr = win_hl_attr(curwin, hlf == HLF_PSI ? HLF_PMSI : HLF_PMNI);
break;
}
}
} else if (!in_fuzzy && ptr < text + leader_len
&& strncmp(text, match_leader, leader_len) == 0) {
} else if (matched_start && ptr < text + leader_len) {
new_attr = win_hl_attr(curwin, hlf == HLF_PSI ? HLF_PMSI : HLF_PMNI);
}
grid_line_puts(col, ptr, char_len, new_attr);
col += cells;
ptr += char_len;
int char_cells = utf_ptr2cells(ptr);
for (int i = 0; i < char_cells; i++) {
attrs[cell_idx + i] = new_attr;
}
cell_idx += char_cells;
MB_PTR_ADV(ptr);
char_pos++;
}
if (ga != NULL) {
ga_clear(ga);
xfree(ga);
}
if (rt_leader) {
xfree(rt_leader);
return attrs;
}
/// Displays text on the popup menu with specific attributes.
static void pum_grid_puts_with_attrs(int col, int cells, const char *text, int textlen,
const int *attrs)
{
const int col_start = col;
const char *ptr = text;
// Render text with proper attributes
while (*ptr != NUL && (textlen < 0 || ptr < text + textlen)) {
int char_len = utfc_ptr2len(ptr);
int attr = attrs[pum_rl ? (col_start + cells - col - 1) : (col - col_start)];
grid_line_puts(col, ptr, char_len, attr);
col += utf_ptr2cells(ptr);
ptr += char_len;
}
}
@@ -643,35 +654,50 @@ void pum_redraw(void)
*p = saved;
}
int *attrs = pum_compute_text_attrs(st, hlf);
if (pum_rl) {
char *rt = reverse_text(st);
char *rt_start = rt;
int size = vim_strsize(rt);
int cells = vim_strsize(rt);
if (size > pum_width) {
if (cells > pum_width) {
do {
size -= utf_ptr2cells(rt);
cells -= utf_ptr2cells(rt);
MB_PTR_ADV(rt);
} while (size > pum_width);
} while (cells > pum_width);
if (size < pum_width) {
if (cells < pum_width) {
// Most left character requires 2-cells but only 1 cell
// is available on screen. Put a '<' on the left of the
// pum item
*(--rt) = '<';
size++;
cells++;
}
}
pum_puts_with_attr(grid_col - size + 1, rt, hlf);
if (attrs == NULL) {
grid_line_puts(grid_col - cells + 1, rt, -1, attr);
} else {
pum_grid_puts_with_attrs(grid_col - cells + 1, cells, rt, -1, attrs);
}
xfree(rt_start);
xfree(st);
grid_col -= width;
} else {
pum_puts_with_attr(grid_col, st, hlf);
if (attrs == NULL) {
grid_line_puts(grid_col, st, -1, attr);
} else {
pum_grid_puts_with_attrs(grid_col, vim_strsize(st), st, -1, attrs);
}
xfree(st);
grid_col += width;
}
xfree(attrs);
if (*p != TAB) {
break;
}

View File

@@ -1177,8 +1177,8 @@ describe('builtin popupmenu', function()
ks = { foreground = Screen.colors.Red, background = Screen.colors.Grey },
xn = { foreground = Screen.colors.White, background = Screen.colors.Magenta },
xs = { foreground = Screen.colors.Black, background = Screen.colors.Grey },
mn = { foreground = Screen.colors.Blue, background = Screen.colors.White },
ms = { foreground = Screen.colors.Green, background = Screen.colors.White },
mn = { foreground = Screen.colors.Blue, background = Screen.colors.Magenta },
ms = { foreground = Screen.colors.Blue, background = Screen.colors.Grey },
})
screen:attach({ ext_multigrid = multigrid })
end)
@@ -4669,6 +4669,7 @@ describe('builtin popupmenu', function()
return {
\ 'words': [
\ { 'word': 'foo', 'kind': 'fookind' },
\ { 'word': 'foofoo', 'kind': 'fookind' },
\ { 'word': 'foobar', 'kind': 'fookind' },
\ { 'word': 'fooBaz', 'kind': 'fookind' },
\ { 'word': 'foobala', 'kind': 'fookind' },
@@ -4680,13 +4681,14 @@ describe('builtin popupmenu', function()
endfunc
set omnifunc=Omni_test
set completeopt=menu,noinsert,fuzzy
hi PmenuMatchSel guifg=Green guibg=White
hi PmenuMatch guifg=Blue guibg=White
hi PmenuMatchSel guifg=Blue guibg=Grey
hi PmenuMatch guifg=Blue guibg=Magenta
]])
feed('i<C-X><C-O>')
local pum_start = [[
^ |
{s:foo fookind }{1: }|
{n:foofoo fookind }{1: }|
{n:foobar fookind }{1: }|
{n:fooBaz fookind }{1: }|
{n:foobala fookind }{1: }|
@@ -4694,19 +4696,20 @@ describe('builtin popupmenu', function()
{n:你好吗 }{1: }|
{n:你不好吗 }{1: }|
{n:你可好吗 }{1: }|
{1:~ }|*10
{2:-- }{5:match 1 of 8} |
{1:~ }|*9
{2:-- }{5:match 1 of 9} |
]]
screen:expect(pum_start)
feed('fo')
screen:expect([[
fo^ |
{ms:fo}{s:o fookind }{1: }|
{mn:fo}{n:ofoo fookind }{1: }|
{mn:fo}{n:obar fookind }{1: }|
{mn:fo}{n:oBaz fookind }{1: }|
{mn:fo}{n:obala fookind }{1: }|
{1:~ }|*14
{2:-- }{5:match 1 of 8} |
{1:~ }|*13
{2:-- }{5:match 1 of 9} |
]])
feed('<Esc>S<C-X><C-O>')
screen:expect(pum_start)
@@ -4718,7 +4721,7 @@ describe('builtin popupmenu', function()
{mn:你}{n:不好吗 }{1: }|
{mn:你}{n:可好吗 }{1: }|
{1:~ }|*14
{2:-- }{5:match 1 of 8} |
{2:-- }{5:match 1 of 9} |
]])
feed('')
screen:expect([[
@@ -4727,15 +4730,16 @@ describe('builtin popupmenu', function()
{mn:你}{n:不好}{mn:吗}{n: }{1: }|
{mn:你}{n:可好}{mn:吗}{n: }{1: }|
{1:~ }|*15
{2:-- }{5:match 1 of 8} |
{2:-- }{5:match 1 of 9} |
]])
feed('<C-E><Esc>')
command('set rightleft')
feed('S<C-X><C-O>')
screen:expect([[
local pum_start_rl = [[
^ |
{1: }{s: dnikoof oof}|
{1: }{n: dnikoof oofoof}|
{1: }{n: dnikoof raboof}|
{1: }{n: dnikoof zaBoof}|
{1: }{n: dnikoof alaboof}|
@@ -4743,18 +4747,41 @@ describe('builtin popupmenu', function()
{1: }{n: 吗好你}|
{1: }{n: 吗好不你}|
{1: }{n: 吗好可你}|
{1: ~}|*10
{2:-- }{5:match 1 of 8} |
]])
{1: ~}|*9
{2:-- }{5:match 1 of 9} |
]]
screen:expect(pum_start_rl)
feed('fo')
screen:expect([[
^ of|
{1: }{s: dnikoof o}{ms:of}|
{1: }{n: dnikoof oofo}{mn:of}|
{1: }{n: dnikoof rabo}{mn:of}|
{1: }{n: dnikoof zaBo}{mn:of}|
{1: }{n: dnikoof alabo}{mn:of}|
{1: ~}|*13
{2:-- }{5:match 1 of 9} |
]])
feed('<Esc>S<C-X><C-O>')
screen:expect(pum_start_rl)
feed('')
screen:expect([[
^ 你|
{1: }{s: 好}{ms:你}|
{1: }{n: 吗好}{mn:你}|
{1: }{n: 吗好不}{mn:你}|
{1: }{n: 吗好可}{mn:你}|
{1: ~}|*14
{2:-- }{5:match 1 of 8} |
{2:-- }{5:match 1 of 9} |
]])
feed('')
screen:expect([[
^ 吗你|
{1: }{s: }{ms:吗}{s:好}{ms:你}|
{1: }{n: }{mn:吗}{n:好不}{mn:你}|
{1: }{n: }{mn:吗}{n:好可}{mn:你}|
{1: ~}|*15
{2:-- }{5:match 1 of 9} |
]])
feed('<C-E><Esc>')
command('set norightleft')
@@ -4766,12 +4793,31 @@ describe('builtin popupmenu', function()
screen:expect([[
fo^ |
{ms:fo}{s:o fookind }{1: }|
{mn:fo}{n:ofoo fookind }{1: }|
{mn:fo}{n:obar fookind }{1: }|
{mn:fo}{n:oBaz fookind }{1: }|
{mn:fo}{n:obala fookind }{1: }|
{1:~ }|*14
{2:-- }{5:match 1 of 8} |
{1:~ }|*13
{2:-- }{5:match 1 of 9} |
]])
feed('<C-E><Esc>')
command('set rightleft')
feed('S<C-X><C-O>')
screen:expect(pum_start_rl)
feed('fo')
screen:expect([[
^ of|
{1: }{s: dnikoof o}{ms:of}|
{1: }{n: dnikoof oofo}{mn:of}|
{1: }{n: dnikoof rabo}{mn:of}|
{1: }{n: dnikoof zaBo}{mn:of}|
{1: }{n: dnikoof alabo}{mn:of}|
{1: ~}|*13
{2:-- }{5:match 1 of 9} |
]])
feed('<C-E><Esc>')
command('set norightleft')
end)
end
end

View File

@@ -1397,6 +1397,7 @@ func Test_pum_highlights_match()
return {
\ 'words': [
\ { 'word': 'foo', 'kind': 'fookind' },
\ { 'word': 'foofoo', 'kind': 'fookind' },
\ { 'word': 'foobar', 'kind': 'fookind' },
\ { 'word': 'fooBaz', 'kind': 'fookind' },
\ { 'word': 'foobala', 'kind': 'fookind' },
@@ -1408,7 +1409,7 @@ func Test_pum_highlights_match()
endfunc
set omnifunc=Omni_test
set completeopt=menu,noinsert,fuzzy
hi PmenuMatchSel ctermfg=6 ctermbg=225
hi PmenuMatchSel ctermfg=6 ctermbg=7
hi PmenuMatch ctermfg=4 ctermbg=225
END
call writefile(lines, 'Xscript', 'D')
@@ -1419,7 +1420,7 @@ func Test_pum_highlights_match()
call term_sendkeys(buf, "fo")
call TermWait(buf, 50)
call VerifyScreenDump(buf, 'Test_pum_highlights_03', {})
call term_sendkeys(buf, "\<ESC>S\<C-x>\<C-O>")
call term_sendkeys(buf, "\<Esc>S\<C-X>\<C-O>")
call TermWait(buf, 50)
call term_sendkeys(buf, "你")
call TermWait(buf, 50)
@@ -1427,28 +1428,48 @@ func Test_pum_highlights_match()
call term_sendkeys(buf, "吗")
call TermWait(buf, 50)
call VerifyScreenDump(buf, 'Test_pum_highlights_05', {})
call term_sendkeys(buf, "\<C-E>\<Esc>")
if has('rightleft')
call term_sendkeys(buf, "\<C-E>\<ESC>u:set rightleft\<CR>")
call term_sendkeys(buf, ":set rightleft\<CR>")
call TermWait(buf, 50)
call term_sendkeys(buf, "i\<C-X>\<C-O>")
call term_sendkeys(buf, "S\<C-X>\<C-O>")
call TermWait(buf, 50)
call term_sendkeys(buf, "fo")
call TermWait(buf, 50)
call VerifyScreenDump(buf, 'Test_pum_highlights_06', {})
call term_sendkeys(buf, "\<C-E>\<ESC>u:set norightleft\<CR>")
call term_sendkeys(buf, "\<Esc>S\<C-X>\<C-O>")
call TermWait(buf, 50)
call term_sendkeys(buf, "你")
call VerifyScreenDump(buf, 'Test_pum_highlights_06a', {})
call term_sendkeys(buf, "吗")
call VerifyScreenDump(buf, 'Test_pum_highlights_06b', {})
call term_sendkeys(buf, "\<C-E>\<Esc>")
call term_sendkeys(buf, ":set norightleft\<CR>")
call TermWait(buf)
endif
call term_sendkeys(buf, ":set completeopt-=fuzzy\<CR>")
call TermWait(buf)
call term_sendkeys(buf, "\<ESC>S\<C-x>\<C-O>")
call term_sendkeys(buf, "S\<C-X>\<C-O>")
call TermWait(buf, 50)
call term_sendkeys(buf, "fo")
call TermWait(buf, 50)
call VerifyScreenDump(buf, 'Test_pum_highlights_07', {})
call term_sendkeys(buf, "\<C-E>\<Esc>")
if has('rightleft')
call term_sendkeys(buf, ":set rightleft\<CR>")
call TermWait(buf, 50)
call term_sendkeys(buf, "S\<C-X>\<C-O>")
call TermWait(buf, 50)
call term_sendkeys(buf, "fo")
call TermWait(buf, 50)
call VerifyScreenDump(buf, 'Test_pum_highlights_08', {})
call term_sendkeys(buf, "\<C-E>\<Esc>")
call term_sendkeys(buf, ":set norightleft\<CR>")
endif
call term_sendkeys(buf, "\<C-E>\<Esc>u")
call TermWait(buf)
call StopVimInTerminal(buf)
endfunc