Add python snippets.

main
Joshua Potter 2024-01-04 10:21:22 -07:00
parent 7e5b575053
commit 2cfa18616b
4 changed files with 66 additions and 1 deletions

View File

@ -19,6 +19,10 @@
nvim-lspconfig = '' nvim-lspconfig = ''
require('python.init').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 = '' xdg.configFile."nvim/after/ftplugin/python.lua".text = ''

View File

@ -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) }
)
),
}

View File

@ -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

View File

@ -27,6 +27,13 @@ let
''; '';
}; };
luasnip = {
plugin = pkgs.vimPlugins.luasnip;
config = ''
${config.programs.neovim.nvim-snippets}
'';
};
nvim-cmp = { nvim-cmp = {
plugin = pkgs.vimPlugins.nvim-cmp; plugin = pkgs.vimPlugins.nvim-cmp;
config = '' config = ''
@ -97,6 +104,16 @@ in
Language-specific configurations for the `nvim-lspconfig` plugin. 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 = { config = {
@ -113,6 +130,7 @@ in
} else p) [ } else p) [
colorscheme # Is always first. colorscheme # Is always first.
lualine lualine
luasnip
nvim-cmp nvim-cmp
nvim-dap nvim-dap
nvim-lspconfig nvim-lspconfig
@ -121,7 +139,6 @@ in
pkgs.vimPlugins.cmp-buffer pkgs.vimPlugins.cmp-buffer
pkgs.vimPlugins.cmp-nvim-lsp pkgs.vimPlugins.cmp-nvim-lsp
pkgs.vimPlugins.cmp_luasnip pkgs.vimPlugins.cmp_luasnip
pkgs.vimPlugins.luasnip
pkgs.vimPlugins.nvim-web-devicons pkgs.vimPlugins.nvim-web-devicons
pkgs.vimPlugins.vim-prettier pkgs.vimPlugins.vim-prettier
]; ];