init.lua/lua/scratch/dlv.lua(view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
local dlv = {}
local cache = {
tab_id = nil,
signs = {},
}
vim.fn.sign_define("Breakpoint", { text = "b", texthl = "Error" })
local function kitty(args)
local res = vim.system({ "kitty", "@", unpack(args) }, { text = true }):wait()
if res.code ~= 0 then
vim.notify("failed to kitty @ " .. vim.inspect(args), vim.log.levels.ERROR)
end
return res
end
local function get_dlv_tab_id()
if cache.tab_id then
return cache.tab_id
end
local res = kitty { "ls" }
local out = vim.json.decode(res.stdout)
for _, tab in ipairs(out) do
for _, win in ipairs(tab.tabs or {}) do
for _, w in ipairs(win.windows or {}) do
if
w.title:match "^dlv connect"
or w.title:match "^dlv debug"
or w.title:match "^dlv test"
then
cache.tab_id = w.id
return w.id
end
end
end
end
vim.notify("it seems like there's no dlv running", vim.log.levels.ERROR)
end
function dlv.bset()
local line = vim.fn.line "."
-- send breakpoint
local tid = get_dlv_tab_id()
local cmd = string.format("break %s:%d\n", vim.fn.expand "%:t", line)
kitty { "send-text", "-m", "id:" .. tid, cmd }
-- set sign
local fullpath = vim.fn.expand "%:p"
local sid = tonumber(string.format("%d%04d", vim.fn.bufnr(), line)) --[[ @as number]]
vim.fn.sign_place(sid, "Breakpoints", "Breakpoint", fullpath, { lnum = line })
table.insert(cache.signs, sid)
end
function dlv.clear()
for _, sid in ipairs(cache.signs) do
vim.fn.sign_unplace("Breakpoints", { id = sid })
end
cache.signs = {}
cache.tab_id = nil
vim.notify("Cleared all breakpoints.", vim.log.levels.INFO)
end
return dlv
|