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

This commit is contained in:
zeertzjq 2024-01-19 11:04:01 +08:00 committed by GitHub
parent 78b000c74d
commit ba9f86a9ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 74 additions and 4 deletions

View File

@ -2277,6 +2277,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;
@ -2548,10 +2549,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(kOptInccommand, "nosplit", 0, SID_NONE);
icm_split = false;
}
// Setup preview namespace if it's not already set.
if (!cmdpreview_ns) {

View File

@ -2895,3 +2895,54 @@ it("'inccommand' cannot be changed during preview #23136", function()
feed(':%s/foo/bar<C-E><C-E><C-E>')
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()
api.nvim_buf_set_name(0, '[Preview]')
local screen = Screen.new(30, 20)
common_setup(screen, 'split', 'foo1\nfoo2\nfoo3')
eq('split', api.nvim_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', api.nvim_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 api = helpers.api
local clear = helpers.clear
local eq = helpers.eq
local exec_lua = helpers.exec_lua
local insert = helpers.insert
local feed = helpers.feed
@ -495,6 +497,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')
api.nvim_buf_set_name(0, '[Preview]')
exec_lua([[
vim.api.nvim_create_user_command('Test', function() end, {
nargs = '*',
preview = function(_, _, _)
return 2
end
})
]])
eq('split', api.nvim_get_option_value('inccommand', {}))
feed(':Test')
eq('nosplit', api.nvim_get_option_value('inccommand', {}))
end)
end)
describe("'inccommand' with multiple buffers", function()