summary refs log tree commit diff
path: root/.vim/lua/snips.lua
blob: 32e97d14a83937b467b504d985d9a68ddc0d74ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
local ls = require("luasnip")
local fmt = require('luasnip.extras.fmt').fmt

local s = ls.s
local sn = ls.sn
local t = ls.text_node
local f = ls.function_node
local i = ls.insert_node
local c = ls.choice_node

local exec = function(command)
  return f(function(_, _, arg)
    local p = io.popen(arg, 'r')
    local res = {}
    for line in p:lines() do
      table.insert(res, line)
    end
    return res
  end, {}, { user_args = { command }})
end

local calculate_commentstring = require('Comment.ft').calculate
local get_region = require('Comment.utils').get_region

---@param ctype number|(ex.: 1 (line), 2 (block))
local comment = function(ctype, inner)
  local get_cstring = function()
    local cs = calculate_commentstring({ ctype = ctype, range = get_region() }) or ''
    local tbl = vim.split(cs, '%s', {plain = true, trimempty = true})
    return (#tbl == 0) and {'', ''}
      or ((#tbl == 1) and {tbl[1], ''}
        or {tbl[1], tbl[2]})
  end
  return sn(nil, {
    f(function() return get_cstring()[1] end),
    t(' '),
    inner,
    f(function() return get_cstring()[2] end),
  })
end

-- snipmate snippets
-- require('luasnip.loaders.from_snipmate').load({ paths = {'~/.config/nvim/after/snippets'} })

ls.add_snippets('all', {
  s('sign', t('Robert Günzler <r@gnzler.io>')),
  s('me',
    fmt(
      [[{} <{}>]], {
        exec('git config --get user.name'),
        exec('git config --get user.email'),
      }
    )
  ),
}, { type = 'snippets', key = 'signature_helpers'})

ls.add_snippets('all', {
  s('today', f(function() return os.date('%Y-%m-%d') end, {})),
  s('now', f(function() return os.date('%Y-%m-%dT%H:%M:%SZ') end, {}))
}, { type = 'snippets', key = 'date_helpers'})

ls.add_snippets('all', {
  s({ trig = 'todo', docstring = 'TODO:' }, comment(1, fmt([[{}: ]], { t('TODO') }))),
  -- s({ trig = 'todo', docstring = '{1:TODO}: {2}' }, comment(1, fmt([[{}: {}]], { c(1, {t('TODO')}), i(2) }))),
  -- s('note', comment(1, fmt([[NOTE: {}]], { i(0) }))),
  -- s('hack', comment(1, fmt([[HACK: {}]], { i(0) }))),
  -- s('warn', comment(1, fmt([[{}: {}]], {
  --   c(1, {t('WARN'), t('WARNING'), t('XXX')}),
  --   i(0),
  -- }))),
  -- s('fix', comment(1, fmt([[{}: {}]], {
  --   c(1, {t('FIX'), t('FIXME'), t('ISSUE'), t('BUG')}),
  --   i(0),
  -- }))),
}, { type = 'snippets', key = 'todo_comments'})