lazy clangd
This commit is contained in:
parent
9cbdbe7a82
commit
46d13be5f6
|
@ -2,17 +2,15 @@ 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 uv = vim.uv or vim.loop
|
||||||
|
local watcher, debounce_timer
|
||||||
|
|
||||||
function M.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' }
|
||||||
if vim.tbl_isempty(results) then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
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 vim.fn.fnamemodify(results[1], ':h')
|
return results[1] and vim.fn.fnamemodify(results[1], ':h') or nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.stop_clangd()
|
function M.stop_clangd()
|
||||||
|
@ -37,15 +35,14 @@ function M.start_clangd(dir)
|
||||||
'--query-driver=' .. vim.fn.exepath 'clang++',
|
'--query-driver=' .. vim.fn.exepath 'clang++',
|
||||||
'--resource-dir=' .. vim.fn.systemlist({ 'clang++', '--print-resource-dir' })[1],
|
'--resource-dir=' .. vim.fn.systemlist({ 'clang++', '--print-resource-dir' })[1],
|
||||||
}
|
}
|
||||||
if not dir or dir == '' then
|
|
||||||
vim.notify '[clangd] Could not find compile_commands.json.\nUse <leader>lc to manually set location when available.'
|
if dir then
|
||||||
dir = '.'
|
|
||||||
end
|
|
||||||
vim.notify('[clangd] Setting up with: ' .. dir)
|
vim.notify('[clangd] Setting up with: ' .. dir)
|
||||||
table.insert(cmd, '--compile-commands-dir=' .. dir)
|
table.insert(cmd, '--compile-commands-dir=' .. dir)
|
||||||
M.watch_compile_commands(dir)
|
else
|
||||||
|
vim.notify '[clangd] No compile_commands.json found.\nUse <leader>lc to manually set location.'
|
||||||
|
end
|
||||||
|
|
||||||
print(vim.inspect(cmd))
|
|
||||||
lspconfig.clangd.setup {
|
lspconfig.clangd.setup {
|
||||||
cmd = cmd,
|
cmd = cmd,
|
||||||
filetypes = M.clang_filetypes,
|
filetypes = M.clang_filetypes,
|
||||||
|
@ -56,23 +53,20 @@ function M.start_clangd(dir)
|
||||||
vim.notify('[clangd] Attached to buffer ' .. bufnr)
|
vim.notify('[clangd] Attached to buffer ' .. bufnr)
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dir then
|
||||||
|
M.watch_compile_commands(dir)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local watcher, debounce_timer
|
|
||||||
|
|
||||||
function M.watch_compile_commands(dir)
|
function M.watch_compile_commands(dir)
|
||||||
local uv = vim.uv or vim.loop
|
|
||||||
|
|
||||||
if watcher then
|
if watcher then
|
||||||
watcher:stop()
|
watcher:stop()
|
||||||
watcher:close()
|
watcher:close()
|
||||||
watcher = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if debounce_timer then
|
if debounce_timer then
|
||||||
debounce_timer:stop()
|
debounce_timer:stop()
|
||||||
debounce_timer:close()
|
debounce_timer:close()
|
||||||
debounce_timer = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
watcher = uv.new_fs_event()
|
watcher = uv.new_fs_event()
|
||||||
|
@ -86,16 +80,12 @@ function M.watch_compile_commands(dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
if fname and fname:match '[/\\]compile_commands%.json$' and status.change then
|
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 = uv.new_timer()
|
||||||
debounce_timer:start(200, 0, function()
|
debounce_timer:start(200, 0, function()
|
||||||
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.start_clangd(vim.fn.fnamemodify(fname, ':h'))
|
local new_dir = vim.fn.fnamemodify(fname, ':h')
|
||||||
|
M.start_clangd(new_dir)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
@ -116,18 +106,11 @@ function M.pick_commands_dir()
|
||||||
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()
|
||||||
require('telescope.actions').close(prompt_bufnr)
|
require('telescope.actions').close(prompt_bufnr)
|
||||||
vim.defer_fn(function()
|
|
||||||
if entry then
|
if entry then
|
||||||
if type(entry[1]) == 'string' then
|
vim.defer_fn(function()
|
||||||
vim.notify('[clangd] pick_commands_dir: ' .. entry[1])
|
|
||||||
M.start_clangd(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, 100)
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
|
@ -137,6 +120,7 @@ end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'neovim/nvim-lspconfig',
|
'neovim/nvim-lspconfig',
|
||||||
|
ft = M.clang_filetypes,
|
||||||
config = function()
|
config = function()
|
||||||
vim.api.nvim_create_autocmd('BufReadPost', {
|
vim.api.nvim_create_autocmd('BufReadPost', {
|
||||||
group = vim.api.nvim_create_augroup('clangd-once', { clear = true }),
|
group = vim.api.nvim_create_augroup('clangd-once', { clear = true }),
|
||||||
|
@ -144,12 +128,12 @@ return {
|
||||||
callback = function(args)
|
callback = function(args)
|
||||||
local ft = vim.bo[args.buf].filetype
|
local ft = vim.bo[args.buf].filetype
|
||||||
if vim.tbl_contains(M.clang_filetypes, ft) then
|
if vim.tbl_contains(M.clang_filetypes, ft) then
|
||||||
local dir = M.find_compile_commands()
|
vim.notify('[clangd] BufReadPost fired for ft=' .. ft)
|
||||||
|
local dir = find_compile_commands()
|
||||||
M.start_clangd(dir)
|
M.start_clangd(dir)
|
||||||
vim.api.nvim_clear_autocmds { group = 'clangd-once' }
|
vim.api.nvim_clear_autocmds { group = 'clangd-once' }
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
once = true,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.keymap.set('n', '<leader>lc', M.pick_commands_dir, {
|
vim.keymap.set('n', '<leader>lc', M.pick_commands_dir, {
|
||||||
|
|
Loading…
Reference in New Issue