From bb6190bec5f18c1f9e2c1d29ef1f7cf7912ea625 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 1 Jun 2024 08:19:41 -0700 Subject: refactor: move shared messages to errors.h #26214 --- src/nvim/undo.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/nvim/undo.c') diff --git a/src/nvim/undo.c b/src/nvim/undo.c index ba720c9f6a..ed5c9a508c 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -92,6 +92,7 @@ #include "nvim/decoration.h" #include "nvim/drawscreen.h" #include "nvim/edit.h" +#include "nvim/errors.h" #include "nvim/eval/funcs.h" #include "nvim/eval/typval.h" #include "nvim/ex_cmds_defs.h" -- cgit From d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Fri, 19 Jul 2024 11:00:13 +0100 Subject: 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; ``` --- src/nvim/undo.c | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) (limited to 'src/nvim/undo.c') 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) { -- cgit From f926cc32c9262b6254e2843276b951cef9da1afe Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 2 Jul 2024 13:45:50 +0200 Subject: refactor(shada): rework msgpack decoding without msgpack-c This also makes shada reading slightly faster due to avoiding some copying and allocation. Use keysets to drive decoding of msgpack maps for shada entries. --- src/nvim/undo.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nvim/undo.c') diff --git a/src/nvim/undo.c b/src/nvim/undo.c index c403e94184..15c8e0b283 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -343,8 +343,7 @@ static OptInt get_undolevel(buf_T *buf) static inline void zero_fmark_additional_data(fmark_T *fmarks) { for (size_t i = 0; i < NMARKS; i++) { - tv_dict_unref(fmarks[i].additional_data); - fmarks[i].additional_data = NULL; + XFREE_CLEAR(fmarks[i].additional_data); } } -- cgit