Add cmp, and some auto formatting and inlay hints to lsps, add some tweaks to clangd

This commit is contained in:
Martin Larsson 2024-04-08 10:47:23 +02:00
parent 64b0035e16
commit 0c897e42bd
3 changed files with 95 additions and 0 deletions

67
nvim/lua/plugs/cmp.lua Normal file
View file

@ -0,0 +1,67 @@
return {
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"hrsh7th/cmp-cmdline",
},
config = function()
-- Set up nvim-cmp.
local cmp = require("cmp")
cmp.setup({
snippet = {
expand = function(args)
vim.snippet.expand(args.body) -- For native neovim snippets (Neovim v0.10+)
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(),
["<CR>"] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
},
{
{ name = "buffer" },
})
})
-- Set configuration for specific filetype.
cmp.setup.filetype("gitcommit", {
sources = cmp.config.sources({
{ name = "git" }, -- You can specify the `git` source if [you were installed it](https://github.com/petertriho/cmp-git).
}, {
{ name = "buffer" },
})
})
-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline({ "/", "?" }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = "buffer" }
}
})
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" }
}, {
{ name = "cmdline" }
}),
matching = { disallow_symbol_nonprefix_matching = false }
})
end,
}

View file

@ -20,6 +20,8 @@ return {
end, lua_files)
-- Create a new table which contains the non-lsp setups for Mason (linters, formatters, etc)
-- IMPORTANT: Make sure to leave rust-analyzer out of this list, as it can cause conflicts with rustaceanvim.
-- Install rust-analyzer using your systems package manager instead.
local mason_installs = vim.list_extend({
"clang-format",
"cmakelang",
@ -34,11 +36,27 @@ return {
require("mason-update-all").setup()
-- Iterate each server and setup
local capabilities = require("cmp_nvim_lsp").default_capabilities()
local lspconfig = require("lspconfig")
for _, server_name in ipairs(server_names) do
local server = lspconfig[server_name]
if server then
local server_table = require("language_servers/" .. server_name)
server_table.capabilities = capabilities
server_table.on_attach = function(client, bufnr)
vim.lsp.inlay_hint.enable(bufnr, true)
if client.server_capabilities.documentFormattingProvider then
vim.api.nvim_buf_create_user_command(bufnr, "Format", vim.lsp.buf.format, { nargs = 0 })
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = bufnr,
callback = function()
vim.lsp.buf.format()
end,
})
end
end
server.setup(server_table)
-- Run the post_setup function if it exists