Add clangd support

This commit is contained in:
Steins7 2023-02-11 16:03:56 +01:00
parent d54ba8d8c6
commit 52f6ce4fb5
2 changed files with 44 additions and 25 deletions

View File

@ -25,14 +25,17 @@ Here is a list of all external depencies you'll need:
- universal ctags, ctags won't work (note that arch's universal ctags package is named ctags...), for tagbar - universal ctags, ctags won't work (note that arch's universal ctags package is named ctags...), for tagbar
- tree-sitter, for syntax coloration - tree-sitter, for syntax coloration
- rust-analyzer, as LSP server - rust-analyzer and clangd as LSP servers
On arch, you can just use the following command : On arch, you can just use the following command :
``` ```
pacman -Syu ctags tree-sitter rust-analyzer pacman -Syu ctags tree-sitter rust-analyzer clangd
``` ```
Once the dependencies are installed, type `:checkhealth` to verify that everything is properly detected. You should, at most, see a warning about node and another about the clipboard. Once the dependencies are installed, type `:checkhealth` to verify that everything is properly detected. You should, at most, see a warning about node and another about the clipboard.
For clangd to work properly, create a compile_flags.txt file at the root of the project and fill it
with the flags used for compilation (one per line). Cpu-specific flags may cause the analysis to
fail and should be ignored (e.g. mcpu, mthumb, ...)
### Syntax coloration ### Syntax coloration

View File

@ -9,6 +9,29 @@ local feedkey = function(key, mode)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true)
end end
-- LSP keybindings
local lsp_on_attach = function(_, bufnr)
local nmap = function(keys, func, desc)
if desc then
desc = 'LSP: ' .. desc
end
vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
end
-- Define standard LSP shortcuts
nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
nmap('gd', vim.lsp.buf.definition, '[G]oto [d]efinition')
nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
-- See `:help K` for why this keymap
nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
-- Create a command `:Format` local to the LSP buffer
vim.api.nvim_buf_create_user_command(bufnr, 'Format',
vim.lsp.buf.format or vim.lsp.buf.formatting, { desc = 'Format current buffer with LSP' })
end
-- plugins definition -- plugins definition
local function manage_plugins() local function manage_plugins()
@ -159,30 +182,22 @@ local function manage_plugins()
}, },
-- LSP configuration -- LSP configuration
["https://github.com/neovim/nvim-lspconfig.git"] = {}, ["https://github.com/neovim/nvim-lspconfig.git"] = {
["https://github.com/simrat39/rust-tools.nvim.git"] = {
packadd_after = { packadd_after = {
["https://github.com/neovim/nvim-lspconfig.git"] = true,
["https://github.com/hrsh7th/nvim-cmp.git"] = true, ["https://github.com/hrsh7th/nvim-cmp.git"] = true,
}, },
packadd_hook = function() packadd_hook = function()
local rt = require("rust-tools")
local capabilities = require("cmp_nvim_lsp").default_capabilities() local capabilities = require("cmp_nvim_lsp").default_capabilities()
local lsp_config = require("lspconfig")
local servers = { "clangd", "rust_analyzer" }
rt.setup({ for _, lsp in ipairs(servers) do
server = { lsp_config[lsp].setup {
capabilities = capabilities, capabilities = capabilities,
on_attach = function(_, bufnr) on_attach = lsp_on_attach,
-- Hover actions }
vim.keymap.set("n", "<C-space>",
rt.hover_actions.hover_actions, { buffer = bufnr })
---- Code action groups
vim.keymap.set("n", "<Leader>a",
rt.code_action_group.code_action_group, { buffer = bufnr })
end,
},
})
end end
end,
}, },
-- LSP alternative -- LSP alternative
@ -273,3 +288,4 @@ else
manage_plugins() manage_plugins()
end end