Optimize Neovim configuration structure and LSP setup

- Remove duplicate nvim-web-devicons loading (already in kickstart)
- Add clangd configuration to custom LSP setup with proper flags
- Replace complex clangd_helper with universal LSP reload keybind (<leader>lr)
- Clean up unused clangd target selection keybind
- Streamline plugin organization for better maintainability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
dlond 2025-07-26 19:56:13 +12:00
parent e9c3342170
commit 68f26dc50d
8 changed files with 48 additions and 140 deletions

View file

@ -1,110 +0,0 @@
local M = {}
local uv = vim.uv or vim.loop
local lspconfig_util = require 'lspconfig.util'
local debounce_timer, watcher
local function find_compile_commands()
local results = vim.fn.systemlist { 'fd', '-u', '-t', 'f', 'compile_commands.json' }
table.sort(results, function(a, b)
return a:match 'debug' and not b:match 'debug'
end)
local path = results[1]
return path and vim.fn.fnamemodify(path, ':h') or nil
end
local function start_watcher(dir, reload_callback)
if watcher then
watcher:stop()
watcher:close()
watcher = nil
end
if debounce_timer then
debounce_timer:stop()
debounce_timer:close()
debounce_timer = nil
end
if not dir then
return
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
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. Restarting clangd...'
reload_callback()
end)
end)
end
end)
)
end
M.get_server_config = function()
local dir = find_compile_commands()
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 then
table.insert(cmd, '--compile-commands-dir=' .. dir)
else
vim.notify '[clangd] Could not find compile_commands.json. clangd may still work in single-file mode.'
end
return {
cmd = cmd,
filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' },
capabilities = require('blink.cmp').get_lsp_capabilities(),
root_dir = lspconfig_util.root_pattern '.git',
single_file_support = true,
on_attach = function(client, bufnr)
vim.notify('[clangd] Attached to buffer ' .. bufnr)
if dir then
start_watcher(dir, function()
client.stop()
vim.defer_fn(function()
vim.cmd 'edit' -- reloads current buffer to re-trigger LSP attach
end, 100)
end)
end
end,
}
end
vim.api.nvim_create_user_command('ReloadClangd', function()
local clients = vim.lsp.get_active_clients()
for _, client in ipairs(clients) do
if client.name == 'clangd' then
client.stop { force = true }
end
end
vim.defer_fn(function()
vim.cmd 'edit'
end, 100)
vim.notify '[clangd] Restarted manually via :ReloadClangd'
end, { desc = 'manually reload clangd with updated compile_commands.json' })
return M

View file

@ -1,4 +1,3 @@
return {
require 'custom.plugins.lsp.lsp',
require 'custom.plugins.lsp.clangd',
}

View file

@ -5,12 +5,25 @@ return {
{
'neovim/nvim-lspconfig',
-- only load when editing these filetypes (optional)
ft = { 'python', 'lua', 'nix', 'rust', 'go' },
ft = { 'python', 'lua', 'nix', 'rust', 'go', 'c', 'cpp' },
opts = function()
local lspconfig = require 'lspconfig'
local capabilities = require('blink.cmp').get_lsp_capabilities()
local servers = {
clangd = {
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],
},
filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' },
root_dir = require('lspconfig.util').root_pattern('.git'),
single_file_support = true,
},
pyright = {
settings = {
python = {
@ -32,20 +45,6 @@ return {
filetypes = { 'cmake' },
root_dir = require('lspconfig.util').root_pattern('CMakeLists.txt', '.git'),
},
clangd = (function()
local cmd = { 'clangd', '--background-index' }
return {
cmd = cmd,
filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' },
root_dir = lspconfig.util.root_pattern('compile_commands.json', '.git'),
single_file_support = true,
-- on_attach = function(client, bufnr)
-- local util = require(custom.plugins.lsp.clangd_helper)
-- util.notify_compile_commands(bufnr)
-- util.start_compile_commands_watcher(client, bufnr)
-- end,
}
end)(),
}
for name, config in pairs(servers) do