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;

```
This commit is contained in:
Lewis Russell
2024-07-19 11:00:13 +01:00
committed by Lewis Russell
parent 1b5a394ffd
commit d1bd3d643e
36 changed files with 419 additions and 1214 deletions

View File

@@ -1079,7 +1079,6 @@ void *getline_cookie(LineGetter fgetline, void *cookie)
/// @return the buffer number.
static int compute_buffer_local_count(cmd_addr_T addr_type, linenr_T lnum, int offset)
{
buf_T *nextbuf;
int count = offset;
buf_T *buf = firstbuf;
@@ -1088,7 +1087,7 @@ static int compute_buffer_local_count(cmd_addr_T addr_type, linenr_T lnum, int o
}
while (count != 0) {
count += (count < 0) ? 1 : -1;
nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
buf_T *nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
if (nextbuf == NULL) {
break;
}
@@ -1107,7 +1106,7 @@ static int compute_buffer_local_count(cmd_addr_T addr_type, linenr_T lnum, int o
// we might have gone too far, last buffer is not loaded
if (addr_type == ADDR_LOADED_BUFFERS) {
while (buf->b_ml.ml_mfp == NULL) {
nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
buf_T *nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
if (nextbuf == NULL) {
break;
}
@@ -1696,9 +1695,7 @@ static int execute_cmd0(int *retv, exarg_T *eap, const char **errormsg, bool pre
// ":silent! try" was used, it should only apply to :try itself.
if (eap->cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0) {
emsg_silent -= cmdmod.cmod_did_esilent;
if (emsg_silent < 0) {
emsg_silent = 0;
}
emsg_silent = MAX(emsg_silent, 0);
cmdmod.cmod_did_esilent = 0;
}
@@ -2444,9 +2441,7 @@ static char *ex_range_without_command(exarg_T *eap)
ex_print(eap);
}
} else if (eap->addr_count != 0) {
if (eap->line2 > curbuf->b_ml.ml_line_count) {
eap->line2 = curbuf->b_ml.ml_line_count;
}
eap->line2 = MIN(eap->line2, curbuf->b_ml.ml_line_count);
if (eap->line2 < 0) {
errormsg = _(e_invrange);
@@ -2785,9 +2780,7 @@ void undo_cmdmod(cmdmod_T *cmod)
msg_silent = cmod->cmod_save_msg_silent - 1;
}
emsg_silent -= cmod->cmod_did_esilent;
if (emsg_silent < 0) {
emsg_silent = 0;
}
emsg_silent = MAX(emsg_silent, 0);
// Restore msg_scroll, it's set by file I/O commands, even when no
// message is actually displayed.
msg_scroll = cmod->cmod_save_msg_scroll;
@@ -3520,11 +3513,7 @@ static linenr_T get_address(exarg_T *eap, char **ptr, cmd_addr_T addr_type, bool
// This makes sure we never match in the current
// line, and can match anywhere in the
// next/previous line.
if (c == '/' && curwin->w_cursor.lnum > 0) {
curwin->w_cursor.col = MAXCOL;
} else {
curwin->w_cursor.col = 0;
}
curwin->w_cursor.col = (c == '/' && curwin->w_cursor.lnum > 0) ? MAXCOL : 0;
searchcmdlen = 0;
flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
if (!do_search(NULL, c, c, cmd, strlen(cmd), 1, flags, NULL)) {
@@ -4793,11 +4782,9 @@ static void ex_cquit(exarg_T *eap)
int before_quit_all(exarg_T *eap)
{
if (cmdwin_type != 0) {
if (eap->forceit) {
cmdwin_result = K_XF1; // open_cmdwin() takes care of this
} else {
cmdwin_result = K_XF2;
}
cmdwin_result = eap->forceit
? K_XF1 // open_cmdwin() takes care of this
: K_XF2;
return FAIL;
}
@@ -5594,7 +5581,6 @@ static void ex_swapname(exarg_T *eap)
static void ex_syncbind(exarg_T *eap)
{
linenr_T topline;
int y;
linenr_T old_linenr = curwin->w_cursor.lnum;
setpcmark();
@@ -5604,15 +5590,10 @@ static void ex_syncbind(exarg_T *eap)
topline = curwin->w_topline;
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp->w_p_scb && wp->w_buffer) {
y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value(curwin);
if (topline > y) {
topline = y;
}
topline = MIN(topline, wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value(curwin));
}
}
if (topline < 1) {
topline = 1;
}
topline = MAX(topline, 1);
} else {
topline = 1;
}
@@ -5620,7 +5601,7 @@ static void ex_syncbind(exarg_T *eap)
// Set all scrollbind windows to the same topline.
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp->w_p_scb) {
y = topline - wp->w_topline;
int y = topline - wp->w_topline;
if (y > 0) {
scrollup(wp, y, true);
} else {