Skip to content

Commit bbf0c73

Browse files
committed
updates for luasnip video (YES IT IS COMING SOON)
1 parent 8f14ab2 commit bbf0c73

5 files changed

Lines changed: 121 additions & 65 deletions

File tree

xdg_config/nvim/after/plugin/luasnip.lua

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@ if vim.g.snippets ~= "luasnip" then
33
end
44

55
local ls = require "luasnip"
6+
local types = require "luasnip.util.types"
67

78
ls.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\nfunc $1($2) $3 {\n\t$0\n}"))
208+
209+
-- luasnip.lua
210+
-- func() {}
211+
199212
table.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
231254
table.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

251275
local 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
271295
end
272296

@@ -291,12 +315,35 @@ snippets.rst = make {
291315

292316
ls.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>")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local lua_dirs = vim.fn.glob("./lua/*", 0, 1)
2+
for _, dir in ipairs(lua_dirs) do
3+
dir = string.gsub(dir, "./lua/", "")
4+
require("plenary.reload").reload_module(dir)
5+
end

xdg_config/nvim/lua/tj/lsp/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ local servers = {
136136
gdscript = true,
137137
graphql = true,
138138
html = true,
139-
-- pyright = true,
139+
pyright = true,
140140
vimls = true,
141141
yamlls = true,
142142

@@ -220,7 +220,7 @@ local setup_server = function(server, config)
220220
on_attach = custom_attach,
221221
capabilities = updated_capabilities,
222222
flags = {
223-
debounce_text_changes = 50,
223+
debounce_text_changes = nil,
224224
},
225225
}, config)
226226

xdg_config/nvim/lua/tj/snips/ft/go.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ local transform = function(text, info)
4646
end
4747
elseif text == "bool" then
4848
return t "false"
49+
elseif text == "string" then
50+
return t '""'
4951
elseif string.find(text, "*", 1, true) then
5052
return t "nil"
5153
end
@@ -137,4 +139,6 @@ local M = {
137139
ie = { "if err != nil {", "\treturn err", i(0), "}" },
138140
}
139141

142+
M.f = fmt("func {}({}) {} {{\n\t{}\n}}", { i(1, "name"), i(2), i(3), i(0) })
143+
140144
return M

0 commit comments

Comments
 (0)