diff options
author | zeertzjq <zeertzjq@outlook.com> | 2024-02-16 06:56:12 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2024-02-16 07:18:06 +0800 |
commit | 163add40b8b98b91dfb8eff589f49dc75f1032ea (patch) | |
tree | c6502c68406be5ae3ef88e292f81cff9f9d71ac2 /src | |
parent | c1fa8789c1ba5549dc37163dfa9be039c5092a41 (diff) | |
download | rneovim-163add40b8b98b91dfb8eff589f49dc75f1032ea.tar.gz rneovim-163add40b8b98b91dfb8eff589f49dc75f1032ea.tar.bz2 rneovim-163add40b8b98b91dfb8eff589f49dc75f1032ea.zip |
vim-patch:9.1.0113: duplicate code when cleaning undo stack
Problem: duplicate code when cleaning undo stack
Solution: refactor undo cleanup into a single public function
related: vim/vim#13928
https://github.com/vim/vim/commit/9071ed8107244e0c56a16b77d1c28e975cb21dd2
Co-authored-by: Christian Brabandt <cb@256bit.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/buffer.c | 5 | ||||
-rw-r--r-- | src/nvim/fileio.c | 3 | ||||
-rw-r--r-- | src/nvim/quickfix.c | 3 | ||||
-rw-r--r-- | src/nvim/undo.c | 36 |
4 files changed, 27 insertions, 20 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 38c3ee13aa..bf1d2ac6dd 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -843,8 +843,9 @@ void buf_freeall(buf_T *buf, int flags) ml_close(buf, true); // close and delete the memline/memfile buf->b_ml.ml_line_count = 0; // no lines in buffer if ((flags & BFA_KEEP_UNDO) == 0) { - u_blockfree(buf); // free the memory allocated for undo - u_clearall(buf); // reset all undo information + // free the memory allocated for undo + // and reset all undo information + u_clearallandblockfree(buf); } syntax_clear(&buf->b_s); // reset syntax info buf->b_flags &= ~BF_READERR; // a read error is no longer relevant diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index a06a773f47..2c96e4bd87 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3171,8 +3171,7 @@ void buf_reload(buf_T *buf, int orig_mode, bool reload_options) // Mark the buffer as unmodified and free undo info. unchanged(buf, true, true); if ((flags & READ_KEEP_UNDO) == 0) { - u_blockfree(buf); - u_clearall(buf); + u_clearallandblockfree(buf); } else { // Mark all undo states as changed. u_unchanged(curbuf); diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 16290b76c6..651ebc9f93 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -4159,8 +4159,7 @@ static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int q } // Remove all undo information - u_blockfree(curbuf); - u_clearall(curbuf); + u_clearallandblockfree(curbuf); } // Check if there is anything to display diff --git a/src/nvim/undo.c b/src/nvim/undo.c index 6081268e53..e9170ba858 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -2988,6 +2988,28 @@ void u_clearall(buf_T *buf) buf->b_u_line_lnum = 0; } +/// Free all allocated memory blocks for the buffer 'buf'. +void u_blockfree(buf_T *buf) +{ + while (buf->b_u_oldhead != NULL) { +#ifndef NDEBUG + u_header_T *previous_oldhead = buf->b_u_oldhead; +#endif + + u_freeheader(buf, buf->b_u_oldhead, NULL); + assert(buf->b_u_oldhead != previous_oldhead); + } + xfree(buf->b_u_line_ptr); +} + +/// Free all allocated memory blocks for the buffer 'buf'. +/// and invalidate the undo buffer +void u_clearallandblockfree(buf_T *buf) +{ + u_blockfree(buf); + u_clearall(buf); +} + /// Save the line "lnum" for the "U" command. void u_saveline(buf_T *buf, linenr_T lnum) { @@ -3054,20 +3076,6 @@ void u_undoline(void) check_cursor_col(); } -/// Free all allocated memory blocks for the buffer 'buf'. -void u_blockfree(buf_T *buf) -{ - while (buf->b_u_oldhead != NULL) { -#ifndef NDEBUG - u_header_T *previous_oldhead = buf->b_u_oldhead; -#endif - - u_freeheader(buf, buf->b_u_oldhead, NULL); - assert(buf->b_u_oldhead != previous_oldhead); - } - xfree(buf->b_u_line_ptr); -} - /// Allocate memory and copy curbuf line into it. /// /// @param lnum the line to copy |