Skip to content

Commit 7f42fb4

Browse files
committed
feat(nvim): Code completion and Snippets.
Add nvim-cmp to code completion. Reference: [Autocomplete and Snippets in Neovim | FREE COURSE // EP 5](https://youtu.be/iXIwm4mCpuc?si=eEz9muJrw-AjX_ge)
1 parent 8961785 commit 7f42fb4

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

nvim/lua/plugins/completions.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
return {
2+
{
3+
"hrsh7th/cmp-nvim-lsp"
4+
},
5+
{
6+
'L3MON4D3/LuaSnip',
7+
dependencies = {
8+
'saadparwaiz1/cmp_luasnip',
9+
'rafamadriz/friendly-snippets'
10+
},
11+
},
12+
{
13+
"hrsh7th/nvim-cmp",
14+
dependencies = { 'hrsh7th/cmp-nvim-lsp' },
15+
config = function()
16+
-- Set up nvim-cmp.
17+
local cmp = require'cmp'
18+
require("luasnip.loaders.from_vscode").lazy_load()
19+
20+
cmp.setup({
21+
snippet = {
22+
-- REQUIRED - you must specify a snippet engine
23+
expand = function(args)
24+
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
25+
end,
26+
},
27+
window = {
28+
completion = cmp.config.window.bordered(),
29+
documentation = cmp.config.window.bordered(),
30+
},
31+
mapping = cmp.mapping.preset.insert({
32+
['<C-b>'] = cmp.mapping.scroll_docs(-4),
33+
['<C-f>'] = cmp.mapping.scroll_docs(4),
34+
['<C-Space>'] = cmp.mapping.complete(),
35+
['<C-e>'] = cmp.mapping.abort(),
36+
['<CR>'] = cmp.mapping.confirm({ select = true }),
37+
}),
38+
sources = cmp.config.sources({
39+
{ name = 'nvim_lsp' },
40+
{ name = 'luasnip' }, -- For luasnip users.
41+
}, {
42+
{ name = 'buffer' },
43+
})
44+
})
45+
end,
46+
}
47+
}

nvim/lua/plugins/lsp.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ return {
2424
{
2525
"neovim/nvim-lspconfig",
2626
config = function()
27+
local capabilities = require('cmp_nvim_lsp').default_capabilities()
2728
local lspconfig = require("lspconfig")
28-
lspconfig.lua_ls.setup({})
29-
lspconfig.tsserver.setup({})
30-
lspconfig.pyright.setup({})
29+
lspconfig.lua_ls.setup({
30+
capabilities = capabilities
31+
})
32+
lspconfig.tsserver.setup({
33+
capabilities = capabilities
34+
})
35+
lspconfig.pyright.setup({
36+
capabilities = capabilities
37+
})
3138

3239
-- Buffer local mappings.
3340
-- See `:help vim.lsp.*` for documentation on any of the below functions

0 commit comments

Comments
 (0)