fix(api): nvim_win_set_buf(0, 0) fails if 'winfixbuf' is set #31576

## Problem
With 'winfixbuf' enabled, `nvim_win_set_buf` and `nvim_set_current_buf` fail
even if targeting the already-current buffer.

    vim.wo.winfixbuf = true
    vim.api.nvim_win_set_buf(0, 0)
    vim.api.nvim_set_current_buf(0)

Solution:
Check for this condition.
This commit is contained in:
phanium 2024-12-16 16:59:24 +08:00 committed by GitHub
parent cc38630d39
commit 01a97d2ad7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 12 deletions

View File

@ -888,7 +888,7 @@ void nvim_set_current_buf(Buffer buffer, Error *err)
{ {
buf_T *buf = find_buffer_by_handle(buffer, err); buf_T *buf = find_buffer_by_handle(buffer, err);
if (!buf) { if (!buf || curwin->w_buffer == buf) {
return; return;
} }

View File

@ -59,7 +59,7 @@ void nvim_win_set_buf(Window window, Buffer buffer, Error *err)
{ {
win_T *win = find_window_by_handle(window, err); win_T *win = find_window_by_handle(window, err);
buf_T *buf = find_buffer_by_handle(buffer, err); buf_T *buf = find_buffer_by_handle(buffer, err);
if (!win || !buf) { if (!win || !buf || win->w_buffer == buf) {
return; return;
} }

View File

@ -8,8 +8,8 @@ describe("Nvim API calls with 'winfixbuf'", function()
clear() clear()
end) end)
it("Calling vim.api.nvim_win_set_buf with 'winfixbuf'", function() it('vim.api.nvim_win_set_buf on non-current buffer', function()
local results = exec_lua([[ local ok = exec_lua([[
local function _setup_two_buffers() local function _setup_two_buffers()
local buffer = vim.api.nvim_create_buf(true, true) local buffer = vim.api.nvim_create_buf(true, true)
@ -23,16 +23,16 @@ describe("Nvim API calls with 'winfixbuf'", function()
local other_buffer = _setup_two_buffers() local other_buffer = _setup_two_buffers()
local current_window = 0 local current_window = 0
local results, _ = pcall(vim.api.nvim_win_set_buf, current_window, other_buffer) local ok, _ = pcall(vim.api.nvim_win_set_buf, current_window, other_buffer)
return results return ok
]]) ]])
assert(results == false) assert(not ok)
end) end)
it("Calling vim.api.nvim_set_current_buf with 'winfixbuf'", function() it('vim.api.nvim_set_current_buf on non-current buffer', function()
local results = exec_lua([[ local ok = exec_lua([[
local function _setup_two_buffers() local function _setup_two_buffers()
local buffer = vim.api.nvim_create_buf(true, true) local buffer = vim.api.nvim_create_buf(true, true)
@ -45,11 +45,29 @@ describe("Nvim API calls with 'winfixbuf'", function()
end end
local other_buffer = _setup_two_buffers() local other_buffer = _setup_two_buffers()
local results, _ = pcall(vim.api.nvim_set_current_buf, other_buffer) local ok, _ = pcall(vim.api.nvim_set_current_buf, other_buffer)
return results return ok
]]) ]])
assert(results == false) assert(not ok)
end)
it('vim.api.nvim_win_set_buf on current buffer', function()
exec_lua([[
vim.wo.winfixbuf = true
local curbuf = vim.api.nvim_get_current_buf()
vim.api.nvim_win_set_buf(0, curbuf)
assert(vim.api.nvim_get_current_buf() == curbuf)
]])
end)
it('vim.api.nvim_set_current_buf on current buffer', function()
exec_lua([[
vim.wo.winfixbuf = true
local curbuf = vim.api.nvim_get_current_buf()
vim.api.nvim_set_current_buf(curbuf)
assert(vim.api.nvim_get_current_buf() == curbuf)
]])
end) end)
end) end)