Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto compile before debug (#1367). #1368

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint_code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ jobs:
- uses: actions/checkout@v4
- uses: lunarmodules/luacheck@v1
with:
args: . --std luajit --globals vim _debugging _command_panel _flash_esc_or_noh _telescope_collections _toggle_lazygit _toggle_diagnostic _toggle_inlayhint --max-line-length 150 --no-config
args: . --std luajit --globals vim _debugging _command_panel _flash_esc_or_noh _telescope_collections _toggle_lazygit _toggle_diagnostic _toggle_inlayhint _async_compile_and_debug --max-line-length 150 --no-config
37 changes: 37 additions & 0 deletions lua/keymap/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,40 @@ _G._toggle_diagnostic = function()
end
end
end

_G._async_compile_and_debug = function()
local file_ext = vim.fn.expand("%:e")
local file_path = vim.fn.expand("%:p")
local out_name = vim.fn.expand("%:p:h") .. "/" .. vim.fn.expand("%:t:r") .. ".out"
local compile_cmd
if file_ext == "cpp" or file_ext == "cc" then
compile_cmd = string.format("g++ -g %s -o %s", file_path, out_name)
elseif file_ext == "c" then
compile_cmd = string.format("gcc -g %s -o %s", file_path, out_name)
elseif file_ext == "go" then
compile_cmd = string.format("go build -o %s %s", out_name, file_path)
else
require("dap").continue()
return
end
local notify_title = "Debug Pre-compile"
vim.fn.jobstart(compile_cmd, {
on_exit = function(_, exit_code, _)
if exit_code == 0 then
vim.notify(
"Compilation succeeded! Executable: " .. out_name,
vim.log.levels.INFO,
{ title = notify_title }
)
require("dap").continue()
return
else
vim.notify(
"Compilation failed with exit code: " .. exit_code,
vim.log.levels.ERROR,
{ title = notify_title }
)
end
end,
})
end
2 changes: 1 addition & 1 deletion lua/keymap/tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ local plug_map = {

-- Plugin: dap
["n|<F6>"] = map_callback(function()
require("dap").continue()
_async_compile_and_debug()
end)
:with_noremap()
:with_silent()
Expand Down
6 changes: 5 additions & 1 deletion lua/modules/utils/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ function M.input_args()
end

function M.input_exec_path()
return vim.fn.input('Path to executable (default to "a.out"): ', vim.fn.expand("%:p:h") .. "/a.out", "file")
return vim.fn.input(
'Path to executable (default to "filename.out"): ',
vim.fn.expand("%:p:h") .. "/" .. vim.fn.expand("%:t:r") .. ".out",
"file"
)
end

function M.input_file_path()
Expand Down