From 37d2389ed8d497df25e8b4ec21424b87eacac4e3 Mon Sep 17 00:00:00 2001 From: Joshua Finch Date: Wed, 21 May 2025 05:38:25 -0500 Subject: [PATCH] feat: Adding some additions to the luasnip code for nvim to start building out a snippet library for common actions, starting with docstrings in python. --- deploy.sh | 5 ++++ nvim/lua/plugins/luasnip.lua | 16 +++++++++++++ nvim/snippets/python.lua | 45 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 nvim/snippets/python.lua diff --git a/deploy.sh b/deploy.sh index 0fa2f96..1f06d83 100755 --- a/deploy.sh +++ b/deploy.sh @@ -26,3 +26,8 @@ if [ ! -L $HOME/.wezterm.lua ] then ln -fs $PWD/wezterm.lua $HOME/.wezterm.lua fi + +if [ ! -L $HOME/.config/nvim/snippets ] +then + ln -fs $PWD/nvim/snippets $HOME/.config/nvim/snippets +fi diff --git a/nvim/lua/plugins/luasnip.lua b/nvim/lua/plugins/luasnip.lua index a457834..9f9e5d6 100644 --- a/nvim/lua/plugins/luasnip.lua +++ b/nvim/lua/plugins/luasnip.lua @@ -1,7 +1,23 @@ return { 'L3MON4D3/LuaSnip', config = function() + local ls = require('luasnip') + local extras = require('luasnip.extras') + local l = extras.lambda + ls.setup({ + snip_env = { + s = function(...) + local snip = ls.s(...) + table.insert(getfenv(2).ls_file_snippets, snip) + end, + parse = function(...) + local snip = ls.parser.parse_snippet(...) + table.insert(getfenv(2).ls_file_snippets, snip) + end, + }, + }) require('luasnip.loaders.from_vscode').lazy_load() + require('luasnip.loaders.from_lua').lazy_load({paths = "./snippets"}) end, dependencies = { 'saadparwaiz1/cmp_luasnip', -- Wrapper to load snippets in nvim-cmp diff --git a/nvim/snippets/python.lua b/nvim/snippets/python.lua new file mode 100644 index 0000000..ee0cd92 --- /dev/null +++ b/nvim/snippets/python.lua @@ -0,0 +1,45 @@ +local ls = require('luasnip') +local s = ls.snippet +local i = ls.insert_node +local t = ls.text_node +local l = require('luasnip.extras').lambda +local fmt = require('luasnip.extras.fmt').fmt +local ts_post = require('luasnip.extras.treesitter_postfix').treesitter_postfix + +return { + ts_post({ + matchTSNode = { + query = [[ + (function_definition + parameters: (parameters) @params + return_type: (type) @return + ) @prefix + ]], + query_lang = "python", + }, + trig = "docstring", + }, fmt([[ + ''' {} + + Parameters: + {} () - + + Returns: + {} - + {} + ''' + ]], { + i(1), + l(l.LS_TSCAPTURE_PARAMS), + l(l.LS_TSCAPTURE_RETURN), + l(l.LS_TSDATA), + })), + s( + { trig = "thingy" }, + { t('Woot!') } + ), + s( + { trig = 'thingy2' }, + { t('Woot2!') } + ) +}