config/nvim/lua/completion.lua (view raw)
1local cmp = require 'cmp'
2local luasnip = require 'luasnip'
3
4local has_words_before = function()
5 local line, col = unpack(vim.api.nvim_win_get_cursor(0))
6 return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
7end
8
9cmp.setup({
10 snippet = {
11 expand = function(args)
12 require('luasnip').lsp_expand(args.body)
13 end
14 },
15 window = {
16 completion = cmp.config.window.bordered(),
17 documentation = cmp.config.window.bordered(),
18 },
19 mapping = cmp.mapping.preset.insert({
20 ['<C-b>'] = cmp.mapping.scroll_docs(-4),
21 ['<C-f>'] = cmp.mapping.scroll_docs(4),
22 ['<C-Space>'] = cmp.mapping.complete(),
23 ['<C-e>'] = cmp.mapping.abort(),
24 ['<CR>'] = cmp.mapping.confirm({ select = true }),
25 ["<Tab>"] = cmp.mapping(function(fallback)
26 if cmp.visible() then
27 cmp.select_next_item()
28 elseif luasnip.expand_or_jumpable() then
29 luasnip.expand_or_jump()
30 elseif has_words_before() then
31 cmp.complete()
32 else
33 fallback()
34 end
35 end, { "i", "s" }),
36
37 ["<S-Tab>"] = cmp.mapping(function(fallback)
38 if cmp.visible() then
39 cmp.select_prev_item()
40 elseif luasnip.jumpable(-1) then
41 luasnip.jump(-1)
42 else
43 fallback()
44 end
45 end, { "i", "s" }),
46
47 }),
48 sources = cmp.config.sources({
49 { name = 'nvim_lsp' },
50 { name = 'luasnip' },
51 { name = 'path' },
52 { name = 'buffer' },
53 })
54})
55
56cmp.setup.cmdline('/', {
57 mapping = cmp.mapping.preset.cmdline(),
58 sources = {
59 { name = 'buffer' },
60 { name = 'luasnip' }
61 }
62})
63
64cmp.setup.cmdline(':', {
65 mapping = cmp.mapping.preset.cmdline(),
66 sources = cmp.config.sources({
67 { name = 'path' }
68 }, {
69 { name = 'cmdline' }
70 })
71})
72
73local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
74require('lspconfig')['gopls'].setup {
75 capabilities = capabilities
76}
77