Skip to content

Commit

Permalink
Add dap_enrich_config to pass enrich_config to dap adapter config (#522)
Browse files Browse the repository at this point in the history
* Add dap_enrich_config to pass enrich_config to dap adapter config

* Update README.md on loading envs

---------

Co-authored-by: rayx <[email protected]>
  • Loading branch information
gcollura and ray-x authored Nov 12, 2024
1 parent ca343ef commit 5c7ade4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,19 @@ Here is a sample [launch.json](https://github.com/ray-x/go.nvim/blob/master/play
### Load Env file

- GoEnv {filename} By default load .env file in current directory, if you want to load other file, use {filename} option
- Alternatively, you can specify an `dap_enrich_config` function, to modify the selected launch.json configuration on the fly,
as suggested by https://github.com/mfussenegger/nvim-dap/discussions/548#discussioncomment-8778225:
```lua
dap_enrich_config = function(config, on_config)
local final_config = vim.deepcopy(finalConfig)
final_config.env['NEW_ENV_VAR'] = 'env-var-value'
-- load .env file for your project
local workspacefolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd()
local envs_from_file = require('go.env').load_env(workspacefolder .. 'your_project_dot_env_file_name')
final_config = vim.tbl_extend("force", final_config, envs_from_file)
on_config(final_config)
end
```

### Generate return value

Expand Down Expand Up @@ -852,6 +865,7 @@ require('go').setup({
dap_port = 38697, -- can be set to a number, if set to -1 go.nvim will pick up a random port
dap_timeout = 15, -- see dap option initialize_timeout_sec = 15,
dap_retries = 20, -- see dap option max_retries
dap_enrich_config = nil, -- see dap option enrich_config
build_tags = "tag1,tag2", -- set default build tags
textobjects = true, -- enable default text objects through treesittter-text-objects
test_runner = 'go', -- one of {`go`, `dlv`, `ginkgo`, `gotestsum`}
Expand Down
1 change: 1 addition & 0 deletions lua/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ _GO_NVIM_CFG = {
dap_debug_vt = { enabled = true, enabled_commands = true, all_frames = true }, -- bool|table put your dap-virtual-text setup here set to false to disable
dap_port = 38697, -- can be set to a number or -1 so go.nvim will pickup a random port
dap_timeout = 15, -- see dap option initialize_timeout_sec = 15,
dap_enrich_config = nil, -- see dap option enrich_config
dap_retries = 20, -- see dap option max_retries
build_tags = '', --- you can provide extra build tags for tests or debugger
textobjects = true, -- treesitter binding for text objects
Expand Down
16 changes: 14 additions & 2 deletions lua/go/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,13 @@ M.run = function(...)
}
dap.adapters.go = function(callback, config)
if config.request == 'attach' and config.mode == 'remote' and config.host then
callback({ type = 'server', host = config.host, port = config.port, options = con_options })
callback({
type = 'server',
host = config.host,
port = config.port,
options = con_options,
enrich_config = _GO_NVIM_CFG.dap_enrich_config,
})
return
end
stdout = vim.loop.new_pipe(false)
Expand Down Expand Up @@ -443,7 +449,13 @@ M.run = function(...)
stderr:read_start(onread)

vim.defer_fn(function()
callback({ type = 'server', host = host, port = port, options = con_options })
callback({
type = 'server',
host = host,
port = port,
options = con_options,
enrich_config = _GO_NVIM_CFG.dap_enrich_config,
})
end, 1000)
end

Expand Down

0 comments on commit 5c7ade4

Please sign in to comment.