Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dap_enrich_config to pass enrich_config to dap adapter config #522

Merged
merged 2 commits into from
Nov 12, 2024

Conversation

gcollura
Copy link
Contributor

@gcollura gcollura commented Nov 11, 2024

DAP suggests passing an enrich_config function to the adapter configuration to allow "hydrating" the launch.json configuration in case it contains an envFile, eg. mfussenegger/nvim-dap#548 (comment)

It can also be useful to expose this function given that dap.adapters.go gets overwritten by go.nvim. https://github.com/mfussenegger/nvim-dap/blob/8517126e9323e346f6a99b3b594c5a940b914dcd/doc/dap.txt#L199-L220

If nil, the enrich_config function is ignored, so we're going to default to that: https://github.com/mfussenegger/nvim-dap/blob/8517126e9323e346f6a99b3b594c5a940b914dcd/lua/dap.lua#L458

The motivation for me is that calling GoEnv depending on which launch configuration I select is a bit cumbersome, especially when different configurations specify different envFiles.

Copy link
Owner

@ray-x ray-x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good to me.

@ray-x ray-x merged commit 5c7ade4 into ray-x:master Nov 12, 2024
3 checks passed
@gcollura gcollura deleted the add-dap-enrich-config-option branch November 12, 2024 03:37
@gcollura
Copy link
Contributor Author

Thanks for merging my PRs!

For reference this is how I implemented envFile with this new feature:

	{
		"ray-x/go.nvim",
		event = { "CmdlineEnter" },
		ft = { "go", "gomod" },
		dependencies = {
			"ray-x/guihua.lua",
			"neovim/nvim-lspconfig",
			"nvim-treesitter/nvim-treesitter",
			"mfussenegger/nvim-dap",
		},
		build = ':lua require("go.install").update_all_sync()',
		opts = {
			dap_enrich_config = require("dap.enrich_config").enrich_config,
		}
	}

in ~/.config/nvim/lua/dap/enrich_config.lua:

local M = {}

local var_placeholders = {
	["${file}"] = function(_)
		return vim.fn.expand("%:p")
	end,
	["${fileBasename}"] = function(_)
		return vim.fn.expand("%:t")
	end,
	["${fileBasenameNoExtension}"] = function(_)
		return vim.fn.fnamemodify(vim.fn.expand("%:t"), ":r")
	end,
	["${fileDirname}"] = function(_)
		return vim.fn.expand("%:p:h")
	end,
	["${fileExtname}"] = function(_)
		return vim.fn.expand("%:e")
	end,
	["${relativeFile}"] = function(_)
		return vim.fn.expand("%:.")
	end,
	["${relativeFileDirname}"] = function(_)
		return vim.fn.fnamemodify(vim.fn.expand("%:.:h"), ":r")
	end,
	["${workspaceFolder}"] = function(_)
		return vim.fn.getcwd()
	end,
	["${workspaceFolderBasename}"] = function(_)
		return vim.fn.fnamemodify(vim.fn.getcwd(), ":t")
	end,
	["${env:([%w_]+)}"] = function(match)
		return os.getenv(match) or ""
	end,
}

function M.enrich_config(config, on_config)
	local final_config = vim.deepcopy(config)

	if final_config.envFile then
		local filePath = final_config.envFile
		for key, fn in pairs(var_placeholders) do
			filePath = filePath:gsub(key, fn)
		end

		for line in io.lines(filePath) do
			local words = {}
			for word in string.gmatch(line, "[^=]+") do
				table.insert(words, word)
			end
			if not final_config.env then
				final_config.env = {}
			end
			final_config.env[words[1]] = words[2]
		end
	end

	on_config(final_config)
end

return M

Credits to @niklod for the code for enrich_config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants