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

feat: set cwd for dlv based on program #99

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lua/dap-go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ local function filtered_pick_process()
return require("dap.utils").pick_process(opts)
end

local get_parent = (function()
local sep = "\\"

local os = string.lower(jit.os)
if os ~= "windows" then
sep = "/"
end

local pattern = string.format("^(.+)%s[^%s]+", sep, sep)

return function(abs_path)
local parent = abs_path:match(pattern)
if parent ~= nil and not parent:find(sep) then
return parent .. sep
end
return parent
end
end)()

local function setup_delve_adapter(dap, config)
local args = { "dap", "-l", "127.0.0.1:" .. config.delve.port }
vim.list_extend(args, config.delve.args)
Expand All @@ -81,6 +100,24 @@ local function setup_delve_adapter(dap, config)
}

dap.adapters.go = function(callback, client_config)
if client_config.program ~= nil then
local program_absolute = vim.loop.fs_realpath(client_config.program)

if program_absolute ~= nil then
client_config.program = program_absolute

local is_dir = vim.loop.fs_stat(program_absolute).type == "directory"
if is_dir then
delve_config.executable.cwd = program_absolute
elseif program_absolute:sub(-3) == ".go" then -- file extension is '.go'
local parent = get_parent(program_absolute)
if parent ~= nil then
delve_config.executable.cwd = parent
end
end
end
end

if client_config.port == nil then
callback(delve_config)
return
Expand Down
Loading