Skip to content

Commit

Permalink
Merge branch 'main' into add_tests_verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
leoluz authored Jul 19, 2024
2 parents 29fb61e + 6da41ff commit 6bf281f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 12 deletions.
53 changes: 50 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ 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.
detached = true,
-- 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.
cwd = nil,
Expand All @@ -102,11 +104,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 @@ -121,9 +124,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 @@ -136,10 +180,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 @@ -137,6 +138,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
45 changes: 38 additions & 7 deletions lua/dap-go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ local default_config = {
port = "${port}",
args = {},
build_flags = "",
detached = true,
-- Automativally handle the issue on Windows where delve needs
-- to be run in attched mode or it will fail (actually crashes).
detached = vim.fn.has("win32") == 0,
},
tests = {
verbose = false,
},
}

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 @@ -37,6 +41,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 @@ -84,6 +98,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 @@ -125,16 +147,17 @@ local function setup_go_configuration(dap, configs)
if config.type == "go" then
table.insert(dap.configurations.go, config)
end
end
end_delve_adapter(dap, internal_global_confi
end

function M.setup(opts)
local config = vim.tbl_deep_extend("force", default_config, opts or {})
M.test_buildflags = config.delve.build_flags
M.test_verbose = config.tests.verbose
internal_global_config = vim.tbl_deep_extend("force", default_config, opts or {})
M.test_buildflags = internal_global_config.delve.build_flags
M.test_verbose = internal_global_config.tests.verbose

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, extra_args)
Expand Down Expand Up @@ -203,4 +226,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
3 changes: 3 additions & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/leoluz/nvim-dap-go

go 1.22.2
12 changes: 12 additions & 0 deletions tests/subtest_bar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tests

import (
"testing"
)

// https://github.com/leoluz/nvim-dap-go/pull/82
func TestWithSubTests(t *testing.T) {
t.Run("subtest with function literal", func(t *testing.T) { t.Fail() })
myFunc := func(t *testing.T) { t.FailNow() }
t.Run("subtest with identifier", myFunc)
}

0 comments on commit 6bf281f

Please sign in to comment.