aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/move.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/move.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/move.c')
-rw-r--r--src/nvim/move.c51
1 files changed, 13 insertions, 38 deletions
diff --git a/src/nvim/move.c b/src/nvim/move.c
index 8c206b3e8d..6324466dcc 100644
--- a/src/nvim/move.c
+++ b/src/nvim/move.c
@@ -894,9 +894,7 @@ void curs_columns(win_T *wp, int may_scroll)
new_leftcol = wp->w_leftcol + diff;
}
}
- if (new_leftcol < 0) {
- new_leftcol = 0;
- }
+ new_leftcol = MAX(new_leftcol, 0);
if (new_leftcol != (int)wp->w_leftcol) {
wp->w_leftcol = new_leftcol;
win_check_anchored_floats(wp);
@@ -969,11 +967,8 @@ void curs_columns(win_T *wp, int may_scroll)
if (n > plines - wp->w_height_inner + 1) {
n = plines - wp->w_height_inner + 1;
}
- if (n > 0) {
- wp->w_skipcol = width1 + (n - 1) * width2;
- } else {
- wp->w_skipcol = 0;
- }
+ wp->w_skipcol = n > 0 ? width1 + (n - 1) * width2
+ : 0;
} else if (extra == 1) {
// less than 'scrolloff' lines above, decrease skipcol
assert(so <= INT_MAX);
@@ -990,9 +985,7 @@ void curs_columns(win_T *wp, int may_scroll)
while (endcol > wp->w_virtcol) {
endcol -= width2;
}
- if (endcol > wp->w_skipcol) {
- wp->w_skipcol = endcol;
- }
+ wp->w_skipcol = MAX(wp->w_skipcol, endcol);
}
// adjust w_wrow for the changed w_skipcol
@@ -1129,9 +1122,7 @@ void f_screenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
semsg(_(e_invalid_line_number_nr), pos.lnum);
return;
}
- if (pos.col < 0) {
- pos.col = 0;
- }
+ pos.col = MAX(pos.col, 0);
int row = 0;
int scol = 0;
int ccol = 0;
@@ -1423,9 +1414,7 @@ bool scrolldown(win_T *wp, linenr_T line_count, int byfold)
foldAdjustCursor(wp);
coladvance(wp, wp->w_curswant);
}
- if (wp->w_cursor.lnum < wp->w_topline) {
- wp->w_cursor.lnum = wp->w_topline;
- }
+ wp->w_cursor.lnum = MAX(wp->w_cursor.lnum, wp->w_topline);
return moved;
}
@@ -1506,12 +1495,8 @@ bool scrollup(win_T *wp, linenr_T line_count, bool byfold)
wp->w_botline += line_count; // approximate w_botline
}
- if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count) {
- wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
- }
- if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1) {
- wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
- }
+ wp->w_topline = MIN(wp->w_topline, wp->w_buffer->b_ml.ml_line_count);
+ wp->w_botline = MIN(wp->w_botline, wp->w_buffer->b_ml.ml_line_count + 1);
check_topfill(wp, false);
@@ -1623,9 +1608,7 @@ void check_topfill(win_T *wp, bool down)
wp->w_topfill = 0;
} else {
wp->w_topfill = wp->w_height_inner - n;
- if (wp->w_topfill < 0) {
- wp->w_topfill = 0;
- }
+ wp->w_topfill = MAX(wp->w_topfill, 0);
}
}
}
@@ -1849,15 +1832,11 @@ void scroll_cursor_top(win_T *wp, int min_scroll, int always)
if (new_topline < wp->w_topline || always) {
wp->w_topline = new_topline;
}
- if (wp->w_topline > wp->w_cursor.lnum) {
- wp->w_topline = wp->w_cursor.lnum;
- }
+ wp->w_topline = MIN(wp->w_topline, wp->w_cursor.lnum);
wp->w_topfill = win_get_fill(wp, wp->w_topline);
if (wp->w_topfill > 0 && extra > off) {
wp->w_topfill -= extra - off;
- if (wp->w_topfill < 0) {
- wp->w_topfill = 0;
- }
+ wp->w_topfill = MAX(wp->w_topfill, 0);
}
check_topfill(wp, false);
if (wp->w_topline != old_topline) {
@@ -2276,18 +2255,14 @@ void cursor_correct(win_T *wp)
if (wp->w_topline == 1) {
above_wanted = 0;
int max_off = wp->w_height_inner / 2;
- if (below_wanted > max_off) {
- below_wanted = max_off;
- }
+ below_wanted = MIN(below_wanted, max_off);
}
validate_botline(wp);
if (wp->w_botline == wp->w_buffer->b_ml.ml_line_count + 1
&& mouse_dragging == 0) {
below_wanted = 0;
int max_off = (wp->w_height_inner - 1) / 2;
- if (above_wanted > max_off) {
- above_wanted = max_off;
- }
+ above_wanted = MIN(above_wanted, max_off);
}
// If there are sufficient file-lines above and below the cursor, we can