-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy path.nvim.lua
More file actions
84 lines (73 loc) · 2.83 KB
/
.nvim.lua
File metadata and controls
84 lines (73 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-- NPDE project-local config (loaded via exrc)
local root = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h")
local debug_dir = root .. "/build-debug"
local release_dir = root .. "/build"
local nproc = "-j" .. tostring(vim.uv.available_parallelism())
-- Detect dir/problem/solution from current buffer path
local function get_info()
local path = vim.fn.expand("%:p")
for _, d in ipairs({ "developers", "homeworks", "lecturecodes" }) do
local dir, prob, sol = path:match("/(" .. d .. ")/([^/]+)/([^/]+)/")
if dir then return { dir = dir, problem = prob, solution = sol } end
end
end
local function target(info)
local sfx = info.dir == "developers" and "_dev" or ""
return info.problem .. "_" .. info.solution .. sfx
end
local function binary_path(bdir, info)
return ("%s/%s/%s/%s_%s"):format(bdir, info.dir, info.problem, info.problem, info.solution)
end
-- Run a command in a small terminal split
local function term_run(cmd)
vim.cmd("botright 15split | terminal " .. cmd)
vim.cmd("startinsert")
end
-- Keymaps
vim.keymap.set("n", "<leader>mb", function()
local info = get_info()
if not info then return end
term_run(("cmake --build %s --target %s %s"):format(debug_dir, target(info), nproc))
end, { desc = "Make build (debug)" })
vim.keymap.set("n", "<leader>mB", function()
local info = get_info()
if not info then return end
term_run(("cmake --build %s --target %s %s"):format(release_dir, target(info), nproc))
end, { desc = "Make release" })
vim.keymap.set("n", "<leader>mc", function()
term_run(("cmake -B %s -DCMAKE_BUILD_TYPE=Debug %s"):format(debug_dir, root))
end, { desc = "Make configure (debug)" })
vim.keymap.set("n", "<leader>mC", function()
term_run(("cmake -B %s %s"):format(release_dir, root))
end, { desc = "Make configure (release)" })
-- DAP config: inject when a C++ file is first opened
local dap_ready = false
vim.api.nvim_create_autocmd("FileType", {
pattern = { "c", "cpp" },
callback = function()
if dap_ready then return end
vim.defer_fn(function()
local ok, dap = pcall(require, "dap")
if not ok then return end
dap_ready = true
local existing = dap.configurations.cpp or {}
dap.configurations.cpp = vim.list_extend({
{
name = "NPDE: Debug current problem",
type = "codelldb", request = "launch",
initCommands = { "command script import " .. root .. "/scripts/lldb_eigen_printer.py" },
program = function()
local info = get_info()
if not info then return dap.ABORT end
return binary_path(debug_dir, info)
end,
cwd = function()
local info = get_info()
if not info then return debug_dir end
return ("%s/%s/%s"):format(debug_dir, info.dir, info.problem)
end,
},
}, existing)
end, 100)
end,
})