aboutsummaryrefslogtreecommitdiffstats
path: root/lua/pi/snips/ft/lua.lua
blob: d896f3e572d9dc9b4399935de249b5fffc74fced (plain)
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
local ls = require "luasnip"

-- local snippet = ls.s
local snippet_from_nodes = ls.sn
local c = ls.choice_node
local d = ls.dynamic_node
local i = ls.insert_node
local f = ls.function_node
local t = ls.text_node

local fmt = require("luasnip.extras.fmt").fmt

local require_var = function(args, _)
  local text = args[1][1] or ""
  local split = vim.split(text, ".", { plain = true })

  local options = {}
  for len = 0, #split - 1 do
    table.insert(options, t(table.concat(vim.list_slice(split, #split - len, #split), "_")))
  end

  return snippet_from_nodes(nil, {
    c(1, options),
  })
end

local newline = function(text)
  return t { "", text }
end

return {
  req = fmt([[local {} = require("{}")]], {
    d(2, require_var, { 1 }),
    i(1),
  }),
  lf = {
    desc = "table function",
    "local ",
    i(1),
    " = function(",
    i(2),
    ")",
    newline "  ",
    i(0),
    newline "end",
  },

  preq = fmt(
    [[local ok, {} = pcall(require, "{}")
if not ok then
    return
end]],
    {
      f(function(args)
        return args[1][1]
      end, { 1 }),
      i(1),
    }
  ),

  plugin = fmt(
    [[return {{
    "{}"
}}]],
    {
      i(1, "plugin"),
    }
  ),
}