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/textformat.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/textformat.c')
-rw-r--r-- | src/nvim/textformat.c | 23 |
1 files changed, 5 insertions, 18 deletions
diff --git a/src/nvim/textformat.c b/src/nvim/textformat.c index 1722bcc968..13607da043 100644 --- a/src/nvim/textformat.c +++ b/src/nvim/textformat.c @@ -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(); |