fixed Dap Debug for .net

This commit is contained in:
Marc Talcott 2024-06-27 13:38:55 -04:00
parent 61087071ab
commit 1313a988a5
2 changed files with 135 additions and 6 deletions

View File

@ -8,17 +8,42 @@ return {
'nvim-neotest/nvim-nio',
-- Installs the debug adapters for you
'williamboman/mason.nvim',
-- 'williamboman/mason.nvim',
'jay-babu/mason-nvim-dap.nvim',
-- Add your own debuggers here
'leoluz/nvim-dap-go',
'dotnet/vscode-csharp',
-- virtual Text
'theHamsta/nvim-dap-virtual-text',
},
config = function()
require('dapui').setup()
require('dap-go').setup()
require('nvim-dap-virtual-text').setup()
require('lspconfig').omnisharp.setup {}
require('dap.ext.vscode').load_launchjs(nil, {})
local dap, dapui = require 'dap', require 'dapui'
local ph_status, dotnet_ph = pcall(require, 'utilities.path_finder')
dap.adapters.coreclr = {
type = 'executable',
command = 'netcoredbg',
args = { '--interpreter=vscode' },
}
dap.configurations.cs = {
{
type = 'coreclr',
name = 'launch - netcoredbg',
request = 'launch',
program = function()
-- return vim.fn.input('Path to dll:', '/Users/marctalcott/Documents/Projects/DotNetProjects/HelloWorld/bin/Debug/net8.0/', 'file')
return vim.fn.input('Path to dll: ', vim.fn.getcwd() .. '/bin/Debug/net8.0/', 'file')
end,
console = 'integratedTerminal',
},
}
dap.listeners.before.attach.dapui_config = function()
dapui.open()
@ -33,9 +58,25 @@ return {
dapui.close()
end
vim.keymap.set('n', '<Leader>db', ':DapToggleBreakpoint<CR>')
vim.keymap.set('n', '<Leader>dc', ':DapContinue<CR>')
vim.keymap.set('n', '<Leader>dx', ':DapTerminate<CR>')
vim.keymap.set('n', '<Leader>do', ':DapStepOver<CR>')
-- Keymaps for all
vim.fn.sign_define('DapBreakpoint', { text = '🔴', texthl = 'DapBreakpoint', linehl = 'DapBreakpoint', numhl = 'DapBreakpoint' })
vim.keymap.set('n', '<Leader>db', dap.toggle_breakpoint, { desc = '[d]ebug toggle [b]reakpoint' })
vim.keymap.set('n', '<Leader>dc', dap.continue, { desc = '[d]ebug [c]continue' })
vim.keymap.set('n', '<Leader>dC', dap.close, { desc = '[d]ebug [C]lose' })
vim.keymap.set('n', '<F5>', dap.continue, { desc = 'Debug Continue' })
vim.keymap.set('n', '<F8>', dap.step_over, { desc = 'Step Over' })
vim.keymap.set('n', '<F9>', dap.step_out, { desc = 'Step Out' })
vim.keymap.set('n', '<F10>', dap.step_into, { desc = 'Step Into' })
vim.keymap.set('n', '<F12>', dap.terminate, { desc = 'Terminate' })
vim.keymap.set('n', '<Leader>dx', dap.terminate, { desc = 'Terminate' })
vim.keymap.set('n', '<Leader>do', dap.step_over, { desc = 'Step over' })
vim.keymap.set('n', '<Leader>dr', dap.restart, { desc = 'Restart' })
vim.api.nvim_set_keymap('n', '<leader>dR', ":lua require('dapui').open({reset = true})<CR>", { noremap = true })
vim.api.nvim_set_keymap('n', '<leader>ht', ":lua require('harpoon.ui').toggle_quick_menu()<CR>", { noremap = true })
vim.keymap.set('n', '<Leader>?', function()
require('dapui').eval(nil, { enter = true })
end, { desc = 'Restart' })
end,
}

View File

@ -0,0 +1,88 @@
-- Check to see if path is a part of the config.projects, if so set last_proj_path and last_dll_path
function M.GetProjConfig(path, config)
local result = {}
if config == nil then
return result
end
local projects = config.projects
if projects == nil then
return result
end
for _, project in ipairs(projects) do
if project.base_path and string.find(path, project.base_path) then
-- TODO: nil check, if even needed.
-- TODO: these names make no sense
result.dotnet_last_proj_path = project.dotnet_proj_file
result.dotnet_last_dll_path = project.dotnet_dll_path
result.dotnet_debug_cwd = project.dotnet_debug_cwd
result.project_found = true
return result
end
end
result.project_found = false
return result
end
function M.search_up_path(project_root, path, pattern_func)
local parent = vim.fn.fnamemodify(path, ':h')
-- the parent of 'C:/' is 'C:/'
if parent == path then
return project_root
end
local parent_root = pattern_func(parent)
-- returns nil when no root is found
if parent_root == nil then
return project_root
end
return M.search_up_path(parent_root, parent, pattern_func)
end
function M.dotnet_get_dll_path(path, proj_found)
local request = function()
return vim.fn.input('Path to dll ', vim.fn.getcwd() .. '/bin/Debug/', 'file')
end
if path == nil then
path = request()
print('Dll path: ' .. path)
else
if proj_found == false and vim.fn.confirm('Do you want to change the path to dll?\n' .. path, '&yes\n&no', 2) == 1 then
path = request()
print('Dll path: ' .. path)
end
end
return path
end
function M.dotnet_build_project(last_path, config)
local default_path = vim.fn.getcwd() .. '/'
if last_path ~= nil then
default_path = last_path
end
local result = nil
-- If project was found in config, use it
if config.dotnet_last_proj_path ~= nil then
print('Found project in config. Project file path is ' .. config.dotnet_last_proj_path)
result = config.dotnet_last_proj_path
else
-- If the project was not found, always ask for it, but fill in the last path
result = vim.fn.input('Path to your *proj file', default_path, 'file')
end
local cmd = 'dotnet build -c Debug ' .. result
print ''
print('Cmd to execute: ' .. cmd)
-- TODO: This should be done in async way
local f = os.execute(cmd)
if f == 0 then
print '\nBuild: ✔️ '
else
print('\nBuild: ❌ (code: ' .. f .. ')')
end
return result
end
return M