aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha/dap.lua
blob: 27b596d64bc1ae34a2834fd168ce38b933894675 (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
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
local dap, dapui, dap_install = require("dap"), require("dapui"), require("dap-install")
local api = vim.api

dapui.setup({
	sidebar = {
		size = 80,
		position = "right",
	},
})

require("dap-go").setup()

vim.fn.sign_define("DapBreakpoint", { text = "ß", texthl = "", linehl = "", numhl = "" })

dap_install.config("php", {
	configurations = {
		{
			type = "php",
			request = "launch",
			name = "Listen for Xdebug",
			port = 9003,
			pathMappings = function()
				local sail = vim.call("composer#query", "require-dev.laravel/sail")
				if sail == "" then
					return { ["/app"] = vim.fn.getcwd() }
				end
				return { ["/var/www/html"] = vim.fn.getcwd() }
			end,
		},
	},
})

-- Events Listeners
dap.listeners.after.event_initialized["dapui_config"] = function()
	dapui.open()
end

local function with_win(win, fn, ...)
	local cur_win = api.nvim_get_current_win()
	api.nvim_set_current_win(win)
	local ok, err = pcall(fn, ...)
	api.nvim_set_current_win(cur_win)
	assert(ok, err)
end

local function jump_to_location(bufnr, line, column)
	-- vscode-go sends columns with 0
	-- That would cause a "Column value outside range" error calling nvim_win_set_cursor
	-- nvim-dap says "columnsStartAt1 = true" on initialize :/
	if column == 0 then
		column = 1
	end
	for _, tab in pairs(api.nvim_list_tabpages()) do
		for _, win in pairs(api.nvim_tabpage_list_wins(tab)) do
			if api.nvim_win_get_buf(win) == bufnr then
				api.nvim_win_set_cursor(win, { line, column - 1 })
				api.nvim_set_current_tabpage(tab)
				api.nvim_set_current_win(win)
				return
			end
		end
	end
	-- TODO review
	-- Buffer isn't active in any window; use the first window that is not special
	-- (Don't want to move to code in the REPL...)
	for _, win in pairs(api.nvim_tabpage_list_wins(0)) do
		local winbuf = api.nvim_win_get_buf(win)
		if api.nvim_buf_get_option(winbuf, "buftype") == "" then
			local bufchanged, _ = pcall(api.nvim_win_set_buf, win, bufnr)
			if bufchanged then
				api.nvim_win_set_cursor(win, { line, column - 1 })
				with_win(win, api.nvim_command, "normal zv")
				return
			end
		end
	end
end

dap.listeners.after["event_stopped"]["alpha"] = function(session, body)
	if body.reason ~= "breakpoint" then
		return
	end
	if not session.current_frame then
		return
	end
	local path = session.current_frame.source.path
	local line = session.current_frame.line
	local column = session.current_frame.column

	local bufnr = vim.uri_to_bufnr(vim.uri_from_fname(path))
	vim.fn.bufload(bufnr)
	jump_to_location(bufnr, line, column)
end

vim.keymap.set("n", "<leader>dbp", require("dap").toggle_breakpoint, { noremap = true })
vim.keymap.set("n", "<leader>dd", require("dap").continue, { noremap = true })
vim.keymap.set("n", "<leader>de", require("alpha.dap-fn").stop, { noremap = true })
vim.keymap.set("n", "<leader>dl", require("dap").step_into, { noremap = true })
vim.keymap.set("n", "<leader>dj", require("dap").step_over, { noremap = true })
vim.keymap.set("n", "<leader>dk", require("dap").step_out, { noremap = true })
vim.keymap.set("n", "<leader>dt", require("dap").repl.toggle, { noremap = true })
vim.keymap.set("n", "<leader>ds", require("alpha.dap-fn").scopes, { noremap = true })
vim.keymap.set("n", "<leader>da", require("alpha.dap-fn").fullscopes, { noremap = true })
vim.keymap.set("n", "<leader>dbl", require("dap").list_breakpoints, { noremap = true })
vim.keymap.set("n", "<leader>dbc", require("dap").clear_breakpoints, { noremap = true })
vim.keymap.set("n", "<leader>dc", require("dap").run_to_cursor, { noremap = true })
vim.keymap.set("v", "<leader>dk", require("dapui").eval, { noremap = true })
-- can be skipped since I can do it running the continue and promt to use the "Debug test"
vim.keymap.set("n", "<leader>dtt", require("dap-go").debug_test, { noremap = true })