From 41579e25a0a6ec122f8e80bd19265dfd05d67932 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 5 Jan 2024 09:58:57 -0700 Subject: [PATCH] Add lua snippets file. --- users/jrpotter/lang/lua.nix | 4 ++ users/jrpotter/neovim/config/lua/snippets.lua | 48 +++++++++++++++++++ .../neovim/config/python/snippets.lua | 10 ++-- .../jrpotter/neovim/config/utils/luasnip.lua | 2 +- 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 users/jrpotter/neovim/config/lua/snippets.lua diff --git a/users/jrpotter/lang/lua.nix b/users/jrpotter/lang/lua.nix index 60561f6..fb25d87 100644 --- a/users/jrpotter/lang/lua.nix +++ b/users/jrpotter/lang/lua.nix @@ -8,5 +8,9 @@ nvim-lspconfig = '' require('lua.init').nvim_lspconfig() ''; + + nvim-snippets = '' + require('luasnip').add_snippets('lua', require('lua.snippets')) + ''; }; } diff --git a/users/jrpotter/neovim/config/lua/snippets.lua b/users/jrpotter/neovim/config/lua/snippets.lua new file mode 100644 index 0000000..4b99289 --- /dev/null +++ b/users/jrpotter/neovim/config/lua/snippets.lua @@ -0,0 +1,48 @@ +local fmt = require('luasnip.extras.fmt').fmt +local ls = require('luasnip') +local ul = require('utils.luasnip') + +local c = ls.choice_node +local i = ls.insert_node +local s = ls.snippet +local sn = ls.snippet_node +local t = ls.text_node + +return { + s( + { name = 'for', trig = 'for' }, + fmt([[ +for {} do + {} +end]], + { + c(1, { + sn(nil, { + i(1, 'i'), + t('='), + i(2, 'm'), + t(','), + i(3, 'n'), + }), + sn(nil, { + i(1, 'k'), + t(', '), + i(2, 'v'), + t(' in pairs('), + i(3, 'tbl'), + t(')'), + }), + sn(nil, { + i(1, 'i'), + t(', '), + i(2, 'v'), + t(' in ipairs('), + i(3, 'seq'), + t(')'), + }), + }), + ul.visual_isn(2), + } + ) + ), +} diff --git a/users/jrpotter/neovim/config/python/snippets.lua b/users/jrpotter/neovim/config/python/snippets.lua index 12f6ec8..0050a76 100644 --- a/users/jrpotter/neovim/config/python/snippets.lua +++ b/users/jrpotter/neovim/config/python/snippets.lua @@ -18,10 +18,10 @@ for {} in {}: {}]], { c(1, { - i(nil, '_1'), - i(nil, '_2'), - i(nil, '_3'), - { i(1, '_4'), t(', '), i(2, '_5') }, + i(nil, 'i'), + i(nil, 'k'), + i(nil, 'v'), + { i(1, 'k'), t(', '), i(2, 'v') }, }), d(2, function(_, parent) local index = ul.choice_index(parent.nodes[2]) @@ -47,7 +47,7 @@ for {} in {}: })[index]) ) end, { 1 }), - ul.visual_dynamic_node(3), + ul.visual_isn(3), } ) ), diff --git a/users/jrpotter/neovim/config/utils/luasnip.lua b/users/jrpotter/neovim/config/utils/luasnip.lua index d8515ac..97be621 100644 --- a/users/jrpotter/neovim/config/utils/luasnip.lua +++ b/users/jrpotter/neovim/config/utils/luasnip.lua @@ -3,7 +3,7 @@ local M = {} local luasnip = require('luasnip') local types = require('luasnip.util.types') -function M.visual_dynamic_node(pos) +function M.visual_isn(pos) local f = luasnip.function_node local isn = luasnip.indent_snippet_node