Skip to content

Commit 2fe7567

Browse files
committed
feat: auto compile before debug (#1367).
1 parent 02aff10 commit 2fe7567

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lua/keymap/helpers.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,44 @@ _G._toggle_lazygit = function()
6565
vim.notify("Command [lazygit] not found!", vim.log.levels.ERROR, { title = "toggleterm.nvim" })
6666
end
6767
end
68+
69+
_G.async_compile_and_debug = function()
70+
local file_ext = vim.fn.expand("%:e")
71+
local file_path = vim.fn.expand("%:p")
72+
local out_name = vim.fn.expand("%:p:h") .. "/" .. vim.fn.expand("%:t:r") .. ".out"
73+
local compile_cmd
74+
if file_ext == "cpp" or file_ext == "cc" then
75+
compile_cmd = string.format("g++ -g %s -o %s", file_path, out_name)
76+
elseif file_ext == "c" then
77+
compile_cmd = string.format("gcc -g %s -o %s", file_path, out_name)
78+
elseif file_ext == "go" then
79+
compile_cmd = string.format("go build -o %s %s", out_name, file_path)
80+
elseif file_ext == "java" then
81+
compile_cmd = string.format("javac %s", file_path)
82+
elseif file_ext == "rs" then
83+
compile_cmd = string.format("rustc %s -o %s", file_path, out_name)
84+
else
85+
require("dap").continue()
86+
return
87+
end
88+
local notify_title = "Debug Pre-compile"
89+
vim.fn.jobstart(compile_cmd, {
90+
on_exit = function(_, exit_code, _)
91+
if exit_code == 0 then
92+
vim.notify(
93+
"Compilation succeeded! Executable: " .. out_name,
94+
vim.log.levels.INFO,
95+
{ title = notify_title }
96+
)
97+
require("dap").continue()
98+
return
99+
else
100+
vim.notify(
101+
"Compilation failed with exit code: " .. exit_code,
102+
vim.log.levels.ERROR,
103+
{ title = notify_title }
104+
)
105+
end
106+
end,
107+
})
108+
end

lua/keymap/tool.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ local plug_map = {
127127

128128
-- Plugin: dap
129129
["n|<F6>"] = map_callback(function()
130-
require("dap").continue()
130+
async_compile_and_debug()
131131
end)
132132
:with_noremap()
133133
:with_silent()

0 commit comments

Comments
 (0)