From 62da4e2949cc906102bd768cdd40b274623822b6 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 28 Mar 2025 15:37:49 +0800 Subject: vim-patch:9.1.1250: cannot set the maximum popup menu width Problem: cannot set the maximum popup menu width (Lucas Mior) Solution: add the new global option value 'pummaxwidth' (glepnir) fixes: vim/vim#10901 closes: vim/vim#16943 https://github.com/vim/vim/commit/88d75934c3d5bc4c406343f106e1a61638abd3a7 Co-authored-by: glepnir --- src/nvim/option_vars.h | 1 + src/nvim/options.lua | 15 +++++++ src/nvim/popupmenu.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/option_vars.h b/src/nvim/option_vars.h index 0b5d0a45b4..e624ab80ef 100644 --- a/src/nvim/option_vars.h +++ b/src/nvim/option_vars.h @@ -306,6 +306,7 @@ EXTERN char *p_csl; ///< 'completeslash' EXTERN OptInt p_pb; ///< 'pumblend' EXTERN OptInt p_ph; ///< 'pumheight' EXTERN OptInt p_pw; ///< 'pumwidth' +EXTERN OptInt p_pmw; ///< 'pummaxwidth' EXTERN char *p_com; ///< 'comments' EXTERN char *p_cpo; ///< 'cpoptions' EXTERN char *p_debug; ///< 'debug' diff --git a/src/nvim/options.lua b/src/nvim/options.lua index 2f77d0ebb7..f261abf30e 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -6425,6 +6425,21 @@ local options = { type = 'number', varname = 'p_ph', }, + { + abbreviation = 'pmw', + defaults = 0, + desc = [=[ + Maximum width for the popup menu (|ins-completion-menu|). When zero, + there is no maximum width limit, otherwise the popup menu will never be + wider than this value. Truncated text will be indicated by "..." at the + end. Takes precedence over 'pumwidth'. + ]=], + full_name = 'pummaxwidth', + scope = { 'global' }, + short_desc = N_('maximum width of the popup menu'), + type = 'number', + varname = 'p_pmw', + }, { abbreviation = 'pw', defaults = 15, diff --git a/src/nvim/popupmenu.c b/src/nvim/popupmenu.c index d26651fb84..75f1c93ed7 100644 --- a/src/nvim/popupmenu.c +++ b/src/nvim/popupmenu.c @@ -199,6 +199,9 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i } int def_width = (int)p_pw; + if (p_pmw > 0 && def_width > p_pmw) { + def_width = (int)p_pmw; + } win_T *pvwin = NULL; FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { @@ -307,6 +310,9 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i pum_compute_size(); int max_width = pum_base_width; + if (p_pmw > 0 && max_width > p_pmw) { + max_width = (int)p_pmw; + } // if there are more items than room we need a scrollbar if (pum_height < size) { @@ -339,6 +345,9 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i if (pum_width > content_width && pum_width > p_pw) { // Reduce width to fit item pum_width = MAX(content_width, (int)p_pw); + if (p_pmw > 0 && pum_width > p_pmw) { + pum_width = (int)p_pmw; + } } else if (((cursor_col - min_col > p_pw || cursor_col - min_col > max_width) && !pum_rl) || (pum_rl && (cursor_col < max_col - p_pw @@ -365,6 +374,9 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i if (pum_width < p_pw) { pum_width = (int)p_pw; + if (p_pmw > 0 && pum_width > p_pmw) { + pum_width = (int)p_pmw; + } if (pum_rl) { if (pum_width > pum_col - min_col) { pum_width = pum_col - min_col; @@ -376,6 +388,9 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i } } else if (pum_width > content_width && pum_width > p_pw) { pum_width = MAX(content_width, (int)p_pw); + if (p_pmw > 0 && pum_width > p_pmw) { + pum_width = (int)p_pmw; + } } } } else if (max_col - min_col < def_width) { @@ -386,11 +401,17 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i pum_col = min_col; } pum_width = max_col - min_col - 1; + if (p_pmw > 0 && pum_width > p_pmw) { + pum_width = (int)p_pmw; + } } else { if (max_width > p_pw) { // truncate max_width = (int)p_pw; } + if (p_pmw > 0 && max_width > p_pmw) { + max_width = (int)p_pmw; + } if (pum_rl) { pum_col = min_col + max_width - 1; } else { @@ -611,6 +632,8 @@ void pum_redraw(void) thumb_pos = (pum_first * (pum_height - thumb_height) + scroll_range / 2) / scroll_range; } + const int ellipsis_width = 3; + for (int i = 0; i < pum_height; i++) { int idx = i + pum_first; const hlf_T *const hlfs = (idx == pum_selected) ? hlfsSel : hlfsNorm; @@ -685,6 +708,9 @@ void pum_redraw(void) char *rt = reverse_text(st); char *rt_start = rt; int cells = vim_strsize(rt); + bool need_ellipsis = p_pmw > ellipsis_width + && pum_width == p_pmw + && grid_col - cells < col_off - pum_width; if (grid_col - cells < col_off - pum_width) { do { @@ -692,7 +718,41 @@ void pum_redraw(void) MB_PTR_ADV(rt); } while (grid_col - cells < col_off - pum_width); - if (grid_col - cells > col_off - pum_width) { + if (need_ellipsis) { + char *orig_rt = rt; + int used_cells = 0; + char *last_char = NULL; + while (*orig_rt != NUL) { + int char_cells = utf_ptr2cells(orig_rt); + if (used_cells + char_cells > ellipsis_width) { + break; + } + used_cells += char_cells; + MB_PTR_ADV(orig_rt); + last_char = orig_rt; + } + + if (last_char != NULL) { + int over_cell = 0; + if (used_cells < ellipsis_width) { + over_cell = ellipsis_width - used_cells; + MB_PTR_ADV(orig_rt); + last_char = orig_rt; + } + size_t kept_len = strlen(last_char); + char *new_str = xmalloc((size_t)ellipsis_width + (size_t)over_cell + + kept_len + 1); + memset(new_str, '.', (size_t)ellipsis_width); + if (over_cell > 0) { + memset(new_str + ellipsis_width, ' ', (size_t)over_cell); + } + memcpy(new_str + ellipsis_width + over_cell, last_char, kept_len); + new_str[(size_t)ellipsis_width + kept_len + (size_t)over_cell] = NUL; + char *old_rt = rt_start; + rt = rt_start = new_str; + xfree(old_rt); + } + } else if (grid_col - cells > col_off - 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) = '<'; @@ -710,6 +770,53 @@ void pum_redraw(void) xfree(st); grid_col -= width; } else { + size_t size = strlen(st); + int cells = (int)mb_string2cells_len(st, size); + bool need_ellipsis = p_pmw > ellipsis_width + && pum_width == p_pmw + && grid_col + cells > col_off + pum_width; + + // Add '...' indicator if truncated due to p_pmw + if (need_ellipsis) { + while (size > 0 && grid_col + cells > col_off + pum_width) { + size--; + size -= (size_t)utf_head_off(st, st + size); + cells -= utf_ptr2cells(st + size); + } + char *st_end = st + size; + int used_cells = 0; + char *last_char = NULL; + while (st_end > st) { + int char_cells = utf_ptr2cells(st_end); + if (used_cells + char_cells > ellipsis_width) { + break; + } + used_cells += char_cells; + MB_PTR_BACK(st, st_end); + last_char = st_end; + } + + if (last_char != NULL) { + int over_cell = 0; + if (used_cells < ellipsis_width) { + MB_PTR_BACK(st, st_end); + last_char = st_end; + over_cell = ellipsis_width - used_cells; + } + size_t kept_len = (size_t)(last_char - st); + char *new_str = xmalloc((size_t)ellipsis_width + (size_t)over_cell + + kept_len + 1); + memcpy(new_str, st, kept_len); + if (over_cell > 0) { + memset(new_str + kept_len, ' ', (size_t)over_cell); + } + memset(new_str + kept_len + over_cell, '.', (size_t)ellipsis_width); + new_str[kept_len + (size_t)ellipsis_width + (size_t)over_cell] = NUL; + xfree(st); + st = new_str; + } + } + if (attrs == NULL) { grid_line_puts(grid_col, st, -1, attr); } else { -- cgit From 675ee057e06578e8d87ed2ea54ab8915caecdb0f Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 29 Mar 2025 06:35:11 +0800 Subject: vim-patch:9.1.1255: missing test condition for 'pummaxwidth' setting Problem: missing test condition for 'pummaxwidth' setting, pummaxwidth not effective when width is 32 and height is 10 (after v9.1.1250) Solution: add missing comparison condition in pum_width() (glepnir) closes: vim/vim#16999 https://github.com/vim/vim/commit/532c5aec6fa8f0a3d743c7d1573d25d75dd36d5f Co-authored-by: glepnir --- src/nvim/popupmenu.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/nvim/popupmenu.c b/src/nvim/popupmenu.c index 75f1c93ed7..4ce4f9632f 100644 --- a/src/nvim/popupmenu.c +++ b/src/nvim/popupmenu.c @@ -391,6 +391,8 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i if (p_pmw > 0 && pum_width > p_pmw) { pum_width = (int)p_pmw; } + } else if (p_pmw > 0 && pum_width > p_pmw) { + pum_width = (int)p_pmw; } } } else if (max_col - min_col < def_width) { -- cgit From 686e7aca40b850789af45ec9a97905518654b49e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 28 Mar 2025 21:18:30 +0800 Subject: fix(pum): simplify 'pummaxwidth' truncation and avoid crash --- src/nvim/popupmenu.c | 114 +++++++++++++-------------------------------------- 1 file changed, 28 insertions(+), 86 deletions(-) (limited to 'src') diff --git a/src/nvim/popupmenu.c b/src/nvim/popupmenu.c index 4ce4f9632f..8ca4b28f3f 100644 --- a/src/nvim/popupmenu.c +++ b/src/nvim/popupmenu.c @@ -658,6 +658,7 @@ void pum_redraw(void) // Do this 3 times and order from p_cia int grid_col = col_off; int totwidth = 0; + bool need_ellipsis = false; int order[3]; int items_width_array[3] = { pum_base_width, pum_kind_width, pum_extra_width }; pum_align_order(order); @@ -710,9 +711,10 @@ void pum_redraw(void) char *rt = reverse_text(st); char *rt_start = rt; int cells = vim_strsize(rt); - bool need_ellipsis = p_pmw > ellipsis_width - && pum_width == p_pmw - && grid_col - cells < col_off - pum_width; + if (p_pmw > ellipsis_width && pum_width == p_pmw + && grid_col - cells < col_off - pum_width) { + need_ellipsis = true; + } if (grid_col - cells < col_off - pum_width) { do { @@ -720,41 +722,7 @@ void pum_redraw(void) MB_PTR_ADV(rt); } while (grid_col - cells < col_off - pum_width); - if (need_ellipsis) { - char *orig_rt = rt; - int used_cells = 0; - char *last_char = NULL; - while (*orig_rt != NUL) { - int char_cells = utf_ptr2cells(orig_rt); - if (used_cells + char_cells > ellipsis_width) { - break; - } - used_cells += char_cells; - MB_PTR_ADV(orig_rt); - last_char = orig_rt; - } - - if (last_char != NULL) { - int over_cell = 0; - if (used_cells < ellipsis_width) { - over_cell = ellipsis_width - used_cells; - MB_PTR_ADV(orig_rt); - last_char = orig_rt; - } - size_t kept_len = strlen(last_char); - char *new_str = xmalloc((size_t)ellipsis_width + (size_t)over_cell - + kept_len + 1); - memset(new_str, '.', (size_t)ellipsis_width); - if (over_cell > 0) { - memset(new_str + ellipsis_width, ' ', (size_t)over_cell); - } - memcpy(new_str + ellipsis_width + over_cell, last_char, kept_len); - new_str[(size_t)ellipsis_width + kept_len + (size_t)over_cell] = NUL; - char *old_rt = rt_start; - rt = rt_start = new_str; - xfree(old_rt); - } - } else if (grid_col - cells > col_off - pum_width) { + if (grid_col - cells > col_off - 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) = '<'; @@ -772,57 +740,16 @@ void pum_redraw(void) xfree(st); grid_col -= width; } else { - size_t size = strlen(st); - int cells = (int)mb_string2cells_len(st, size); - bool need_ellipsis = p_pmw > ellipsis_width - && pum_width == p_pmw - && grid_col + cells > col_off + pum_width; - - // Add '...' indicator if truncated due to p_pmw - if (need_ellipsis) { - while (size > 0 && grid_col + cells > col_off + pum_width) { - size--; - size -= (size_t)utf_head_off(st, st + size); - cells -= utf_ptr2cells(st + size); - } - char *st_end = st + size; - int used_cells = 0; - char *last_char = NULL; - while (st_end > st) { - int char_cells = utf_ptr2cells(st_end); - if (used_cells + char_cells > ellipsis_width) { - break; - } - used_cells += char_cells; - MB_PTR_BACK(st, st_end); - last_char = st_end; - } - - if (last_char != NULL) { - int over_cell = 0; - if (used_cells < ellipsis_width) { - MB_PTR_BACK(st, st_end); - last_char = st_end; - over_cell = ellipsis_width - used_cells; - } - size_t kept_len = (size_t)(last_char - st); - char *new_str = xmalloc((size_t)ellipsis_width + (size_t)over_cell - + kept_len + 1); - memcpy(new_str, st, kept_len); - if (over_cell > 0) { - memset(new_str + kept_len, ' ', (size_t)over_cell); - } - memset(new_str + kept_len + over_cell, '.', (size_t)ellipsis_width); - new_str[kept_len + (size_t)ellipsis_width + (size_t)over_cell] = NUL; - xfree(st); - st = new_str; - } + int cells = (int)mb_string2cells(st); + if (p_pmw > ellipsis_width && pum_width == p_pmw + && grid_col + cells > col_off + pum_width) { + need_ellipsis = true; } 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); + pum_grid_puts_with_attrs(grid_col, cells, st, -1, attrs); } xfree(st); @@ -881,9 +808,24 @@ void pum_redraw(void) } if (pum_rl) { - grid_line_fill(col_off - pum_width + 1, grid_col + 1, schar_from_ascii(' '), orig_attr); + const int lcol = col_off - pum_width + 1; + grid_line_fill(lcol, grid_col + 1, schar_from_ascii(' '), orig_attr); + if (need_ellipsis) { + bool over_wide = pum_width > ellipsis_width && linebuf_char[lcol + ellipsis_width] == NUL; + grid_line_fill(lcol, lcol + ellipsis_width, schar_from_ascii('.'), orig_attr); + if (over_wide) { + grid_line_put_schar(lcol + ellipsis_width, schar_from_ascii(' '), orig_attr); + } + } } else { - grid_line_fill(grid_col, col_off + pum_width, schar_from_ascii(' '), orig_attr); + const int rcol = col_off + pum_width; + grid_line_fill(grid_col, rcol, schar_from_ascii(' '), orig_attr); + if (need_ellipsis) { + if (pum_width > ellipsis_width && linebuf_char[rcol - ellipsis_width] == NUL) { + grid_line_put_schar(rcol - ellipsis_width - 1, schar_from_ascii(' '), orig_attr); + } + grid_line_fill(rcol - ellipsis_width, rcol, schar_from_ascii('.'), orig_attr); + } } if (pum_scrollbar > 0) { -- cgit From 52b19e012474c19601a491bf0a0ab6ba15cfae8d Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 29 Mar 2025 20:12:12 +0800 Subject: vim-patch:9.1.1257: Mixing vim_strsize() with mb_ptr2cells() in pum_redraw() Problem: Mixing vim_strsize() with mb_ptr2cells() in pum_redraw(). Solution: Change vim_strsize() to mb_string2cells() (zeertzjq). Since vim_strsize() uses ptr2cells() for the cell width of each char, it is strange to mix it with mb_ptr2cells(), which is used both just below and in pum_screen_puts_with_attr(), and screen_puts_len() also uses something similar. Meanwhile mb_string2cells() uses mb_ptr2cells() for the cell width of each char. Note that the vim_strsize() and mb_string2cells() actually return the same value here, as the transstr() above makes sure the string only contains printable chars, and ptr2cells() and mb_ptr2cells() only return different values for unprintable chars. closes: vim/vim#17003 https://github.com/vim/vim/commit/90e52490b39f0052fb5313d67728eff77191aaae --- src/nvim/popupmenu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/popupmenu.c b/src/nvim/popupmenu.c index 8ca4b28f3f..2e5d08e5ae 100644 --- a/src/nvim/popupmenu.c +++ b/src/nvim/popupmenu.c @@ -710,7 +710,7 @@ void pum_redraw(void) if (pum_rl) { char *rt = reverse_text(st); char *rt_start = rt; - int cells = vim_strsize(rt); + int cells = (int)mb_string2cells(rt); if (p_pmw > ellipsis_width && pum_width == p_pmw && grid_col - cells < col_off - pum_width) { need_ellipsis = true; -- cgit