aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/mouse.c
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2024-07-19 11:00:13 +0100
committerLewis Russell <me@lewisr.dev>2024-07-30 22:43:29 +0100
commitd1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0 (patch)
treeca5a4a60fca9e814d0a3937cc4ff21ed9e037520 /src/nvim/mouse.c
parent1b5a394ffd4bb638ec9cbbb5e4d12d11225389cf (diff)
downloadrneovim-d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0.tar.gz
rneovim-d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0.tar.bz2
rneovim-d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0.zip
refactor: collapse statements in single assignments
Problem: Variables are often assigned multiple places in common patterns. Solution: Replace these common patterns with different patterns that reduce the number of assignments. Use `MAX` and `MIN`: ```c if (x < y) { x = y; } // --> x = MAX(x, y); ``` ```c if (x > y) { x = y; } // --> x = MIN(x, y); ``` Use ternary: ```c int a; if (cond) { a = b; } els { a = c; } // --> int a = cond ? b : c; ```
Diffstat (limited to 'src/nvim/mouse.c')
-rw-r--r--src/nvim/mouse.c19
1 files changed, 5 insertions, 14 deletions
diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c
index 6bd40003ea..5e2f2f0320 100644
--- a/src/nvim/mouse.c
+++ b/src/nvim/mouse.c
@@ -1050,9 +1050,7 @@ void do_mousescroll(cmdarg_T *cap)
// Horizontal scrolling
int step = shift_or_ctrl ? curwin->w_width_inner : (int)p_mousescroll_hor;
colnr_T leftcol = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
- if (leftcol < 0) {
- leftcol = 0;
- }
+ leftcol = MAX(leftcol, 0);
do_mousescroll_horiz(leftcol);
}
}
@@ -1619,11 +1617,8 @@ bool mouse_comp_pos(win_T *win, int *rowp, int *colp, linenr_T *lnump)
while (row > 0) {
// Don't include filler lines in "count"
if (win_may_fill(win)) {
- if (lnum == win->w_topline) {
- row -= win->w_topfill;
- } else {
- row -= win_get_fill(win, lnum);
- }
+ row -= lnum == win->w_topline ? win->w_topfill
+ : win_get_fill(win, lnum);
count = plines_win_nofill(win, lnum, false);
} else {
count = plines_win(win, lnum, false);
@@ -1664,9 +1659,7 @@ bool mouse_comp_pos(win_T *win, int *rowp, int *colp, linenr_T *lnump)
if (!retval) {
// Compute the column without wrapping.
int off = win_col_off(win) - win_col_off2(win);
- if (col < off) {
- col = off;
- }
+ col = MAX(col, off);
col += row * (win->w_width_inner - off);
// Add skip column for the topline.
@@ -1681,9 +1674,7 @@ bool mouse_comp_pos(win_T *win, int *rowp, int *colp, linenr_T *lnump)
// skip line number and fold column in front of the line
col -= win_col_off(win);
- if (col <= 0) {
- col = 0;
- }
+ col = MAX(col, 0);
*colp = col;
*rowp = row;