vim-patch:9.1.0921: popupmenu logic is a bit convoluted

Problem:  popupmenu logic is a bit convoluted
Solution: slightly refactor logic and use MIN/MAX() macros to simplify
          (glepnir)

Define the MAX/MIN macros. Since we support some older platforms, C
compilers may not be as smart. This helps reduce unnecessary if
statements and redundant ternary expressions. Pre-calculate some
expressions by defining variables. Remove unnecessary parentheses.
Adjust certain lines to avoid exceeding 80 columns.

closes: vim/vim#16205

c942f84aad

Co-authored-by: glepnir <glephunter@gmail.com>
This commit is contained in:
zeertzjq 2024-12-13 19:41:59 +08:00
parent 3e3a984300
commit 8a0b203875

View File

@ -234,12 +234,7 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
} }
// Figure out the size and position of the pum. // Figure out the size and position of the pum.
if (size < PUM_DEF_HEIGHT) { pum_height = MIN(size, PUM_DEF_HEIGHT);
pum_height = size;
} else {
pum_height = PUM_DEF_HEIGHT;
}
if (p_ph > 0 && pum_height > p_ph) { if (p_ph > 0 && pum_height > p_ph) {
pum_height = (int)p_ph; pum_height = (int)p_ph;
} }
@ -256,11 +251,7 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
context_lines = 0; context_lines = 0;
} else { } else {
// Leave two lines of context if possible // Leave two lines of context if possible
if (curwin->w_wrow - curwin->w_cline_row >= 2) { context_lines = MIN(2, curwin->w_wrow - curwin->w_cline_row);
context_lines = 2;
} else {
context_lines = curwin->w_wrow - curwin->w_cline_row;
}
} }
if (pum_win_row - min_row >= size + context_lines) { if (pum_win_row - min_row >= size + context_lines) {
@ -285,20 +276,13 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
} else { } else {
// Leave two lines of context if possible // Leave two lines of context if possible
validate_cheight(curwin); validate_cheight(curwin);
if (curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow >= 3) { int cline_visible_offset = curwin->w_cline_row +
context_lines = 3; curwin->w_cline_height - curwin->w_wrow;
} else { context_lines = MIN(3, cline_visible_offset);
context_lines = curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow;
}
} }
pum_row = pum_win_row + context_lines; pum_row = pum_win_row + context_lines;
if (size > below_row - pum_row) { pum_height = MIN(below_row - pum_row, size);
pum_height = below_row - pum_row;
} else {
pum_height = size;
}
if (p_ph > 0 && pum_height > p_ph) { if (p_ph > 0 && pum_height > p_ph) {
pum_height = (int)p_ph; pum_height = (int)p_ph;
} }
@ -353,15 +337,10 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
pum_width = max_col - pum_col - pum_scrollbar; pum_width = max_col - pum_col - pum_scrollbar;
} }
if (pum_width > max_width + pum_kind_width + pum_extra_width + 1 int content_width = max_width + pum_kind_width + pum_extra_width + 1;
&& pum_width > p_pw) { if (pum_width > content_width && pum_width > p_pw) {
// the width is more than needed for the items, make it // Reduce width to fit item
// narrower pum_width = MAX(content_width, (int)p_pw);
pum_width = max_width + pum_kind_width + pum_extra_width + 1;
if (pum_width < p_pw) {
pum_width = (int)p_pw;
}
} else if (((cursor_col - min_col > p_pw } else if (((cursor_col - min_col > p_pw
|| cursor_col - min_col > max_width) && !pum_rl) || cursor_col - min_col > max_width) && !pum_rl)
|| (pum_rl && (cursor_col < max_col - p_pw || (pum_rl && (cursor_col < max_col - p_pw
@ -373,13 +352,10 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
pum_col = max_col - 1; pum_col = max_col - 1;
} }
} else if (!pum_rl) { } else if (!pum_rl) {
if (win_start_col > max_col - max_width - pum_scrollbar int right_edge_col = max_col - max_width - pum_scrollbar;
&& max_width <= p_pw) { if (win_start_col > right_edge_col && max_width <= p_pw) {
// use full width to end of the screen // use full width to end of the screen
pum_col = max_col - max_width - pum_scrollbar; pum_col = MAX(min_col, right_edge_col);
if (pum_col < min_col) {
pum_col = min_col;
}
} }
} }
@ -400,12 +376,8 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
pum_width = max_col - pum_col - 1; pum_width = max_col - pum_col - 1;
} }
} }
} else if (pum_width > max_width + pum_kind_width + pum_extra_width + 1 } else if (pum_width > content_width && pum_width > p_pw) {
&& pum_width > p_pw) { pum_width = MIN(content_width, (int)p_pw);
pum_width = max_width + pum_kind_width + pum_extra_width + 1;
if (pum_width < p_pw) {
pum_width = (int)p_pw;
}
} }
} }
} else if (max_col - min_col < def_width) { } else if (max_col - min_col < def_width) {
@ -997,9 +969,7 @@ static bool pum_set_selected(int n, int repeat)
} }
} }
// adjust for the number of lines displayed // adjust for the number of lines displayed
if (pum_first > pum_size - pum_height) { pum_first = MIN(pum_first, pum_size - pum_height);
pum_first = pum_size - pum_height;
}
// Show extra info in the preview window if there is something and // Show extra info in the preview window if there is something and
// 'completeopt' contains "preview". // 'completeopt' contains "preview".