From 0596629c20b82f0db0112c45ff515366d2c9206d Mon Sep 17 00:00:00 2001 From: "Alexandre Cavalheiro S. Tiago da Silva" Date: Wed, 16 Apr 2025 22:47:13 -0300 Subject: [PATCH] feat(neovim): dynamically set the cwd for dap in dotnet debugging --- modules/neovim/ftplugin/cs.lua | 35 +++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/modules/neovim/ftplugin/cs.lua b/modules/neovim/ftplugin/cs.lua index 1b6eb4a..d42cbef 100644 --- a/modules/neovim/ftplugin/cs.lua +++ b/modules/neovim/ftplugin/cs.lua @@ -1,14 +1,24 @@ ---@class CSFTPlugin.Project ---@field dll_path string ---@field name string +---@field project_path string ---@field target_framework string +---@class CSFTPlugin.LaunchOptions: dap.Configuration +---@field cwd string +---@field program (fun(): thread)? + ---@class CSFTPlugin ---@field dotnet_cmd string? ----@field projects CSFTPlugin.Project[] +---@field launch_options CSFTPlugin.LaunchOptions? local M = { dotnet_cmd = vim.fn.exepath "dotnet", - projects = {}, + launch_options = { + cwd = "${env:CSPROJ_LOCATION}", + name = "Launch project DLL", + request = "launch", + type = "netcoredbg", + }, } ---@param command string The shell command to execute @@ -39,6 +49,7 @@ end ---@return CSFTPlugin.Project[] function M.find_projects() + ---@type CSFTPlugin.Project[] local projects = {} local csproj_extension = ".csproj" @@ -54,8 +65,9 @@ function M.find_projects() local target_framework = M.cmd("rg -e 'TargetFramework>(.*)<' -r '$1' -o " .. file_path)[1] projects[#projects + 1] = { - dll_path = project_location .. "bin/Debug/" .. target_framework .. "/" .. project_name .. ".dll", + dll_path = "bin/Debug/" .. target_framework .. "/" .. project_name .. ".dll", name = project_name, + project_path = vim.fn.simplify(vim.fn.getcwd() .. "/" .. project_location), target_framework = target_framework, } end @@ -106,6 +118,8 @@ function M:choose_dll() self:run "build" end + vim.fn.setenv("CSPROJ_LOCATION", item.project_path) + coroutine.resume(search_coroutine, path) end ) @@ -133,17 +147,12 @@ function M:start() args = { "--interpreter=vscode" }, } + self.launch_options.program = function() + return self:choose_dll() + end + ---@type dap.Configuration[] - dap.configurations.cs = { - { - type = "netcoredbg", - name = "Launch project DLL", - request = "launch", - program = function() - return self:choose_dll() - end, - }, - } + dap.configurations.cs = { self.launch_options } vim.g.loaded_csftplugin = true end