summary refs log tree commit diff
path: root/.vim/lua/internal/snips.lua
blob: bb9fdef1e86e81797994bc3f7e4fec0c5360ad3d (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
local ls = require('luasnip')
local types = require('luasnip.util.types')

vim.api.nvim_set_hl(0, 'LuasnipIndicator', {
  fg = vim.g.terminal_color_15,
  bg = vim.g.terminal_color_1,
  italic = true,
  nocombine = true,
})

-- autowrap: wraps the function in top and bot if there is any content
local autowrap = function(top, bot, inner)
  local autoinsert = function(args, _, _, wrap_with)
    local nodes = {}
    if vim.trim(table.concat(args[1] or {})) ~= '' then
      table.insert(nodes, wrap_with)
    end
    return ls.sn(nil, nodes)
  end

  local argnodes = {}
  local maxpos = -1
  for _, e in ipairs(inner) do
    if e.pos ~= nil then
      table.insert(argnodes, e.pos)
      if e.pos > maxpos then
        maxpos = e.pos
      end
    end
  end

  local nodes = {}
  table.insert(nodes, ls.d(maxpos + 1, autoinsert, argnodes, { user_args = { top } }))
  for _, e in ipairs(inner) do
    table.insert(nodes, e)
  end
  table.insert(nodes, ls.d(maxpos + 2, autoinsert, argnodes, { user_args = { bot } }))
  return nodes
end

-- comment: wraps the content in &commentstring (or a blockcomment)
local comment = function(wrapped, blockcomment)
  -- commentstring helper
  local get_cstring = function()
    local cs = require('Comment.ft').calculate({
      ctype = blockcomment and 2 or 1,
      range = require('Comment.utils').get_region(),
    })
    local tbl = vim.split(cs or '', '%s', { plain = true, trimempty = true })
    return (#tbl == 0) and { '', '' }
        or ((#tbl == 1) and { tbl[1] .. ' ', '' } or { tbl[1], tbl[2] })
  end

  if type(wrapped) ~= 'table' then
    wrapped = { wrapped }
  end

  local nodes = {}
  table.insert(
    nodes,
    ls.f(function()
      return get_cstring()[1]
    end)
  )
  for _, n in ipairs(wrapped) do
    table.insert(nodes, n)
  end
  table.insert(
    nodes,
    ls.f(function()
      return get_cstring()[2]
    end)
  )
  return nodes
end

-- exec: inserts the output of command
local exec = function(command)
  return ls.f(function(_, _, ...)
    local results, code = require('plenary.job')
        :new({
          command = vim.env.SHELL,
          args = { '-c', ... },
          enable_recording = true,
        })
        :sync()
    if not code == 0 or #results == 0 then
      error(string.format('exec "%s" failed', ...))
    end
    return results
  end, {}, { user_args = { command } })
end

local file_contents = function(path, vargs)
  return ls.d(1, function(_, _, _, ...)
    local lines = {}
    for line in io.lines(path) do
      table.insert(lines, line)
    end
    return ls.sn(nil, require('luasnip.extras.fmt').fmt(table.concat(lines, '\n'), ...))
  end, {}, { user_args = { vargs } })
end

ls.setup({
  history = true,
  update_events = 'TextChanged,TextChangedI',
  delete_check_events = 'TextChanged',

  ext_opts = {
    [types.snippet] = {
      active = {
        virt_text = { { '<-- snippet', 'LuasnipIndicator' } },
        virt_text_pos = 'right_align',
      },
    },
    [types.insertNode] = {
      unvisited = { hl_group = 'LuasnipIndicator' },
    },
    [types.choiceNode] = {
      active = {
        virt_text = { { '<-- choice node', 'LuasnipIndicator' } },
      },
    },
  },
  snip_env = vim.tbl_extend('keep', ls.session.config.snip_env, {
    f = ls.f,
    i = ls.i,
    s = ls.s,
    t = ls.t,
    c = ls.c,
    sn = ls.sn,
    fmt = require('luasnip.extras.fmt').fmt,
    autowrap = autowrap,
    exec = exec,
    comment = comment,
    file_contents = file_contents,
    user_email = {
      exec([[git config --get user.name]]),
      ls.t(' <'),
      ls.c(1, {
        exec([[git config --get user.email]]),
        ls.t([[robert.gunzler@postman.com]]),
        ls.t([[robert@gnzler.io]]),
      }),
      ls.t('>'),
    },
  }),
})

-- snipmate snippets
-- require('luasnip.loaders.from_snipmate').lazy_load({ paths = './after/snippets' })
-- lua snippets
require('luasnip.loaders.from_lua').lazy_load({ paths = './snippets' })

vim.api.nvim_create_user_command('LuaSnipUnlinkAll', function()
  while #package.loaded.luasnip.session.current_nodes > 0 do
    package.loaded.luasnip.unlink_current()
  end
end, { desc = 'unlink all active snippets' })