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: #16205
Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/ex_getln.c b/src/ex_getln.c
index 60e9641..163c474 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -13,10 +13,6 @@
#include "vim.h"
-#ifndef MAX
-# define MAX(x,y) ((x) > (y) ? (x) : (y))
-#endif
-
// Return value when handling keys in command-line mode.
#define CMDLINE_NOT_CHANGED 1
#define CMDLINE_CHANGED 2
diff --git a/src/popupmenu.c b/src/popupmenu.c
index be8016f..1856b6b 100644
--- a/src/popupmenu.c
+++ b/src/popupmenu.c
@@ -100,6 +100,9 @@
int cursor_col;
int above_row;
int below_row;
+ int cline_visible_offset;
+ int content_width;
+ int right_edge_col;
int redo_count = 0;
#if defined(FEAT_QUICKFIX)
win_T *pvwin;
@@ -150,10 +153,7 @@
/*
* Figure out the size and position of the pum.
*/
- if (size < PUM_DEF_HEIGHT)
- pum_height = size;
- else
- pum_height = PUM_DEF_HEIGHT;
+ pum_height = MIN(size, PUM_DEF_HEIGHT);
if (p_ph > 0 && pum_height > p_ph)
pum_height = p_ph;
@@ -168,13 +168,8 @@
// for cmdline pum, no need for context lines
context_lines = 0;
else
- {
// Leave two lines of context if possible
- if (curwin->w_wrow - curwin->w_cline_row >= 2)
- context_lines = 2;
- else
- context_lines = curwin->w_wrow - curwin->w_cline_row;
- }
+ context_lines = MIN(2, curwin->w_wrow - curwin->w_cline_row);
if (pum_win_row >= size + context_lines)
{
@@ -203,19 +198,13 @@
{
// Leave two lines of context if possible
validate_cheight();
- if (curwin->w_cline_row
- + curwin->w_cline_height - curwin->w_wrow >= 3)
- context_lines = 3;
- else
- context_lines = curwin->w_cline_row
- + curwin->w_cline_height - curwin->w_wrow;
+ cline_visible_offset = curwin->w_cline_row +
+ curwin->w_cline_height - curwin->w_wrow;
+ context_lines = MIN(3, cline_visible_offset);
}
pum_row = pum_win_row + context_lines;
- if (size > below_row - pum_row)
- pum_height = below_row - pum_row;
- else
- pum_height = size;
+ pum_height = MIN(below_row - pum_row, size);
if (p_ph > 0 && pum_height > p_ph)
pum_height = p_ph;
}
@@ -284,15 +273,10 @@
#endif
pum_width = Columns - pum_col - pum_scrollbar;
- if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
- && pum_width > p_pw)
- {
- // the width is more than needed for the items, make it
- // narrower
- pum_width = max_width + pum_kind_width + pum_extra_width + 1;
- if (pum_width < p_pw)
- pum_width = p_pw;
- }
+ content_width = max_width + pum_kind_width + pum_extra_width + 1;
+ if (pum_width > content_width && pum_width > p_pw)
+ // Reduce width to fit item
+ pum_width = MAX(content_width , p_pw);
else if (((cursor_col > p_pw || cursor_col > max_width)
#ifdef FEAT_RIGHTLEFT
&& !pum_rl)
@@ -313,14 +297,10 @@
else if (!pum_rl)
#endif
{
- if (curwin->w_wincol > Columns - max_width - pum_scrollbar
- && max_width <= p_pw)
- {
+ right_edge_col = Columns - max_width - pum_scrollbar;
+ if (curwin->w_wincol > right_edge_col && max_width <= p_pw)
// use full width to end of the screen
- pum_col = Columns - max_width - pum_scrollbar;
- if (pum_col < 0)
- pum_col = 0;
- }
+ pum_col = MAX(0, right_edge_col);
}
#ifdef FEAT_RIGHTLEFT
@@ -346,15 +326,8 @@
pum_width = Columns - pum_col - 1;
}
}
- else if (pum_width > max_width + pum_kind_width
- + pum_extra_width + 1
- && pum_width > p_pw)
- {
- pum_width = max_width + pum_kind_width
- + pum_extra_width + 1;
- if (pum_width < p_pw)
- pum_width = p_pw;
- }
+ else if (pum_width > content_width && pum_width > p_pw)
+ pum_width = MIN(content_width, p_pw);
}
}
@@ -823,14 +796,14 @@
if (pum_rl)
{
screen_fill(row, row + 1, pum_col - basic_width - n + 1,
- col + 1, ' ', ' ', orig_attr);
+ col + 1, ' ', ' ', orig_attr);
col = pum_col - basic_width - n;
}
else
#endif
{
screen_fill(row, row + 1, col, pum_col + basic_width + n,
- ' ', ' ', orig_attr);
+ ' ', ' ', orig_attr);
col = pum_col + basic_width + n;
}
totwidth = basic_width + n;
@@ -839,11 +812,11 @@
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
- ' ', orig_attr);
+ ' ', orig_attr);
else
#endif
screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
- orig_attr);
+ orig_attr);
if (pum_scrollbar > 0)
{
#ifdef FEAT_RIGHTLEFT
@@ -998,8 +971,7 @@
}
}
// adjust for the number of lines displayed
- if (pum_first > pum_size - pum_height)
- pum_first = pum_size - pum_height;
+ pum_first = MIN(pum_first, pum_size - pum_height);
#if defined(FEAT_QUICKFIX)
/*
@@ -1322,8 +1294,7 @@
if (pum_in_same_position())
{
- // window position didn't change, redraw in the same position
- pum_redraw();
+ pum_redraw(); // Redraw window in same position
}
else
{
@@ -1403,8 +1374,7 @@
pum_col = mouse_col;
else
// Not enough space, left align with window.
- pum_col = (pum_base_width > min_width
- ? min_width : pum_base_width) - 1;
+ pum_col = MIN(pum_base_width, min_width) - 1;
pum_width = pum_col + 1;
}
else
@@ -1416,8 +1386,7 @@
pum_col = mouse_col;
else
// Not enough space, right align with window.
- pum_col = Columns - (pum_base_width > min_width
- ? min_width : pum_base_width);
+ pum_col = Columns - MIN(pum_base_width, min_width);
pum_width = Columns - pum_col;
}
diff --git a/src/version.c b/src/version.c
index 1c38b62..1c2398e 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 921,
+/**/
920,
/**/
919,