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

@@ -348,9 +348,7 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on
inc_cursor();
}
startcol -= curwin->w_cursor.col;
if (startcol < 0) {
startcol = 0;
}
startcol = MAX(startcol, 0);
if (State & VREPLACE_FLAG) {
// In MODE_VREPLACE state, we will backspace over the text to be
@@ -433,9 +431,7 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on
// may have added or removed indent.
curwin->w_cursor.col += startcol;
colnr_T len = get_cursor_line_len();
if (curwin->w_cursor.col > len) {
curwin->w_cursor.col = len;
}
curwin->w_cursor.col = MIN(curwin->w_cursor.col, len);
}
haveto_redraw = true;
@@ -758,14 +754,9 @@ int comp_textwidth(bool ff)
textwidth -= 8;
}
}
if (textwidth < 0) {
textwidth = 0;
}
textwidth = MAX(textwidth, 0);
if (ff && textwidth == 0) {
textwidth = curwin->w_width_inner - 1;
if (textwidth > 79) {
textwidth = 79;
}
textwidth = MIN(curwin->w_width_inner - 1, 79);
}
return textwidth;
}
@@ -1105,11 +1096,7 @@ void format_lines(linenr_T line_count, bool avoid_fex)
}
first_par_line = false;
// If the line is getting long, format it next time
if (get_cursor_line_len() > max_len) {
force_format = true;
} else {
force_format = false;
}
force_format = get_cursor_line_len() > max_len;
}
}
line_breakcheck();