Skip to content

Commit

Permalink
feat: add get_build_flag and get_arguments as avaiable functions for (#…
Browse files Browse the repository at this point in the history
…92)

custom configurations
  • Loading branch information
nkane authored Jul 19, 2024
1 parent 8e5b23f commit 6da41ff
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 8 deletions.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ lua require('dap-go').setup {
-- compiled during debugging, for example.
-- passing build flags using args is ineffective, as those are
-- ignored by delve in dap mode.
build_flags = "",
-- avaliable ui interactive function to prompt for arguments get_arguments
build_flags = {},
-- whether the dlv process to be created detached or not. there is
-- an issue on Windows where this needs to be set to false
-- otherwise the dlv server creation will fail.
-- avaliable ui interactive function to prompt for build flags: get_build_flags
detached = vim.fn.has("win32") == 0,
-- the current working directory to run dlv from, if other than
-- the current working directory.
Expand All @@ -97,11 +99,12 @@ lua require('dap-go').setup {

### Debugging individual tests


To debug the closest method above the cursor use you can run:

- `:lua require('dap-go').debug_test()`

Once a test was run, you can simply run it again from anywhere:

- `:lua require('dap-go').debug_last_test()`

It is better to define a mapping to invoke this command. See the mapping section below.
Expand All @@ -116,9 +119,50 @@ It is better to define a mapping to invoke this command. See the mapping section
![Enter Arguments](./images/image2.png "Enter Arguments")
![Begin Debugging](./images/image3.png "Being Debugging")

### Debugging with build flags

1. Register a new option to debug with build flags:

```lua
require('dap-go').setup {
dap_configurations = {
{
type = "go",
name = "Debug (Build Flags)",
request = "launch",
program = "${file}",
buildFlags = require("dap-go").get_build_flags,
},
},
})
```

2. To prompt for both build flags and arguments register the following:

```lua
require("dap-go").setup({
dap_configurations = {
{
type = "go",
name = "Debug (Build Flags & Arguments)",
request = "launch",
program = "${file}",
args = require("dap-go").get_arguments,
buildFlags = require("dap-go").get_build_flags,
},
}
})
```

3. To create a custom debugging configuration that requires an interactive prompt the following functions can be
attached to the args and buildFlags fields of dap_configurations.
- `require('dap-go').get_arguments`
- `require('dap-go').get_buid_flags`

### Debugging with dlv in headless mode

1. Register a new option to attach to a remote debugger:

```lua
lua require('dap-go').setup {
dap_configurations = {
Expand All @@ -131,10 +175,13 @@ lua require('dap-go').setup {
},
}
```

1. Start `dlv` in headless mode. You can specify subcommands and flags after `--`, e.g.,

```sh
dlv debug -l 127.0.0.1:38697 --headless ./main.go -- subcommand --myflag=xyz
```

1. Call `:lua require('dap').continue()` to start debugging.
1. Select the new registered option `Attach remote`.

Expand Down
47 changes: 45 additions & 2 deletions doc/nvim-dap-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ CONTENTS *dap-go-toc*
3. Usage ................................. |dap-go-usage|
4. Debugging Individual Tests ............ |dap-go-debug-test|
5. Debugging With Command-Line Arguments . |dap-go-debug-cli-args|
6. Debugging With dlv in Headless Mode ... |dap-go-debug-headless|
7. Mappings .............................. |dap-go-mappings|
6. Debugging With Build Flags ............ |dap-go-debug-cli-args|
7. Debugging With dlv in Headless Mode ... |dap-go-debug-headless|
8. Mappings .............................. |dap-go-mappings|

========================================================================
FEATURES *dap-go-features*
Expand Down Expand Up @@ -132,6 +133,48 @@ Debugging With Command-Line Arguments *dap-go-debug-cli-args*
option3`).
3. Press enter.

-----------------------------------------------------------------------
Debugging With Build Flags *dap-go-debug-build-flags*

1. Register a new option to debug with build flags:
>lua
require('dap-go').setup {
dap_configurations = {
{
type = "go",
name = "Debug (Build Flags)",
request = "launch",
program = "${file}",
buildFlags = require("dap-go").get_build_flags,
},
},
})
<

2. To prompt for both build flags and arguments register the
following:
>lua
require("dap-go").setup({
dap_configurations = {
{
type = "go",
name = "Debug (Build Flags & Arguments)",
request = "launch",
program = "${file}",
args = require("dap-go").get_arguments,
buildFlags = require("dap-go").get_build_flags,
},
}
})
<

3. To create a custom debugging configuration that requires an
interactive prompt the following functions can be attached to
the args and buildFlags fields of dap_configurations.

`require('dap-go').get_arguments`
`require('dap-go').get_buid_flags`

-----------------------------------------------------------------------
Debugging With dlv in Headless Mode *dap-go-debug-headless*

Expand Down
36 changes: 32 additions & 4 deletions lua/dap-go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ local default_config = {
},
}

local internal_global_config = {}

local function load_module(module_name)
local ok, module = pcall(require, module_name)
assert(ok, string.format("dap-go dependency error: %s not installed", module_name))
Expand All @@ -35,6 +37,16 @@ local function get_arguments()
end)
end

local function get_build_flags(config)
return coroutine.create(function(dap_run_co)
local build_flags = config.build_flags
vim.ui.input({ prompt = "Build Flags: " }, function(input)
build_flags = vim.split(input or "", " ")
coroutine.resume(dap_run_co, build_flags)
end)
end)
end

local function filtered_pick_process()
local opts = {}
vim.ui.input(
Expand Down Expand Up @@ -82,6 +94,14 @@ local function setup_go_configuration(dap, configs)
args = get_arguments,
buildFlags = configs.delve.build_flags,
},
{
type = "go",
name = "Debug (Arguments & Build Flags)",
request = "launch",
program = "${file}",
args = get_arguments,
buildFlags = get_build_flags,
},
{
type = "go",
name = "Debug Package",
Expand Down Expand Up @@ -127,11 +147,11 @@ local function setup_go_configuration(dap, configs)
end

function M.setup(opts)
local config = vim.tbl_deep_extend("force", default_config, opts or {})
M.test_buildflags = config.delve.build_flags
internal_global_config = vim.tbl_deep_extend("force", default_config, opts or {})
M.test_buildflags = internal_global_config.delve.build_flags
local dap = load_module("dap")
setup_delve_adapter(dap, config)
setup_go_configuration(dap, config)
setup_delve_adapter(dap, internal_global_config)
setup_go_configuration(dap, internal_global_config)
end

local function debug_test(testname, testpath, build_flags)
Expand Down Expand Up @@ -181,4 +201,12 @@ function M.debug_last_test()
return true
end

function M.get_build_flags()
return get_build_flags(internal_global_config)
end

function M.get_arguments()
return get_arguments()
end

return M

0 comments on commit 6da41ff

Please sign in to comment.