Expand snippet on tabs. Add C++ snippets.

main
Joshua Potter 2024-01-05 14:28:36 -07:00
parent 88f409c2ce
commit 3e2541d396
4 changed files with 74 additions and 3 deletions

View File

@ -27,6 +27,11 @@ in
}, },
}) })
''; '';
nvim-snippets = ''
require('luasnip').add_snippets('c', require('cxx.snippets'))
require('luasnip').add_snippets('cpp', require('cxx.snippets'))
'';
}; };
xdg.configFile."nvim/after/ftplugin/c.lua".text = '' xdg.configFile."nvim/after/ftplugin/c.lua".text = ''

View File

@ -0,0 +1,57 @@
local fmt = require('luasnip.extras.fmt').fmt
local ls = require('luasnip')
local ki = require('luasnip.nodes.key_indexer')
local ul = require('utils.luasnip')
local c = ls.choice_node
local d = ls.dynamic_node
local i = ls.insert_node
local k = ki.new_key
local r = ls.restore_node
local s = ls.snippet
local sn = ls.snippet_node
local t = ls.text_node
return {
s(
{ name = 'for', trig = 'for' },
fmt([[
for (<>; <>) {
<>
}]],
{
c(1, {
sn(nil, {
i(1, 'int'),
t(' '),
i(2, 'i'),
}),
sn(nil, {
i(1, 'char *'),
i(2, 'c'),
}),
}),
d(2, function(_, parent)
local index = ul.choice_index(parent.nodes[2])
return sn(
nil,
r(1, string.format('for-%d', index), ({
sn(nil, {
i(1, 'i < N'),
t('; '),
i(2, '++i'),
}),
sn(nil, {
i(1, 'c'),
t('; '),
i(2, '++c'),
}),
})[index])
)
end, { 1 }),
ul.visual_isn(3),
},
{ delimiters = '<>' }
)
),
}

View File

@ -38,11 +38,19 @@ function M.setup()
}, },
mapping = { mapping = {
['<tab>'] = cmp.mapping(function(fallback) ['<tab>'] = cmp.mapping(function(fallback)
if cmp.get_active_entry() then cmp.confirm() else fallback() end if cmp.visible() then
cmp.confirm({ select = true })
else
fallback()
end
end, { 'i', 's' }), end, { 'i', 's' }),
['<c-l>'] = cmp.mapping(function(fallback) ['<c-l>'] = cmp.mapping(function(fallback)
if cmp.visible() then cmp.abort() else fallback() end if cmp.visible() then
cmp.abort()
else
fallback()
end
end, { 'i', 's' }), end, { 'i', 's' }),
['<c-n>'] = cmp.mapping(function(fallback) ['<c-n>'] = cmp.mapping(function(fallback)

View File

@ -40,11 +40,12 @@ function M.setup()
}, },
[types.insertNode] = { [types.insertNode] = {
passive = { passive = {
hl_group = 'DiagnosticHint', hl_group = 'DiagnosticVirtualTextWarn',
}, },
}, },
[types.choiceNode] = { [types.choiceNode] = {
active = { active = {
hl_group = 'DiagnosticVirtualTextHint',
virt_text = { { '', 'DiagnosticHint' } }, virt_text = { { '', 'DiagnosticHint' } },
}, },
}, },