Skip to content

Commit 975fd87

Browse files
authored
Merge pull request #513 from atticus-sullivan/autotrigger_as_snippet_opt_clean
Autotrigger as snippet option
2 parents 6e506ce + 8fcf735 commit 975fd87

File tree

6 files changed

+167
-12
lines changed

6 files changed

+167
-12
lines changed

DOC.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ entries:
117117
Snippets with high priority will be matched to a trigger before those with a
118118
lower one.
119119
The priority for multiple snippets can also be set in `add_snippets`.
120+
- `snippetType`: string, should be either `snippet` or `autosnippet` (ATTENTION:
121+
singular form is used), decides whether this snippet has to be triggered by
122+
`ls.expand()` or whether is triggered automatically (don't forget to set
123+
`ls.config.setup({ enable_autosnippets = true })` if you want to use this
124+
feature). If unset it depends on how the snippet is added of which type the
125+
snippet will be.
120126

121127
`s` can also be a single string, in which case it is used instead of `trig`, all
122128
other values being defaulted:
@@ -2228,7 +2234,10 @@ the lazy_load.
22282234
If `ft` is `nil`, `snippets` should be a table containing lists of snippets,
22292235
the keys are corresponding filetypes.
22302236
`opts` may contain the following keys:
2231-
- `type`: type of `snippets`, `"snippets"` or `"autosnippets"`.
2237+
- `type`: type of `snippets`, `"snippets"` or `"autosnippets"` (ATTENTION:
2238+
plural form used here). This serves as default value for the `snippetType`
2239+
key of each snippet added by this call see [SNIPPETS](#SNIPPETS).
2240+
22322241
- `key`: Key that identifies snippets added via this call.
22332242
If `add_snippets` is called with a key that was already used, the snippets
22342243
from that previous call will be removed.

doc/luasnip.txt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*luasnip.txt* For NVIM v0.5.0 Last change: 2022 September 08
1+
*luasnip.txt* For NVIM v0.5.0 Last change: 2022 September 13
22

33
==============================================================================
44
Table of Contents *luasnip-table-of-contents*
@@ -168,6 +168,12 @@ entries:
168168
Snippets with high priority will be matched to a trigger before those with a
169169
lower one.
170170
The priority for multiple snippets can also be set in `add_snippets`.
171+
- `snippetType`: string, should be either `snippet` or `autosnippet` (ATTENTION:
172+
singular form is used), decides whether this snippet has to be triggered by
173+
`ls.expand()` or whether is triggered automatically (don’t forget to set
174+
`ls.config.setup({ enable_autosnippets = true })` if you want to use this
175+
feature). If unset it depends on how the snippet is added of which type the
176+
snippet will be.
171177

172178

173179
`s` can also be a single string, in which case it is used instead of `trig`,
@@ -2215,13 +2221,14 @@ empty the snippets table and the caches of the lazy_load.
22152221
Makes `snippets` (list of snippets) available in `ft`. If `ft` is `nil`,
22162222
`snippets` should be a table containing lists of snippets, the keys are
22172223
corresponding filetypes. `opts` may contain the following keys:
2218-
- `type`: type of `snippets`, `"snippets"` or `"autosnippets"`.
2219-
- `key`: Key that identifies snippets added via this call.
2220-
If `add_snippets` is called with a key that was already used, the snippets
2221-
from that previous call will be removed.
2222-
This can be used to reload snippets: pass an unique key to each
2223-
`add_snippets` and just re-do the `add_snippets`-call when the snippets have
2224-
changed.
2224+
- `type`: type of `snippets`, `"snippets"` or `"autosnippets"` (ATTENTION: plural
2225+
form used here). This serves as default value for the `snippetType` key of each
2226+
snippet added by this call see |luasnip-snippets|.
2227+
- `key`: Key that identifies snippets added via this call. If `add_snippets` is
2228+
called with a key that was already used, the snippets from that previous call
2229+
will be removed. This can be used to reload snippets: pass an unique key to
2230+
each `add_snippets` and just re-do the `add_snippets`-call when the snippets
2231+
have changed.
22252232
- `override_priority`: set priority for all snippets.
22262233
- `default_priority`: set priority only for snippets without snippet-priority.
22272234
- `clean_invalidated(opts: table or nil) -> bool`: clean invalidated snippets

lua/luasnip/nodes/snippet.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ local function init_snippet_context(context)
186186
-- might be nil, but whitelisted in snippetProxy.
187187
context.priority = context.priority
188188

189+
-- might be nil, but whitelisted in snippetProxy.
190+
-- shall be a string, allowed values: "snippet", "autosnippet"
191+
assert(
192+
not context.snippetType
193+
or context.snippetType == "snippet"
194+
or context.snippetType == "autosnippet",
195+
"snippetType has to be either 'snippet' or 'autosnippet' (or unset)"
196+
)
197+
-- switch to plural forms so that we can use this for indexing
198+
context.snippetType = context.snippetType == "autosnippet"
199+
and "autosnippets"
200+
or context.snippetType == "snippet" and "snippets"
201+
or nil
202+
189203
-- maybe do this in a better way when we have more parameters, but this is
190204
-- fine for now.
191205

lua/luasnip/nodes/snippetProxy.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747

4848
-- some values of the snippet are nil by default, list them here so snippets
4949
-- aren't instantiated because of them.
50-
local license_to_nil = { priority = true }
50+
local license_to_nil = { priority = true, snippetType = true }
5151

5252
-- context and opts are (almost) the same objects as in s(contex, nodes, opts), snippet is a string representing the snippet.
5353
-- opts can aditionally contain the key `parse_fn`, which will be used to parse

lua/luasnip/session/snippet_collection.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,21 @@ function M.add_snippets(snippets, opts)
253253
or opts.default_priority
254254
or 1000
255255

256+
-- if snippetType undefined by snippet, take default value from opts
257+
snip.snippetType = snip.snippetType ~= nil and snip.snippetType
258+
or opts.type
259+
assert(
260+
snip.snippetType == "autosnippets"
261+
or snip.snippetType == "snippets",
262+
"snipptType must be either 'autosnippets' or 'snippets'"
263+
)
264+
256265
snip.id = current_id
257266
current_id = current_id + 1
258267

259268
-- do the insertion
260-
table.insert(by_prio[opts.type][snip.priority][ft], snip)
261-
table.insert(by_ft[opts.type][ft], snip)
269+
table.insert(by_prio[snip.snippetType][snip.priority][ft], snip)
270+
table.insert(by_ft[snip.snippetType][ft], snip)
262271
by_id[snip.id] = snip
263272
end
264273
end

tests/integration/add_snippets_spec.lua

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,120 @@ describe("add_snippets", function()
193193
{2:-- INSERT --} |]],
194194
})
195195
end)
196+
197+
it("add autosnippets by option", function()
198+
exec_lua("ls.config.setup({ enable_autosnippets = true })")
199+
exec_lua([[
200+
ls.add_snippets("all", {
201+
ls.snippet({trig="triA", snippetType="autosnippet"}, {ls.text_node("helloAworld")}, {})
202+
}, {
203+
key = "a",
204+
} )
205+
]])
206+
exec_lua([[
207+
ls.add_snippets("all", {
208+
ls.snippet({trig="triB", snippetType="snippet"}, {ls.text_node("helloBworld")}, {})
209+
}, {
210+
key = "b",
211+
} )
212+
]])
213+
exec_lua([[
214+
ls.add_snippets("all", {
215+
ls.snippet({trig="triC", snippetType=nil}, {ls.text_node("helloCworld")}, {})
216+
}, {
217+
key = "c",
218+
type="snippets"
219+
} )
220+
]])
221+
exec_lua([[
222+
ls.add_snippets("all", {
223+
ls.snippet({trig="triD", snippetType=nil}, {ls.text_node("helloDworld")}, {})
224+
}, {
225+
key = "d",
226+
type="autosnippets"
227+
} )
228+
]])
229+
exec_lua([[
230+
ls.add_snippets("all", {
231+
ls.snippet({trig="triE", snippetType="snippet"}, {ls.text_node("helloEworld")}, {})
232+
}, {
233+
key = "e",
234+
type="autosnippets"
235+
} )
236+
]])
237+
238+
-- check if snippet "a" is automatically triggered
239+
feed("<ESC>cc") -- rewrite line
240+
feed("triA")
241+
screen:expect({
242+
grid = [[
243+
helloAworld^ |
244+
{0:~ }|
245+
{2:-- INSERT --} |]],
246+
})
247+
248+
feed("<ESC>cc") -- rewrite line
249+
feed("triB")
250+
-- check if snippet "b" is NOT automatically triggered
251+
screen:expect({
252+
grid = [[
253+
triB^ |
254+
{0:~ }|
255+
{2:-- INSERT --} |]],
256+
})
257+
-- check if snippet "b" is working
258+
exec_lua("ls.expand()")
259+
screen:expect({
260+
grid = [[
261+
helloBworld^ |
262+
{0:~ }|
263+
{2:-- INSERT --} |]],
264+
})
265+
266+
feed("<ESC>cc") -- rewrite line
267+
feed("triC")
268+
-- check if snippet "c" is NOT automatically triggered
269+
screen:expect({
270+
grid = [[
271+
triC^ |
272+
{0:~ }|
273+
{2:-- INSERT --} |]],
274+
})
275+
-- check if snippet "c" is working
276+
exec_lua("ls.expand()")
277+
screen:expect({
278+
grid = [[
279+
helloCworld^ |
280+
{0:~ }|
281+
{2:-- INSERT --} |]],
282+
})
283+
284+
feed("<ESC>cc") -- rewrite line
285+
feed("triD")
286+
-- check if snippet "d" is automatically triggered
287+
screen:expect({
288+
grid = [[
289+
helloDworld^ |
290+
{0:~ }|
291+
{2:-- INSERT --} |]],
292+
})
293+
294+
feed("<ESC>cc") -- rewrite line
295+
feed("triE")
296+
-- check if snippet "e" is NOT automatically triggered
297+
screen:expect({
298+
grid = [[
299+
triE^ |
300+
{0:~ }|
301+
{2:-- INSERT --} |]],
302+
})
303+
-- check if snippet "c" is working
304+
exec_lua("ls.expand()")
305+
screen:expect({
306+
grid = [[
307+
helloEworld^ |
308+
{0:~ }|
309+
{2:-- INSERT --} |]],
310+
})
311+
end)
196312
end)

0 commit comments

Comments
 (0)