luasnip.lua (3835B)
1 ---@diagnostic disable: unused-local, unused-function 2 local M = {} 3 4 local keymaps = function(luasnip) 5 vim.keymap.set('i', '<C-l>', function() if luasnip.expand_or_jumpable() then luasnip.expand_or_jump() end end, 6 {noremap = true}) 7 vim.keymap.set('s', '<C-l>', function() if luasnip.expand_or_jumpable() then luasnip.expand_or_jump() end end, 8 {noremap = true}) 9 vim.keymap.set('i', '<C-h>', function() if luasnip.jumpable(-1) then luasnip.jump(-1) end end, {noremap = true}) 10 vim.keymap.set('s', '<C-h>', function() if luasnip.jumpable(-1) then luasnip.jump(-1) end end, {noremap = true}) 11 vim.keymap.set('i', '<C-e>', function() if luasnip.choice_active() then luasnip.change_choice(1) end end, 12 {silent = true, noremap = true}) 13 vim.keymap.set('s', '<C-e>', function() if luasnip.choice_active() then luasnip.change_choice(1) end end, 14 {silent = true, noremap = true}) 15 -- vim.api.nvim_set_keymap("i", "<C-E>", "<Plug>luasnip-next-choice", {}) 16 -- vim.api.nvim_set_keymap("s", "<C-E>", "<Plug>luasnip-next-choice", {}) 17 end 18 19 local function copy(args) return args[1] end 20 -- Make sure to not pass an invalid command, as io.popen() may write over nvim-text. 21 22 local function bash(_, _, command) 23 local file = io.popen(command, 'r') 24 local res = {} 25 for line in file:lines() do table.insert(res, line) end 26 return res 27 end 28 29 local snippets = function(luasnip) 30 local ls = luasnip 31 local s = ls.snippet 32 local sn = ls.snippet_node 33 local isn = ls.indent_snippet_node 34 local t = ls.text_node 35 local i = ls.insert_node 36 local f = ls.function_node 37 local c = ls.choice_node 38 local d = ls.dynamic_node 39 local events = require('luasnip.util.events') 40 local l = require('luasnip.extras').lambda 41 local p = require('luasnip.extras').partial 42 local fmt = require('luasnip.extras.fmt').fmt 43 ls.snippets = { 44 all = { 45 s('bang', t('#!/usr/bin/env ')), 46 s('date', p(os.date, '%d.%m.%Y')), 47 s('time', p(os.date, '%H:%M:%S')), 48 s('todo', { 49 f(function(_, _, _) return require('luasnip.util.util').buffer_comment_chars()[1] end, {}), 50 t(' TODO('), 51 f(bash, {}, 'id -un'), 52 t(') '), 53 p(os.date, '%d.%m.%y'), 54 t(': '), 55 }), 56 }, 57 html = { 58 s('if', fmt('*ngIf="{1}"', {i(1)})), 59 s('nf', fmt('*ngFor="let {1} of {2}"', {i(1), i(2)})), 60 s('nfi', fmt('*ngFor="let {1} of {2}; let i=index"', {i(1), i(2)})), 61 s('ni', fmt('[{1}]="{2}"', {i(1), i(2)})), 62 s('no', fmt('({1})="{2}"', {i(1), i(2)})), 63 s('nb', fmt('[({1})]="{2}"', {i(1), i(2)})), 64 }, 65 lua = { 66 s('f', fmt('function() {1} end', {i(1)})), 67 s('r', fmt('require(\'{1}\')', {i(1)})), 68 s('l', fmt('local {1} = {2}', {i(1), i(2)})), 69 }, 70 dart = { 71 s('s', fmt('String {1}{2}', {i(1), i(2, ';')})), 72 s('S', fmt('String? {1}{2}', {i(1), i(2, ';')})), 73 s('i', fmt('int {1}{2}', {i(1), i(2, ';')})), 74 s('I', fmt('int? {1}{2}', {i(1), i(2, ';')})), 75 s('n', fmt('num {1}{2}', {i(1), i(2, ';')})), 76 s('N', fmt('num? {1}{2}', {i(1), i(2, ';')})), 77 s('d', fmt('double {1}{2}', {i(1), i(2, ';')})), 78 s('D', fmt('double? {1}{2}', {i(1), i(2, ';')})), 79 s('f', fmt('final {1}{2}', {i(1), i(2, ';')})), 80 s('v', fmt('var {1}{2}', {i(1), i(2, ';')})), 81 s('for', fmt([[ 82 for (final {2} in {1}) {{ 83 {3} 84 }} 85 ]], {i(1), i(2), i(3)})), 86 }, 87 scss = {s('v', fmt('var(--{1})', {i(1)}))}, 88 } 89 end 90 91 M.setup = function() 92 local luasnip = require('luasnip') 93 local types = require('luasnip.util.types') 94 luasnip.config.set_config({ 95 store_selection_keys = '<Tab>', 96 ext_opts = {[types.choiceNode] = {active = {virt_text = {{'choiceNode', 'Comment'}}}}}, 97 }) 98 keymaps(luasnip) 99 snippets(luasnip) 100 end 101 102 return M