This commit is contained in:
dlond 2025-05-27 11:59:56 +12:00
parent 270bf3371a
commit 1d03948bb4
1 changed files with 55 additions and 22 deletions

View File

@ -66,6 +66,7 @@ end
return { return {
pick_executable = pick_executable, pick_executable = pick_executable,
clangd_base_cmd = { clangd_base_cmd = {
'clangd', 'clangd',
'--background-index', '--background-index',
@ -74,7 +75,6 @@ return {
'--query-driver=' .. vim.fn.trim(vim.fn.system 'which clang++'), '--query-driver=' .. vim.fn.trim(vim.fn.system 'which clang++'),
'--resource-dir=' .. vim.fn.trim(vim.fn.system 'clang++ --print-resource-dir'), '--resource-dir=' .. vim.fn.trim(vim.fn.system 'clang++ --print-resource-dir'),
}, },
make_clangd_cmd = function(self, compile_commands_dir) make_clangd_cmd = function(self, compile_commands_dir)
local cmd = vim.deepcopy(self.clangd_base_cmd) local cmd = vim.deepcopy(self.clangd_base_cmd)
table.insert(cmd, '--compile-commands-dir=' .. compile_commands_dir) table.insert(cmd, '--compile-commands-dir=' .. compile_commands_dir)
@ -91,6 +91,7 @@ return {
set_target = function(self, dir) set_target = function(self, dir)
vim.g.current_target_dir = dir vim.g.current_target_dir = dir
print(' set_target:', dir)
self:reload_clangd() self:reload_clangd()
end, end,
@ -108,33 +109,65 @@ return {
end, end,
reload_clangd = function(self) reload_clangd = function(self)
print ' reload_clangd called'
local target = self:get_target()
local cmd = self:make_clangd_cmd(target)
print(' clangd cmd:', vim.inspect(cmd))
local lspconfig = require 'lspconfig' local lspconfig = require 'lspconfig'
local buf = vim.api.nvim_get_current_buf() local configs = require"lspconfig.configs")
local ft = vim.bo[buf].filetype
if vim.api.nvim_buf_get_name(buf) == '' or not vim.tbl_contains({ 'c', 'cpp', 'objc', 'objcpp' }, ft) then local cmd = self:make_clangd_cmd(self:get_target())
vim.notify('Not reloading clangd: no file or wrong filetype', vim.log.levels.WARN) print(' clangd cmd:', vim.inspect(cmd))
return
end
for _, client in ipairs(vim.lsp.get_clients()) do for _, client in ipairs(vim.lsp.get_clients()) do
if client.name == 'clangd' then if client.name == 'clangd' then
print(' stopping old clangd')
client.stop() client.stop()
end end
end end
vim.defer_fn(function() if not configs.clangd then
print ' calling clangd.setup' configs.clangd = {
local cmd = self:make_clangd_cmd(self:get_target()) default_config = {
vim.print(cmd) cmd = cmd,
vim.notify(vim.inspect(cmd), vim.log.levels.INFO) filetypes = { 'c', 'cpp', 'objc', 'objcpp' },
lspconfig.clangd.setup { root_dir = lspconfig.util.root_pattern("compile_commands.json", '.git'),
cmd = self:make_clangd_cmd(self:get_target()), single_file_support = true,
} },
vim.cmd 'edit' }
end, 200) else
end, configs.clangd.default_config.cmd = cmd
end
lspconfig.clangd.setup({ cmd = cmd })
local buf = vim.api.nvim_get_current_buf()
local name = vim.api.nvim_buf_get_name(buf)
loal ft = vim.bo[buf].filetype
if name ~= "" and ft:match('c') then
print(" reopening buffer to auto-attach clangd")
vim.cmd("edit")
else
print(" Skipping buffer reload: name or filetype invalid")
end
end,
list_attached_clients = function()
local clients = vim.lsp.get_clients({ bufnr = 0 })
if vim.tbl_isempty(clients) then
print("No LSP clients attached to this buffer.")
return
end
for _, client in ipairs(clients) do
print("LSP:", client.name, "id:", client.id)
end
end,
list_attached_clients = function()
local clients = vim.lsp.get_clients({ bufnr = 0 })
if vim.tbl_isempty(clients) then
print("No LSP clients attached to this buffer.")
return
end
for _, client in ipairs(clients) do
print("LSP:", client.name, "id:", client.id)
end
end,
} }