diff --git a/users/jrpotter/lang/python.nix b/users/jrpotter/lang/python.nix index fab4a7a..211eede 100644 --- a/users/jrpotter/lang/python.nix +++ b/users/jrpotter/lang/python.nix @@ -19,6 +19,10 @@ nvim-lspconfig = '' require('python.init').nvim_lspconfig() ''; + + nvim-snippets = '' + require('luasnip').add_snippets('python', require('python.snippets')) + ''; }; xdg.configFile."nvim/after/ftplugin/python.lua".text = '' diff --git a/users/jrpotter/neovim/config/python/snippets.lua b/users/jrpotter/neovim/config/python/snippets.lua new file mode 100644 index 0000000..995e7bc --- /dev/null +++ b/users/jrpotter/neovim/config/python/snippets.lua @@ -0,0 +1,16 @@ +local s = require('luasnip').snippet +local i = require('luasnip').insert_node +local fmt = require('luasnip.extras.fmt').fmt + +local v = require('utils.snippets').visual_dynamic_node + +return { + s( + { name = 'for', trig = 'for' }, + fmt([[ +for {} in {}: + {}]], + { i(1, ''), i(2, ''), v(3) } + ) + ), +} diff --git a/users/jrpotter/neovim/config/utils/snippets.lua b/users/jrpotter/neovim/config/utils/snippets.lua new file mode 100644 index 0000000..c1f5c6d --- /dev/null +++ b/users/jrpotter/neovim/config/utils/snippets.lua @@ -0,0 +1,28 @@ +local M = {} + +local luasnip = require('luasnip') +local d = luasnip.dynamic_node +local i = luasnip.insert_node +local sn = luasnip.snippet_node + +-- Creates an insertion node with a default value of whatever was stored in the +-- visual selection prior to entering insert mode (refer to the configuration +-- field `store_selection_keys`). +function M.visual_dynamic_node(index, default) + return d(index, function(_, parent) + local res, env = {}, parent.snippet.env + if type(env.LS_SELECT_RAW) ~= 'table' then + return sn(nil, { i(1, default or '') }, '') + end + for k, v in ipairs(env.LS_SELECT_RAW) do + local indent = env.CUSTOM_POS[2] - 1 + table.insert(res, (k == 1 and v) or string.rep(' ', indent) .. v) + end + if table.concat(res):match('^%s*$') then + return sn(nil, { i(1, default or '') }, '') + end + return sn(nil, { i(1, res) }, '') + end, {}) +end + +return M diff --git a/users/jrpotter/neovim/default.nix b/users/jrpotter/neovim/default.nix index 8b23b20..e10d520 100644 --- a/users/jrpotter/neovim/default.nix +++ b/users/jrpotter/neovim/default.nix @@ -27,6 +27,13 @@ let ''; }; + luasnip = { + plugin = pkgs.vimPlugins.luasnip; + config = '' + ${config.programs.neovim.nvim-snippets} + ''; + }; + nvim-cmp = { plugin = pkgs.vimPlugins.nvim-cmp; config = '' @@ -97,6 +104,16 @@ in Language-specific configurations for the `nvim-lspconfig` plugin. ''; }; + + nvim-snippets = lib.mkOption { + type = lib.types.lines; + example = '' + require('...').nvim_lspconfig() + ''; + description = lib.mdDoc '' + Language-specific configurations for the `luasnip` plugin. + ''; + }; }; config = { @@ -113,6 +130,7 @@ in } else p) [ colorscheme # Is always first. lualine + luasnip nvim-cmp nvim-dap nvim-lspconfig @@ -121,7 +139,6 @@ in pkgs.vimPlugins.cmp-buffer pkgs.vimPlugins.cmp-nvim-lsp pkgs.vimPlugins.cmp_luasnip - pkgs.vimPlugins.luasnip pkgs.vimPlugins.nvim-web-devicons pkgs.vimPlugins.vim-prettier ];