|
| 1 | +local M = {} |
| 2 | + |
| 3 | +local float_win = nil |
| 4 | + |
| 5 | +-- close_hover_popups closes the created popup window |
| 6 | +-- if it exists. |
| 7 | +function M.close_hover_popup() |
| 8 | + if float_win ~= nil and |
| 9 | + vim.api.nvim_win_is_valid(float_win) then |
| 10 | + vim.api.nvim_win_close(float_win, true) |
| 11 | + float_win = nil |
| 12 | + end |
| 13 | +end |
| 14 | + |
| 15 | +-- modified from neovim runtime/lua/vim/lsp/handlers.lua |
| 16 | +-- function conforms to client LSP handler signature. |
| 17 | +function M.hover_handler(_, result, ctx, config) |
| 18 | + M.close_hover_popup() |
| 19 | + -- get lines from result |
| 20 | + config = config or {} |
| 21 | + config.focus_id = ctx.method |
| 22 | + if not (result and result.contents) then |
| 23 | + -- return { 'No information available' } |
| 24 | + return |
| 25 | + end |
| 26 | + local lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents) |
| 27 | + lines = vim.lsp.util.trim_empty_lines(lines) |
| 28 | + if vim.tbl_isempty(lines) then |
| 29 | + -- return { 'No information available' } |
| 30 | + return |
| 31 | + end |
| 32 | + |
| 33 | + -- create buffer for popup |
| 34 | + local buf = vim.api.nvim_create_buf(false, false) |
| 35 | + if buf == 0 then |
| 36 | + vim.api.nvim_err_writeln("details_popup: could not create details buffer") |
| 37 | + return |
| 38 | + end |
| 39 | + vim.api.nvim_buf_set_option(buf, 'bufhidden', 'delete') |
| 40 | + vim.api.nvim_buf_set_option(buf, 'syntax', 'markdown') |
| 41 | + vim.api.nvim_buf_set_option(buf, 'filetype', 'markdown') |
| 42 | + |
| 43 | + lines = vim.lsp.util.stylize_markdown(buf, lines, {}) |
| 44 | + |
| 45 | + local width = 20 |
| 46 | + for _, line in ipairs(lines) do |
| 47 | + local line_width = vim.fn.strdisplaywidth(line) |
| 48 | + if line_width > width then |
| 49 | + width = line_width |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + vim.api.nvim_buf_set_option(buf, 'modifiable', true) |
| 54 | + vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines) |
| 55 | + vim.api.nvim_buf_set_option(buf, 'modifiable', false) |
| 56 | + local popup_conf = vim.lsp.util.make_floating_popup_options( |
| 57 | + width, |
| 58 | + #lines, |
| 59 | + { |
| 60 | + border= "rounded", |
| 61 | + focusable= false, |
| 62 | + zindex = 99, |
| 63 | + } |
| 64 | + ) |
| 65 | + float_win = vim.api.nvim_open_win(buf, false, popup_conf) |
| 66 | + |
| 67 | + return float_win |
| 68 | +end |
| 69 | + |
| 70 | + |
| 71 | +return M |
0 commit comments