diff options
author | Lewis Russell <lewis6991@gmail.com> | 2024-07-19 11:00:13 +0100 |
---|---|---|
committer | Lewis Russell <me@lewisr.dev> | 2024-07-30 22:43:29 +0100 |
commit | d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0 (patch) | |
tree | ca5a4a60fca9e814d0a3937cc4ff21ed9e037520 /src/nvim/edit.c | |
parent | 1b5a394ffd4bb638ec9cbbb5e4d12d11225389cf (diff) | |
download | rneovim-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/edit.c')
-rw-r--r-- | src/nvim/edit.c | 31 |
1 files changed, 7 insertions, 24 deletions
diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 5c88cb59e1..00ce38c4b1 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -271,11 +271,7 @@ static void insert_enter(InsertState *s) if (restart_edit != 0 && stuff_empty()) { // After a paste we consider text typed to be part of the insert for // the pasted text. You can backspace over the pasted text too. - if (where_paste_started.lnum) { - arrow_used = false; - } else { - arrow_used = true; - } + arrow_used = where_paste_started.lnum == 0; restart_edit = 0; // If the cursor was after the end-of-line before the CTRL-O and it is @@ -1541,9 +1537,7 @@ static void init_prompt(int cmdchar_todo) if (cmdchar_todo == 'A') { coladvance(curwin, MAXCOL); } - if (curwin->w_cursor.col < (colnr_T)strlen(prompt)) { - curwin->w_cursor.col = (colnr_T)strlen(prompt); - } + curwin->w_cursor.col = MAX(curwin->w_cursor.col, (colnr_T)strlen(prompt)); // Make sure the cursor is in a valid position. check_cursor(curwin); } @@ -1578,7 +1572,7 @@ void edit_unputchar(void) /// text. Only works when cursor is in the line that changes. void display_dollar(colnr_T col_arg) { - colnr_T col = col_arg < 0 ? 0 : col_arg; + colnr_T col = MAX(col_arg, 0); if (!redrawing()) { return; @@ -1736,12 +1730,7 @@ void change_indent(int type, int amount, int round, int replaced, bool call_chan } curwin->w_p_list = save_p_list; - - if (new_cursor_col <= 0) { - curwin->w_cursor.col = 0; - } else { - curwin->w_cursor.col = (colnr_T)new_cursor_col; - } + curwin->w_cursor.col = MAX(0, (colnr_T)new_cursor_col); curwin->w_set_curswant = true; changed_cline_bef_curs(curwin); @@ -2607,9 +2596,7 @@ void cursor_up_inner(win_T *wp, linenr_T n) hasFolding(wp, lnum, &lnum, NULL); } } - if (lnum < 1) { - lnum = 1; - } + lnum = MAX(lnum, 1); } else { lnum -= n; } @@ -2659,9 +2646,7 @@ void cursor_down_inner(win_T *wp, int n) break; } } - if (lnum > line_count) { - lnum = line_count; - } + lnum = MIN(lnum, line_count); } else { lnum += (linenr_T)n; } @@ -4726,9 +4711,7 @@ static void ins_try_si(int c) } // Adjust ai_col, the char at this position can be deleted. - if (ai_col > curwin->w_cursor.col) { - ai_col = curwin->w_cursor.col; - } + ai_col = MIN(ai_col, curwin->w_cursor.col); } // Get the value that w_virtcol would have when 'list' is off. |