simplified clangd setup
This commit is contained in:
parent
c77e2e0a99
commit
75add61c7f
|
@ -1,19 +1,14 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.clang_filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' }
|
M.clang_filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' }
|
||||||
|
|
||||||
local lspconfig = require 'lspconfig'
|
local lspconfig = require 'lspconfig'
|
||||||
local watcher
|
|
||||||
|
|
||||||
local function find_compile_commands()
|
local function find_compile_commands()
|
||||||
local lines = vim.fn.systemlist { 'fd', '-u', '-t', 'f', 'compile_commands.json' }
|
local results = vim.fn.systemlist { 'fd', '-u', '-t', 'f', 'compile_commands.json' }
|
||||||
if vim.tbl_isempty(lines) then
|
table.sort(results, function(a, b)
|
||||||
return nil
|
|
||||||
end
|
|
||||||
table.sort(lines, function(a, b)
|
|
||||||
return a:match 'debug' and not b:match 'debug'
|
return a:match 'debug' and not b:match 'debug'
|
||||||
end)
|
end)
|
||||||
return vim.fn.fnamemodify(lines[1], ':h')
|
return results[1] and vim.fn.fnamemodify(results[1], ':h') or nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.stop_clangd()
|
function M.stop_clangd()
|
||||||
|
@ -25,31 +20,39 @@ function M.stop_clangd()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup_clangd(commands_dir)
|
function M.start_clangd(commands_dir)
|
||||||
vim.notify('[clangd] Setting up with: ' .. commands_dir)
|
|
||||||
M.stop_clangd()
|
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 commands_dir then
|
||||||
|
table.insert(cmd, '--compile_commands-dir=' .. commands_dir)
|
||||||
|
end
|
||||||
|
|
||||||
lspconfig.clangd.setup {
|
lspconfig.clangd.setup {
|
||||||
cmd = {
|
cmd = 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],
|
|
||||||
'--compile-commands-dir=' .. (commands_dir or '.'),
|
|
||||||
},
|
|
||||||
root_dir = lspconfig.util.root_pattern '.git',
|
root_dir = lspconfig.util.root_pattern '.git',
|
||||||
single_file_support = true,
|
-- single_file_support = true,
|
||||||
capabilities = require('blink.cmp').get_lsp_capabilities(),
|
capabilities = require('blink.cmp').get_lsp_capabilities(),
|
||||||
}
|
}
|
||||||
|
|
||||||
-- for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
vim.defer_fn(function()
|
||||||
-- local ft = vim.api.nvim_get_option_value('filetype', { buf = bufnr })
|
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
||||||
-- if vim.tbl_contains(M.clang_filetypes, ft) then
|
local ft = vim.api.nvim_get_option_value('filetype', { buf = bufnr })
|
||||||
-- vim.lsp.buf_attach_client(bufnr, vim.lsp.get_clients({ name = 'clangd' })[1].id)
|
if vim.tbl_contains(M.clang_filetypes, ft) then
|
||||||
-- end
|
local client = vim.lsp.get_clients({ name = 'clangd' })[1]
|
||||||
-- end
|
if client then
|
||||||
|
vim.lsp.buf_attach_client(bufnr, client.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end, 100)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.pick_commands_dir()
|
function M.pick_commands_dir()
|
||||||
|
@ -58,16 +61,16 @@ function M.pick_commands_dir()
|
||||||
local conf = require('telescope.config').values
|
local conf = require('telescope.config').values
|
||||||
pickers
|
pickers
|
||||||
.new({}, {
|
.new({}, {
|
||||||
prompt_title = 'Choose compile_commands.json location',
|
prompt_title = 'Pick compile_commands.json dir',
|
||||||
finder = finders.new_oneshot_job { 'fd', '-u', 'compile_commands.json', '-x', 'dirname', '{}' },
|
finder = finders.new_oneshot_job { 'fd', '-u', 'compile_commands.json', '-x', 'dirname', '{}' },
|
||||||
sorter = conf.generic_sorter {},
|
sorter = conf.generic_sorter {},
|
||||||
attach_mappings = function(_, map)
|
attach_mappings = function(_, map)
|
||||||
map('i', '<CR>', function(prompt_bufnr)
|
map('i', '<CR>', function(prompt_bufnr)
|
||||||
local entry = require('telescope.actions.state').get_selected_entry()
|
local entry = require('telescope.actions.state').get_selected_entry()
|
||||||
local commands_dir = entry[1]
|
local dir = entry[1]
|
||||||
require('telescope.actions').close(prompt_bufnr)
|
require('telescope.actions').close(prompt_bufnr)
|
||||||
if commands_dir ~= '' then
|
if dir then
|
||||||
M.setup_clangd(commands_dir)
|
M.start_clangd(dir)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
return true
|
return true
|
||||||
|
@ -76,58 +79,53 @@ function M.pick_commands_dir()
|
||||||
:find()
|
:find()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.watch_compile_commands()
|
-- function M.watch_compile_commands()
|
||||||
vim.notify('clangd: Starting watcher for compile_commands.json', vim.log.levels.INFO)
|
-- vim.notify('clangd: Starting watcher for compile_commands.json', vim.log.levels.INFO)
|
||||||
local uv = vim.uv or vim.loop
|
-- local uv = vim.uv or vim.loop
|
||||||
local check_interval = 1000 -- ms
|
-- local check_interval = 1000 -- ms
|
||||||
|
--
|
||||||
local function try_attach()
|
-- local function try_attach()
|
||||||
local dir = find_compile_commands()
|
-- local dir = find_compile_commands()
|
||||||
if dir then
|
-- if dir then
|
||||||
vim.notify('[clangd] Found compile_commands at: ' .. dir)
|
-- vim.notify('[clangd] Found compile_commands at: ' .. dir)
|
||||||
M.setup_clangd(dir)
|
-- M.setup_clangd(dir)
|
||||||
return true
|
-- return true
|
||||||
end
|
-- end
|
||||||
return false
|
-- return false
|
||||||
end
|
-- end
|
||||||
|
--
|
||||||
local timer = uv.new_timer()
|
-- local timer = uv.new_timer()
|
||||||
timer:start(0, check_interval, function()
|
-- timer:start(0, check_interval, function()
|
||||||
if try_attach() then
|
-- if try_attach() then
|
||||||
timer:stop()
|
-- timer:stop()
|
||||||
timer:close()
|
-- timer:close()
|
||||||
end
|
-- end
|
||||||
end)
|
-- end)
|
||||||
|
--
|
||||||
local fs_watcher = uv.new_fs_event()
|
-- local fs_watcher = uv.new_fs_event()
|
||||||
fs_watcher:start(vim.fn.getcwd(), { recursive = true }, function(_, fname, status)
|
-- fs_watcher:start(vim.fn.getcwd(), { recursive = true }, function(_, fname, status)
|
||||||
if fname and fname:match 'compile_commands%.json$' and status.change then
|
-- if fname and fname:match 'compile_commands%.json$' and status.change then
|
||||||
fs_watcher:stop()
|
-- fs_watcher:stop()
|
||||||
vim.schedule(function()
|
-- vim.schedule(function()
|
||||||
vim.notify '[clangd] Detected compile_commands.json change, reloading ...'
|
-- vim.notify '[clangd] Detected compile_commands.json change, reloading ...'
|
||||||
M.setup_clangd(vim.fn.fnamemodify(fname, ':h'))
|
-- M.setup_clangd(vim.fn.fnamemodify(fname, ':h'))
|
||||||
end)
|
-- end)
|
||||||
end
|
-- end
|
||||||
end)
|
-- end)
|
||||||
end
|
-- end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'neovim/nvim-lspconfig',
|
'neovim/nvim-lspconfig',
|
||||||
ft = M.clang_filetypes,
|
ft = M.clang_filetypes,
|
||||||
config = function()
|
config = function()
|
||||||
vim.api.nvim_create_autocmd('FileType', {
|
local dir = find_compile_commands()
|
||||||
pattern = M.clang_filetypes,
|
if dir then
|
||||||
callback = function()
|
vim.notify('[clangd] Starting with compile_commands from: ' .. dir)
|
||||||
local dir = find_compile_commands()
|
M.start_clangd(dir)
|
||||||
if dir then
|
else
|
||||||
vim.notify('[clangd] Found compile_commands at: ' .. dir)
|
vim.notify '[clangd] No compile_commands found. Using fallback config.\nUse <leader>cc to manually set location.'
|
||||||
M.setup_clangd(dir)
|
M.start_clangd(nil)
|
||||||
else
|
end
|
||||||
vim.notify '[clangd] No compile_commands found, watching ...'
|
|
||||||
M.watch_compile_commands()
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
vim.keymap.set('n', '<leader>cc', M.pick_commands_dir, { desc = 'Pick location of compile_commands.json for clangd' })
|
vim.keymap.set('n', '<leader>cc', M.pick_commands_dir, { desc = 'Pick location of compile_commands.json for clangd' })
|
||||||
end,
|
end,
|
||||||
|
|
Loading…
Reference in New Issue