Files
2026-05-07 02:06:34 +02:00

129 lines
3.3 KiB
Lua

local Item = require("dap.item")
local Node = require("dap.hover.node")
local Window = require("dap.hover.window")
local log = require("log")
---@async
local function hover_async()
local dap = require("dap")
local session = dap.session()
if not session then
return
end
if Window.focus_if_shown() then
return
end
local capabilities = session.capabilities or {}
local supports_hover = capabilities.supportsEvaluateForHovers
if not supports_hover then
log.warning("Hover is not supported by this adapter")
return
end
local cursor_pos = vim.api.nvim_win_get_cursor(0)
local line_nr = cursor_pos[1] -- nvim-dap sets linesStartAt1=true
local col_nr = cursor_pos[2] + 1 -- nvim-dap sets columnsStartAt1=true
local current_file = vim.api.nvim_buf_get_name(0)
local expr
local mode = vim.api.nvim_get_mode()
if mode.mode == "v" then
local start_pos = vim.fn.getpos("v")
local end_pos = vim.fn.getpos(".")
local start_row, start_col = start_pos[2], start_pos[3]
local end_row, end_col = end_pos[2], end_pos[3]
if start_row == end_row and end_col < start_col then
start_col, end_col = end_col, start_col
elseif end_row < start_row then
start_row, end_row = end_row, start_row
start_col, end_col = end_col, start_col
end
local lines = vim.api.nvim_buf_get_text(
0,
start_row - 1,
start_col - 1,
end_row - 1,
end_col,
{}
)
expr = table.concat(lines, "\n")
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes("<ESC>", true, false, true),
"n",
false
)
else
expr = vim.fn.expand("<cexpr>") --[[@as string]]
end
if expr == "" then
return
end
local thread_id
do
local err, resp = session:request("threads", nil)
if err then
log.warning("Failed to get threads: %s", err)
end
if err or not resp or #resp.threads == 0 then
return
end
thread_id = resp.threads[1].id
end
local frame_id
do
local err, resp =
session:request("stackTrace", { threadId = thread_id })
if err then
log.warning("Failed to get stack trace: %s", err)
end
if err or not resp or #resp.stackFrames == 0 then
return
end
frame_id = resp.stackFrames[1].id
end
local request = {
expression = expr,
frameId = frame_id,
context = "hover",
line = line_nr,
column = col_nr,
source = {
path = current_file,
},
}
local err, resp = session:request("evaluate", request)
if err then
log.warning("Failed to evaluate '%s': %s", expr, err)
end
if err or not resp then
return
end
local item = Item.new(expr, resp.type, resp.result, resp.variablesReference)
local root = Node.new(item, nil, session.filetype)
root:load_children(session)
Window.show(session, root)
end
local function hover()
coroutine.wrap(function()
local ok, err = xpcall(hover_async, debug.traceback)
if not ok then
log.error("Hover failed:\n%s", err)
end
end)()
end
return hover