neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

commit 4200a0f1678c06c6da4e4cfb0184c29c1174ed21
parent dfa8b582a64aa22d3c57261bfcdc970b26cb58f3
Author: glepnir <glephunter@gmail.com>
Date:   Wed, 27 Sep 2023 17:23:42 +0800

feat(float): support toggle show float window

Diffstat:
Mruntime/doc/api.txt | 1+
Mruntime/doc/news.txt | 3+++
Mruntime/lua/vim/_meta/api.lua | 1+
Mruntime/lua/vim/_meta/api_keysets.lua | 1+
Msrc/nvim/api/keysets.h | 1+
Msrc/nvim/api/win_config.c | 6++++++
Msrc/nvim/buffer_defs.h | 2++
Msrc/nvim/window.c | 29+++++++++++++++++++----------
Mtest/functional/ui/float_spec.lua | 126++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
9 files changed, 156 insertions(+), 14 deletions(-)

diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt @@ -3181,6 +3181,7 @@ nvim_open_win({buffer}, {enter}, {*config}) *nvim_open_win()* fire from calling this function. • fixed: If true when anchor is NW or SW, the float window would be kept fixed even if the window would be truncated. + • hide: If true the floating window will be hidden. Return: ~ Window handle, or 0 on error diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt @@ -65,6 +65,9 @@ The following changes may require adaptations in user config or plugins. now requires an explicit range argument to be passed. If injections are required, provide an explicit range via `parser:parse({ start_row, end_row })`. +• Float window support hide and show by setting `hide` on `nvim_open_win` and + `nvim_win_set_config`. + ============================================================================== NEW FEATURES *news-features* diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua @@ -1617,6 +1617,7 @@ function vim.api.nvim_open_term(buffer, opts) end --- fire from calling this function. --- • fixed: If true when anchor is NW or SW, the float window --- would be kept fixed even if the window would be truncated. +--- • hide: If true the floating window will be hidden. --- @return integer function vim.api.nvim_open_win(buffer, enter, config) end diff --git a/runtime/lua/vim/_meta/api_keysets.lua b/runtime/lua/vim/_meta/api_keysets.lua @@ -113,6 +113,7 @@ error('Cannot require a meta file') --- @field style? string --- @field noautocmd? boolean --- @field fixed? boolean +--- @field hide? boolean --- @class vim.api.keyset.get_autocmds --- @field event? any diff --git a/src/nvim/api/keysets.h b/src/nvim/api/keysets.h @@ -113,6 +113,7 @@ typedef struct { String style; Boolean noautocmd; Boolean fixed; + Boolean hide; } Dict(float_config); typedef struct { diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c @@ -165,6 +165,7 @@ /// calling this function. /// - fixed: If true when anchor is NW or SW, the float window /// would be kept fixed even if the window would be truncated. +/// - hide: If true the floating window will be hidden. /// /// @param[out] err Error details, if any /// @@ -323,6 +324,7 @@ Dictionary nvim_win_get_config(Window window, Error *err) PUT(rv, "focusable", BOOLEAN_OBJ(config->focusable)); PUT(rv, "external", BOOLEAN_OBJ(config->external)); + PUT(rv, "hide", BOOLEAN_OBJ(config->hide)); if (wp->w_floating) { PUT(rv, "width", INTEGER_OBJ(config->width)); @@ -848,6 +850,10 @@ static bool parse_float_config(Dict(float_config) *config, FloatConfig *fconfig, fconfig->fixed = config->fixed; } + if (HAS_KEY_X(config, hide)) { + fconfig->hide = config->hide; + } + return true; #undef HAS_KEY_X } diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h @@ -967,6 +967,7 @@ typedef struct { int footer_width; bool noautocmd; bool fixed; + bool hide; } FloatConfig; #define FLOAT_CONFIG_INIT ((FloatConfig){ .height = 0, .width = 0, \ @@ -977,6 +978,7 @@ typedef struct { .zindex = kZIndexFloatDefault, \ .style = kWinStyleUnused, \ .noautocmd = false, \ + .hide = false, \ .fixed = false }) // Structure to store last cursor position and topline. Used by check_lnums() diff --git a/src/nvim/window.c b/src/nvim/window.c @@ -990,9 +990,13 @@ void ui_ext_win_position(win_T *wp, bool validate) wp->w_grid_alloc.zindex = wp->w_float_config.zindex; if (ui_has(kUIMultigrid)) { String anchor = cstr_as_string((char *)float_anchor_str[c.anchor]); - ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor, - grid->handle, row, col, c.focusable, - wp->w_grid_alloc.zindex); + if (!c.hide) { + ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor, + grid->handle, row, col, c.focusable, + wp->w_grid_alloc.zindex); + } else { + ui_call_win_hide(wp->w_grid_alloc.handle); + } } else { bool valid = (wp->w_redr_type == 0); if (!valid && !validate) { @@ -1014,13 +1018,18 @@ void ui_ext_win_position(win_T *wp, bool validate) } wp->w_winrow = comp_row; wp->w_wincol = comp_col; - ui_comp_put_grid(&wp->w_grid_alloc, comp_row, comp_col, - wp->w_height_outer, wp->w_width_outer, valid, false); - ui_check_cursor_grid(wp->w_grid_alloc.handle); - wp->w_grid_alloc.focusable = wp->w_float_config.focusable; - if (!valid) { - wp->w_grid_alloc.valid = false; - redraw_later(wp, UPD_NOT_VALID); + + if (!c.hide) { + ui_comp_put_grid(&wp->w_grid_alloc, comp_row, comp_col, + wp->w_height_outer, wp->w_width_outer, valid, false); + ui_check_cursor_grid(wp->w_grid_alloc.handle); + wp->w_grid_alloc.focusable = wp->w_float_config.focusable; + if (!valid) { + wp->w_grid_alloc.valid = false; + redraw_later(wp, UPD_NOT_VALID); + } + } else { + ui_comp_remove_grid(&wp->w_grid_alloc); } } } else { diff --git a/test/functional/ui/float_spec.lua b/test/functional/ui/float_spec.lua @@ -1172,14 +1172,14 @@ describe('float window', function() it('return their configuration', function() local buf = meths.create_buf(false, false) local win = meths.open_win(buf, false, {relative='editor', width=20, height=2, row=3, col=5, zindex=60}) - local expected = {anchor='NW', col=5, external=false, focusable=true, height=2, relative='editor', row=3, width=20, zindex=60} + local expected = {anchor='NW', col=5, external=false, focusable=true, height=2, relative='editor', row=3, width=20, zindex=60, hide=false} eq(expected, meths.win_get_config(win)) - eq({relative='', external=false, focusable=true}, meths.win_get_config(0)) + eq({relative='', external=false, focusable=true, hide=false}, meths.win_get_config(0)) if multigrid then meths.win_set_config(win, {external=true, width=10, height=1}) - eq({external=true,focusable=true,width=10,height=1,relative=''}, meths.win_get_config(win)) + eq({external=true,focusable=true,width=10,height=1,relative='',hide=false}, meths.win_get_config(win)) end end) @@ -4281,7 +4281,7 @@ describe('float window', function() | ]]} end - eq({relative='win', width=12, height=1, bufpos={1,32}, anchor='NW', + eq({relative='win', width=12, height=1, bufpos={1,32}, anchor='NW', hide=false, external=false, col=0, row=1, win=firstwin, focusable=true, zindex=50}, meths.win_get_config(win)) feed('<c-e>') @@ -10809,6 +10809,124 @@ describe('float window', function() ]]} end end) + + it('float window with hide option', function() + local buf = meths.create_buf(false,false) + local win = meths.open_win(buf, false, {relative='editor', width=10, height=2, row=2, col=5, hide = true}) + local expected_pos = { + [4]={{id=1001}, 'NW', 1, 2, 5, true}, + } + + if multigrid then + screen:expect{grid=[[ + ## grid 1 + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [3:----------------------------------------]| + ## grid 2 + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + ## grid 3 + | + + ## grid 4 (hidden) + {1: }| + {2:~ }| + ]], float_pos = {}} + else + screen:expect([[ + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]) + end + + meths.win_set_config(win, {hide = false}) + if multigrid then + screen:expect{grid=[[ + ## grid 1 + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [3:----------------------------------------]| + ## grid 2 + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + ## grid 3 + | + + ## grid 4 + {1: }| + {2:~ }| + ]], float_pos = expected_pos} + else + screen:expect([[ + ^ | + {0:~ }| + {0:~ }{1: }{0: }| + {0:~ }{2:~ }{0: }| + {0:~ }| + {0:~ }| + | + ]]) + end + + meths.win_set_config(win, {hide=true}) + if multigrid then + screen:expect{grid=[[ + ## grid 1 + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [2:----------------------------------------]| + [3:----------------------------------------]| + ## grid 2 + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + ## grid 3 + | + + ## grid 4 (hidden) + {1: }| + {2:~ }| + ]], float_pos = {}} + else + screen:expect([[ + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]) + end + end) end describe('with ext_multigrid', function()