From 4627e180062bde26673e33c9342c3b3fcef7fb9d Mon Sep 17 00:00:00 2001 From: Anthony Rodriguez Date: Wed, 14 Aug 2024 00:14:26 +0200 Subject: [PATCH] Update .config/nvim/lua/plugins/lsp.lua --- dot_config/nvim/lua/plugins/lsp.lua | 40 ++++++++++++++++------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/dot_config/nvim/lua/plugins/lsp.lua b/dot_config/nvim/lua/plugins/lsp.lua index 971340a..11ae9d3 100644 --- a/dot_config/nvim/lua/plugins/lsp.lua +++ b/dot_config/nvim/lua/plugins/lsp.lua @@ -36,17 +36,16 @@ return { }, opts = function(_, opts) local cmp = require "cmp" + 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 -- Setup cmp with options opts.snippet = { - -- REQUIRED - you must specify a snippet engine expand = function(args) - -- Uncomment and use the snippet engine you prefer: - -- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. - -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users. - -- require('snippy').expand_snippet(args.body) -- For `snippy` users. - -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. - vim.snippet.expand(args.body) -- For native Neovim snippets (Neovim v0.10+) + vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. end, } @@ -55,15 +54,27 @@ return { [''] = cmp.mapping.scroll_docs(4), [''] = cmp.mapping.complete(), [''] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }), + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + else + fallback() + end + end, { "i", "s" }), }) opts.sources = cmp.config.sources({ { name = 'nvim_lsp' }, { name = 'vsnip' }, -- For vsnip users. - -- { name = 'luasnip' }, -- For luasnip users. - -- { name = 'ultisnips' }, -- For ultisnips users. - -- { name = 'snippy' }, -- For snippy users. }, { { name = 'buffer' }, { name = 'nvim_lsp_signature_help' } @@ -71,13 +82,6 @@ return { return opts end, - keys = { - { "", function() require('cmp').mapping.scroll_docs(-4) end, mode = "i", desc = "Scroll docs up" }, - { "", function() require('cmp').mapping.scroll_docs(4) end, mode = "i", desc = "Scroll docs down" }, - { "", function() require('cmp').mapping.complete() end, mode = "i", desc = "Trigger completion" }, - { "", function() require('cmp').mapping.abort() end, mode = "i", desc = "Abort completion" }, - { "", function() require('cmp').mapping.confirm({ select = true }) end, mode = "i", desc = "Confirm selection" }, - }, }, { 'numToStr/Comment.nvim',