diff --git a/README.md b/README.md index 64297fe..3570a45 100644 --- a/README.md +++ b/README.md @@ -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 - 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 : ``` -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. +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 diff --git a/lua/plugins.lua b/lua/plugins.lua index b71ce41..e8eb8ee 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -9,6 +9,29 @@ local feedkey = function(key, mode) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) 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('rn', vim.lsp.buf.rename, '[R]e[n]ame') + nmap('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 local function manage_plugins() @@ -159,31 +182,23 @@ local function manage_plugins() }, -- LSP configuration - ["https://github.com/neovim/nvim-lspconfig.git"] = {}, - ["https://github.com/simrat39/rust-tools.nvim.git"] = { - packadd_after = { - ["https://github.com/neovim/nvim-lspconfig.git"] = true, + ["https://github.com/neovim/nvim-lspconfig.git"] = { + packadd_after = { ["https://github.com/hrsh7th/nvim-cmp.git"] = true, - }, - packadd_hook = function() - local rt = require("rust-tools") - local capabilities = require("cmp_nvim_lsp").default_capabilities() + }, + packadd_hook = function() + local capabilities = require("cmp_nvim_lsp").default_capabilities() + local lsp_config = require("lspconfig") + local servers = { "clangd", "rust_analyzer" } - rt.setup({ - server = { - capabilities = capabilities, - on_attach = function(_, bufnr) - -- Hover actions - vim.keymap.set("n", "", - rt.hover_actions.hover_actions, { buffer = bufnr }) - ---- Code action groups - vim.keymap.set("n", "a", - rt.code_action_group.code_action_group, { buffer = bufnr }) - end, - }, - }) - end - }, + for _, lsp in ipairs(servers) do + lsp_config[lsp].setup { + capabilities = capabilities, + on_attach = lsp_on_attach, + } + end + end, + }, -- LSP alternative ["https://github.com/preservim/tagbar.git" ] = { @@ -273,3 +288,4 @@ else manage_plugins() end +