From c97ad3cb41f1944fbfc6af1b5b6a86a5827acf75 Mon Sep 17 00:00:00 2001 From: "neovim-backports[bot]" <175700243+neovim-backports[bot]@users.noreply.github.com> Date: Tue, 8 Jul 2025 23:10:18 +0000 Subject: [PATCH] fix(health): floating window closes when opening TOC (gO) fix(health): floating window closes when opening TOC (gO) #34794 Problem: Health check floating window gets closed when pressing 'gO' to show TOC because LSP floating preview system auto-closes on BufEnter events triggered by :lopen. Solution: Temporarily disable BufEnter event for the current window during TOC operations and adjust window layout to prevent overlap. (cherry picked from commit 28b7c2df521744347e3286927af13fd8421f0ea1) Co-authored-by: glepnir --- runtime/ftplugin/checkhealth.lua | 2 +- runtime/lua/vim/health.lua | 6 ++++-- runtime/lua/vim/lsp/util.lua | 11 +++++++++-- runtime/lua/vim/treesitter/_headings.lua | 5 +++-- test/functional/plugin/health_spec.lua | 13 ++++++++++++- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/runtime/ftplugin/checkhealth.lua b/runtime/ftplugin/checkhealth.lua index 1ceee7cfc3..818871c3d7 100644 --- a/runtime/ftplugin/checkhealth.lua +++ b/runtime/ftplugin/checkhealth.lua @@ -1,5 +1,5 @@ vim.keymap.set('n', 'gO', function() - require('vim.treesitter._headings').show_toc() + require('vim.treesitter._headings').show_toc(6) end, { buffer = 0, silent = true, desc = 'Show an Outline of the current buffer' }) vim.keymap.set('n', ']]', function() diff --git a/runtime/lua/vim/health.lua b/runtime/lua/vim/health.lua index 4646d9d038..5ca465aa82 100644 --- a/runtime/lua/vim/health.lua +++ b/runtime/lua/vim/health.lua @@ -389,15 +389,17 @@ function M._check(mods, plugin_names) and type(vim.g.health) == 'table' and vim.tbl_get(vim.g.health, 'style') == 'float' then - local max_height = math.floor(vim.o.lines * 0.8) + local available_lines = vim.o.lines - 12 + local max_height = math.min(math.floor(vim.o.lines * 0.8), available_lines) local max_width = 80 local float_winid bufnr, float_winid = vim.lsp.util.open_floating_preview({}, '', { height = max_height, width = max_width, offset_x = math.floor((vim.o.columns - max_width) / 2), - offset_y = math.floor((vim.o.lines - max_height) / 2) - 1, + offset_y = math.floor((available_lines - max_height) / 2), relative = 'editor', + close_events = {}, }) vim.api.nvim_set_current_win(float_winid) vim.bo[bufnr].modifiable = true diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index 84c836a6b9..f56fe9207e 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -1349,10 +1349,17 @@ local function close_preview_autocmd(events, winnr, bufnrs) -- close the preview window when entered a buffer that is not -- the floating window buffer or the buffer that spawned it - api.nvim_create_autocmd('BufEnter', { + api.nvim_create_autocmd('BufLeave', { group = augroup, + buffer = bufnrs[1], callback = function() - close_preview_window(winnr, bufnrs) + vim.schedule(function() + -- When jumping to the quickfix window from the preview window, + -- do not close the preview window. + if api.nvim_get_option_value('filetype', { buf = 0 }) ~= 'qf' then + close_preview_window(winnr, bufnrs) + end + end) end, }) diff --git a/runtime/lua/vim/treesitter/_headings.lua b/runtime/lua/vim/treesitter/_headings.lua index 7cd9dfa4b0..bc37131b04 100644 --- a/runtime/lua/vim/treesitter/_headings.lua +++ b/runtime/lua/vim/treesitter/_headings.lua @@ -83,8 +83,9 @@ local get_headings = function(bufnr) return headings end +--- @param qf_height? integer height of loclist window --- Shows an Outline (table of contents) of the current buffer, in the loclist. -function M.show_toc() +function M.show_toc(qf_height) local bufnr = api.nvim_get_current_buf() local bufname = api.nvim_buf_get_name(bufnr) local headings = get_headings(bufnr) @@ -102,7 +103,7 @@ function M.show_toc() end vim.fn.setloclist(0, headings, ' ') vim.fn.setloclist(0, {}, 'a', { title = 'Table of contents' }) - vim.cmd.lopen() + vim.cmd.lopen({ count = qf_height }) vim.w.qf_toc = bufname -- reload syntax file after setting qf_toc variable vim.bo.filetype = 'qf' diff --git a/test/functional/plugin/health_spec.lua b/test/functional/plugin/health_spec.lua index 0a1db297c2..7e4118e8e0 100644 --- a/test/functional/plugin/health_spec.lua +++ b/test/functional/plugin/health_spec.lua @@ -68,7 +68,10 @@ describe(':checkhealth', function() end) it('vim.g.health', function() - clear() + clear { + args_rm = { '-u' }, + args = { '--clean', '+set runtimepath+=test/functional/fixtures' }, + } command("let g:health = {'style':'float'}") command('checkhealth lsp') eq( @@ -77,6 +80,14 @@ describe(':checkhealth', function() return vim.api.nvim_win_get_config(0).relative ]]) ) + + -- gO should not close the :checkhealth floating window. #34784 + command('checkhealth full_render') + local win = api.nvim_get_current_win() + api.nvim_win_set_cursor(win, { 5, 1 }) + n.feed('gO') + eq(true, api.nvim_win_is_valid(win)) + eq('qf', api.nvim_get_option_value('filetype', { buf = 0 })) end) it("vim.provider works with a misconfigured 'shell'", function()