fix(inccommand): improve preview buffer number handling (#27087)

This commit is contained in:
zeertzjq 2024-01-19 11:04:01 +08:00
parent 773877e024
commit 26f8365864
3 changed files with 74 additions and 4 deletions

View File

@ -2252,6 +2252,7 @@ static buf_T *cmdpreview_open_buf(void)
///
/// @return Pointer to command preview window if succeeded, NULL if failed.
static win_T *cmdpreview_open_win(buf_T *cmdpreview_buf)
FUNC_ATTR_NONNULL_ALL
{
win_T *save_curwin = curwin;
@ -2523,10 +2524,10 @@ static bool cmdpreview_may_show(CommandLineState *s)
cmdpreview_prepare(&cpinfo);
// Open preview buffer if inccommand=split.
if (!icm_split) {
cmdpreview_bufnr = 0;
} else if ((cmdpreview_buf = cmdpreview_open_buf()) == NULL) {
abort();
if (icm_split && (cmdpreview_buf = cmdpreview_open_buf()) == NULL) {
// Failed to create preview buffer, so disable preview.
set_string_option_direct("icm", -1, "nosplit", OPT_FREE, SID_NONE);
icm_split = false;
}
// Setup preview namespace if it's not already set.
if (!cmdpreview_ns) {

View File

@ -3250,3 +3250,54 @@ it("with 'inccommand' typing :filter doesn't segfault or leak memory #19057", fu
feed('i')
assert_alive()
end)
it("'inccommand' value can be changed multiple times #27086", function()
clear()
local screen = Screen.new(30, 20)
common_setup(screen, 'split', 'foo1\nfoo2\nfoo3')
for _ = 1, 3 do
feed(':%s/foo/bar')
screen:expect([[
{12:bar}1 |
{12:bar}2 |
{12:bar}3 |
{15:~ }|*7
{11:[No Name] [+] }|
|1| {12:bar}1 |
|2| {12:bar}2 |
|3| {12:bar}3 |
{15:~ }|*4
{10:[Preview] }|
:%s/foo/bar^ |
]])
feed('<Esc>')
command('set inccommand=nosplit')
feed(':%s/foo/bar')
screen:expect([[
{12:bar}1 |
{12:bar}2 |
{12:bar}3 |
{15:~ }|*16
:%s/foo/bar^ |
]])
feed('<Esc>')
command('set inccommand=split')
end
end)
it("'inccommand' disables preview if preview buffer can't be created #27086", function()
clear()
meths.buf_set_name(0, '[Preview]')
local screen = Screen.new(30, 20)
common_setup(screen, 'split', 'foo1\nfoo2\nfoo3')
eq('split', meths.get_option_value('inccommand', {}))
feed(':%s/foo/bar')
screen:expect([[
{12:bar}1 |
{12:bar}2 |
{12:bar}3 |
{15:~ }|*16
:%s/foo/bar^ |
]])
eq('nosplit', meths.get_option_value('inccommand', {}))
end)

View File

@ -1,6 +1,8 @@
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local meths = helpers.meths
local clear = helpers.clear
local eq = helpers.eq
local exec_lua = helpers.exec_lua
local insert = helpers.insert
local feed = helpers.feed
@ -543,6 +545,22 @@ describe("'inccommand' for user commands", function()
test_preview_break_undo()
end)
end)
it('disables preview if preview buffer cannot be created #27086', function()
command('set inccommand=split')
meths.buf_set_name(0, '[Preview]')
exec_lua([[
vim.api.nvim_create_user_command('Test', function() end, {
nargs = '*',
preview = function(_, _, _)
return 2
end
})
]])
eq('split', meths.get_option_value('inccommand', {}))
feed(':Test')
eq('nosplit', meths.get_option_value('inccommand', {}))
end)
end)
describe("'inccommand' with multiple buffers", function()