neovim

Personal neovim configuration files
git clone git://gtms.dev/neovim
Log | Files | Refs

luasnip.lua (3413B)


      1 if not pcall(require, 'luasnip') then
      2   return
      3 end
      4 
      5 local ls = require('luasnip')
      6 local types = require('luasnip.util.types')
      7 
      8 ls.config.set_config({
      9   history = true,
     10   updateevents = 'TextChanged,TextChangedI',
     11   ext_opts = { [types.choiceNode] = { active = { virt_text = { { '<- choice', 'Comment' } } } } },
     12 })
     13 
     14 local opts = { silent = true }
     15 vim.keymap.set({ 'i', 's' }, '<c-l>', function()
     16   if ls.expand_or_jumpable() then
     17     ls.expand_or_jump()
     18   end
     19 end, opts)
     20 vim.keymap.set({ 'i', 's' }, '<c-h>', function()
     21   if ls.jumpable(-1) then
     22     ls.jump(-1)
     23   end
     24 end, opts)
     25 vim.keymap.set({ 'i' }, '<c-e>', function()
     26   if ls.choice_active() then
     27     ls.change_choice(1)
     28   end
     29 end, opts)
     30 vim.keymap.set({ 'i' }, '<c-u>', require('luasnip.extras.select_choice'), opts)
     31 
     32 local s = ls.snippet
     33 local sn = ls.snippet_node
     34 local isn = ls.indent_snippet_node
     35 local t = ls.text_node
     36 local i = ls.insert_node
     37 local f = ls.function_node
     38 local c = ls.choice_node
     39 local d = ls.dynamic_node
     40 local r = ls.restore_node
     41 local events = require('luasnip.util.events')
     42 local ai = require('luasnip.nodes.absolute_indexer')
     43 local p = require('luasnip.extras').partial
     44 local rep = require('luasnip.extras').rep
     45 local fmt = require('luasnip.extras.fmt').fmt
     46 
     47 local shell = function(command)
     48   return function()
     49     local file = io.popen(command, 'r')
     50     local res = {}
     51     for line in file:lines() do
     52       table.insert(res, line)
     53     end
     54     return res[1]
     55   end
     56 end
     57 local cmt = function()
     58   return require('luasnip.util.util').buffer_comment_chars()[1]
     59 end
     60 
     61 ls.cleanup()
     62 
     63 ls.add_snippets('all', {
     64   s('bang', t('#!/usr/bin/env ')),
     65   s('date', p(os.date, '%d.%m.%Y')),
     66   s('time', p(os.date, '%H:%M:%S')),
     67   s('todo', { f(cmt), t(' TODO('), f(shell('id -un')), t(') '), p(os.date, '%d.%m.%y'), t(': ') }),
     68 })
     69 
     70 ls.add_snippets('lua', {
     71   s('f', fmt('function({}) {} end', { i(1), i(2) })),
     72   s('F', fmt('function() {} end', { i(1) })),
     73   s('r', fmt('require(\'{}\')', { i(1) })),
     74   s('l', fmt('local {} = {}', { i(1), i(2) })),
     75   s('lr', fmt('local {} = require(\'{}\')', { i(1), rep(1) })),
     76 })
     77 
     78 ls.add_snippets('dart', {
     79   s('for', fmt([[
     80         for (final {} in {}) {{
     81           {}
     82         }}
     83         ]], { i(1), i(2), i(3) })),
     84   s('fori', fmt([[
     85         for (var i = {}; i < {}; i++) {{
     86           {}
     87         }}
     88         ]], { i(1), i(2), i(3) })),
     89   -- Angular
     90   s('i', fmt([[
     91   @Input()
     92   {};
     93   ]], { i(1) })),
     94   s('o', fmt([[
     95   @Output()
     96   Stream<{}> get {};
     97   ]], { i(1), i(2) })),
     98   s('c', fmt([[
     99   import 'package:ngdart/angular.dart';
    100 
    101   @Component(
    102     selector: '{}',
    103     template: '{}'
    104     changeDetection: ChangeDetectionStrategy.OnPush,
    105   )
    106   class {} {{
    107     {}
    108   }}
    109   ]], { i(1), i(2), i(3), i(4) })),
    110   s('d', fmt([[
    111   import 'package:ngdart/angular.dart';
    112 
    113   @Directive(selector: '{}')
    114   class {} {{
    115     {}
    116   }}
    117   ]], { i(1), i(2), i(3) })),
    118   s('vc', fmt([[
    119   @ViewChild({})
    120   {}? {}
    121   ]], { i(1), i(2), i(3) })),
    122   s('vch', fmt([[
    123   @ViewChildren({})
    124   List<{}>? {}
    125   ]], { i(1), i(2), i(3) })),
    126 })
    127 
    128 -- Angular
    129 ls.add_snippets('html', {
    130   s('if', fmt('*ngIf="{}"', { i(1) })),
    131   s('nf', fmt('*ngFor="let {} of {}"', { i(1), i(2) })),
    132   s('nfi', fmt('*ngFor="let {} of {}; let i=index"', { i(1), i(2) })),
    133   s('ni', fmt('[{}]="{}"', { i(1), i(2) })),
    134   s('no', fmt('({})="{}"', { i(1), i(2) })),
    135   s('nb', fmt('[({})]="{}"', { i(1), i(2) })),
    136 })
    137 
    138 ls.add_snippets('scss', { s('v', fmt('var(--{})', { i(1) })) })