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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
local ls = require("luasnip") --{{{
local s = ls.s --> snippet
local i = ls.i --> insert node
local t = ls.t --> text node
local d = ls.dynamic_node
local c = ls.choice_node --> takes in a pos as first arg and a table of nodes
local f = ls.function_node
local sn = ls.snippet_node
local fmt = require("luasnip.extras.fmt").fmt
local rep = require("luasnip.extras").rep
local snippets, autosnippets = {}, {} --}}}
local group = vim.api.nvim_create_augroup("Lua Snippets", { clear = true })
local file_pattern = "*.lua"
local function cs(trigger, nodes, opts) --{{{
local snippet = s(trigger, nodes)
local target_table = snippets
local pattern = file_pattern
local keymaps = {}
if opts ~= nil then
-- check for custom pattern
if opts.pattern then
pattern = opts.pattern
end
-- if opts is a string
if type(opts) == "string" then
if opts == "auto" then
target_table = autosnippets
else
table.insert(keymaps, { "i", opts })
end
end
-- if opts is a table
if opts ~= nil and type(opts) == "table" then
for _, keymap in ipairs(opts) do
if type(keymap) == "string" then
table.insert(keymaps, { "i", keymap })
else
table.insert(keymaps, keymap)
end
end
end
-- set autocmd for each keymap
if opts ~= "auto" then
for _, keymap in ipairs(keymaps) do
vim.api.nvim_create_autocmd("BufEnter", {
pattern = pattern,
group = group,
callback = function()
vim.keymap.set(keymap[1], keymap[2], function()
ls.snip_expand(snippet)
end, { noremap = true, silent = true, buffer = true })
end,
})
end
end
end
table.insert(target_table, snippet) -- insert snippet into appropriate table
end --}}}
-- Start Refactoring --
cs("CMD", { -- [CMD] multiline vim.cmd{{{
t({ "vim.cmd[[", " " }),
i(1, ""),
t({ "", "]]" }),
}) --}}}
cs("cmd", fmt("vim.cmd[[{}]]", { i(1, "") })) -- single line vim.cmd
cs({ -- github import for packer{{{
trig = "https://github%.com/([%w-%._]+)/([%w-%._]+)!",
regTrig = true,
hidden = true,
}, {
t([[use "]]),
f(function(_, snip)
return snip.captures[1]
end),
t("/"),
f(function(_, snip)
return snip.captures[2]
end),
t({ [["]], "" }),
i(1, ""),
}, "auto") --}}}
cs( -- {regexSnippet} LuaSnippet{{{
"regexSnippet",
fmt(
[=[
cs( -- {}
{{ trig = "{}", regTrig = true, hidden = true }}, fmt([[
{}
]], {{
{}
}}))
]=],
{
i(1, "Description"),
i(2, ""),
i(3, ""),
i(4, ""),
}
),
{ pattern = "*/snippets/*.lua", "<C-d>" }
) --}}}
cs( -- [luaSnippet] LuaSnippet{{{
"luaSnippet",
fmt(
[=[
cs("{}", fmt( -- {}
[[
{}
]], {{
{}
}}){})
]=],
{
i(1, ""),
i(2, "Description"),
i(3, ""),
i(4, ""),
c(5, {
t(""),
fmt([[, "{}"]], { i(1, "keymap") }),
fmt([[, {{ pattern = "{}", {} }}]], { i(1, "*/snippets/*.lua"), i(2, "keymap") }),
}),
}
),
{ pattern = "*/snippets/*.lua", "jcs" }
) --}}}
cs( -- choice_node_snippet luaSnip choice node{{{
"choice_node_snippet",
fmt(
[[
c({}, {{ {} }}),
]],
{
i(1, ""),
i(2, ""),
}
),
{ pattern = "*/snippets/*.lua", "jcn" }
) --}}}
cs( -- [function] Lua function snippet{{{
"function",
fmt(
[[
function {}({})
{}
end
]],
{
i(1, ""),
i(2, ""),
i(3, ""),
}
),
"jff"
) --}}}
cs( -- [local_function] Lua function snippet{{{
"local_function",
fmt(
[[
local function {}({})
{}
end
]],
{
i(1, ""),
i(2, ""),
i(3, ""),
}
),
"jlf"
) --}}}
cs( -- [local] Lua local variable snippet{{{
"local",
fmt(
[[
local {} = {}
]],
{ i(1, ""), i(2, "") }
),
"jj"
) --}}}
-- Tutorial Snippets go here --
local myFirstSnippet = s("myFirstSnippet", {
t("Hi! This is my first snippet in Luasnip "),
i(1, "placeholder"),
t({"", "this is another text node", ""}),
i(2, "put here"),
})
table.insert(snippets, myFirstSnippet)
local mySecondSnippet = s(
"mySecondSnippet",
fmt(
[[
local {} = function({})
{} {{ im in a curly braces }}
end {}
]],
{
i(1, "myVar"),
c(2, { t(""), i(1, "myArg") }),
i(3, "-- TODO: something"),
i(4, "-- nice")
}
)
)
table.insert(snippets, mySecondSnippet)
local myFirstAutoSnippet = s("automatic", { t("This was auto triggered") })
table.insert(autosnippets, myFirstAutoSnippet)
local mySecondAutoSnippet = s({ trig = "normal", regTrig = true }, { t("This was auto triggered") })
table.insert(autosnippets, mySecondAutoSnippet)
-- End Refactoring --
return snippets, autosnippets
|