all repos

init.lua @ main

my nvim config

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
local dlv = {}
local cache = {
  pane_id = nil,
  signs = {},
}

vim.fn.sign_define("Breakpoint", { text = "b", texthl = "Error" })

local function get_dlv_pane_id()
  if cache.pane_id then
    return cache.pane_id
  end

  local res = vim
    .system({
      "tmux",
      "list-panes",
      "-s",
      "-F",
      "#{pane_id}:#{pane_current_command}",
    }, { text = true })
    :wait()

  if res.code ~= 0 then
    vim.notify("Failed to list tmux panes", vim.log.levels.ERROR)
    return
  end

  for line in res.stdout:gmatch "[^\n]+" do
    local pane_id, command = line:match "([^:]+):(.*)"
    if command and command:match "^dlv" then
      cache.pane_id = pane_id
      return pane_id
    end
  end

  vim.notify("No dlv session found", vim.log.levels.ERROR)
end

function dlv.bset()
  local line = vim.fn.line "."

  -- send breakpoint
  local cmd = string.format("break %s:%d", vim.fn.expand "%:p", line) 
  vim.system { "tmux", "send-keys", "-t", get_dlv_pane_id(), cmd, "Enter" }

  -- 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