fix(dap.hover): more cleanup

This commit is contained in:
2025-09-27 02:29:34 +02:00
parent 3456dcc47c
commit b91ab499de
5 changed files with 37 additions and 50 deletions
+18 -23
View File
@@ -10,18 +10,11 @@ local log = require("ow.log")
---@param line_nr integer
---@param col_nr integer
---@param current_file string
local function hover_eval(
expr,
session,
frame_id,
line_nr,
col_nr,
current_file
)
local function eval(expr, session, frame_id, line_nr, col_nr, current_file)
local win = Window.get_instance()
win:close()
local eval_request = {
local request = {
expression = expr,
frameId = frame_id,
context = "hover",
@@ -32,23 +25,21 @@ local function hover_eval(
},
}
local err, resp = session:request("evaluate", eval_request)
if err or not resp then
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, 0)
local tree = Tree.new(session)
local item = Item.new(expr, resp.type, resp.result, resp.variablesReference)
win.tree = Tree.new(session)
tree:build(item)
local content = tree:render()
local lines = content:get_lines()
win.tree:build(item)
local content = win.tree:render()
win.tree = tree
win:show(lines, content)
win:show(content)
end
---@async
@@ -119,8 +110,10 @@ local function hover_async()
local thread_id
do
local err, resp = session:request("threads", nil)
if err or not resp or #resp.threads == 0 then
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
@@ -130,14 +123,16 @@ local function hover_async()
do
local err, resp =
session:request("stackTrace", { threadId = thread_id })
if err or not resp or #resp.stackFrames == 0 then
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
hover_eval(expr, session, frame_id, line_nr, col_nr, current_file)
eval(expr, session, frame_id, line_nr, col_nr, current_file)
end
local function hover()
+1 -1
View File
@@ -108,7 +108,7 @@ end
---@return string[]
function Content:get_lines()
return vim.split(self.text, "\n", { trimempty = true })
return vim.split(vim.trim(self.text), "\n")
end
---@param ns_id integer
+10 -15
View File
@@ -1,5 +1,7 @@
local Content = require("ow.dap.hover.content")
local Item = require("ow.dap.item")
local Node = require("ow.dap.hover.node")
local log = require("ow.log")
---@class ow.dap.hover.Tree
---@field session dap.Session
@@ -12,9 +14,7 @@ Tree.__index = Tree
function Tree.new(session)
return setmetatable({
session = session,
root_node = nil,
line_to_node = {},
extmark_to_node = {},
root = nil,
}, Tree)
end
@@ -40,25 +40,20 @@ function Tree:load_children(node)
local err, resp = self.session:request("variables", {
variablesReference = node.item.variablesReference,
})
if err then
log.warning("Failed to get variables for %s: %s", node.item.name, err)
end
if err or not resp or #resp.variables == 0 then
return false
end
for i, var in ipairs(resp.variables) do
local child_item = {
name = var.name,
type = var.type,
value = var.value,
variablesReference = var.variablesReference,
depth = node.item.depth + 1,
}
local child = Node.new(child_item, node)
local item = Item.from_var(var)
local child = Node.new(item, node)
child.is_last_child = (i == #resp.variables)
if child_item.name:match("^%d+$") then
child_item.name = "[" .. child_item.name .. "]"
if item.name:match("^%d+$") then
item.name = "[" .. item.name .. "]"
end
table.insert(node.children, child)
+5 -3
View File
@@ -42,9 +42,11 @@ function Window:close()
vim.api.nvim_del_augroup_by_id(self.augroup)
end
self.augroup = nil
self.max_width = nil
self.max_height = nil
self.winid = nil
self.bufnr = nil
self.augroup = nil
self.tree = nil
end
@@ -69,12 +71,12 @@ function Window:compute_height()
return math.min(self.max_height or text_height, text_height)
end
---@param lines string[]
---@param content ow.dap.hover.Content
function Window:show(lines, content)
function Window:show(content)
local prev_buf = vim.api.nvim_get_current_buf()
self.bufnr = vim.api.nvim_create_buf(false, true)
local lines = content:get_lines()
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
vim.bo[self.bufnr].modifiable = false
+3 -8
View File
@@ -3,7 +3,6 @@
---@field type string
---@field value string
---@field variablesReference? number
---@field depth integer
local Item = {}
Item.__index = Item
@@ -11,28 +10,24 @@ Item.__index = Item
---@param type string
---@param value string
---@param variablesReference? number
---@param depth integer
---@return ow.dap.Item
function Item.new(name, type, value, variablesReference, depth)
function Item.new(name, type, value, variablesReference)
return setmetatable({
name = name,
type = type,
value = value,
variablesReference = variablesReference,
depth = depth,
}, Item)
end
---@param var dap.Variable
---@param depth integer
---@return ow.dap.Item
function Item.from_var(var, depth)
function Item.from_var(var)
return Item.new(
var.name,
var.type,
var.value,
var.variablesReference,
depth
var.variablesReference
)
end