diff options
author | Saumit Dinesan <justsaumit@protonmail.com> | 2023-07-15 15:49:24 +0530 |
---|---|---|
committer | Saumit Dinesan <justsaumit@protonmail.com> | 2023-07-15 15:49:24 +0530 |
commit | 16ee7441f40a71872f8fb6a89da470564e76ce04 (patch) | |
tree | 79bd0516cb4230593de537f283e6db537990666c /.config/nvim/lua/justsaumit/cmp-config.lua | |
parent | 32077b19df1d8895e051de1c4e323aeff6b084c9 (diff) |
nvim: Adding autocompletions- nvim-cmp+luasnip+nvim-lsp+cmdline+path
Diffstat (limited to '.config/nvim/lua/justsaumit/cmp-config.lua')
-rw-r--r-- | .config/nvim/lua/justsaumit/cmp-config.lua | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/.config/nvim/lua/justsaumit/cmp-config.lua b/.config/nvim/lua/justsaumit/cmp-config.lua new file mode 100644 index 0000000..7e29155 --- /dev/null +++ b/.config/nvim/lua/justsaumit/cmp-config.lua @@ -0,0 +1,88 @@ +vim.g.completeopt="menu,menuone,noselect,noinsert" + +local has_words_before = function() + unpack = unpack or table.unpack + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil +end + +local luasnip = require("luasnip") +local cmp = require'cmp' + +cmp.setup({ + snippet = { + -- REQUIRED - you must specify a snippet engine + expand = function(args) + -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users. + luasnip.lsp_expand(args.body) + 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. + ["<Tab>"] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() + -- they way you will only jump inside the snippet region + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }), + + ["<S-Tab>"] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), + }), + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'luasnip' }, -- For luasnip users. + { name = 'buffer' }, + { name = 'path' }, + }), +}) + +-- 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' } + }) +}) + |