summaryrefslogtreecommitdiffstats
path: root/lua/conf/plugins/luasnip.lua
blob: fc6b2e4e57c359b2964b298af2d474b0bcea7fc6 (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
local map, auto = core.misc.map, core.misc.auto

return { "L3MON4D3/LuaSnip",
  branch = "v2.4.0",
  disable = not vim.fn.has("nvim-0.7.0"),
  config = function()
    vim.cmd("make install_jsregexp")
  end,
  function()
    local luasnip = require("luasnip")
    local types = require("luasnip.util.types")

    luasnip.config.set_config {
      history = true, -- return back into snippet
      enable_autosnippets = true,

      -- update on text insert and cursor hold
      updateevents = { "TextChanged", "TextChangedI", "CursorHold" },
      ext_opts = {
        [types.choiceNode] = {
          active = {
            virt_text = {{ "●", "@boolean" }}
          }
        },
        [types.insertNode] = {
          active = {
            virt_text = {{ "●", "@constant" }}
          }
        }
      }
    }

    map({"i", "s"}, "<c-a>", function()
      if luasnip.choice_active() then
        luasnip.change_choice(1)
      end
    end)

    map({"i", "s"}, "<c-e>", function()
      if luasnip.expandable() then
        luasnip.expand()
      end
    end)

    map({"i", "s"}, "<C-j>", function()
      if luasnip.jumpable(1) then
        luasnip.jump(1)
      end
    end)

    map({"i", "s"}, "<C-k>", function()
      if luasnip.jumpable(-1) then
        luasnip.jump(-1)
      end
    end)

    -- collect all snippets and add them when in the correct file type
    for _, file in ipairs(vim.api.nvim_get_runtime_file("lua/snippets/*.lua",
      true)) do
      local fn = file:gsub("^.*/", ""):gsub("%.lua$", "")

      auto("FileType", {
        pattern = fn,
        once = true,
        callback = function()
          local ret = require("snippets."..fn)
          if type(ret) ~= "boolean" then
            luasnip.add_snippets(fn, ret, { key = fn })
          end
        end
      })
    end
  end
}