@@ -2,6 +2,8 @@ if vim.g.snippets ~= "luasnip" or not pcall(require, "luasnip") then
22 return
33end
44
5+ local make = R (" tj.snips" ).make
6+
57local ls = require " luasnip"
68local types = require " luasnip.util.types"
79
@@ -77,44 +79,18 @@ local events = require "luasnip.util.events"
7779-- return ls.parser.parse_snippet({ trig = trig }, expanded)
7880-- end
7981
80- local shortcut = function (val )
81- if type (val ) == " string" then
82- return { t { val }, i (0 ) }
83- end
84-
85- if type (val ) == " table" then
86- for k , v in ipairs (val ) do
87- if type (v ) == " string" then
88- val [k ] = t { v }
89- end
90- end
91- end
92-
93- return val
94- end
95-
96- local make = function (tbl )
97- local result = {}
98- for k , v in pairs (tbl ) do
99- table.insert (result , (snippet ({ trig = k , desc = v .desc }, shortcut (v ))))
100- end
101-
102- return result
103- end
104-
10582local same = function (index )
10683 return f (function (args )
10784 return args [1 ]
10885 end , { index })
10986end
11087
111- local snippets = {}
112-
11388local toexpand_count = 0
11489
11590-- `all` key means for all filetypes.
11691-- Shared between all filetypes. Has lower priority than a particular ft tho
117- snippets .all = {
92+ -- snippets.all = {
93+ ls .add_snippets (nil , {
11894 -- basic, don't need to know anything else
11995 -- arg 1: string
12096 -- arg 2: a node
@@ -202,31 +178,25 @@ snippets.all = {
202178
203179 --
204180 -- ls.parser.parse_snippet({trig = "lsp"}, "$1 is ${2|hard,easy,challenging|}")
205- }
206-
207- table.insert (snippets .all , ls .parser .parse_snippet (" example" , " -- $TM_FILENAME\n func $1($2) $3 {\n\t $0\n }" ))
208-
209- -- luasnip.lua
210- -- func() {}
211-
212- table.insert (
213- snippets .all ,
214- snippet (" cond" , {
215- t " will only expand in c-style comments" ,
216- }, {
217- condition = function (
218- line_to_cursor --[[ , matched_trigger, captures ]]
219- )
220- local commentstring = " %s*" .. vim .bo .commentstring :gsub (" %%s" , " " )
221- -- optional whitespace followed by //
222- return line_to_cursor :match (commentstring )
223- end ,
224- })
225- )
181+ })
182+
183+ -- table.insert(
184+ -- snippets.all,
185+ -- snippet("cond", {
186+ -- t "will only expand in c-style comments",
187+ -- }, {
188+ -- condition = function(
189+ -- line_to_cursor --[[ , matched_trigger, captures ]]
190+ -- )
191+ -- local commentstring = "%s*" .. vim.bo.commentstring:gsub("%%s", "")
192+ -- -- optional whitespace followed by //
193+ -- return line_to_cursor:match(commentstring)
194+ -- end,
195+ -- })
196+ -- )
226197
227198-- Make sure to not pass an invalid command, as io.popen() may write over nvim-text.
228- table.insert (
229- snippets .all ,
199+ ls .add_snippets (nil , {
230200 snippet (
231201 { trig = " $$ (.*)" , regTrig = true },
232202 f (function (_ , snip , command )
@@ -247,30 +217,8 @@ table.insert(
247217 return false
248218 end ,
249219 }
250- )
251- )
252-
253- -- Lambda example
254- table.insert (
255- snippets .all ,
256- snippet (" transform2" , {
257- i (1 , " initial text here" ),
258- t " :: " ,
259- i (2 , " replacement for text" ),
260- t " :: " ,
261- -- t { "", "" },
262- -- Lambdas can also apply transforms USING the text of other nodes:
263- l (l ._1 :gsub (" text" , l ._2 ), { 1 , 2 }),
264- })
265- )
266-
267- -- initial text :: this is going to be replaced :: initial tthis is going to be replacedxt
268- -- this is where we have text :: TEXT :: this is where we have TEXT
269-
270- for _ , ft_path in ipairs (vim .api .nvim_get_runtime_file (" lua/tj/snips/ft/*.lua" , true )) do
271- local ft = vim .fn .fnamemodify (ft_path , " :t:r" )
272- snippets [ft ] = make (loadfile (ft_path )())
273- end
220+ ),
221+ })
274222
275223local js_attr_split = function (args )
276224 local val = args [1 ][1 ]
@@ -294,32 +242,31 @@ local fill_line = function(char)
294242 end
295243end
296244
297- snippets . rst = make {
298- jsa = {
299- " :js:attr:` " ,
300- d ( 2 , js_attr_split , { 1 }),
301- " < " ,
302- i ( 1 ),
303- " > " ,
304- " ` " ,
305- } ,
306-
307- link = { " .. _ " , i ( 1 ), " : " },
245+ ls . add_snippets (
246+ " rst " ,
247+ make {
248+ jsa = {
249+ " :js:attr:` " ,
250+ d ( 2 , js_attr_split , { 1 } ),
251+ " < " ,
252+ i ( 1 ) ,
253+ " > " ,
254+ " ` " ,
255+ },
308256
309- head = f (fill_line " =" , {}),
310- sub = f (fill_line " -" , {}),
311- subsub = f (fill_line " ^" , {}),
257+ link = { " .. _" , i (1 ), " :" },
312258
313- ref = { " :ref:`" , same (1 ), " <" , i (1 ), " >`" },
314- }
259+ head = f (fill_line " =" , {}),
260+ sub = f (fill_line " -" , {}),
261+ subsub = f (fill_line " ^" , {}),
315262
316- ls .snippets = snippets
263+ ref = { " :ref:`" , same (1 ), " <" , i (1 ), " >`" },
264+ }
265+ )
317266
318- ls .autosnippets = {
319- all = {
320- ls .parser .parse_snippet (" $file$" , " $TM_FILENAME" ),
321- },
322- }
267+ for _ , ft_path in ipairs (vim .api .nvim_get_runtime_file (" lua/tj/snips/ft/*.lua" , true )) do
268+ loadfile (ft_path )()
269+ end
323270
324271-- <c-k> is my expansion key
325272-- this will expand the current item or jump to the next item within the snippet.
0 commit comments