summaryrefslogtreecommitdiffstats
path: root/plugin/50-dap.lua
blob: 4f07214d1ce06de0a77fc41782c8f0a42f7fc0c9 (plain)
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
--[[ 50-dap.lua — debug Lua running inside Neovim via OSV (one-small-step-for-vimkind).

Wires nvim-dap to the OSV adapter so the running Neovim instance can be attached to and debugged.

User commands:
  `OSVLaunch`:   start the OSV server (on the debuggee)
  `OSVStatus`:   show the status of the OSV server and client
]]

local dap = require("dap")
local osv = require("osv")

-- Port shared by the OSV server and the dap attach configuration.
local OSV_PORT = 8086

----------------------------------------------------------------------------------------------------
-- OSV (Lua inside Neovim) -------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

-- Start the OSV server so the running Neovim instance can be attached to.
local function osv_launch()
	osv.launch({ port = OSV_PORT })
end

-- Report whether the OSV server is running and whether a client is attached.
local function osv_status()
	vim.notify("OSV server running: " .. tostring(osv.is_running()))
	vim.notify("OSV client attached: " .. tostring(osv.is_attached()))
end

----------------------------------------------------------------------------------------------------
-- Configuration & commands ------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

--[[ The cast restores dap.configurations' own type: with fzf-lua in the lua_ls library its dap
provider cross-merges a `configurations` field, which otherwise mistypes this as a function. ]]
local configurations = dap.configurations --[[@as table<string, dap.Configuration[]>]]
configurations.lua = {
	{
		type = "nlua",
		request = "attach",
		name = "Attach to running Neovim instance",
		port = OSV_PORT,
	},
}

dap.adapters.nlua = function(callback, config)
	callback({ type = "server", host = config.host or "127.0.0.1", port = config.port })
end

vim.api.nvim_create_user_command("OSVLaunch", osv_launch, { desc = "Launch OSV server" })
vim.api.nvim_create_user_command("OSVStatus", osv_status, { desc = "Show OSV status" })