-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: using DAP without nvim-dap-go (#216)
- Loading branch information
Showing
6 changed files
with
208 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- DAP (dap-go) setup related functions. | ||
|
||
local options = require("neotest-golang.options") | ||
local logger = require("neotest-golang.logging") | ||
|
||
local M = {} | ||
|
||
---This will prepare and setup nvim-dap-go for debugging. | ||
---@param cwd string | ||
function M.setup_debugging(cwd) | ||
local dap_go_opts = options.get().dap_go_opts or {} | ||
if type(dap_go_opts) == "function" then | ||
dap_go_opts = dap_go_opts() | ||
end | ||
local dap_go_opts_original = vim.deepcopy(dap_go_opts) | ||
if dap_go_opts.delve == nil then | ||
dap_go_opts.delve = {} | ||
end | ||
dap_go_opts.delve.cwd = cwd | ||
logger.debug({ "Provided dap_go_opts for DAP: ", dap_go_opts }) | ||
require("dap-go").setup(dap_go_opts) | ||
|
||
-- reset nvim-dap-go (and cwd) after debugging with nvim-dap | ||
require("dap").listeners.after.event_terminated["neotest-golang-debug"] = function() | ||
logger.debug({ | ||
"Resetting provided dap_go_opts for DAP: ", | ||
dap_go_opts_original, | ||
}) | ||
require("dap-go").setup(dap_go_opts_original) | ||
end | ||
end | ||
|
||
--- @param test_path string | ||
--- @param test_name_regex string? | ||
--- @return table | nil | ||
function M.get_dap_config(test_path, test_name_regex) | ||
-- :help dap-configuration | ||
local dap_config = { | ||
type = "go", | ||
name = "Neotest-golang", | ||
request = "launch", | ||
mode = "test", | ||
program = test_path, | ||
} | ||
|
||
if test_name_regex ~= nil then | ||
dap_config.args = { "-test.run", test_name_regex } | ||
end | ||
|
||
local dap_go_opts = options.get().dap_go_opts or {} | ||
if dap_go_opts.delve ~= nil and dap_go_opts.delve.build_flags ~= nil then | ||
dap_config.buildFlags = dap_go_opts.delve.build_flags | ||
end | ||
|
||
return dap_config | ||
end | ||
|
||
function M.assert_dap_prerequisites() | ||
local dap_go_found = pcall(require, "dap-go") | ||
if not dap_go_found then | ||
local msg = "You must have leoluz/nvim-dap-go installed to use DAP strategy. " | ||
.. "See the neotest-golang README for more information." | ||
logger.error(msg) | ||
error(msg) | ||
end | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- DAP (manual dap configuration) setup related functions. | ||
|
||
local options = require("neotest-golang.options") | ||
|
||
local M = {} | ||
|
||
---@param cwd string | ||
function M.setup_debugging(cwd) | ||
local dap_manual_config = options.get().dap_manual_config or {} | ||
if type(dap_manual_config) == "function" then | ||
dap_manual_config = dap_manual_config() | ||
end | ||
|
||
dap_manual_config.cwd = cwd | ||
end | ||
|
||
---This will setup a dap configuration to run tests | ||
---@param test_path string | ||
---@param test_name_regex string? | ||
---@return table | nil | ||
function M.get_dap_config(test_path, test_name_regex) | ||
local dap_manual_config = options.get().dap_manual_config or {} | ||
if type(dap_manual_config) == "function" then | ||
dap_manual_config = dap_manual_config() | ||
end | ||
|
||
dap_manual_config.program = test_path | ||
|
||
if test_name_regex ~= nil then | ||
dap_manual_config.args = dap_manual_config.args or {} | ||
table.insert(dap_manual_config.args, "-test.run") | ||
table.insert(dap_manual_config.args, test_name_regex) | ||
end | ||
return dap_manual_config | ||
end | ||
|
||
---Dummy function is needed to be corresponding to dap-go setup (just like trait implementation) | ||
function M.assert_dap_prerequisites() end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters