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

@@ -216,12 +216,7 @@ static int coladvance2(win_T *wp, pos_T *pos, bool addspaces, bool finetune, col
}
}
if (idx < 0) {
pos->col = 0;
} else {
pos->col = idx;
}
pos->col = MAX(idx, 0);
pos->coladd = 0;
if (finetune) {
@@ -310,15 +305,9 @@ linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T lnum)
/// This allows for the col to be on the NUL byte.
void check_pos(buf_T *buf, pos_T *pos)
{
if (pos->lnum > buf->b_ml.ml_line_count) {
pos->lnum = buf->b_ml.ml_line_count;
}
pos->lnum = MIN(pos->lnum, buf->b_ml.ml_line_count);
if (pos->col > 0) {
colnr_T len = ml_get_buf_len(buf, pos->lnum);
if (pos->col > len) {
pos->col = len;
}
pos->col = MIN(pos->col, ml_get_buf_len(buf, pos->lnum));
}
}
@@ -385,9 +374,7 @@ void check_cursor_col(win_T *win)
int cs, ce;
getvcol(win, &win->w_cursor, &cs, NULL, &ce);
if (win->w_cursor.coladd > ce - cs) {
win->w_cursor.coladd = ce - cs;
}
win->w_cursor.coladd = MIN(win->w_cursor.coladd, ce - cs);
}
} else {
// avoid weird number when there is a miscalculation or overflow