diff .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
line wrap: on
line diff
--- a/.chezmoitemplates/init.lua	Tue Nov 09 16:23:04 2021 +0100
+++ b/.chezmoitemplates/init.lua	Tue Nov 09 16:40:35 2021 +0100
@@ -221,10 +221,17 @@
       requires = { 'hrsh7th/cmp-buffer', 'hrsh7th/cmp-nvim-lsp', 'saadparwaiz1/cmp_luasnip', 'hrsh7th/cmp-path' },
       config = function()
         local cmp = require 'cmp'
+        local luasnip = require 'luasnip'
+
+        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
+
         cmp.setup {
           snippet = {
             expand = function(args)
-              require('luasnip').lsp_expand(args.body)
+              luasnip.lsp_expand(args.body)
             end,
           },
           mapping = {
@@ -236,6 +243,33 @@
             ['<CR>'] = cmp.mapping.confirm {
               behavior = cmp.ConfirmBehavior.Replace,
             },
+            ['<Tab>'] = cmp.mapping(function(fallback)
+              if cmp.visible() then
+                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',
+            }),
+
+            ['<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 = {
             { name = 'nvim_lsp' },