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/diff.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/diff.c')
-rw-r--r-- | src/nvim/diff.c | 41 |
1 files changed, 9 insertions, 32 deletions
diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 45bcb94a1f..0b9bdb6181 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -694,7 +694,7 @@ void diff_redraw(bool dofold) if (((wp != curwin) && (wp->w_topfill > 0)) || (n > 0)) { if (wp->w_topfill > n) { - wp->w_topfill = (n < 0 ? 0 : n); + wp->w_topfill = MAX(n, 0); } else if ((n > 0) && (n > wp->w_topfill)) { wp->w_topfill = n; if (wp == curwin) { @@ -2679,9 +2679,7 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) si_org -= utf_head_off(line_org, line_org + si_org); si_new -= utf_head_off(line_new, line_new + si_new); - if (*startp > si_org) { - *startp = si_org; - } + *startp = MIN(*startp, si_org); // Search for end of difference, if any. if ((line_org[si_org] != NUL) || (line_new[si_new] != NUL)) { @@ -2721,9 +2719,7 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) } } - if (*endp < ei_org) { - *endp = ei_org; - } + *endp = MAX(*endp, ei_org); } } } @@ -3051,19 +3047,11 @@ static void diffgetput(const int addr_count, const int idx_cur, const int idx_fr // range ends above end of current/from diff block if (idx_cur == idx_from) { // :diffput - int i = dp->df_count[idx_cur] - start_skip - end_skip; - - if (count > i) { - count = i; - } + count = MIN(count, dp->df_count[idx_cur] - start_skip - end_skip); } else { // :diffget count -= end_skip; - end_skip = dp->df_count[idx_from] - start_skip - count; - - if (end_skip < 0) { - end_skip = 0; - } + end_skip = MAX(dp->df_count[idx_from] - start_skip - count, 0); } } else { end_skip = 0; @@ -3249,9 +3237,7 @@ int diff_move_to(int dir, int count) } // don't end up past the end of the file - if (lnum > curbuf->b_ml.ml_line_count) { - lnum = curbuf->b_ml.ml_line_count; - } + lnum = MIN(lnum, curbuf->b_ml.ml_line_count); // When the cursor didn't move at all we fail. if (lnum == curwin->w_cursor.lnum) { @@ -3297,10 +3283,7 @@ static linenr_T diff_get_corresponding_line_int(buf_T *buf1, linenr_T lnum1) if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1) { // Inside the diffblock baseline = lnum1 - dp->df_lnum[idx1]; - - if (baseline > dp->df_count[idx2]) { - baseline = dp->df_count[idx2]; - } + baseline = MIN(baseline, dp->df_count[idx2]); return dp->df_lnum[idx2] + baseline; } @@ -3335,10 +3318,7 @@ linenr_T diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1) linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1); // don't end up past the end of the file - if (lnum > curbuf->b_ml.ml_line_count) { - return curbuf->b_ml.ml_line_count; - } - return lnum; + return MIN(lnum, curbuf->b_ml.ml_line_count); } /// For line "lnum" in the current window find the equivalent lnum in window @@ -3381,10 +3361,7 @@ linenr_T diff_lnum_win(linenr_T lnum, win_T *wp) } linenr_T n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]); - if (n > dp->df_lnum[i] + dp->df_count[i]) { - n = dp->df_lnum[i] + dp->df_count[i]; - } - return n; + return MIN(n, dp->df_lnum[i] + dp->df_count[i]); } /// Handle an ED style diff line. |