|
| 1 | +local lsp_util = require('calltree.lsp.util') |
| 2 | + |
| 3 | +local M = {} |
| 4 | + |
| 5 | +local direction_map = { |
| 6 | + from = "Incoming Calls: ", |
| 7 | + to = "Outgoing Calls: " |
| 8 | +} |
| 9 | + |
| 10 | +local float_win = nil |
| 11 | + |
| 12 | +-- close_details_popups closes the created popup window |
| 13 | +-- if it exists. |
| 14 | +function M.close_details_popup() |
| 15 | + if float_win ~= nil and |
| 16 | + vim.api.nvim_win_is_valid(float_win) then |
| 17 | + vim.api.nvim_win_close(float_win, true) |
| 18 | + float_win = nil |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +-- details_popup creates a popup window showing futher details |
| 23 | +-- about a symbol. |
| 24 | +-- |
| 25 | +-- node : tree.Node - the node to show details for |
| 26 | +-- |
| 27 | +-- direction : string - the current direction of the call tree |
| 28 | +-- must be "to" or "from" |
| 29 | +-- |
| 30 | +-- calltree_buffer : buffer_handle - the buffer_handle for the |
| 31 | +-- current calltree. |
| 32 | +function M.details_popup(node, direction, calltree_buffer) |
| 33 | + local buf = vim.api.nvim_create_buf(false, false) |
| 34 | + if buf == 0 then |
| 35 | + vim.api.nvim_err_writeln("details_popup: could not create details buffer") |
| 36 | + return |
| 37 | + end |
| 38 | + vim.api.nvim_buf_set_option(buf, 'bufhidden', 'delete') |
| 39 | + vim.api.nvim_buf_set_option(buf, 'syntax', 'yaml') |
| 40 | + |
| 41 | + local lines = {} |
| 42 | + table.insert(lines, "Symbol Details") |
| 43 | + table.insert(lines, "==============") |
| 44 | + table.insert(lines, "Name: " .. node.name) |
| 45 | + table.insert(lines, "Kind: " .. vim.lsp.protocol.SymbolKind[node.kind]) |
| 46 | + |
| 47 | + if node.expanded then |
| 48 | + table.insert(lines, direction_map[direction] .. #node.children) |
| 49 | + end |
| 50 | + |
| 51 | + if node.references ~= nil then |
| 52 | + table.insert(lines, "References: " .. #node.references) |
| 53 | + end |
| 54 | + table.insert(lines, "File: " .. lsp_util.relative_path_from_uri(node.call_hierarchy_obj.uri)) |
| 55 | + table.insert(lines, "Details: " .. node.call_hierarchy_obj.detail) |
| 56 | + |
| 57 | + local width = 20 |
| 58 | + for _, line in ipairs(lines) do |
| 59 | + local line_width = vim.fn.strdisplaywidth(line) |
| 60 | + if line_width > width then |
| 61 | + width = line_width |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + vim.api.nvim_buf_set_option(buf, 'modifiable', true) |
| 66 | + vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines) |
| 67 | + vim.api.nvim_buf_set_option(buf, 'modifiable', false) |
| 68 | + local popup_conf = vim.lsp.util.make_floating_popup_options( |
| 69 | + width, |
| 70 | + #lines, |
| 71 | + { |
| 72 | + border= "rounded", |
| 73 | + focusable= false, |
| 74 | + zindex = 99, |
| 75 | + } |
| 76 | + ) |
| 77 | + float_win = vim.api.nvim_open_win(buf, false, popup_conf) |
| 78 | + vim.api.nvim_win_set_option(float_win, 'winhighlight', 'NormalFloat:Normal') |
| 79 | +end |
| 80 | + |
| 81 | +return M |
0 commit comments