Add autocmd to run and build c# project

This commit is contained in:
2025-12-16 15:56:10 +09:00
parent 30ac6080c3
commit 77807133cb
5 changed files with 78 additions and 105 deletions

View File

@@ -18,31 +18,6 @@ vim.api.nvim_create_autocmd("FileType", {
})
-- 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 = {}
@@ -90,33 +65,34 @@ vim.api.nvim_create_autocmd("FileType", {
pattern = { "cs", "tt" },
callback = function(event)
local ft = event.match
local cwd = LazyVim.root()
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)
coroutine.wrap(function()
local csproj = ShowSelectionPanel(cwd, "Select Project:", { "/**/*.csproj" })
if csproj then
local cmd = "dotnet build "
.. vim.fn.fnameescape(csproj.path)
.. " /p:GenerateFullPaths=true /consoleloggerparameters:NoSummary"
)
else
print("No project file found in parent directories.")
end
run_build_qf(cmd)
end
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)
coroutine.wrap(function()
local sln = ShowSelectionPanel(cwd, "Select Solution:", { "/**/*.slnx", "/**/*.sln" })
if sln then
local cmd = "dotnet build "
.. vim.fn.fnameescape(sln.path)
.. " /p:GenerateFullPaths=true /consoleloggerparameters:NoSummary"
)
else
print("No solution file found in parent directories.")
end
run_build_qf(cmd)
end
end)()
end, { buffer = event.buf, desc = "Build C# Solution" })
elseif ft == "tt" then
-- <leader>cb to generate T4 templates