aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/undo.c
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2024-07-19 11:00:13 +0100
committerLewis Russell <me@lewisr.dev>2024-07-30 22:43:29 +0100
commitd1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0 (patch)
treeca5a4a60fca9e814d0a3937cc4ff21ed9e037520 /src/nvim/undo.c
parent1b5a394ffd4bb638ec9cbbb5e4d12d11225389cf (diff)
downloadrneovim-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/undo.c')
-rw-r--r--src/nvim/undo.c34
1 files changed, 9 insertions, 25 deletions
diff --git a/src/nvim/undo.c b/src/nvim/undo.c
index ed5c9a508c..c403e94184 100644
--- a/src/nvim/undo.c
+++ b/src/nvim/undo.c
@@ -1132,17 +1132,11 @@ static void serialize_pos(bufinfo_T *bi, pos_T pos)
static void unserialize_pos(bufinfo_T *bi, pos_T *pos)
{
pos->lnum = undo_read_4c(bi);
- if (pos->lnum < 0) {
- pos->lnum = 0;
- }
+ pos->lnum = MAX(pos->lnum, 0);
pos->col = undo_read_4c(bi);
- if (pos->col < 0) {
- pos->col = 0;
- }
+ pos->col = MAX(pos->col, 0);
pos->coladd = undo_read_4c(bi);
- if (pos->coladd < 0) {
- pos->coladd = 0;
- }
+ pos->coladd = MAX(pos->coladd, 0);
}
/// Serializes "info".
@@ -1209,14 +1203,12 @@ void u_write_undo(const char *const name, const bool forceit, buf_T *const buf,
// Strip any sticky and executable bits.
perm = perm & 0666;
- int fd;
-
// If the undo file already exists, verify that it actually is an undo
// file, and delete it.
if (os_path_exists(file_name)) {
if (name == NULL || !forceit) {
// Check we can read it and it's an undo file.
- fd = os_open(file_name, O_RDONLY, 0);
+ int fd = os_open(file_name, O_RDONLY, 0);
if (fd < 0) {
if (name != NULL || p_verbose > 0) {
if (name == NULL) {
@@ -1261,7 +1253,7 @@ void u_write_undo(const char *const name, const bool forceit, buf_T *const buf,
goto theend;
}
- fd = os_open(file_name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
+ int fd = os_open(file_name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
if (fd < 0) {
semsg(_(e_not_open), file_name);
goto theend;
@@ -2001,9 +1993,7 @@ void undo_time(int step, bool sec, bool file, bool absolute)
target = curbuf->b_u_seq_cur + step;
}
if (step < 0) {
- if (target < 0) {
- target = 0;
- }
+ target = MAX(target, 0);
closest = -1;
} else {
if (dosec) {
@@ -2396,9 +2386,7 @@ static void u_undoredo(bool undo, bool do_buf_event)
}
// Set the '[ mark.
- if (top + 1 < curbuf->b_op_start.lnum) {
- curbuf->b_op_start.lnum = top + 1;
- }
+ curbuf->b_op_start.lnum = MIN(curbuf->b_op_start.lnum, top + 1);
// Set the '] mark.
if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum) {
curbuf->b_op_end.lnum = top + 1;
@@ -2419,12 +2407,8 @@ static void u_undoredo(bool undo, bool do_buf_event)
}
// Ensure the '[ and '] marks are within bounds.
- if (curbuf->b_op_start.lnum > curbuf->b_ml.ml_line_count) {
- curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
- }
- if (curbuf->b_op_end.lnum > curbuf->b_ml.ml_line_count) {
- curbuf->b_op_end.lnum = curbuf->b_ml.ml_line_count;
- }
+ curbuf->b_op_start.lnum = MIN(curbuf->b_op_start.lnum, curbuf->b_ml.ml_line_count);
+ curbuf->b_op_end.lnum = MIN(curbuf->b_op_end.lnum, curbuf->b_ml.ml_line_count);
// Adjust Extmarks
if (undo) {