summary refs log tree commit diff
path: root/.vim/lua/internal/snips.lua
blob: 318c9f741247b7ba3a904d7692c0c41f1cdd73c7 (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
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

ls.setup {
  ext_opts = {
    [types.snippet] = {
      active = { virt_text = {{ '<-- luasnip', '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, {
    autowrap = autowrap,
    exec = exec,
    comment = comment,

    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' })