.dotfiles/nvim/lua/autocompletion.lua

74 lines
2.4 KiB
Lua
Raw Normal View History

2021-12-15 12:31:17 +01:00
-- Setup nvim-cmp.
2023-06-16 01:47:30 +02:00
local cmp = require 'cmp'
2021-12-15 12:31:17 +01:00
cmp.setup({
snippet = {
expand = function(args)
2023-06-16 01:47:30 +02:00
require('luasnip').lsp_expand(args.body)
2021-12-15 12:31:17 +01:00
end,
},
mapping = {
['<C-b>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
2023-06-16 01:47:30 +02:00
['<C-n>'] = cmp.mapping(cmp.mapping.select_next_item(), {'i','c'}),
['<C-p>'] = cmp.mapping(cmp.mapping.select_prev_item(), {'i','c'}),
2022-01-23 23:24:50 +01:00
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
2021-12-15 12:31:17 +01:00
['<C-y>'] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping.
['<C-e>'] = cmp.mapping({
i = cmp.mapping.abort(),
c = cmp.mapping.close(),
}),
-- Accept currently selected item. If none selected, `select` first item.
-- Set `select` to `false` to only confirm explicitly selected items.
['<CR>'] = cmp.mapping.confirm({ select = true }),
},
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
2023-06-16 01:47:30 +02:00
{ name = 'luasnip' },
2021-12-15 12:31:17 +01:00
}, {
{ name = 'buffer' },
})
})
2022-01-24 23:54:15 +01:00
-- Use buffer source for `/`
2021-12-15 12:31:17 +01:00
cmp.setup.cmdline('/', {
sources = {
{ name = 'buffer' }
}
})
2022-01-24 23:54:15 +01:00
-- Use cmdline & path source for ':'
2021-12-15 12:31:17 +01:00
cmp.setup.cmdline(':', {
sources = cmp.config.sources({
{ name = 'path' }
}, {
{ name = 'cmdline' }
})
})
2022-01-24 23:54:15 +01:00
local on_attach = function(client, bufnr)
local function buf_set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
2021-12-15 12:31:17 +01:00
2022-01-24 23:54:15 +01:00
local opts = { noremap = true, silent = true }
buf_set_keymap("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts)
buf_set_keymap("n", "gh", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
buf_set_keymap("n", "gD", "<cmd>Telescope lsp_implementations<CR>", opts)
buf_set_keymap("n", "gs", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
buf_set_keymap("n", "gR", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
buf_set_keymap("n", "gH", "<cmd>Telescope lsp_code_actions<CR>", opts)
buf_set_keymap("n", "gr", "<cmd>Telescope lsp_references<CR>", opts)
buf_set_keymap("n", "ge", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
2022-01-24 23:54:15 +01:00
end
local lsp_installer = require 'nvim-lsp-installer'
2022-01-24 23:54:15 +01:00
lsp_installer.on_server_ready(function(server)
local opts = {}
opts.on_attach = on_attach
opts.capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
2022-01-24 23:54:15 +01:00
server:setup(opts)
vim.cmd [[ do User LspAttachBuffers ]]
end)