From 6471d96193c74072aef3d4fca753520637dd98f9 Mon Sep 17 00:00:00 2001 From: glepnir Date: Tue, 5 Nov 2024 18:30:17 +0800 Subject: [PATCH] feat(health): support show health in floating window Problem: health can not shown in a floating window Solution: add g:health variable --- runtime/doc/health.txt | 10 ++++++++++ runtime/doc/lsp.txt | 3 ++- runtime/doc/news.txt | 3 ++- runtime/lua/vim/health.lua | 37 ++++++++++++++++++++++++++++++------ runtime/lua/vim/lsp/util.lua | 12 ++++++++---- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/runtime/doc/health.txt b/runtime/doc/health.txt index cb70961b55..ca27cedd2f 100644 --- a/runtime/doc/health.txt +++ b/runtime/doc/health.txt @@ -21,6 +21,16 @@ To run all healthchecks, use: >vim < Plugin authors are encouraged to write new healthchecks. |health-dev| + *g:health* +g:health This global variable controls the behavior and appearance of the + `health` floating window. It should be a dictionary containing the + following optional keys: + - `style`: string? Determines the display style of the `health` window. + Set to `'float'` to enable a floating window. Other + styles are not currently supported. + Example: >vim + let g:health = { 'style': 'float' } + Commands *health-commands* *:che* *:checkhealth* diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt index 7d50cb52eb..afcd93008d 100644 --- a/runtime/doc/lsp.txt +++ b/runtime/doc/lsp.txt @@ -1755,7 +1755,8 @@ Lua module: vim.lsp.util *lsp-util* • {zindex}? (`integer`) override `zindex`, defaults to 50 • {title}? (`string`) • {title_pos}? (`'left'|'center'|'right'`) - • {relative}? (`'mouse'|'cursor'`) (default: `'cursor'`) + • {pos}? (`'center'`) + • {relative}? (`'mouse'|'cursor'|'editor'`) (default: `'cursor'`) • {anchor_bias}? (`'auto'|'above'|'below'`, default: `'auto'`) - "auto": place window based on which side of the cursor has more lines diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index d19df84023..d90f998f0a 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -287,8 +287,9 @@ UI |hl-PmenuSel| and |hl-PmenuMatch| both inherit from |hl-Pmenu|, and |hl-PmenuMatchSel| inherits highlights from both |hl-PmenuSel| and |hl-PmenuMatch|. - • |ui-messages| content chunks now also contain the highlight group ID. +• |health| can be displayed in a floating window and controlled by + the |g:health| variable. ============================================================================== CHANGED FEATURES *news-changed* diff --git a/runtime/lua/vim/health.lua b/runtime/lua/vim/health.lua index 52a7a13966..eabb7e4399 100644 --- a/runtime/lua/vim/health.lua +++ b/runtime/lua/vim/health.lua @@ -11,6 +11,16 @@ --- < --- Plugin authors are encouraged to write new healthchecks. |health-dev| --- +--- *g:health* +--- g:health This global variable controls the behavior and appearance of the +--- `health` floating window. It should be a dictionary containing the +--- following optional keys: +--- - `style`: string? Determines the display style of the `health` window. +--- Set to `'float'` to enable a floating window. Other +--- styles are not currently supported. +--- Example: >vim +--- let g:health = { 'style': 'float' } +--- --- Commands *health-commands* --- --- *:che* *:checkhealth* @@ -331,13 +341,28 @@ function M._check(mods, plugin_names) local emptybuf = vim.fn.bufnr('$') == 1 and vim.fn.getline(1) == '' and 1 == vim.fn.line('$') - -- When no command modifiers are used: - -- - If the current buffer is empty, open healthcheck directly. - -- - If not specified otherwise open healthcheck in a tab. - local buf_cmd = #mods > 0 and (mods .. ' sbuffer') or emptybuf and 'buffer' or 'tab sbuffer' - local bufnr = vim.api.nvim_create_buf(true, true) - vim.cmd(buf_cmd .. ' ' .. bufnr) + if + vim.g.health + and type(vim.g.health) == 'table' + and vim.tbl_get(vim.g.health, 'style') == 'float' + then + local float_bufnr, float_winid = vim.lsp.util.open_floating_preview({}, '', { + height = math.floor(vim.o.lines * 0.8), + width = math.floor(vim.o.columns * 0.8), + pos = 'center', + relative = 'editor', + }) + vim.api.nvim_set_current_win(float_winid) + vim.bo[float_bufnr].modifiable = true + vim.wo[float_winid].list = false + else + -- When no command modifiers are used: + -- - If the current buffer is empty, open healthcheck directly. + -- - If not specified otherwise open healthcheck in a tab. + local buf_cmd = #mods > 0 and (mods .. ' sbuffer') or emptybuf and 'buffer' or 'tab sbuffer' + vim.cmd(buf_cmd .. ' ' .. bufnr) + end if vim.fn.bufexists('health://') == 1 then vim.cmd.bwipe('health://') diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index 6eab0f3da4..cd4cd54c6d 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -875,13 +875,16 @@ function M.make_floating_popup_options(width, height, opts) title_pos = opts.title_pos or 'center' end + local in_center = opts.pos == 'center' return { anchor = anchor, - col = col + (opts.offset_x or 0), + row = in_center and math.floor((vim.o.lines - height) / 2) or row + (opts.offset_y or 0), + col = in_center and math.floor((vim.o.columns - width) / 2) or col + (opts.offset_x or 0), height = height, focusable = opts.focusable, - relative = opts.relative == 'mouse' and 'mouse' or 'cursor', - row = row + (opts.offset_y or 0), + relative = opts.relative == 'mouse' and 'mouse' + or opts.relative == 'editor' and 'editor' + or 'cursor', style = 'minimal', width = width, border = opts.border or default_border, @@ -1491,9 +1494,10 @@ end --- @field zindex? integer override `zindex`, defaults to 50 --- @field title? string --- @field title_pos? 'left'|'center'|'right' +--- @field pos? 'center' --- --- (default: `'cursor'`) ---- @field relative? 'mouse'|'cursor' +--- @field relative? 'mouse'|'cursor'|'editor' --- --- - "auto": place window based on which side of the cursor has more lines --- - "above": place the window above the cursor unless there are not enough lines