Dotfiles_nvim/lua/editor/snippets.lua
2024-09-10 09:41:43 +02:00

88 lines
3 KiB
Lua

local K = require('core.keymap')
return {
{
'L3MON4D3/LuaSnip',
version = '2.*',
dependencies = {
'rafamadriz/friendly-snippets',
'mireq/luasnip-snippets',
},
build = 'make install_jsregexp',
config = function()
-- Setup --
require('luasnip.loaders.from_vscode').lazy_load()
require('luasnip.loaders.from_snipmate').lazy_load()
require('luasnip_snippets.common.snip_utils').setup()
end,
},
{
'hrsh7th/nvim-cmp',
dependencies = {
'L3MON4D3/LuaSnip',
'saadparwaiz1/cmp_luasnip',
'hrsh7th/cmp-nvim-lsp',
},
config = function()
-- Setup --
local cmp = require('cmp')
local luasnip = require('luasnip')
luasnip.setup({
load_ft_func = require('luasnip_snippets.common.snip_utils').load_ft_func,
ft_func = require('luasnip_snippets.common.snip_utils').ft_func,
enable_autosnippets = true,
sources = {
{
name = "lazydev",
group_index = 0
}
}
})
-- Keybinding --
vim.keymap.set(
K.SNIPPETS.ACCEPT_OR_JUMP.mode,
K.SNIPPETS.ACCEPT_OR_JUMP.shortcut,
function()
if luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
vim.api.nvim_input('<C-V><Tab>') --Jump Placeholder of Snippets
end
end,
{ silent = true }
)
vim.keymap.set(
K.SNIPPETS.JUMP_BACKWARDS.mode,
K.SNIPPETS.JUMP_BACKWARDS.shortcut,
function() luasnip.jump(-1) end,
{ silent = true }
)
local doc_keybindings = {
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }),
}
-- Setup --
cmp.setup({
snippet = {
expand = function(args) luasnip.lsp_expand(args.body) end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert(doc_keybindings),
sources = cmp.config.sources({
{ name = 'lazydev', group_index = 0 },
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
}, { { name = 'buffer' } }),
})
end,
},
}