-
Notifications
You must be signed in to change notification settings - Fork 1
/
dap-lldb.lua
112 lines (91 loc) · 2.7 KB
/
dap-lldb.lua
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
local M = {}
local function require_dap()
local ok, dap = pcall(require, "dap")
assert(ok, "nvim-dap is required to use dap-lldb")
return dap
end
local function find_codelldb()
local ok, registry = pcall(require, "mason-registry")
if ok and registry.is_installed("codelldb") then
local pkg = registry.get_package("codelldb")
local sep = package.config:sub(1, 1)
return table.concat({ pkg:get_install_path(), "extension", "adapter", "codelldb" }, sep)
end
return nil
end
local function list_targets()
local targets = {}
local command = { "cargo", "build", "--quiet", "--message-format", "json" }
local outputs = vim.fn.systemlist(command)
if vim.v.shell_error ~= 0 then
vim.notify("cargo build failed", vim.log.levels.ERROR)
return targets
end
for _, line in ipairs(outputs) do
local json = vim.fn.json_decode(line)
if
type(json) == "table"
and json.reason == "compiler-artifact"
and json.executable ~= nil
and vim.tbl_contains(json.target.kind, "bin")
then
table.insert(targets, json.executable)
end
end
return targets
end
local function select_target()
local targets = list_targets()
local sep = package.config:sub(1, 1)
if #targets == 0 then
return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. sep, "file")
end
if #targets == 1 then
return targets[1]
end
local options = { "Select a target:" }
for index, target in ipairs(targets) do
local parts = vim.split(target, sep, { trimempty = true })
local option = index .. ". " .. parts[#parts]
table.insert(options, option)
end
local choice = vim.fn.inputlist(options)
return targets[choice]
end
local function read_args()
local args = vim.fn.input("Enter args: ")
return vim.split(args, " ", { trimempty = true })
end
function M.setup(opts)
local dap = require_dap()
local codelldb = opts.codelldb_path or find_codelldb() or "codelldb"
dap.adapters.codelldb = {
type = "server",
port = "${port}",
executable = {
command = codelldb,
args = { "--port", "${port}" },
detached = vim.loop.os_uname().sysname ~= "Windows",
},
}
dap.configurations.rust = {
{
name = "Debug",
type = "codelldb",
request = "launch",
cwd = "${workspaceFolder}",
program = select_target,
stopOnEntry = false,
},
{
name = "Debug w/ args",
type = "codelldb",
request = "launch",
cwd = "${workspaceFolder}",
program = select_target,
args = read_args,
stopOnEntry = false,
},
}
end
return M