comparison .chezmoitemplates/init.lua @ 486:3449f059b93e

Add/fix expansion mapping for snippets
author zegervdv <zegervdv@me.com>
date Tue, 09 Nov 2021 16:40:35 +0100
parents 4ad3b5ada36b
children 0252f9494d64
comparison
equal deleted inserted replaced
485:4ad3b5ada36b 486:3449f059b93e
219 use { 219 use {
220 'hrsh7th/nvim-cmp', 220 'hrsh7th/nvim-cmp',
221 requires = { 'hrsh7th/cmp-buffer', 'hrsh7th/cmp-nvim-lsp', 'saadparwaiz1/cmp_luasnip', 'hrsh7th/cmp-path' }, 221 requires = { 'hrsh7th/cmp-buffer', 'hrsh7th/cmp-nvim-lsp', 'saadparwaiz1/cmp_luasnip', 'hrsh7th/cmp-path' },
222 config = function() 222 config = function()
223 local cmp = require 'cmp' 223 local cmp = require 'cmp'
224 local luasnip = require 'luasnip'
225
226 local has_words_before = function()
227 local line, col = unpack(vim.api.nvim_win_get_cursor(0))
228 return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match '%s' == nil
229 end
230
224 cmp.setup { 231 cmp.setup {
225 snippet = { 232 snippet = {
226 expand = function(args) 233 expand = function(args)
227 require('luasnip').lsp_expand(args.body) 234 luasnip.lsp_expand(args.body)
228 end, 235 end,
229 }, 236 },
230 mapping = { 237 mapping = {
231 ['<C-p>'] = cmp.mapping.select_prev_item(), 238 ['<C-p>'] = cmp.mapping.select_prev_item(),
232 ['<C-n>'] = cmp.mapping.select_next_item(), 239 ['<C-n>'] = cmp.mapping.select_next_item(),
234 ['<C-y>'] = cmp.mapping.complete(), 241 ['<C-y>'] = cmp.mapping.complete(),
235 ['<C-e>'] = cmp.mapping.close(), 242 ['<C-e>'] = cmp.mapping.close(),
236 ['<CR>'] = cmp.mapping.confirm { 243 ['<CR>'] = cmp.mapping.confirm {
237 behavior = cmp.ConfirmBehavior.Replace, 244 behavior = cmp.ConfirmBehavior.Replace,
238 }, 245 },
246 ['<Tab>'] = cmp.mapping(function(fallback)
247 if cmp.visible() then
248 cmp.select_next_item()
249 elseif luasnip.expand_or_jumpable() then
250 luasnip.expand_or_jump()
251 elseif has_words_before() then
252 cmp.complete()
253 else
254 fallback()
255 end
256 end, {
257 'i',
258 's',
259 }),
260
261 ['<S-Tab>'] = cmp.mapping(function(fallback)
262 if cmp.visible() then
263 cmp.select_prev_item()
264 elseif luasnip.jumpable(-1) then
265 luasnip.jump(-1)
266 else
267 fallback()
268 end
269 end, {
270 'i',
271 's',
272 }),
239 }, 273 },
240 sources = { 274 sources = {
241 { name = 'nvim_lsp' }, 275 { name = 'nvim_lsp' },
242 { name = 'buffer', keyword_length = 5 }, 276 { name = 'buffer', keyword_length = 5 },
243 { name = 'luasnip' }, 277 { name = 'luasnip' },