From 8eecbb115e695eed968b6d54476e8346d0e87e59 Mon Sep 17 00:00:00 2001 From: Martin Larsson Date: Thu, 25 Jan 2024 00:21:27 +0100 Subject: [PATCH] Add a code snippet which I will use later to auto generate compile_database.json files when working with clangd --- nvim/init.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/nvim/init.lua b/nvim/init.lua index fdfde98..ac9bfcd 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -31,3 +31,29 @@ require("lazy").setup("plugs") -- See ":help vim.highlight.on_yank()" setup_yank_highlight() + + +-- Save this code snippet for later, it is used to automatically run cmake when saving a file in a project, this way we can have an up to date compile_commands.json file for clangd. +-- TODO: Make sure to only run this when clangd is being used as the language server. Additionally, find a way to make this work on projects with multiple CMakeLists.txt files, or that arent using CMake at all. +-- -- Define a function to find the main.cpp and CMakeLists.txt files using fzf +-- function FindMainAndCMakeLists() +-- local main_cpp = vim.fn.systemlist('fzf --preview "bat {}" --query main.cpp --select-1')[1] +-- local cmake_lists = vim.fn.systemlist('fzf --preview "bat {}" --query CMakeLists.txt --select-1')[1] +-- return main_cpp, cmake_lists +-- end +-- +-- -- Function to update the autocmd based on the found main.cpp and CMakeLists.txt files +-- function UpdateAutocmd(main_cpp_path, cmake_lists_path) +-- if main_cpp_path ~= '' and cmake_lists_path ~= '' then +-- local main_cpp_directory = vim.fn.fnamemodify(main_cpp_path, ':h') +-- vim.api.nvim_exec(string.format([[ +-- autocmd! BufWritePost %s/**/* :call system('cmake %s') +-- ]], main_cpp_directory, cmake_lists_path), false) +-- else +-- print("main.cpp or CMakeLists.txt not found. Autocmd not updated.") +-- end +-- end +-- +-- -- Automatically find main.cpp and CMakeLists.txt and set up the initial autocmd +-- local main_cpp_path, cmake_lists_path = FindMainAndCMakeLists() +-- UpdateAutocmd(main_cpp_path, cmake_lists_path)