nvim: Add nvim-cmp; enable luasnip autosnippets

This commit is contained in:
Andreas Tsouchlos 2025-11-15 23:55:05 +01:00
parent b2be54b2b9
commit ba425ecf49
3 changed files with 121 additions and 59 deletions

View File

@ -31,64 +31,21 @@ vim.lsp.config("texlab", {
})
--
--
-- Completion config
--
--
vim.o.completeopt = "menu,menuone,noinsert,fuzzy"
vim.api.nvim_set_keymap("i", "<C-Space>", "<C-x><C-o>", { noremap = true })
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('my.lsp', {}),
callback = function(args)
local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
-- Make tab accept the selected completion
vim.keymap.set('i', '<Tab>', function()
if vim.fn.pumvisible() == 1 then
return '<C-y>'
else
return '<Tab>'
end
end, { expr = true })
-- Make enter just insert a newline (don't accept completion)
vim.keymap.set('i', '<CR>', function()
if vim.fn.pumvisible() == 1 then
return '<C-e><CR>'
else
return '<CR>'
end
end, { expr = true })
if client:supports_method('textDocument/completion') then
-- Trigger autocompletion on every keypress
local chars = {}; for i = 32, 126 do table.insert(chars, string.char(i)) end
client.server_capabilities.completionProvider.triggerCharacters = chars
vim.lsp.completion.enable(true, client.id, args.buf, { autotrigger = true })
end
vim.keymap.set('n', '<M-L>',
function() vim.lsp.buf.format() end, {
desc = "Format",
noremap = true,
silent = true
})
vim.keymap.set('i', '<M-L>',
function() vim.lsp.buf.format() end, {
desc = "Format",
noremap = true,
silent = true
})
vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, { desc = "Go to definition" })
end,
vim.lsp.config('clangd', {
cmd = {
"clangd",
"--background-index",
"--clang-tidy",
"--completion-style=bundled",
"--cross-file-rename",
"--header-insertion=iwyu",
-- Required for embedded system compilers
"--query-driver=/**/*g++",
"--offset-encoding=utf-16"
-- "-j=8",
-- "--malloc-trim",
-- "--pch-storage=memory"
}
})
@ -125,3 +82,38 @@ vim.api.nvim_create_autocmd("CursorHold", {
})
end,
})
--
--
-- Keymaps
--
--
-- TODO: Do I need this if I use nvim-cmp?
vim.o.completeopt = "menu,menuone,noinsert,fuzzy"
vim.api.nvim_set_keymap("i", "<C-Space>", "<C-x><C-o>", { noremap = true })
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('my.lsp', {}),
callback = function(args)
vim.keymap.set('n', '<M-L>',
function() vim.lsp.buf.format() end, {
desc = "Format",
noremap = true,
silent = true
})
vim.keymap.set('i', '<M-L>',
function() vim.lsp.buf.format() end, {
desc = "Format",
noremap = true,
silent = true
})
vim.keymap.set("n", "gd",
function() vim.lsp.buf.definition() end, {
desc = "Go to definition"
})
end,
})

View File

@ -8,7 +8,7 @@ return {
-- TODO: Find out how to enable autosnippets without having the status line disappear
require("luasnip").config.set_config({
-- enable_autosnippets = true,
enable_autosnippets = true,
store_selection_keys = "<leader>v"
})

View File

@ -0,0 +1,70 @@
return
{
'hrsh7th/nvim-cmp',
event = 'InsertEnter',
dependencies = {
'L3MON4D3/LuaSnip',
'hrsh7th/cmp-buffer',
'hrsh7th/cmp-path',
'hrsh7th/cmp-nvim-lsp',
-- TODO: Do I need these?
'hrsh7th/cmp-cmdline',
-- 'hrsh7th/cmp-nvim-lsp-signature-help',
'saadparwaiz1/cmp_luasnip'
},
config = function()
local cmp = require('cmp')
cmp.setup({
snippet = {
-- REQUIRED - you must specify a snippet engine
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
-- window = {
-- -- completion = cmp.config.window.bordered(),
-- -- documentation = cmp.config.window.bordered(),
-- },
mapping = cmp.mapping.preset.insert({
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<Tab>'] = cmp.mapping.confirm({ select = true }),
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
}, {
{ name = 'buffer' },
})
})
-- -- To use git you need to install the plugin petertriho/cmp-git and uncomment lines below
-- -- Set configuration for specific filetype.
-- cmp.setup.filetype('gitcommit', {
-- sources = cmp.config.sources({
-- { name = 'git' },
-- }, {
-- { name = 'buffer' },
-- })
-- })
-- require("cmp_git").setup()
-- 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' }
}
})
-- -- Set up lspconfig.
-- local capabilities = require('cmp_nvim_lsp').default_capabilities()
-- -- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled.
-- vim.lsp.config('<YOUR_LSP_SERVER>', {
-- capabilities = capabilities
-- })
-- vim.lsp.enable('<YOUR_LSP_SERVER>')
end
}