Skip to content

Commit

Permalink
ui: improved hover popup
Browse files Browse the repository at this point in the history
this commit creates a custom hover handler which floats above all other
windows when trigged from the calltree ui.

Signed-off-by: ldelossa <[email protected]>
  • Loading branch information
ldelossa committed Nov 19, 2021
1 parent 0a3b32e commit 8827b06
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lua/calltree/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local help_buf = require('calltree.ui.help_buffer')
local marshal = require('calltree.ui.marshal')
local jumps = require('calltree.ui.jumps')
local deets = require('calltree.ui.details')
local hover = require('calltree.ui.hover')

local M = {}

Expand Down Expand Up @@ -274,6 +275,7 @@ end
-- hover will show LSP hover information for the symbol
-- under the cursor.
M.hover = function()
ui_buf.close_all_popups()
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
if node == nil then
Expand All @@ -288,12 +290,13 @@ M.hover = function()
character = node.call_hierarchy_obj.range.start.character
}
}
lsp_util.multi_client_request(M.active_lsp_clients, "textDocument/hover", params, nil, M.buffer_handle)
lsp_util.multi_client_request(M.active_lsp_clients, "textDocument/hover", params, hover.hover_handler, M.buffer_handle)
end

-- details opens a popup window for the given symbol
-- showing more information.
M.details = function()
ui_buf.close_all_popups()
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
if node == nil then
Expand Down
7 changes: 6 additions & 1 deletion lua/calltree/ui/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ local direction_map = {
to = {method="callHierarchy/outgoingCalls", buf_name="outgoingCalls"}
}

function M.close_all_popups()
require('calltree.ui.hover').close_hover_popup()
require('calltree.ui.details').close_details_popup()
end

-- _setup_buffer performs an idempotent creation
-- of the calltree buffer
--
Expand Down Expand Up @@ -45,7 +50,7 @@ function M._setup_buffer(direction, buffer_handle)
-- au to clear highlights on window close
vim.cmd("au BufWinLeave <buffer=" .. buffer_handle .. "> lua require('calltree.ui.jumps').set_jump_hl(false)")
-- au to close popup with cursor moves or buffer is closed.
vim.cmd("au CursorMoved,BufWinLeave,WinLeave <buffer=" .. buffer_handle .. "> lua require('calltree.ui.details').close_details_popup()")
vim.cmd("au CursorMoved,BufWinLeave,WinLeave <buffer=" .. buffer_handle .. "> lua require('calltree.ui.buffer').close_all_popups()")

-- set buffer local keymaps
local opts = {silent=true}
Expand Down
71 changes: 71 additions & 0 deletions lua/calltree/ui/hover.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
local M = {}

local float_win = nil

-- close_hover_popups closes the created popup window
-- if it exists.
function M.close_hover_popup()
if float_win ~= nil and
vim.api.nvim_win_is_valid(float_win) then
vim.api.nvim_win_close(float_win, true)
float_win = nil
end
end

-- modified from neovim runtime/lua/vim/lsp/handlers.lua
-- function conforms to client LSP handler signature.
function M.hover_handler(_, result, ctx, config)
M.close_hover_popup()
-- get lines from result
config = config or {}
config.focus_id = ctx.method
if not (result and result.contents) then
-- return { 'No information available' }
return
end
local lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
lines = vim.lsp.util.trim_empty_lines(lines)
if vim.tbl_isempty(lines) then
-- return { 'No information available' }
return
end

-- create buffer for popup
local buf = vim.api.nvim_create_buf(false, false)
if buf == 0 then
vim.api.nvim_err_writeln("details_popup: could not create details buffer")
return
end
vim.api.nvim_buf_set_option(buf, 'bufhidden', 'delete')
vim.api.nvim_buf_set_option(buf, 'syntax', 'markdown')
vim.api.nvim_buf_set_option(buf, 'filetype', 'markdown')

lines = vim.lsp.util.stylize_markdown(buf, lines, {})

local width = 20
for _, line in ipairs(lines) do
local line_width = vim.fn.strdisplaywidth(line)
if line_width > width then
width = line_width
end
end

vim.api.nvim_buf_set_option(buf, 'modifiable', true)
vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines)
vim.api.nvim_buf_set_option(buf, 'modifiable', false)
local popup_conf = vim.lsp.util.make_floating_popup_options(
width,
#lines,
{
border= "rounded",
focusable= false,
zindex = 99,
}
)
float_win = vim.api.nvim_open_win(buf, false, popup_conf)

return float_win
end


return M

0 comments on commit 8827b06

Please sign in to comment.