From 5b6685b165752753199db21b5a9b37e643f41cb4 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Fri, 10 Sep 2021 13:56:25 +0530 Subject: [PATCH] nvim: lsp: Enable clangd for C and C++ --- nvim/.config/nvim/lua/lsp.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nvim/.config/nvim/lua/lsp.lua b/nvim/.config/nvim/lua/lsp.lua index 809c69c..d01d164 100644 --- a/nvim/.config/nvim/lua/lsp.lua +++ b/nvim/.config/nvim/lua/lsp.lua @@ -3,6 +3,7 @@ local protocol = require('vim.lsp.protocol') local signature = require('lsp_signature') local ts_utils = require("nvim-lsp-ts-utils") local null_ls = require("null-ls") +local util = require("lspconfig/util") -- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/ function preview_location(location, context, before_context) @@ -199,6 +200,21 @@ for _, lsp in ipairs(servers) do } end +nvim_lsp.clangd.setup { + on_attach = on_attach, + default_config = { + cmd = { "clangd", "--background-index", "--pch-storage=memory", "--clang-tidy", "--suggest-missing-includes" }, + filetypes = { 'c', 'cpp' }, + root_dir = function(fname) + -- We specify build/compile_commands.json as that is where the compile_commands.json + -- gets generated automatically for meson projects. + local root_pattern = util.root_pattern('build/compile_commands.json', 'compile_commands.json', 'compile_flags.txt', '.git') + local filename = util.path.is_absolute(fname) and fname or util.path.join(vim.loop.cwd(), fname) + return root_pattern(filename) or util.path.dirname(filename) + end, + } +} + -- See https://github.com/sumneko/lua-language-server/wiki/Setting-without-VSCode#neovim-with-built-in-lsp-client -- on why we do this library thing -- Idea taken from https://gist.github.com/folke/fe5d28423ea5380929c3f7ce674c41d8