Add dap for csharp debug and autocmd to build c# project/solution and text template
This commit is contained in:
@@ -7,11 +7,146 @@
|
||||
-- Or remove existing autocmds by their group name (which is prefixed with `lazyvim_` for the defaults)
|
||||
-- e.g. vim.api.nvim_del_augroup_by_name("lazyvim_wrap_spell")
|
||||
|
||||
-- Comment String
|
||||
vim.api.nvim_create_augroup("SetCommentString", { clear = true })
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
group = "SetCommentString",
|
||||
pattern = { "hlsl", "cs", "c", "cpp" },
|
||||
callback = function()
|
||||
vim.opt_local.commentstring = "//%s"
|
||||
end,
|
||||
group = "SetCommentString",
|
||||
pattern = { "hlsl", "cs", "c", "cpp" },
|
||||
callback = function()
|
||||
vim.opt_local.commentstring = "//%s"
|
||||
end,
|
||||
})
|
||||
|
||||
-- Build
|
||||
local function find_nearest(patterns)
|
||||
-- Start at the directory of the current buffer
|
||||
local dir = vim.fn.expand("%:p:h")
|
||||
|
||||
-- Stop if we hit the root directory
|
||||
while dir do
|
||||
for _, pattern in ipairs(patterns) do
|
||||
-- Check for the pattern in the current directory
|
||||
local matches = vim.fn.glob(dir .. "/" .. pattern, true, true)
|
||||
if #matches > 0 then
|
||||
return matches[1] -- Return the first match found
|
||||
end
|
||||
end
|
||||
|
||||
-- Move to the parent directory
|
||||
local parent = vim.fn.fnamemodify(dir, ":h")
|
||||
if parent == dir then
|
||||
break
|
||||
end -- We reached the filesystem root
|
||||
dir = parent
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
local function run_build_qf(cmd)
|
||||
local lines = {}
|
||||
|
||||
vim.notify("Build started...", vim.log.levels.INFO, { title = "Build" })
|
||||
|
||||
vim.fn.jobstart(cmd, {
|
||||
on_stdout = function(_, data)
|
||||
if data then
|
||||
for _, line in ipairs(data) do
|
||||
if line ~= "" then
|
||||
table.insert(lines, line)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
on_stderr = function(_, data)
|
||||
if data then
|
||||
for _, line in ipairs(data) do
|
||||
if line ~= "" then
|
||||
table.insert(lines, line)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
on_exit = function(_, exit_code)
|
||||
vim.fn.setqflist({}, "r", {
|
||||
title = cmd,
|
||||
lines = lines,
|
||||
})
|
||||
|
||||
if exit_code ~= 0 then
|
||||
vim.notify("Build Failed!", vim.log.levels.ERROR, { title = "Build" })
|
||||
vim.cmd("copen")
|
||||
else
|
||||
vim.notify("Build Succeeded!", vim.log.levels.INFO, { title = "Build" })
|
||||
vim.cmd("cclose")
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
vim.api.nvim_create_augroup("Build", { clear = true })
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
group = "Build",
|
||||
pattern = { "cs", "tt" },
|
||||
callback = function(event)
|
||||
local ft = event.match
|
||||
|
||||
if ft == "cs" then
|
||||
-- <leader>cb to build C# projects and <leader>cB to build C# solutions
|
||||
vim.keymap.set("n", "<leader>cb", function()
|
||||
local csproj = find_nearest({ "*.csproj" })
|
||||
if csproj then
|
||||
run_build_qf(
|
||||
"dotnet build "
|
||||
.. vim.fn.fnameescape(csproj)
|
||||
.. " /p:GenerateFullPaths=true /consoleloggerparameters:NoSummary"
|
||||
)
|
||||
else
|
||||
print("No project file found in parent directories.")
|
||||
end
|
||||
end, { buffer = event.buf, desc = "Build C# Project" })
|
||||
|
||||
vim.keymap.set("n", "<leader>cB", function()
|
||||
local sln = find_nearest({ "*.slnx", "*.sln" })
|
||||
if sln then
|
||||
run_build_qf(
|
||||
"dotnet build "
|
||||
.. vim.fn.fnameescape(sln)
|
||||
.. " /p:GenerateFullPaths=true /consoleloggerparameters:NoSummary"
|
||||
)
|
||||
else
|
||||
print("No solution file found in parent directories.")
|
||||
end
|
||||
end, { buffer = event.buf, desc = "Build C# Solution" })
|
||||
elseif ft == "tt" then
|
||||
-- <leader>cb to generate T4 templates
|
||||
vim.keymap.set("n", "<leader>cb", function()
|
||||
local ttfile = vim.fn.expand("%:p")
|
||||
run_build_qf("t4 " .. vim.fn.fnameescape(ttfile))
|
||||
end, { buffer = event.buf, desc = "Generate T4 Template" })
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_augroup("Run", { clear = true })
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
group = "Run",
|
||||
pattern = { "cs" },
|
||||
callback = function(event)
|
||||
local cwd = LazyVim.root()
|
||||
|
||||
-- <leader>cR to run C# projects
|
||||
vim.keymap.set("n", "<leader>cR", function()
|
||||
coroutine.wrap(function()
|
||||
local selected = ShowRunnableCspjPanel(cwd)
|
||||
if selected then
|
||||
Snacks.terminal("dotnet run --project " .. vim.fn.fnameescape(selected.path), {
|
||||
cwd = cwd,
|
||||
auto_close = false,
|
||||
name = "Run C# Project: " .. selected.name,
|
||||
})
|
||||
end
|
||||
end)()
|
||||
end, { buffer = event.buf, desc = "Run C# Project" })
|
||||
end,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user