just get clangd working
This commit is contained in:
parent
46d13be5f6
commit
551ee11550
2
init.lua
2
init.lua
|
@ -665,7 +665,7 @@ require('lazy').setup({
|
||||||
-- - settings (table): Override the default settings passed when initializing the server.
|
-- - settings (table): Override the default settings passed when initializing the server.
|
||||||
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
|
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
|
||||||
local servers = {
|
local servers = {
|
||||||
-- clangd = {},
|
clangd = require('custom.plugins.lsp.clangd').get_server_config(),
|
||||||
-- gopls = {},
|
-- gopls = {},
|
||||||
-- pyright = {},
|
-- pyright = {},
|
||||||
-- rust_analyzer = {},
|
-- rust_analyzer = {},
|
||||||
|
|
|
@ -1,32 +1,62 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
M.clang_filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' }
|
|
||||||
|
|
||||||
local lspconfig = require 'lspconfig'
|
|
||||||
local uv = vim.uv or vim.loop
|
local uv = vim.uv or vim.loop
|
||||||
local watcher, debounce_timer
|
local lspconfig_util = require 'lspconfig.util'
|
||||||
|
|
||||||
|
local debounce_timer, watcher
|
||||||
|
|
||||||
local function find_compile_commands()
|
local function find_compile_commands()
|
||||||
local results = vim.fn.systemlist { 'fd', '-u', '-t', 'f', 'compile_commands.json' }
|
local results = vim.fn.systemlist { 'fd', '-u', '-t', 'f', 'compile_commands.json' }
|
||||||
table.sort(results, function(a, b)
|
table.sort(results, function(a, b)
|
||||||
return a:match 'debug' and not b:match 'debug'
|
return a:match 'debug' and not b:match 'debug'
|
||||||
end)
|
end)
|
||||||
return results[1] and vim.fn.fnamemodify(results[1], ':h') or nil
|
local path = results[1]
|
||||||
|
return path and vim.fn.fnamemodify(path, ':h') or nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.stop_clangd()
|
local function start_watcher(dir, reload_callback)
|
||||||
for _, client in ipairs(vim.lsp.get_clients()) do
|
if watcher then
|
||||||
if client.name == 'clangd' then
|
watcher:stop()
|
||||||
pcall(function()
|
watcher:close()
|
||||||
client.stop { force = true }
|
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)
|
||||||
vim.notify '[clangd] Stopped clangd'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.start_clangd(dir)
|
M.get_server_config = function()
|
||||||
M.stop_clangd()
|
local dir = find_compile_commands()
|
||||||
|
|
||||||
local cmd = {
|
local cmd = {
|
||||||
'clangd',
|
'clangd',
|
||||||
'--background-index',
|
'--background-index',
|
||||||
|
@ -37,107 +67,44 @@ function M.start_clangd(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
if dir then
|
if dir then
|
||||||
vim.notify('[clangd] Setting up with: ' .. dir)
|
|
||||||
table.insert(cmd, '--compile-commands-dir=' .. dir)
|
table.insert(cmd, '--compile-commands-dir=' .. dir)
|
||||||
else
|
else
|
||||||
vim.notify '[clangd] No compile_commands.json found.\nUse <leader>lc to manually set location.'
|
vim.notify '[clangd] Could not find compile_commands.json. clangd may still work in single-file mode.'
|
||||||
end
|
end
|
||||||
|
|
||||||
lspconfig.clangd.setup {
|
return {
|
||||||
cmd = cmd,
|
cmd = cmd,
|
||||||
filetypes = M.clang_filetypes,
|
filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' },
|
||||||
root_dir = lspconfig.util.root_pattern '.git',
|
|
||||||
capabilities = require('blink.cmp').get_lsp_capabilities(),
|
capabilities = require('blink.cmp').get_lsp_capabilities(),
|
||||||
|
root_dir = lspconfig_util.root_pattern '.git',
|
||||||
single_file_support = true,
|
single_file_support = true,
|
||||||
on_attach = function(client, bufnr)
|
on_attach = function(client, bufnr)
|
||||||
vim.notify('[clangd] Attached to buffer ' .. 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,
|
||||||
}
|
}
|
||||||
|
|
||||||
if dir then
|
|
||||||
M.watch_compile_commands(dir)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.watch_compile_commands(dir)
|
vim.api.nvim_create_user_command('ReloadClangd', function()
|
||||||
if watcher then
|
local clients = vim.lsp.get_active_clients()
|
||||||
watcher:stop()
|
for _, client in ipairs(clients) do
|
||||||
watcher:close()
|
if client.name == 'clangd' then
|
||||||
|
client.stop { force = true }
|
||||||
end
|
end
|
||||||
if debounce_timer then
|
|
||||||
debounce_timer:stop()
|
|
||||||
debounce_timer:close()
|
|
||||||
end
|
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
|
|
||||||
debounce_timer = uv.new_timer()
|
|
||||||
debounce_timer:start(200, 0, function()
|
|
||||||
vim.schedule(function()
|
|
||||||
vim.notify '[clangd] Detected compile_commands.json change. Reloading ...'
|
|
||||||
local new_dir = vim.fn.fnamemodify(fname, ':h')
|
|
||||||
M.start_clangd(new_dir)
|
|
||||||
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', '<CR>', function(prompt_bufnr)
|
|
||||||
local entry = require('telescope.actions.state').get_selected_entry()
|
|
||||||
require('telescope.actions').close(prompt_bufnr)
|
|
||||||
if entry then
|
|
||||||
vim.defer_fn(function()
|
vim.defer_fn(function()
|
||||||
M.start_clangd(entry[1])
|
vim.cmd 'edit'
|
||||||
end, 100)
|
end, 100)
|
||||||
end
|
|
||||||
end)
|
|
||||||
return true
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
:find()
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
vim.notify '[clangd] Restarted manually via :ReloadClangd'
|
||||||
'neovim/nvim-lspconfig',
|
end, { desc = 'manually reload clangd with updated compile_commands.json' })
|
||||||
ft = M.clang_filetypes,
|
|
||||||
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
|
|
||||||
vim.notify('[clangd] BufReadPost fired for ft=' .. ft)
|
|
||||||
local dir = find_compile_commands()
|
|
||||||
M.start_clangd(dir)
|
|
||||||
vim.api.nvim_clear_autocmds { group = 'clangd-once' }
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
vim.keymap.set('n', '<leader>lc', M.pick_commands_dir, {
|
return M
|
||||||
desc = '[L]ocate [c]ompile_commands.json for clangd',
|
|
||||||
})
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue