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/ex_getln.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/ex_getln.c')
-rw-r--r-- | src/nvim/ex_getln.c | 57 |
1 files changed, 16 insertions, 41 deletions
diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index e3c7008d3b..c8ca6f0e4e 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -390,13 +390,8 @@ static bool do_incsearch_highlighting(int firstc, int *search_delim, incsearch_s parse_cmd_address(&ea, &dummy, true); if (ea.addr_count > 0) { // Allow for reverse match. - if (ea.line2 < ea.line1) { - search_first_line = ea.line2; - search_last_line = ea.line1; - } else { - search_first_line = ea.line1; - search_last_line = ea.line2; - } + search_first_line = MIN(ea.line1, ea.line1); + search_last_line = MAX(ea.line2, ea.line1); } else if (cmd[0] == 's' && cmd[1] != 'o') { // :s defaults to the current line search_first_line = curwin->w_cursor.lnum; @@ -1034,11 +1029,7 @@ static int command_line_handle_ctrl_bsl(CommandLineState *s) // Restore the cursor or use the position set with // set_cmdline_pos(). - if (new_cmdpos > ccline.cmdlen) { - ccline.cmdpos = ccline.cmdlen; - } else { - ccline.cmdpos = new_cmdpos; - } + ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos); KeyTyped = false; // Don't do p_wc completion. redrawcmd(); @@ -1658,11 +1649,7 @@ static int command_line_insert_reg(CommandLineState *s) KeyTyped = false; // Don't do p_wc completion. if (new_cmdpos >= 0) { // set_cmdline_pos() was used - if (new_cmdpos > ccline.cmdlen) { - ccline.cmdpos = ccline.cmdlen; - } else { - ccline.cmdpos = new_cmdpos; - } + ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos); } } new_cmdpos = save_new_cmdpos; @@ -3546,10 +3533,6 @@ void unputcmdline(void) // called afterwards. void put_on_cmdline(const char *str, int len, bool redraw) { - int i; - int m; - int c; - if (len < 0) { len = (int)strlen(str); } @@ -3563,7 +3546,8 @@ void put_on_cmdline(const char *str, int len, bool redraw) ccline.cmdlen += len; } else { // Count nr of characters in the new string. - m = 0; + int m = 0; + int i; for (i = 0; i < len; i += utfc_ptr2len(str + i)) { m++; } @@ -3587,8 +3571,8 @@ void put_on_cmdline(const char *str, int len, bool redraw) { // When the inserted text starts with a composing character, // backup to the character before it. There could be two of them. - i = 0; - c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); + int i = 0; + int c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); while (ccline.cmdpos > 0 && utf_iscomposing(c)) { i = utf_head_off(ccline.cmdbuff, ccline.cmdbuff + ccline.cmdpos - 1) + 1; ccline.cmdpos -= i; @@ -3619,7 +3603,7 @@ void put_on_cmdline(const char *str, int len, bool redraw) if (redraw && !cmd_silent) { msg_no_more = true; - i = cmdline_row; + int i = cmdline_row; cursorcmd(); draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); // Avoid clearing the rest of the line too often. @@ -3628,6 +3612,7 @@ void put_on_cmdline(const char *str, int len, bool redraw) } msg_no_more = false; } + int m; if (KeyTyped) { m = Columns * Rows; if (m < 0) { // overflow, Columns or Rows at weird value @@ -3636,8 +3621,8 @@ void put_on_cmdline(const char *str, int len, bool redraw) } else { m = MAXCOL; } - for (i = 0; i < len; i++) { - c = cmdline_charsize(ccline.cmdpos); + for (int i = 0; i < len; i++) { + int c = cmdline_charsize(ccline.cmdpos); // count ">" for a double-wide char that doesn't fit. correct_screencol(ccline.cmdpos, c, &ccline.cmdspos); // Stop cursor at the end of the screen, but do increment the @@ -3647,9 +3632,7 @@ void put_on_cmdline(const char *str, int len, bool redraw) ccline.cmdspos += c; } c = utfc_ptr2len(ccline.cmdbuff + ccline.cmdpos) - 1; - if (c > len - i - 1) { - c = len - i - 1; - } + c = MIN(c, len - i - 1); ccline.cmdpos += c; i += c; ccline.cmdpos++; @@ -3886,17 +3869,13 @@ void cursorcmd(void) } if (ui_has(kUICmdline)) { - if (ccline.redraw_state < kCmdRedrawPos) { - ccline.redraw_state = kCmdRedrawPos; - } + ccline.redraw_state = MAX(ccline.redraw_state, kCmdRedrawPos); return; } msg_row = cmdline_row + (ccline.cmdspos / Columns); msg_col = ccline.cmdspos % Columns; - if (msg_row >= Rows) { - msg_row = Rows - 1; - } + msg_row = MIN(msg_row, Rows - 1); msg_cursor_goto(msg_row, msg_col); } @@ -4192,11 +4171,7 @@ static int set_cmdline_pos(int pos) // The position is not set directly but after CTRL-\ e or CTRL-R = has // changed the command line. - if (pos < 0) { - new_cmdpos = 0; - } else { - new_cmdpos = pos; - } + new_cmdpos = MAX(0, pos); return 0; } |