folke / noice.nvim

💥 Highly experimental plugin that completely replaces the UI for messages, cmdline and the popupmenu.
Apache License 2.0
4.36k stars 101 forks source link

bug: `focusable = false` not working for LSP hover #698

Closed kristijanhusak closed 9 months ago

kristijanhusak commented 9 months ago

Did you check docs and existing issues?

Neovim version (nvim -v)

v0.10.0-dev-2121+g06aebfa99

Operating system/version

Arch linux

Describe the bug

focusable = false option for LSP hover is not working (Docs link: https://neovim.io/doc/user/lsp.html#vim.lsp.util.open_floating_preview()) . It keeps focusing the hover when I press the trigger key twice.

Steps To Reproduce

  1. Use the min repro provided
  2. Open the repro file itself nvim -u repro.lua repro.lua
  3. Wait for lsp to load, go to line 43 on colorscheme, press <Space>
  4. Press <space> again, it focuses the hover

Expected Behavior

Expected to not focus the hover since it's disabled via options. These are the steps to check how it works with built in settings:

  1. Uncomment lines 28 and 49-50 in provided repro
  2. Open the repro file itself nvim -u repro.lua repro.lua
  3. Wait for lsp to load, go to line 43 on colorscheme, press <Space>
  4. Press <space> again, it doesn't focus the hover window

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify('./.repro', ':p')

-- set stdpaths to use .repro
for _, name in ipairs({ 'config', 'data', 'state', 'cache' }) do
  vim.env[('XDG_%s_HOME'):format(name:upper())] = root .. '/' .. name
end

-- bootstrap lazy
local lazypath = root .. '/plugins/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ 'git', 'clone', '--filter=blob:none', 'https://github.com/folke/lazy.nvim.git', lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  'folke/tokyonight.nvim',
  {
    'folke/noice.nvim',
    dependencies = {
      'MunifTanjim/nui.nvim',
    },
    opts = {
      lsp = {
        hover = {
          -- Uncomment this to make it work
          -- enabled = false,
          opts = {
            focusable = false,
          },
        },
      },
    },
  },
  'neovim/nvim-lspconfig',
  -- add any other plugins here
}
require('lazy').setup(plugins, {
  root = root .. '/plugins',
})

vim.cmd.colorscheme('tokyonight')
require('lspconfig').lua_ls.setup({})

vim.keymap.set('n', '<space>', vim.lsp.buf.hover)

-- Uncomment this to make it work
-- vim.lsp.handlers['textDocument/hover'] =
--   vim.lsp.with(vim.lsp.handlers.hover, { focusable = false })
folke commented 9 months ago

focusable is just an option for the window, and has nothing to do with the feature that pressing twice focuses the window

kristijanhusak commented 9 months ago

How can I pass that option to the window that noice uses?

rishyramen commented 4 months ago

I came upon this issue since I wanted to toggle the Noice hovers instead of letting them auto open. Noice already has a feature to let you scroll through the hovers without focusing so I wanted a double tap of the hover key to close the hover. Some google searching led me to https://vi.stackexchange.com/questions/37225/how-do-i-close-a-hovered-window-with-lsp-information-escape-does-not-work

So if anyone is interested here is the solution adapted to Noice

vim.keymap.set({ 'n' }, 'K', function()
    local base_win_id = vim.api.nvim_get_current_win()
    local windows = vim.api.nvim_tabpage_list_wins(0)
    for _, win_id in ipairs(windows) do
        if win_id ~= base_win_id then
            local win_cfg = vim.api.nvim_win_get_config(win_id)
            if win_cfg.relative == "win" and win_cfg.win == base_win_id then
                require("noice.lsp.docs").hide(require("noice.lsp.docs").get("hover"))
                return
            end
        end
    end
    require("noice.lsp").hover()
end , { remap = false, silent = true, buffer = event.buf })

You can also apply this to signature help (substitute hover with signature in the code) but if you are using nvim-cmp you will need to lower the nvim-cmp window's z-index.