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/fileio.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/fileio.c')
-rw-r--r-- | src/nvim/fileio.c | 15 |
1 files changed, 3 insertions, 12 deletions
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 3078f00fbb..6fc1d0717a 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -888,10 +888,7 @@ retry: // Use buffer >= 64K. Add linerest to double the size if the // line gets very long, to avoid a lot of copying. But don't // read more than 1 Mbyte at a time, so we can be interrupted. - size = 0x10000 + linerest; - if (size > 0x100000) { - size = 0x100000; - } + size = MIN(0x10000 + linerest, 0x100000); } // Protect against the argument of lalloc() going negative. @@ -2800,9 +2797,7 @@ int check_timestamps(int focus) bufref_T bufref; set_bufref(&bufref, buf); const int n = buf_check_timestamp(buf); - if (didit < n) { - didit = n; - } + didit = MAX(didit, n); if (n > 0 && !bufref_valid(&bufref)) { // Autocommands have removed the buffer, start at the first one again. buf = firstbuf; @@ -3192,11 +3187,7 @@ void buf_reload(buf_T *buf, int orig_mode, bool reload_options) // Restore the topline and cursor position and check it (lines may // have been removed). - if (old_topline > curbuf->b_ml.ml_line_count) { - curwin->w_topline = curbuf->b_ml.ml_line_count; - } else { - curwin->w_topline = old_topline; - } + curwin->w_topline = MIN(old_topline, curbuf->b_ml.ml_line_count); curwin->w_cursor = old_cursor; check_cursor(curwin); update_topline(curwin); |