From ce611a0e14387db7c80c8351d4d6b738c0431b06 Mon Sep 17 00:00:00 2001 From: James Bulman Date: Wed, 24 Sep 2025 23:03:16 +0100 Subject: [PATCH] Added building via shell in neovim Display diagnostics inline to buffer List compile errors/warnings in quickfix list Added 'push' command to install script Make sure install script executes from the correct directory --- config/nvim/init.lua | 96 +++++++++++++++++++++++++++++++++++++++++--- install | 19 ++++++++- 2 files changed, 109 insertions(+), 6 deletions(-) diff --git a/config/nvim/init.lua b/config/nvim/init.lua index 96724b1..63539c6 100644 --- a/config/nvim/init.lua +++ b/config/nvim/init.lua @@ -59,6 +59,90 @@ vim.opt.listchars:append({ lead = "." }) -- Functions -------------------------------------------------------------------------------- +-- Building & Errors + +-- Diagnostics configuration +vim.diagnostic.config({ + signs = { + text = { + [vim.diagnostic.severity.ERROR] = "", + [vim.diagnostic.severity.WARN] = "" + }, + linehl = { + [vim.diagnostic.severity.ERROR] = "DiagnosticLineError", + [vim.diagnostic.severity.WARN] = "DiagnosticLineWarn" + }, + }, + virtual_text = { + virt_text_pos = "eol_right_align", + prefix = + function(d, i, total) + return d.severity == vim.diagnostic.severity.ERROR and "!" or "#" + end + }, + underline = false, + severity_sort = true +}) + +local _last_build = "" +local _quickfix = {} +local _namespace = vim.api.nvim_create_namespace("__qf.buffer.errors") + +local function LoadDiagnostics(args) + local bufnr = args.buf + + if _quickfix[bufnr] then + vim.diagnostic.set(_namespace, bufnr, _quickfix[bufnr]) + end +end + +local function ExecuteBuild() + vim.cmd("silent make") + + -- Reset currently displayed diagnostics + _quickfix = {} + vim.diagnostic.reset() + + local entries = vim.fn.getqflist() + if #entries ~= 0 then + -- Group diagnostics by buffer number + local diagnostics = vim.diagnostic.fromqflist(entries) + for _, d in ipairs(diagnostics) do + if not _quickfix[d.bufnr] then + _quickfix[d.bufnr] = {} + end + + table.insert(_quickfix[d.bufnr], d) + end + + -- Display the new diagnostics + for it, v in pairs(_quickfix) do + vim.diagnostic.set(_namespace, it, v) + end + end +end + +local function PromptBuild() + vim.ui.input( + { + prompt = "Compile: ", + completion = "shellcmdline", + default = _last_build + }, + function(input) + if input ~= nil then + local makeprg = vim.o.makeprg + vim.o.makeprg = input + + ExecuteBuild() + + _last_build = input + vim.o.makeprg = makeprg + end + end + ) +end + -- Buffers local function BufferComplete() @@ -185,6 +269,7 @@ MAP("i", "", "<<") MAP("i", "", BufferComplete, { expr = true }) MAP("n", "s", ManageSplit) MAP("n", "f", ProjectSearch) +MAP("n", "m", PromptBuild) -------------------------------------------------------------------------------- -- Autocommands @@ -193,8 +278,9 @@ MAP("n", "f", ProjectSearch) AUTOCMD('BufEnter', { command = "let b:man_default_sects=\"2,3\"", pattern = "*.c" }) AUTOCMD('BufReadPost', { command = "setlocal nornu", pattern = "quickfix" }) -AUTOCMD('BufWritePre', { callback = TrimBuffer, pattern = "*" }) -AUTOCMD('VimEnter', { callback = MakeScratch, pattern = "*" }) -AUTOCMD('BufWinEnter', { callback = LayoutHelp, pattern = "*" }) -AUTOCMD('WinEnter', { callback = ExpandHelp, pattern = "*" }) -AUTOCMD('WinLeave', { callback = ExpandHelp, pattern = "*" }) +AUTOCMD('BufWritePre', { callback = TrimBuffer, pattern = "*" }) +AUTOCMD('VimEnter', { callback = MakeScratch, pattern = "*" }) +AUTOCMD('BufWinEnter', { callback = LayoutHelp, pattern = "*" }) +AUTOCMD('WinEnter', { callback = ExpandHelp, pattern = "*" }) +AUTOCMD('WinLeave', { callback = ExpandHelp, pattern = "*" }) +AUTOCMD('BufRead', { callback = LoadDiagnostics, pattern = "*" }) diff --git a/install b/install index c9f8137..4a5e428 100755 --- a/install +++ b/install @@ -1,6 +1,18 @@ #!/bin/sh set -e +pushd "$(dirname $0)" > /dev/null + +Push() { + # Installs current dotfiles onto the system + # + echo "[Info] :: Installing dotfiles into $1" + + [[ ! -d "$1" ]] && mkdir -p "$1" + + cp -RT "config" "$1/.config" +} + Pull() { # Allows system changes to be pulled into the repository to be committed # @@ -10,11 +22,14 @@ Pull() { } INSTALL_DIR=${2:-$HOME} -ACTION=${1:-push} +ACTION=$1 [[ -z $INSTALL_DIR ]] && echo "[Error] :: Installation directory not set" && exit 1 case $ACTION in + push) + Push $INSTALL_DIR + ;; pull) Pull $INSTALL_DIR ;; @@ -22,3 +37,5 @@ case $ACTION in echo "usage: $0 [dir]" ;; esac + +popd > /dev/null