From 0d804702fbdf06051e841812a96bd2fd89c144cd Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Fri, 29 Mar 2024 18:46:42 -0400 Subject: [PATCH] nvim: get c++ binary compiling working --- src/.config/nvim/lua/debugging.lua | 38 +++++++++++++++++++++++++++++- src/.config/nvim/makefile | 8 +++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/.config/nvim/lua/debugging.lua b/src/.config/nvim/lua/debugging.lua index 390819d..163cc36 100644 --- a/src/.config/nvim/lua/debugging.lua +++ b/src/.config/nvim/lua/debugging.lua @@ -6,6 +6,8 @@ local keymap = confutil.keymap local dap = require("dap") local dapui = require("dapui") +local M = {} + -------------------------------- -------------------------------- -- dap-ui configuration @@ -113,7 +115,31 @@ vim.cmd [[ sign define DapBreakpointCondition text=◒ ]] -------------------------------- -------------------------------- +---------------- +-- debug directory utilities +---------------- +function M.dbg_dir(file) + -- get a directory to store files needed for debugging + -- like ad hoc test cases, or compiled binaries + local dir = assert(vim.env.XDG_CACHE_HOME, "$XDG_CACHE_HOME is unset") .. "/nvimdbg" + local file = file or vim.fn.expand("%:p") + local subdir = dir .. file + assert(vim.fn.mkdir(subdir, "p"), "Could not create debug directory.") + return subdir +end + +function compile(file) + local file = file or vim.fn.expand("%:p") + local subdir = M.dbg_dir(file) + vim.fn.execute("make " .. subdir .. "/binary " .. "-f $XDG_CONFIG_HOME/nvim/makefile") +end +keymap("dc", compile) + + +---------------- +-- python -- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#python +---------------- dap.adapters.python = function(cb, config) if config.request == 'attach' then assert(false, "attach not implemented") @@ -144,7 +170,10 @@ dap.configurations.python = { } } +---------------- +-- c++ -- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#ccrust-via-gdb +---------------- dap.adapters.lldb = { type = "executable", command = "/usr/bin/lldb-vscode", @@ -157,10 +186,17 @@ dap.configurations.cpp = { type = "lldb", request = "launch", program = function() - return vim.fn.input("binary: ", vim.fn.getcwd() .. "/", "file") + local binary = M.dbg_dir() .. "/binary" + if not vim.fn.filereadable(binary) then + binary = vim.fn.input("binary: ", vim.fn.getcwd() .. "/", "file") + end + + return binary end, cwd = "${workspaceFolder}", stopOnEntry = false, runInTerminal = true, } } + +return M diff --git a/src/.config/nvim/makefile b/src/.config/nvim/makefile index 2c5fa38..ceec1c3 100644 --- a/src/.config/nvim/makefile +++ b/src/.config/nvim/makefile @@ -1,9 +1,9 @@ # makefile for compiling individual files from vim -~/.cache/termdebug/bin/%: %.cpp - @mkdir -p $(@D) +DBG_DIR=$(XDG_CACHE_HOME)/nvimdbg + +$(DBG_DIR)%.cpp/binary: %.cpp $(LINK.cpp) -g -Wall -Wpedantic -std=c++20 $^ $(LOADLIBES) $(LDLIBS) -o $@ -~/.cache/termdebug/bin/%: %.c - @mkdir -p $(@D) +$(DBG_DIR)%.c/binary: %.c $(LINK.c) -g -Wall -Wpedantic $^ $(LOADLIBES) $(LDLIBS) -o $@