feat(api): allow floats to be opened in non-current tabpage (#28480)

\
This commit is contained in:
Will Hopkins 2024-04-24 18:14:05 -07:00 committed by GitHub
parent c32fcd1ed5
commit 16513b3033
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 95 additions and 28 deletions

View File

@ -478,7 +478,8 @@ The following changes to existing APIs or features add new behavior.
|:vertical|, |:horizontal| and |:botright|. |:vertical|, |:horizontal| and |:botright|.
• |nvim_open_win()| and |nvim_win_set_config()| now support opening normal (split) • |nvim_open_win()| and |nvim_win_set_config()| now support opening normal (split)
windows, and moving floating windows into split windows. windows, moving floating windows into split windows, and opening windows in
non-current tabpages.
• 'errorfile' (|-q|) accepts `-` as an alias for stdin. • 'errorfile' (|-q|) accepts `-` as an alias for stdin.

View File

@ -236,19 +236,19 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(win_config) *config, Err
win_T *wp = NULL; win_T *wp = NULL;
tabpage_T *tp = curtab; tabpage_T *tp = curtab;
if (is_split) { win_T *parent = NULL;
win_T *parent = NULL; if (config->win != -1) {
if (config->win != -1) { parent = find_window_by_handle(fconfig.window, err);
parent = find_window_by_handle(fconfig.window, err); if (!parent) {
if (!parent) { // find_window_by_handle has already set the error
// find_window_by_handle has already set the error goto cleanup;
goto cleanup; } else if (is_split && parent->w_floating) {
} else if (parent->w_floating) { api_set_error(err, kErrorTypeException, "Cannot split a floating window");
api_set_error(err, kErrorTypeException, "Cannot split a floating window"); goto cleanup;
goto cleanup;
}
} }
tp = win_find_tabpage(parent);
}
if (is_split) {
if (!check_split_disallowed_err(parent ? parent : curwin, err)) { if (!check_split_disallowed_err(parent ? parent : curwin, err)) {
goto cleanup; // error already set goto cleanup; // error already set
} }
@ -267,7 +267,6 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(win_config) *config, Err
if (parent == NULL || parent == curwin) { if (parent == NULL || parent == curwin) {
wp = win_split_ins(size, flags, NULL, 0, NULL); wp = win_split_ins(size, flags, NULL, 0, NULL);
} else { } else {
tp = win_find_tabpage(parent);
switchwin_T switchwin; switchwin_T switchwin;
// `parent` is valid in `tp`, so switch_win should not fail. // `parent` is valid in `tp`, so switch_win should not fail.
const int result = switch_win(&switchwin, parent, tp, true); const int result = switch_win(&switchwin, parent, tp, true);
@ -395,6 +394,7 @@ void nvim_win_set_config(Window window, Dict(win_config) *config, Error *err)
if (!win) { if (!win) {
return; return;
} }
tabpage_T *win_tp = win_find_tabpage(win); tabpage_T *win_tp = win_find_tabpage(win);
bool was_split = !win->w_floating; bool was_split = !win->w_floating;
bool has_split = HAS_KEY_X(config, split); bool has_split = HAS_KEY_X(config, split);
@ -409,23 +409,28 @@ void nvim_win_set_config(Window window, Dict(win_config) *config, Error *err)
if (!parse_win_config(win, config, &fconfig, !was_split || to_split, err)) { if (!parse_win_config(win, config, &fconfig, !was_split || to_split, err)) {
return; return;
} }
win_T *parent = NULL;
if (config->win != -1) {
parent = find_window_by_handle(fconfig.window, err);
if (!parent) {
return;
} else if (to_split && parent->w_floating) {
api_set_error(err, kErrorTypeException, "Cannot split a floating window");
return;
}
// Prevent autocmd window from being moved into another tabpage
if (is_aucmd_win(win) && win_find_tabpage(win) != win_find_tabpage(parent)) {
api_set_error(err, kErrorTypeException, "Cannot move autocmd win to another tabpage");
return;
}
}
if (was_split && !to_split) { if (was_split && !to_split) {
if (!win_new_float(win, false, fconfig, err)) { if (!win_new_float(win, false, fconfig, err)) {
return; return;
} }
redraw_later(win, UPD_NOT_VALID); redraw_later(win, UPD_NOT_VALID);
} else if (to_split) { } else if (to_split) {
win_T *parent = NULL;
if (config->win != -1) {
parent = find_window_by_handle(fconfig.window, err);
if (!parent) {
return;
} else if (parent->w_floating) {
api_set_error(err, kErrorTypeException, "Cannot split a floating window");
return;
}
}
WinSplit old_split = win_split_dir(win); WinSplit old_split = win_split_dir(win);
if (has_vertical && !has_split) { if (has_vertical && !has_split) {
if (config->vertical) { if (config->vertical) {

View File

@ -5127,7 +5127,14 @@ win_T *win_alloc(win_T *after, bool hidden)
block_autocmds(); block_autocmds();
// link the window in the window list // link the window in the window list
if (!hidden) { if (!hidden) {
win_append(after, new_wp, NULL); tabpage_T *tp = NULL;
if (after) {
tp = win_find_tabpage(after);
if (tp == curtab) {
tp = NULL;
}
}
win_append(after, new_wp, tp);
} }
new_wp->w_wincol = 0; new_wp->w_wincol = 0;

View File

@ -41,7 +41,24 @@
win_T *win_new_float(win_T *wp, bool last, WinConfig fconfig, Error *err) win_T *win_new_float(win_T *wp, bool last, WinConfig fconfig, Error *err)
{ {
if (wp == NULL) { if (wp == NULL) {
wp = win_alloc(last ? lastwin : lastwin_nofloating(), false); tabpage_T *tp = NULL;
win_T *tp_last = last ? lastwin : lastwin_nofloating();
if (fconfig.window != 0) {
assert(!last);
win_T *parent_wp = find_window_by_handle(fconfig.window, err);
if (!parent_wp) {
return NULL;
}
tp = win_find_tabpage(parent_wp);
if (!tp) {
return NULL;
}
tp_last = tp->tp_lastwin;
while (tp_last->w_floating && tp_last->w_prev) {
tp_last = tp_last->w_prev;
}
}
wp = win_alloc(tp_last, false);
win_init(wp, curwin, 0); win_init(wp, curwin, 0);
} else { } else {
assert(!last); assert(!last);

View File

@ -1454,6 +1454,40 @@ describe('API/win', function()
}, layout) }, layout)
end) end)
it('opens floating windows in other tabpages', function()
local first_win = api.nvim_get_current_win()
local first_tab = api.nvim_get_current_tabpage()
command('tabnew')
local new_tab = api.nvim_get_current_tabpage()
local win = api.nvim_open_win(0, false, {
relative = 'win',
win = first_win,
width = 5,
height = 5,
row = 1,
col = 1,
})
eq(api.nvim_win_get_tabpage(win), first_tab)
eq(api.nvim_get_current_tabpage(), new_tab)
end)
it('switches to new windows in non-current tabpages when enter=true', function()
local first_win = api.nvim_get_current_win()
local first_tab = api.nvim_get_current_tabpage()
command('tabnew')
local win = api.nvim_open_win(0, true, {
relative = 'win',
win = first_win,
width = 5,
height = 5,
row = 1,
col = 1,
})
eq(api.nvim_win_get_tabpage(win), first_tab)
eq(api.nvim_get_current_tabpage(), first_tab)
end)
local function setup_tabbed_autocmd_test() local function setup_tabbed_autocmd_test()
local info = {} local info = {}
info.orig_buf = api.nvim_get_current_buf() info.orig_buf = api.nvim_get_current_buf()

View File

@ -532,7 +532,10 @@ describe('float window', function()
local closed_win = api.nvim_get_current_win() local closed_win = api.nvim_get_current_win()
command('close') command('close')
local buf = api.nvim_create_buf(false,false) local buf = api.nvim_create_buf(false,false)
api.nvim_open_win(buf, true, {relative='win', win=closed_win, width=1, height=1, bufpos={0,0}}) eq(
'Invalid window id: ' .. closed_win,
pcall_err(api.nvim_open_win, buf, true, {relative='win', win=closed_win, width=1, height=1, bufpos={0,0}})
)
assert_alive() assert_alive()
end) end)