luasnip.lua
1 -- luasnip.lua 2 3 local M = {} 4 local ls = require('luasnip') 5 -- local lsnip = require('luasnip.loaders.from_vscode').lazy_load() 6 7 local s = ls.snippet 8 local t = ls.text_node 9 local i = ls.insert_node 10 local extras = require('luasnip.extras') 11 local rep = extras.rep 12 local fmt = require('luasnip.extras.fmt').fmt 13 local c = ls.choice_node 14 local f = ls.function_node 15 local d = ls.dynamic_node 16 -- local sn = ls.snippet_node 17 18 vim.keymap.set({ 'i', 's' }, '<A-n>', function() 19 if ls.choice_active() then 20 ls.change_choice(1) 21 end 22 end, { desc = 'Luasnip: next choice' }) 23 24 vim.keymap.set({ 'i', 's' }, '<A-k>', function() 25 if ls.expand_or_jumpable() then 26 ls.expand_or_jump() 27 end 28 end, { silent = true, desc = 'Luasnip: expand or jump forward' }) 29 30 vim.keymap.set({ 'i', 's' }, '<A-j>', function() 31 if ls.jumpable(-1) then 32 ls.jump(-1) 33 end 34 end, { silent = true, desc = 'Luasnip: jump backward' }) 35 36 ls.add_snippets('lua', { 37 s('hello', { 38 t('print("hello '), 39 i(1), 40 t(' world")'), 41 }), 42 43 s('if', { 44 t('if '), 45 i(1, 'true'), 46 t(' then '), 47 i(2), 48 t(' end'), 49 }), 50 }) 51 52 ls.add_snippets('tex', { 53 s('beg', { 54 t('\\begin{'), 55 i(1), 56 t('}'), 57 t({ '', '\t' }), 58 i(0), 59 t({ '', '\\end{' }), 60 rep(1), 61 t('}'), 62 }), 63 }) 64 65 ls.add_snippets('cs', { 66 s( 67 'logc', 68 fmt([[Debug.Log($"<color={}>{}</color>");]], { 69 c(1, { 70 t('red'), 71 t('green'), 72 t('blue'), 73 t('cyan'), 74 t('magenta'), 75 }), 76 i(2), 77 }) 78 ), 79 }) 80 81 ls.add_snippets('typescriptreact', { 82 83 -- 1st version 84 s('co', { 85 t('position(['), 86 f(function() 87 local register_data = vim.fn.getreg() .. '' 88 if string.match(register_data, '[%d-]+,%s*[%d-]+') then 89 return register_data 90 else 91 print('register does not contain the pattern') 92 end 93 end), 94 t('])'), 95 }), 96 97 s('co', { 98 d(function() 99 local register_data = vim.fn.getreg() .. '' 100 if string.match(register_data, '[%d-]+,%s*[%d-]+') then 101 return M.sn(nil, { 102 M.t('position([' .. register_data .. '])'), 103 }) 104 else 105 print('register does not contain the pattern') 106 return M.sn(nil, {}) 107 end 108 end), 109 i(1), 110 }), 111 }) 112 113 return M