From 1c746863702a26f781f38f2450b73eaee75d1427 Mon Sep 17 00:00:00 2001 From: listout Date: Tue, 13 Sep 2022 21:14:51 +0530 Subject: nvim: plugins: cmp: Minor fixes - Removing view from nvim-cmp setup - Using bodered windows for completion and documentation - New sources priority - Don't auto complete cmdline for command `:` mode - Lazy loading snippets Signed-off-by: listout --- lua/plugins/cmp.lua | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'lua/plugins') diff --git a/lua/plugins/cmp.lua b/lua/plugins/cmp.lua index 6c09612..ac9e746 100644 --- a/lua/plugins/cmp.lua +++ b/lua/plugins/cmp.lua @@ -1,3 +1,8 @@ +local has_words_before = function() + 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 + -- luasnip setup local luasnip = require 'luasnip' @@ -50,14 +55,15 @@ cmp.setup { return vim_item end }, - view = { - entries = "custom", - }, snippet = { expand = function(args) require('luasnip').lsp_expand(args.body) -- For `luasnip` users. end, }, + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, mapping = cmp.mapping.preset.insert({ [''] = cmp.mapping.scroll_docs(-4), [''] = cmp.mapping.scroll_docs(4), @@ -71,10 +77,12 @@ cmp.setup { cmp.select_next_item() elseif luasnip.expand_or_jumpable() then luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() else fallback() end - end, { 'i', 's' }), + end, { "i", "s" }), [''] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() @@ -85,14 +93,14 @@ cmp.setup { end end, { 'i', 's' }), }), - sources = { + sources = cmp.config.sources({ + { name = 'luasnip', option = { use_show_condition = false } }, + { name = 'nvim_lsp' }, + { name = 'nvim_lsp_signature_help' }, { name = 'path' }, { name = 'buffer' }, - { name = 'nvim_lsp_signature_help' }, - { name = 'nvim_lsp' }, - { name = 'luasnip' }, { name = 'nvim_lua' }, - }, + }), } -- Set configuration for specific filetype. cmp.setup.filetype('gitcommit', { @@ -111,7 +119,15 @@ require'cmp'.setup.cmdline('/', { -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). require'cmp'.setup.cmdline(':', { sources = { - { name = 'cmdline' }, { name = 'path' }, -- cmp-path needed } }) + +-- lazy loading to get in memory snippets of languages you use +require("luasnip/loaders/from_vscode").lazy_load() +local keymap = vim.api.nvim_set_keymap +local opts = { noremap = true, silent = true } +keymap("i", "", "lua require'luasnip'.jump(1)", opts) +keymap("s", "", "lua require'luasnip'.jump(1)", opts) +keymap("i", "", "lua require'luasnip'.jump(-1)", opts) +keymap("s", "", "lua require'luasnip'.jump(-1)", opts) -- cgit v1.2.3