vim-patch:9.1.0112: Remove undo information, when cleaning quickfix buffer

Problem:  When the quickfix buffer has been modified an autocommand
          may invalidate the undo stack (kawarimidoll)
Solution: When clearing the quickfix buffer, also wipe the undo stack

fixes: vim/vim#13905
closes: vim/vim#13928

f0d3d4a426

Co-authored-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
zeertzjq
2024-02-16 06:35:30 +08:00
parent d60eeacae4
commit c1fa8789c1
3 changed files with 79 additions and 0 deletions

View File

@@ -68,6 +68,7 @@
#include "nvim/strings.h"
#include "nvim/types_defs.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/vim_defs.h"
#include "nvim/window.h"
@@ -4142,6 +4143,12 @@ static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int q
}
// delete all existing lines
//
// Note: we cannot store undo information, because
// qf buffer is usually not allowed to be modified.
//
// So we need to clean up undo information
// otherwise autocommands may invalidate the undo stack
while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0) {
// If deletion fails, this loop may run forever, so
// signal error and return.
@@ -4150,6 +4157,10 @@ static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int q
return;
}
}
// Remove all undo information
u_blockfree(curbuf);
u_clearall(curbuf);
}
// Check if there is anything to display

View File

@@ -0,0 +1,40 @@
local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear
local write_file = helpers.write_file
local command = helpers.command
local feed = helpers.feed
local api = helpers.api
local eq = helpers.eq
before_each(clear)
-- oldtest: Test_autocmd_invalidates_undo_on_textchanged()
it('no E440 in quickfix window when autocommand invalidates undo', function()
write_file(
'XTest_autocmd_invalidates_undo_on_textchanged',
[[
set hidden
" create quickfix list (at least 2 lines to move line)
vimgrep /u/j %
" enter quickfix window
cwindow
" set modifiable
setlocal modifiable
" set autocmd to clear quickfix list
autocmd! TextChanged <buffer> call setqflist([])
" move line
move+1
]]
)
finally(function()
os.remove('XTest_autocmd_invalidates_undo_on_textchanged')
end)
command('edit XTest_autocmd_invalidates_undo_on_textchanged')
command('so %')
feed('G')
eq('', api.nvim_get_vvar('errmsg'))
end)

View File

@@ -3840,4 +3840,32 @@ func Test_autocmd_shortmess()
delfunc SetupVimTest_shm
endfunc
func Test_autocmd_invalidates_undo_on_textchanged()
CheckRunVimInTerminal
let script =<< trim END
set hidden
" create quickfix list (at least 2 lines to move line)
vimgrep /u/j %
" enter quickfix window
cwindow
" set modifiable
setlocal modifiable
" set autocmd to clear quickfix list
autocmd! TextChanged <buffer> call setqflist([])
" move line
move+1
END
call writefile(script, 'XTest_autocmd_invalidates_undo_on_textchanged', 'D')
let buf = RunVimInTerminal('XTest_autocmd_invalidates_undo_on_textchanged', {'rows': 20})
call term_sendkeys(buf, ":so %\<cr>")
call term_sendkeys(buf, "G")
call WaitForAssert({-> assert_match('^XTest_autocmd_invalidates_undo_on_textchanged\s*$', term_getline(buf, 20))}, 1000)
call StopVimInTerminal(buf)
endfunc
" vim: shiftwidth=2 sts=2 expandtab