mirror of
https://github.com/neovim/neovim
synced 2025-07-16 01:01:49 +00:00
feat(ui): use builtin completion popupmenu with ext_cmdline (#31269)
Problem: UIs implementing ext_cmdline/message must also implement ext_popupmenu in order to get cmdline completion with wildoptions+=pum. Solution: Allow marking a window as the ext_cmdline window through nvim_open_win(), including prompt offset. Anchor the cmdline- completion popupmenu to this window.
This commit is contained in:
@ -3475,8 +3475,8 @@ nvim_open_win({buffer}, {enter}, {config}) *nvim_open_win()*
|
||||
like a tooltip near the buffer text).
|
||||
• row: Row position in units of "screen cell height", may be
|
||||
fractional.
|
||||
• col: Column position in units of "screen cell width", may
|
||||
be fractional.
|
||||
• col: Column position in units of screen cell width, may be
|
||||
fractional.
|
||||
• focusable: Enable focus by user actions (wincmds, mouse
|
||||
events). Defaults to true. Non-focusable windows can be
|
||||
entered by |nvim_set_current_win()|, or, when the `mouse`
|
||||
@ -3558,6 +3558,9 @@ nvim_open_win({buffer}, {enter}, {config}) *nvim_open_win()*
|
||||
cursor will be invisible when focused on it.
|
||||
• vertical: Split vertically |:vertical|.
|
||||
• split: Split direction: "left", "right", "above", "below".
|
||||
• _cmdline_offset: (EXPERIMENTAL) When provided, anchor the
|
||||
|cmdline-completion| popupmenu to this window, with an
|
||||
offset in screen cell width.
|
||||
|
||||
Return: ~
|
||||
|window-ID|, or 0 on error
|
||||
|
5
runtime/lua/vim/_meta/api.lua
generated
5
runtime/lua/vim/_meta/api.lua
generated
@ -1771,8 +1771,7 @@ function vim.api.nvim_open_term(buffer, opts) end
|
||||
--- - `row=0` and `col=0` if `anchor` is "SW" or "SE"
|
||||
--- (thus like a tooltip near the buffer text).
|
||||
--- - row: Row position in units of "screen cell height", may be fractional.
|
||||
--- - col: Column position in units of "screen cell width", may be
|
||||
--- fractional.
|
||||
--- - col: Column position in units of screen cell width, may be fractional.
|
||||
--- - focusable: Enable focus by user actions (wincmds, mouse events).
|
||||
--- Defaults to true. Non-focusable windows can be entered by
|
||||
--- `nvim_set_current_win()`, or, when the `mouse` field is set to true,
|
||||
@ -1852,6 +1851,8 @@ function vim.api.nvim_open_term(buffer, opts) end
|
||||
--- focused on it.
|
||||
--- - vertical: Split vertically `:vertical`.
|
||||
--- - split: Split direction: "left", "right", "above", "below".
|
||||
--- - _cmdline_offset: (EXPERIMENTAL) When provided, anchor the `cmdline-completion`
|
||||
--- popupmenu to this window, with an offset in screen cell width.
|
||||
--- @return integer # |window-ID|, or 0 on error
|
||||
function vim.api.nvim_open_win(buffer, enter, config) end
|
||||
|
||||
|
1
runtime/lua/vim/_meta/api_keysets.lua
generated
1
runtime/lua/vim/_meta/api_keysets.lua
generated
@ -312,6 +312,7 @@ error('Cannot require a meta file')
|
||||
--- @field noautocmd? boolean
|
||||
--- @field fixed? boolean
|
||||
--- @field hide? boolean
|
||||
--- @field _cmdline_offset? integer
|
||||
|
||||
--- @class vim.api.keyset.win_text_height
|
||||
--- @field start_row? integer
|
||||
|
@ -135,6 +135,7 @@ typedef struct {
|
||||
Boolean noautocmd;
|
||||
Boolean fixed;
|
||||
Boolean hide;
|
||||
Integer _cmdline_offset;
|
||||
} Dict(win_config);
|
||||
|
||||
typedef struct {
|
||||
|
@ -124,8 +124,7 @@
|
||||
/// - `row=0` and `col=0` if `anchor` is "SW" or "SE"
|
||||
/// (thus like a tooltip near the buffer text).
|
||||
/// - row: Row position in units of "screen cell height", may be fractional.
|
||||
/// - col: Column position in units of "screen cell width", may be
|
||||
/// fractional.
|
||||
/// - col: Column position in units of screen cell width, may be fractional.
|
||||
/// - focusable: Enable focus by user actions (wincmds, mouse events).
|
||||
/// Defaults to true. Non-focusable windows can be entered by
|
||||
/// |nvim_set_current_win()|, or, when the `mouse` field is set to true,
|
||||
@ -205,6 +204,8 @@
|
||||
/// focused on it.
|
||||
/// - vertical: Split vertically |:vertical|.
|
||||
/// - split: Split direction: "left", "right", "above", "below".
|
||||
/// - _cmdline_offset: (EXPERIMENTAL) When provided, anchor the |cmdline-completion|
|
||||
/// popupmenu to this window, with an offset in screen cell width.
|
||||
///
|
||||
/// @param[out] err Error details, if any
|
||||
///
|
||||
@ -288,6 +289,10 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(win_config) *config, Err
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (fconfig._cmdline_offset < INT_MAX) {
|
||||
cmdline_win = wp;
|
||||
}
|
||||
|
||||
// Autocommands may close `wp` or move it to another tabpage, so update and check `tp` after each
|
||||
// event. In each case, `wp` should already be valid in `tp`, so switch_win should not fail.
|
||||
// Also, autocommands may free the `buf` to switch to, so store a bufref to check.
|
||||
@ -636,6 +641,11 @@ restore_curwin:
|
||||
didset_window_options(win, true);
|
||||
}
|
||||
}
|
||||
if (fconfig._cmdline_offset < INT_MAX) {
|
||||
cmdline_win = win;
|
||||
} else if (win == cmdline_win && fconfig._cmdline_offset == INT_MAX) {
|
||||
cmdline_win = NULL;
|
||||
}
|
||||
#undef HAS_KEY_X
|
||||
}
|
||||
|
||||
@ -768,6 +778,9 @@ Dict(win_config) nvim_win_get_config(Window window, Arena *arena, Error *err)
|
||||
const char *rel = (wp->w_floating && !config->external
|
||||
? float_relative_str[config->relative] : "");
|
||||
PUT_KEY_X(rv, relative, cstr_as_string(rel));
|
||||
if (config->_cmdline_offset < INT_MAX) {
|
||||
PUT_KEY_X(rv, _cmdline_offset, config->_cmdline_offset);
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
@ -1318,6 +1331,10 @@ static bool parse_win_config(win_T *wp, Dict(win_config) *config, WinConfig *fco
|
||||
fconfig->hide = config->hide;
|
||||
}
|
||||
|
||||
if (HAS_KEY_X(config, _cmdline_offset)) {
|
||||
fconfig->_cmdline_offset = (int)config->_cmdline_offset;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
fail:
|
||||
|
@ -996,6 +996,7 @@ typedef struct {
|
||||
bool noautocmd;
|
||||
bool fixed;
|
||||
bool hide;
|
||||
int _cmdline_offset;
|
||||
} WinConfig;
|
||||
|
||||
#define WIN_CONFIG_INIT ((WinConfig){ .height = 0, .width = 0, \
|
||||
@ -1009,7 +1010,8 @@ typedef struct {
|
||||
.style = kWinStyleUnused, \
|
||||
.noautocmd = false, \
|
||||
.hide = false, \
|
||||
.fixed = false })
|
||||
.fixed = false, \
|
||||
._cmdline_offset = INT_MAX })
|
||||
|
||||
// Structure to store last cursor position and topline. Used by check_lnums()
|
||||
// and reset_lnums().
|
||||
|
@ -372,7 +372,7 @@ static int cmdline_pum_create(CmdlineInfo *ccline, expand_T *xp, char **matches,
|
||||
|
||||
// Compute the popup menu starting column
|
||||
char *endpos = showtail ? showmatches_gettail(xp->xp_pattern, true) : xp->xp_pattern;
|
||||
if (ui_has(kUICmdline)) {
|
||||
if (ui_has(kUICmdline) && cmdline_win == NULL) {
|
||||
compl_startcol = (int)(endpos - ccline->cmdbuff);
|
||||
} else {
|
||||
compl_startcol = cmd_screencol((int)(endpos - ccline->cmdbuff));
|
||||
@ -1091,13 +1091,8 @@ int showmatches(expand_T *xp, bool wildmenu)
|
||||
showtail = cmd_showtail;
|
||||
}
|
||||
|
||||
bool compl_use_pum = (ui_has(kUICmdline)
|
||||
? ui_has(kUIPopupmenu)
|
||||
: wildmenu && (wop_flags & kOptWopFlagPum))
|
||||
|| ui_has(kUIWildmenu);
|
||||
|
||||
if (compl_use_pum) {
|
||||
// cmdline completion popup menu (with wildoptions=pum)
|
||||
if (((!ui_has(kUICmdline) || cmdline_win != NULL) && wildmenu && (wop_flags & kOptWopFlagPum))
|
||||
|| ui_has(kUIWildmenu) || (ui_has(kUICmdline) && ui_has(kUIPopupmenu))) {
|
||||
return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
|
||||
}
|
||||
|
||||
|
@ -3008,10 +3008,9 @@ static int cmd_startcol(void)
|
||||
int cmd_screencol(int bytepos)
|
||||
{
|
||||
int m; // maximum column
|
||||
|
||||
int col = cmd_startcol();
|
||||
if (KeyTyped) {
|
||||
m = Columns * Rows;
|
||||
m = cmdline_win ? cmdline_win->w_view_width * cmdline_win->w_view_height : Columns * Rows;
|
||||
if (m < 0) { // overflow, Columns or Rows at weird value
|
||||
m = MAXCOL;
|
||||
}
|
||||
|
@ -746,6 +746,7 @@ EXTERN int cmdwin_level INIT( = 0); ///< cmdline recursion level
|
||||
EXTERN buf_T *cmdwin_buf INIT( = NULL); ///< buffer of cmdline window or NULL
|
||||
EXTERN win_T *cmdwin_win INIT( = NULL); ///< window of cmdline window or NULL
|
||||
EXTERN win_T *cmdwin_old_curwin INIT( = NULL); ///< curwin before opening cmdline window or NULL
|
||||
EXTERN win_T *cmdline_win INIT( = NULL); ///< window in use by ext_cmdline
|
||||
|
||||
EXTERN char no_lines_msg[] INIT( = N_("--No lines in buffer--"));
|
||||
|
||||
|
@ -158,13 +158,15 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
|
||||
if (State & MODE_CMDLINE) {
|
||||
below_row = cmdline_row;
|
||||
}
|
||||
win_T *target_win = (State & MODE_CMDLINE) ? cmdline_win : curwin;
|
||||
pum_win_row_offset = 0;
|
||||
pum_win_col_offset = 0;
|
||||
|
||||
// wildoptions=pum
|
||||
if (State == MODE_CMDLINE) {
|
||||
pum_win_row = ui_has(kUICmdline) ? 0 : cmdline_row;
|
||||
cursor_col = cmd_startcol;
|
||||
pum_win_row = cmdline_win ? cmdline_win->w_wrow : ui_has(kUICmdline) ? 0 : cmdline_row;
|
||||
cursor_col = (cmdline_win ? cmdline_win->w_config._cmdline_offset : 0) + cmd_startcol;
|
||||
cursor_col %= cmdline_win ? cmdline_win->w_view_width : Columns;
|
||||
pum_anchor_grid = ui_has(kUICmdline) ? -1 : DEFAULT_GRID_HANDLE;
|
||||
} else {
|
||||
// anchor position: the start of the completed word
|
||||
@ -174,19 +176,21 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
|
||||
} else {
|
||||
cursor_col = curwin->w_wcol;
|
||||
}
|
||||
}
|
||||
|
||||
pum_anchor_grid = (int)curwin->w_grid.target->handle;
|
||||
pum_win_row += curwin->w_grid.row_offset;
|
||||
cursor_col += curwin->w_grid.col_offset;
|
||||
if (curwin->w_grid.target != &default_grid) {
|
||||
pum_win_row += curwin->w_winrow;
|
||||
cursor_col += curwin->w_wincol;
|
||||
// ext_popupmenu should always anchor to the default grid when multigrid is disabled
|
||||
if (target_win != NULL) {
|
||||
// ext_popupmenu should always anchor to the default grid when multigrid is disabled
|
||||
pum_anchor_grid = target_win->w_grid.target->handle;
|
||||
pum_win_row += target_win->w_grid.row_offset;
|
||||
cursor_col += target_win->w_grid.col_offset;
|
||||
if (target_win->w_grid.target != &default_grid) {
|
||||
pum_win_row += target_win->w_winrow;
|
||||
cursor_col += target_win->w_wincol;
|
||||
if (!ui_has(kUIMultigrid)) {
|
||||
pum_anchor_grid = (int)default_grid.handle;
|
||||
pum_anchor_grid = DEFAULT_GRID_HANDLE;
|
||||
} else {
|
||||
pum_win_row_offset = curwin->w_winrow;
|
||||
pum_win_col_offset = curwin->w_wincol;
|
||||
pum_win_row_offset = target_win->w_winrow;
|
||||
pum_win_col_offset = target_win->w_wincol;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -236,12 +240,9 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
|
||||
|
||||
int min_row = 0;
|
||||
int min_col = 0;
|
||||
int max_col = MAX(Columns, curwin->w_wincol + curwin->w_view_width);
|
||||
if (State & MODE_CMDLINE) {
|
||||
max_col = Columns;
|
||||
}
|
||||
int win_start_col = curwin->w_wincol;
|
||||
int win_end_col = W_ENDCOL(curwin);
|
||||
int max_col = MAX(Columns, target_win ? (target_win->w_wincol + target_win->w_view_width) : 0);
|
||||
int win_start_col = target_win ? target_win->w_wincol : 0;
|
||||
int win_end_col = target_win ? W_ENDCOL(target_win) : 0;
|
||||
|
||||
// Figure out the size and position of the pum.
|
||||
pum_height = MIN(size, PUM_DEF_HEIGHT);
|
||||
@ -256,12 +257,12 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
|
||||
// pum above "pum_win_row"
|
||||
pum_above = true;
|
||||
|
||||
if (State == MODE_CMDLINE) {
|
||||
// for cmdline pum, no need for context lines
|
||||
if (State == MODE_CMDLINE && target_win == NULL) {
|
||||
// For cmdline pum, no need for context lines unless target_win is set
|
||||
context_lines = 0;
|
||||
} else {
|
||||
// Leave two lines of context if possible
|
||||
context_lines = MIN(2, curwin->w_wrow - curwin->w_cline_row);
|
||||
context_lines = MIN(2, target_win->w_wrow - target_win->w_cline_row);
|
||||
}
|
||||
|
||||
if (pum_win_row - min_row >= size + context_lines) {
|
||||
@ -280,14 +281,14 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i
|
||||
// pum below "pum_win_row"
|
||||
pum_above = false;
|
||||
|
||||
if (State == MODE_CMDLINE) {
|
||||
// for cmdline pum, no need for context lines
|
||||
if (State == MODE_CMDLINE && target_win == NULL) {
|
||||
// for cmdline pum, no need for context lines unless target_win is set
|
||||
context_lines = 0;
|
||||
} else {
|
||||
// Leave two lines of context if possible
|
||||
validate_cheight(curwin);
|
||||
int cline_visible_offset = curwin->w_cline_row +
|
||||
curwin->w_cline_height - curwin->w_wrow;
|
||||
validate_cheight(target_win);
|
||||
int cline_visible_offset = target_win->w_cline_row +
|
||||
target_win->w_cline_height - target_win->w_wrow;
|
||||
context_lines = MIN(3, cline_visible_offset);
|
||||
}
|
||||
|
||||
|
@ -320,6 +320,101 @@ describe('vim.ui_attach', function()
|
||||
},
|
||||
})
|
||||
end)
|
||||
|
||||
it('ext_cmdline completion popupmenu', function()
|
||||
screen:try_resize(screen._width, 10)
|
||||
screen:add_extra_attr_ids { [100] = { background = Screen.colors.Black } }
|
||||
exec_lua([[
|
||||
vim.o.wildoptions = 'pum'
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
vim.cmd('call setline(1, range(1, 10))')
|
||||
_G.win = vim.api.nvim_open_win(buf, false, {
|
||||
relative = 'editor',
|
||||
col = 3,
|
||||
row = 3,
|
||||
width = 20,
|
||||
height = 1,
|
||||
style = 'minimal',
|
||||
focusable = false,
|
||||
zindex = 300,
|
||||
_cmdline_offset = 0,
|
||||
})
|
||||
vim.ui_attach(ns, { ext_cmdline = true }, function(event, content, _, firstc)
|
||||
if event == 'cmdline_show' then
|
||||
local prompt = vim.api.nvim_win_get_config(_G.win)._cmdline_offset == 0
|
||||
prompt = (prompt and firstc or 'Excommand:') .. content[1][2]
|
||||
vim.api.nvim_buf_set_lines(buf, -2, -1, false, { prompt })
|
||||
vim.api.nvim_win_set_cursor(_G.win, { 1, #prompt })
|
||||
vim.api.nvim__redraw({ win = _G.win, cursor = true, flush = true })
|
||||
end
|
||||
return true
|
||||
end)
|
||||
vim.api.nvim_set_hl(0, 'Pmenu', {})
|
||||
]])
|
||||
feed(':call buf<tab>')
|
||||
screen:expect([[
|
||||
1 |
|
||||
2 |
|
||||
3 |
|
||||
4 :call bufadd^( |
|
||||
5 {12: bufadd( }{100: } |
|
||||
6 bufexists( {100: } |
|
||||
7 buffer_exists( {12: } |
|
||||
8 buffer_name( {12: } |
|
||||
9 buffer_number( {12: } |
|
||||
|
|
||||
]])
|
||||
exec_lua([[
|
||||
vim.api.nvim_win_set_config(_G.win, {
|
||||
relative = 'editor',
|
||||
col = 0,
|
||||
row = 1000,
|
||||
width = 1000,
|
||||
height = 1,
|
||||
})
|
||||
vim.api.nvim__redraw({flush = true})
|
||||
]])
|
||||
screen:expect([[
|
||||
1 |
|
||||
2 |
|
||||
3 |
|
||||
4 |
|
||||
5 {12: bufadd( }{100: } |
|
||||
6 bufexists( {100: } |
|
||||
7 buffer_exists( {12: } |
|
||||
8 buffer_name( {12: } |
|
||||
9 buffer_number( {12: } |
|
||||
:call bufadd^( |
|
||||
]])
|
||||
feed('<tab>')
|
||||
screen:expect([[
|
||||
1 bufadd( {100: } |
|
||||
2 {12: bufexists( }{100: } |
|
||||
3 buffer_exists( {100: } |
|
||||
4 buffer_name( {100: } |
|
||||
5 buffer_number( {100: } |
|
||||
6 buflisted( {100: } |
|
||||
7 bufload( {12: } |
|
||||
8 bufloaded( {12: } |
|
||||
9 bufname( {12: } |
|
||||
:call bufexists^( |
|
||||
]])
|
||||
-- Test different offset (e.g. for custom prompt)
|
||||
exec_lua('vim.api.nvim_win_set_config(_G.win, { _cmdline_offset = 9 })')
|
||||
feed('<Esc>:call buf<Tab>')
|
||||
screen:expect([[
|
||||
1 {12: bufadd( }{100: } |
|
||||
2 bufexists( {100: } |
|
||||
3 buffer_exists( {100: } |
|
||||
4 buffer_name( {100: } |
|
||||
5 buffer_number( {100: } |
|
||||
6 buflisted( {100: } |
|
||||
7 bufload( {12: } |
|
||||
8 bufloaded( {12: } |
|
||||
9 bufname( {12: } |
|
||||
Excommand:call bufadd^( |
|
||||
]])
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('vim.ui_attach', function()
|
||||
|
Reference in New Issue
Block a user