hover.nvim icon indicating copy to clipboard operation
hover.nvim copied to clipboard

Allow cursor entering hover window with multiple calls in a row

Open Sleepful opened this issue 1 year ago • 7 comments

A bit of an inverse of this issue: https://github.com/lewis6991/hover.nvim/issues/19

I updated the plugin and calling hover multiple times no longer 'jumps' the cursor into the window. Now instead, I have to use window functions in nvim to get there. I quite liked the "press K twice to jump inside" feature.

:)

Thanks! <3

Sleepful avatar Nov 25 '23 20:11 Sleepful

I think

            preview_window = true,

also broke, not sure

Sleepful avatar Nov 25 '23 20:11 Sleepful

@lewis6991 I see that you marked this as “not planned”. So I am wondering, is there a reliable way of jumping into the Hover window with the cursor?

It seems I need to be inside the window in order to scroll up/down, and the swap-window functions from NVIM are not reliable to jump into the hover window.

Sleepful avatar Nov 27 '23 04:11 Sleepful

I Liked that too, I'm trying to think if there is a good way to get this rolling again, do you know how to get it working, @Sleepful, or @lewis6991?

I Imagine some sort of.. autocmd that happened to set a temporary keymap if a hover was opened, then unset it if the hover closes... it's probably not hard to accomplish..

WillEhrendreich avatar Mar 19 '24 13:03 WillEhrendreich

I added the function below to util.lua:

function M.switch_to_preview()
  local current_win = api.nvim_get_current_win()
  local windows = api.nvim_list_wins()
  local next_win

  -- Find the next window
  for i, win in ipairs(windows) do
    if win == current_win then
      next_win = windows[(i % #windows) + 1]
      break
    end
  end

  -- Switch focus to the next window
  if next_win then
    api.nvim_set_current_win(next_win)
  end
end

When it runs, if there is a hover window open, it switches to it. You can import it and call it like this:

local util = require('hover.util')
util.switch_to_preview()

I do not understand the structure of the entire plugin to know the best way to implement this. However, I did notice that I can include it like below and it switches on the second Shift+k:

local util = require('hover.util')
-- ...
require("hover").register({
  name = "MyPlugin",
  -- ...
  execute = function(opts, done)
    -- ...
    done({ lines = lines, filetype = "markdown"})
    util.switch_to_preview()
  end,
})

I hope this helps you work towards the solution and/or function as a temporary workaround.

Edit: I fixed indentation.

MasterTemple avatar Mar 29 '24 23:03 MasterTemple

Same as #52, maybe you could try the code below.

vim.keymap.set("n", "K", function()
	local api = vim.api
	local hover_win = vim.b.hover_preview
	if hover_win and api.nvim_win_is_valid(hover_win) then
		api.nvim_set_current_win(hover_win)
	else
		require("hover").hover()
	end
end, { desc = "hover.nvim" })


gh-liu avatar Apr 24 '24 02:04 gh-liu

Same as #52, maybe you could try the code below.

vim.keymap.set("n", "K", function()
	local api = vim.api
	local hover_win = vim.b.hover_preview
	if hover_win and api.nvim_win_is_valid(hover_win) then
		api.nvim_set_current_win(hover_win)
	else
		require("hover").hover()
	end
end, { desc = "hover.nvim" })

This seems to work for me, thank you !

pyrho avatar Apr 25 '24 09:04 pyrho

Thank you @gh-liu 🙏

Sleepful avatar Apr 26 '24 23:04 Sleepful