lewis6991/hover.nvim

Support position other than keyboard cursor position

airtonix opened this issue · 2 comments

hover and hover_select should not assume they are using the current keyboard cursor position as the "hover" position.

Neovim now supports a trivial way to know the mouse cursor position (I'm using alacritty and gnome-terminal):

Currently I'm using this:

return {
    "lewis6991/hover.nvim",
    config = function()
        require("hover").setup {
            init = function()
                -- Require providers
                require("hover.providers.lsp")
                -- require('hover.providers.gh')
                -- require('hover.providers.gh_user')
                -- require('hover.providers.jira')
                -- require('hover.providers.man')
                -- require('hover.providers.dictionary')
            end,
            preview_opts = {
                border = nil
            },
            -- Whether the contents of a currently open hover window should be moved
            -- to a :h preview-window when pressing the hover keymap.
            preview_window = false,
            title = true
        }

        local hover_time = 500
        local hover_timer = nil
        local mouse_position = nil

        vim.o.mousemoveevent = true

        vim.keymap.set({ '', 'i' }, '<MouseMove>', function()
            if hover_timer then
                hover_timer:close()
            end
            hover_timer = vim.defer_fn(function()
                mouse_position = vim.fn.getmousepos()
                hover_timer = nil
                require('notify').notify(vim.inspect(mouse_position))
                require("hover").hover()
            end, hover_time)
            return '<MouseMove>'
        end, { expr = true })

    end

Which ends up lookin like this:

Screencast.from.2023-02-27.12-08-24.webm
  • The screenrecording in gnome-shell doesn't capture the mousecursor for somereason that's why i'm using the a11y ping thing... everytime you see a blip i'm hittin the ctrl key to show where the mouse cursor is.
  • when the mouse stops moving, you'll see a notification popup

I got some of the way with default lsp hover:

return {
    "lewis6991/hover.nvim",
    config = function()
        require("hover").setup {
            init = function()
                -- Require providers
                require("hover.providers.lsp")
                require('hover.providers.gh')
                require('hover.providers.gh_user')
                -- require('hover.providers.jira')
                require('hover.providers.man')
                require('hover.providers.dictionary')
            end,
            preview_opts = {
                border = nil
            },
            -- Whether the contents of a currently open hover window should be moved
            -- to a :h preview-window when pressing the hover keymap.
            preview_window = false,
            title = true
        }

        local hover_time = 500
        local hover_timer = nil
        local mouse_position = nil

        vim.o.mousemoveevent = true

        vim.keymap.set({ '', 'i' }, '<MouseMove>', function()
            if hover_timer then
                hover_timer:close()
            end
            hover_timer = vim.defer_fn(function()
                mouse_position = vim.fn.getmousepos()
                hover_timer = nil
                local row, col = unpack(vim.api.nvim_win_get_cursor(0))

                print(string.format(
                    "keyboard [ %d/%d ] mouse [ %d/%d win: %d/%d screen: %d/%d ]",
                    row, col,
                    mouse_position.line, mouse_position.column,
                    mouse_position.winrow, mouse_position.wincol,
                    mouse_position.screenrow, mouse_position.screencol
                ))
                vim.lsp.buf_request(0,
                    "textDocument/hover",
                    {
                        textDocument = vim.lsp.util.make_text_document_params(
                            vim.api.nvim_win_get_buf(0)
                        ),
                        position = {
                            -- make tip of mouse cursor the position
                            line =  mouse_position.line - 1,
                            character = mouse_position.column
                        }
                    }
                )
            end, hover_time)
            return '<MouseMove>'
        end, { expr = true })

    end

Mouse support is added. But invoking this with a keybind will show the hover on the cursor.