@@ -3,21 +3,29 @@ if vim.g.snippets ~= "luasnip" then
33end
44
55local ls = require " luasnip"
6+ local types = require " luasnip.util.types"
67
78ls .config .set_config {
8- -- I have this on, but might not be necessary
9+ -- This tells LuaSnip to remember to keep around the last snippet.
10+ -- You can jump back into it even if you move outside of the selection
911 history = true ,
1012
1113 -- This one is cool cause if you have dynamic snippets, it updates as you type!
1214 updateevents = " TextChanged,TextChangedI" ,
1315
14- region_check_events = nil ,
15-
16- -- Autosnippets??
17- enable_autosnippets = nil ,
16+ -- Autosnippets:
17+ enable_autosnippets = true ,
1818
1919 -- Crazy highlights!!
20- ext_opts = nil ,
20+ -- #vid3
21+ -- ext_opts = nil,
22+ ext_opts = {
23+ [types .choiceNode ] = {
24+ active = {
25+ virt_text = { { " <-" , " Error" } },
26+ },
27+ },
28+ },
2129}
2230
2331-- create snippet
@@ -196,6 +204,11 @@ snippets.all = {
196204 -- ls.parser.parse_snippet({trig = "lsp"}, "$1 is ${2|hard,easy,challenging|}")
197205}
198206
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+
199212table.insert (
200213 snippets .all ,
201214 snippet (" cond" , {
@@ -212,20 +225,30 @@ table.insert(
212225)
213226
214227-- Make sure to not pass an invalid command, as io.popen() may write over nvim-text.
215- local function bash (_ , snip , command )
216- if snip .captures [1 ] then
217- command = snip .captures [1 ]
218- end
219-
220- local file = io.popen (command , " r" )
221- local res = { " $ " .. snip .captures [1 ] }
222- for line in file :lines () do
223- table.insert (res , line )
224- end
225- return res
226- end
228+ table.insert (
229+ snippets .all ,
230+ snippet (
231+ { trig = " $$ (.*)" , regTrig = true },
232+ f (function (_ , snip , command )
233+ if snip .captures [1 ] then
234+ command = snip .captures [1 ]
235+ end
227236
228- -- table.insert(snippets.all, snippet({ trig = "$$ (.*)", regTrig = true }, f(bash, {}, "ls")))
237+ local file = io.popen (command , " r" )
238+ local res = { " $ " .. snip .captures [1 ] }
239+ for line in file :lines () do
240+ table.insert (res , line )
241+ end
242+ return res
243+ end , {}, " ls" ),
244+ {
245+ -- Don't show this one, because it's not useful as a general purpose snippet.
246+ show_condition = function ()
247+ return false
248+ end ,
249+ }
250+ )
251+ )
229252
230253-- Lambda example
231254table.insert (
@@ -244,9 +267,10 @@ table.insert(
244267-- initial text :: this is going to be replaced :: initial tthis is going to be replacedxt
245268-- this is where we have text :: TEXT :: this is where we have TEXT
246269
247- snippets .lua = make (R " tj.snips.ft.lua" )
248- snippets .go = make (R " tj.snips.ft.go" )
249- snippets .rust = make (R " tj.snips.ft.rust" )
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
250274
251275local js_attr_split = function (args )
252276 local val = args [1 ][1 ]
@@ -266,7 +290,7 @@ local fill_line = function(char)
266290 return function ()
267291 local row = vim .api .nvim_win_get_cursor (0 )[1 ]
268292 local lines = vim .api .nvim_buf_get_lines (0 , row - 2 , row , false )
269- return string.rep (char , # lines [1 ] - # lines [ 2 ] )
293+ return string.rep (char , # lines [1 ])
270294 end
271295end
272296
@@ -291,12 +315,35 @@ snippets.rst = make {
291315
292316ls .snippets = snippets
293317
294- vim .cmd [[
295- imap <silent><expr> <c-k> luasnip#expand_or_jumpable() ? '<Plug>luasnip-expand-or-jump' : ''
296- inoremap <silent> <c-j> <cmd>lua require('luasnip').jump(-1)<CR>
318+ ls .autosnippets = {
319+ all = {
320+ ls .parser .parse_snippet (" $file$" , " $TM_FILENAME" ),
321+ },
322+ }
297323
298- imap <silent><expr> <C-l> luasnip#choice_active() ? '<Plug>luasnip-next-choice' : '<C-l>'
324+ -- <c-k> is my expansion key
325+ -- this will expand the current item or jump to the next item within the snippet.
326+ vim .keymap .set ({ " i" , " s" }, " <c-k>" , function ()
327+ if ls .expand_or_jumpable () then
328+ ls .expand_or_jump ()
329+ end
330+ end , { silent = true })
331+
332+ -- <c-j> is my jump backwards key.
333+ -- this always moves to the previous item within the snippet
334+ vim .keymap .set ({ " i" , " s" }, " <c-j>" , function ()
335+ if ls .jumpable (- 1 ) then
336+ ls .jump (- 1 )
337+ end
338+ end , { silent = true })
339+
340+ -- <c-l> is selecting within a list of options.
341+ -- This is useful for choice nodes (introduced in the forthcoming episode 2)
342+ vim .keymap .set (" i" , " <c-l>" , function ()
343+ if ls .choice_active () then
344+ ls .change_choice (1 )
345+ end
346+ end )
299347
300- snoremap <silent> <c-k> <cmd>lua require('luasnip').jump(1)<CR>
301- snoremap <silent> <c-j> <cmd>lua require('luasnip').jump(-1)<CR>
302- ]]
348+ -- shorcut to source my luasnips file again, which will reload my snippets
349+ vim .keymap .set (" n" , " <leader><leader>s" , " <cmd>source ~/.config/nvim/after/plugin/luasnip.lua<CR>" )
0 commit comments