local M = {} M.clang_filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' } local lspconfig = require 'lspconfig' function M.find_compile_commands() local results = vim.fn.systemlist { 'fd', '-u', '-t', 'f', 'compile_commands.json' } if vim.tbl_isempty(results) then return nil end table.sort(results, function(a, b) return a:match 'debug' and not b:match 'debug' end) return vim.fn.fnamemodify(results[1], ':h') end function M.stop_clangd() for _, client in ipairs(vim.lsp.get_clients()) do if client.name == 'clangd' then pcall(function() client.stop { force = true } end) vim.notify '[clangd] Stopped clangd' end end end function M.start_clangd(dir) M.stop_clangd() local cmd = { 'clangd', '--background-index', '--clang-tidy', '--header-insertion=never', '--query-driver=' .. vim.fn.exepath 'clang++', '--resource-dir=' .. vim.fn.systemlist({ 'clang++', '--print-resource-dir' })[1], } if dir and dir ~= '' then vim.notify('[clangd] Setting up with: ' .. dir) table.insert(cmd, '--compile-commands-dir=' .. dir) M.watch_compile_commands(dir) else vim.notify '[clangd] Could not find compile_commands.json.\nUse lc to manually set location when available.' end print(vim.inspect(cmd)) lspconfig.clangd.setup { cmd = cmd, filetypes = M.clang_filetypes, root_dir = lspconfig.util.root_pattern '.git', capabilities = require('blink.cmp').get_lsp_capabilities(), single_file_support = true, on_attach = function(client, bufnr) vim.notify('[clangd] Attached to buffer ' .. bufnr) end, } end local watcher, debounce_timer function M.watch_compile_commands(dir) local uv = vim.uv or vim.loop if watcher then watcher:stop() watcher:close() watcher = nil end if debounce_timer then debounce_timer:stop() debounce_timer:close() debounce_timer = nil end watcher = uv.new_fs_event() watcher:start( dir, { recursive = true }, vim.schedule_wrap(function(err, fname, status) if err then vim.notify('[clangd] Watcher error: ' .. err, vim.log.levels.ERROR) return end if fname and fname:match '[/\\]compile_commands%.json$' and status.change then vim.notify('[clangd] Watcher triggered: ' .. fname) if debounce_timer then debounce_timer:stop() debounce_timer:close() end debounce_timer = uv.new_timer() debounce_timer:start(200, 0, function() vim.schedule(function() vim.notify '[clangd] Detected compile_commands.json change. Reloading ...' M.start_clangd(vim.fn.fnamemodify(fname, ':h')) end) end) end end) ) end function M.pick_commands_dir() local pickers = require 'telescope.pickers' local finders = require 'telescope.finders' local conf = require('telescope.config').values pickers .new({}, { prompt_title = 'Pick compile_commands.json dir', finder = finders.new_oneshot_job { 'fd', '-u', 'compile_commands.json', '-x', 'dirname', '{}' }, sorter = conf.generic_sorter {}, attach_mappings = function(_, map) map('i', '', function(prompt_bufnr) local entry = require('telescope.actions.state').get_selected_entry() require('telescope.actions').close(prompt_bufnr) vim.defer_fn(function() if entry then if type(entry[1]) == 'string' then vim.notify('[clangd] pick_commands_dir: ' .. entry[1]) M.start_clangd(entry[1]) else vim.notify('[clangd] pick_commands_dir: ' .. entry[1]) end else vim.notify '[clangd] pick_commands_dir is nil' end end, 100) end) return true end, }) :find() end return { 'neovim/nvim-lspconfig', config = function() vim.api.nvim_create_autocmd('BufReadPost', { group = vim.api.nvim_create_augroup('clangd-once', { clear = true }), pattern = '*', callback = function(args) local ft = vim.bo[args.buf].filetype if vim.tbl_contains(M.clang_filetypes, ft) then local dir = M.find_compile_commands() M.start_clangd(dir) vim.api.nvim_clear_autocmds { group = 'clangd-once' } end end, }) vim.keymap.set('n', 'lc', M.pick_commands_dir, { desc = '[L]ocate [c]ompile_commands.json for clangd', }) end, }