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
|
local ls_helpers = require("helpers")
local exec_lua, feed, exec =
ls_helpers.exec_lua, ls_helpers.feed, ls_helpers.exec
local Screen = require("test.functional.ui.screen")
describe("multisnippets", function()
local screen
before_each(function()
ls_helpers.clear()
ls_helpers.session_setup_luasnip()
exec_lua([[
ms = require("luasnip.nodes.multiSnippet").new_multisnippet
add_ms = function(...)
ls.add_snippets("all", {
ms(...)
}, {key = "ms"})
end
]])
screen = Screen.new(50, 3)
screen:attach()
screen:set_default_attr_ids({
[0] = { bold = true, foreground = Screen.colors.Blue },
[1] = { bold = true, foreground = Screen.colors.Brown },
[2] = { bold = true },
[3] = { background = Screen.colors.LightGray },
})
end)
after_each(function()
screen:detach()
end)
it("work at all", function()
exec_lua([[
add_ms({"a", "b", "c", "d"}, {t"a or b or c or d"})
]])
local function test()
screen:expect({
grid = [[
a or b or c or d^ |
{0:~ }|
{2:-- INSERT --} |]],
})
end
feed("ia<Plug>luasnip-expand-or-jump")
test()
feed("<Esc>ccb<Plug>luasnip-expand-or-jump")
test()
feed("<Esc>ccc<Plug>luasnip-expand-or-jump")
test()
feed("<Esc>ccd<Plug>luasnip-expand-or-jump")
test()
-- can expand multiple at once.
feed(
"<Esc>cca<Plug>luasnip-expand-or-jump<Space>b<Plug>luasnip-expand-or-jump"
)
screen:expect({
grid = [[
a or b or c or d a or b or c or d^ |
{0:~ }|
{2:-- INSERT --} |]],
})
exec_lua([[
m_snip = ms({"a", "b", "c", "d"}, {t"a or b or c or d"})
for _, snip in ipairs(m_snip:retrieve_all()) do
assert(snip:get_docstring()[1] == "a or b or c or d$0", "get_docstring works")
assert(snip:copy() ~= snip:copy(), "copy produces new snippet")
end
]])
end)
it("can merge string-context with table-context", function()
exec_lua([[
ls.setup({enable_autosnippets = true})
add_ms({{trig="a",snippetType="autosnippet"}, "b", "c", "d"}, {t"a or b or c or d"})
]])
local function test()
screen:expect({
grid = [[
a or b or c or d^ |
{0:~ }|
{2:-- INSERT --} |]],
})
end
-- autotriggered!
feed("ia")
test()
feed("<Esc>ccb<Plug>luasnip-expand-or-jump")
test()
feed("<Esc>ccc<Plug>luasnip-expand-or-jump")
test()
feed("<Esc>ccd<Plug>luasnip-expand-or-jump")
test()
end)
it("respects `common` context", function()
exec_lua([[
ls.setup({enable_autosnippets = true})
add_ms({common={trig="a",snippetType="autosnippet"}, "b", "c", {snippetType="snippet"}}, {t"a or b or c or d"})
]])
local function test()
screen:expect({
grid = [[
a or b or c or d^ |
{0:~ }|
{2:-- INSERT --} |]],
})
end
feed("ia<Plug>luasnip-expand-or-jump")
test()
feed("<Esc>ccb")
test()
feed("<Esc>ccc")
test()
end)
it("respects `opts`", function()
exec_lua([[
ls.setup({enable_autosnippets = true})
ls.__did_expand = false
add_ms({"a", "b"}, {t"a or b or c or d"}, {
common_opts = {
callbacks = {
[-1] = {
[events.pre_expand] = function()
ls.__did_expand = true
end
}
}
}
})
]])
feed("ia")
exec_lua([[
ls.expand()
assert(ls.__did_expand)
]])
feed("<Esc>ccb")
exec_lua([[
ls.__did_expand = false
ls.expand()
assert(ls.__did_expand)
]])
end)
it("work with extend_decorator", function()
ls_helpers.session_setup_luasnip({
setup_extend = { enable_autosnippets = true },
})
exec_lua([[
-- contexts without trigger get "asdf", add one context which has
-- the default-trigger and is an autosnippet.
local auto_multisnippet = ls.extend_decorator.apply(ls.multi_snippet, {common = "asdf", {snippetType = "autosnippet"}})
ls.add_snippets("all", {
auto_multisnippet({"bsdf"}, {t"csdf"})
}, {key = "asdf"})
]])
feed("iasdf")
screen:expect({
grid = [[
csdf^ |
{0:~ }|
{2:-- INSERT --} |]],
})
feed("<Cr>bsdf")
exec_lua("ls.expand()")
screen:expect({
grid = [[
csdf |
csdf^ |
{2:-- INSERT --} |]],
})
end)
end)
|