add luasnip snippets

This commit is contained in:
2024-10-14 17:04:25 +03:00
parent b1a0742ffa
commit 7dcd4ec2f7
4 changed files with 87 additions and 6 deletions

View File

@@ -3,4 +3,5 @@ require("crentist.set")
require("crentist.autocmd")
require("crentist.lazy_init")
require("crentist.filetype")
require("crentist.luasnip")

View File

@@ -1,5 +1,6 @@
return {
'neovim/nvim-lspconfig',
tag = "v1.0.0",
dependencies = {
'williamboman/mason.nvim',
'williamboman/mason-lspconfig.nvim',
@@ -62,7 +63,7 @@ return {
['<CR>'] = cmp.mapping({
i = function(fallback)
if cmp.visible() and cmp.get_active_entry() then
cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false })
cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true })
else
fallback()
end

80
lua/crentist/luasnip.lua Normal file
View File

@@ -0,0 +1,80 @@
-- luasnip.lua
local ls = require('luasnip')
local s = ls.snippet
local i = ls.insert_node
local f = ls.function_node
local fmt = require("luasnip.extras.fmt").fmt
vim.keymap.set({ "i", "s" }, "<C-l>", function()
if ls.expand_or_jumpable() then
ls.expand_or_jump()
end
end, { desc = "Snippet next argument", silent = true })
-- nvim config
ls.add_snippets('lua', {
s({ name = 'LuaSnip Snippet', trig = 'snipf' },
fmt([[
s({ name='<name>', trig='<trig>'},
fmt(<template>,
{ <inputs> },
{ <opts> }
)
),
]], {
name = i(1, 'name'),
trig = i(2, 'trig'),
template = i(3, '[[]]'),
inputs = i(4, 'nodes'),
opts = i(5,
'opts')
},
{ delimiters = '<>' }
)
),
s({ name = 'Add snippets block', trig = 'add_snip' },
fmt([[
ls.add_snippets('<filetype>', {
<snippets>
})
]],
{ filetype = i(1, 'Filetype'), snippets = i(2, 'Snippets') },
{ delimiters = '<>' }
)
),
})
-- Comment block
local function com_string(_, snip, to_multiply)
local comment_string = vim.bo.commentstring
local repeats = snip.captures[1]
local result = comment_string:gsub("%%s", ""):gsub(" ", "")
if to_multiply then
return string.sub(
string.rep(result, repeats),
0,
repeats
)
end
return result
end
ls.add_snippets('all', {
s({ name = 'Comment block', trig = '#com(%d+)', regTrig = true },
fmt([[
{comment_block}
{comment} {comment_text}
{comment_block}
]],
{
comment_block = f(com_string, {}, { user_args = { true } }),
comment = f(com_string, {}, { user_args = { false } }),
comment_text = i(1,
'Comment')
}
)
),
})