refactor
This commit is contained in:
parent
ee0741597f
commit
d523eeb957
|
@ -8,7 +8,7 @@ local function find_compile_commands()
|
||||||
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
|
return vim.fn.fnamemodify(results[1] or '', ':h')
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.stop_clangd()
|
function M.stop_clangd()
|
||||||
|
@ -20,7 +20,7 @@ function M.stop_clangd()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.start_clangd(commands_dir)
|
function M.start_clangd(dir)
|
||||||
M.stop_clangd()
|
M.stop_clangd()
|
||||||
|
|
||||||
local cmd = {
|
local cmd = {
|
||||||
|
@ -31,32 +31,43 @@ function M.start_clangd(commands_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 commands_dir then
|
if dir and dir ~= '' then
|
||||||
table.insert(cmd, '--compile-commands-dir=' .. commands_dir)
|
table.insert(cmd, '--compile-commands-dir=' .. dir)
|
||||||
|
vim.notify('[clangd] Setting up with: ' .. dir)
|
||||||
|
else
|
||||||
|
table.insert(cmd, '--compile-commands-dir=.')
|
||||||
|
vim.notify '[clangd] No compile_commands.json found.\nUse <leader>cc to manually set location'
|
||||||
end
|
end
|
||||||
|
|
||||||
print(vim.inspect(cmd))
|
|
||||||
|
|
||||||
lspconfig.clangd.setup {
|
lspconfig.clangd.setup {
|
||||||
cmd = cmd,
|
cmd = cmd,
|
||||||
|
filetypes = M.clang_filetypes,
|
||||||
root_dir = lspconfig.util.root_pattern '.git',
|
root_dir = lspconfig.util.root_pattern '.git',
|
||||||
-- single_file_support = true,
|
|
||||||
capabilities = require('blink.cmp').get_lsp_capabilities(),
|
capabilities = require('blink.cmp').get_lsp_capabilities(),
|
||||||
|
single_file_support = true,
|
||||||
}
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.reload_clangd()
|
||||||
|
M.stop_clangd()
|
||||||
|
|
||||||
vim.defer_fn(function()
|
vim.defer_fn(function()
|
||||||
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
M.start_clangd(find_compile_commands())
|
||||||
local ft = vim.api.nvim_get_option_value('filetype', { buf = bufnr })
|
|
||||||
if vim.tbl_contains(M.clang_filetypes, ft) then
|
|
||||||
local client = vim.lsp.get_clients({ name = 'clangd' })[1]
|
|
||||||
if client then
|
|
||||||
vim.lsp.buf_attach_client(bufnr, client.id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end, 100)
|
end, 100)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
||||||
|
-- local ft = vim.api.nvim_get_option_value('filetype', { buf = bufnr })
|
||||||
|
-- if vim.tbl_contains(M.clang_filetypes, ft) then
|
||||||
|
-- local client = vim.lsp.get_clients({ name = 'clangd' })[1]
|
||||||
|
-- if client then
|
||||||
|
-- vim.lsp.buf_attach_client(bufnr, client.id)
|
||||||
|
-- end
|
||||||
|
-- end
|
||||||
|
-- end
|
||||||
|
-- end, 100)
|
||||||
|
-- end
|
||||||
|
|
||||||
function M.pick_commands_dir()
|
function M.pick_commands_dir()
|
||||||
local pickers = require 'telescope.pickers'
|
local pickers = require 'telescope.pickers'
|
||||||
local finders = require 'telescope.finders'
|
local finders = require 'telescope.finders'
|
||||||
|
@ -69,11 +80,11 @@ function M.pick_commands_dir()
|
||||||
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 dir = entry[1]
|
|
||||||
require('telescope.actions').close(prompt_bufnr)
|
require('telescope.actions').close(prompt_bufnr)
|
||||||
if dir then
|
M.stop_clangd()
|
||||||
M.start_clangd(dir)
|
vim.defer_fn(function()
|
||||||
end
|
M.start_clangd(entry[1])
|
||||||
|
end, 100)
|
||||||
end)
|
end)
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
|
@ -120,22 +131,23 @@ 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', {
|
-- vim.api.nvim_create_autocmd('FileType', {
|
||||||
pattern = M.clang_filetypes,
|
-- pattern = M.clang_filetypes,
|
||||||
group = vim.api.nvim_create_augroup('clangd-setup', { clear = true }),
|
-- group = vim.api.nvim_create_augroup('clangd-setup', { clear = true }),
|
||||||
callback = function()
|
-- callback = function()
|
||||||
if not vim.lsp.get_clients({ name = 'clangd' })[1] then
|
-- if not vim.lsp.get_clients({ name = 'clangd' })[1] then
|
||||||
local dir = find_compile_commands()
|
-- local dir = find_compile_commands()
|
||||||
if dir then
|
-- if dir then
|
||||||
vim.notify('[clangd] Starting with compile_commands from: ' .. dir)
|
-- vim.notify('[clangd] Starting with compile_commands from: ' .. dir)
|
||||||
M.start_clangd(dir)
|
-- M.start_clangd(dir)
|
||||||
else
|
-- else
|
||||||
vim.notify '[clangd] No compile_commands found. Using fallback config.\nUse <leader>cc to manually set location.'
|
-- vim.notify '[clangd] No compile_commands found. Using fallback config.\nUse <leader>cc to manually set location.'
|
||||||
M.start_clangd(nil)
|
-- M.start_clangd(nil)
|
||||||
end
|
-- end
|
||||||
end
|
-- end
|
||||||
end,
|
-- end,
|
||||||
})
|
-- })
|
||||||
|
M.start_clangd(find_compile_commands())
|
||||||
|
|
||||||
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