clangd lazy spec

This commit is contained in:
dlond 2025-05-29 22:36:54 +12:00
parent 68450346a1
commit aabb6bf2bc
1 changed files with 28 additions and 33 deletions

View File

@ -1,45 +1,48 @@
local M = {} local M = {}
-- local Path = require 'plenary.path'
local pickers = require 'telescope.pickers' local pickers = require 'telescope.pickers'
local finders = require 'telescope.finders' local finders = require 'telescope.finders'
local conf = require('telescope.config').values local conf = require('telescope.config').values
local current_target = './build/debug' local function build_cmd(commands_dir)
local function make_clangd_cmd()
return { return {
'clangd', 'clangd',
'--background-index', '--background-index',
'--clang-tidy', '--clang-tidy',
'--header-insertion=never', '--header-insertion=never',
'--query-driver=' .. vim.fn.exepath 'clang++', '--query-driver=' .. vim.vn.exepath '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'),
'--compile-commands-dir=' .. current_target, '--compile-commands-dir=' .. commands_dir,
} }
end end
local function reload_clangd() function M.reload_clangd(commands_dir)
for _, client in pairs(vim.lsp.get_clients()) do local lspconfig = require 'lspconfig'
if client.name == 'clangd' then
client.stop() commands_dir = commands_dir or './build/debug'
end
end lspconfig.clangd.setup {
M.setup() cmd = build_cmd(commands_dir),
root_dir = lspconfig.util.root_pattern '.git',
single_file_support = true,
capabilities = require('blink.cmp').get_lsp_capabilities(),
}
vim.cmd.edit()
end end
function M.pick_target() function M.pick_commands_dir()
pickers pickers
.new({}, { .new({}, {
prompt_title = 'Choose build target', prompt_title = 'Choose compile_commands.json location',
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()
current_target = entry[1] local commands_dir = entry[1]
require('telescope.actions').close(prompt_bufnr) require('telescope.actions').close(prompt_bufnr)
reload_clangd() M.reload_clangd(commands_dir)
end) end)
return true return true
end, end,
@ -47,19 +50,11 @@ function M.pick_target()
:find() :find()
end end
function M.setup() return {
local lspconfig = require 'lspconfig' 'neovim/nvim-lspconfig',
local capabilities = require('blink.cmp').get_lsp_capabilities() ft = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' },
config = function()
lspconfig.clangd.setup { M.reload_clangd()
cmd = make_clangd_cmd(), vim.keymap.set('n', '<leader>cc', M.pick_commands_dir, { desc = 'Pick location of compile_commands.json for clangd' })
filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' }, end,
root_dir = lspconfig.util.root_pattern '.git',
single_file_support = true,
capabilities = capabilities,
} }
end
M.setup()
return {}