diff --git a/lua/plugins/completions.lua b/lua/plugins/completions.lua index 56751af..dabc6b7 100644 --- a/lua/plugins/completions.lua +++ b/lua/plugins/completions.lua @@ -138,8 +138,8 @@ return { }), sources = { - { name = 'nvim_lsp' }, { name = 'luasnip' }, + { name = 'nvim_lsp' }, { name = 'buffer' }, { name = 'path' }, { name = 'git' }, diff --git a/lua/plugins/snippets.lua b/lua/plugins/snippets.lua index 5cb7ca2..793de87 100644 --- a/lua/plugins/snippets.lua +++ b/lua/plugins/snippets.lua @@ -22,5 +22,15 @@ return { luasnip.jump(-1) end end, { silent = true }) + -- vim.keymap.set('s', '', function() + -- if luasnip.choice_active() then + -- luasnip.change_choice(1) + -- end + -- end, { silent = true }) + -- vim.keymap.set('s', '', function() + -- if luasnip.choice_active() then + -- luasnip.change_choice(-1) + -- end + -- end, { silent = true }) end } diff --git a/snippets/all.lua b/snippets/all.lua index d25440d..99492cc 100644 --- a/snippets/all.lua +++ b/snippets/all.lua @@ -6,7 +6,7 @@ local snip = luasnip.snippet local text = luasnip.text_node -- local isn = luasnip.indent_snippet_node -- local t = luasnip.text_node -local insert = luasnip.insert_node +local ins = luasnip.insert_node local func = luasnip.function_node -- local c = luasnip.choice_node -- local d = luasnip.dynamic_node @@ -67,7 +67,7 @@ for _, name in ipairs({ 'fixme', 'todo', 'hack', 'warn', 'note' }) do snip(name, { func(comment_prefix), text(string.upper(name) .. ': '), - insert(0), + ins(0), func(comment_suffix), }) ) diff --git a/snippets/cpp.lua b/snippets/cpp.lua index f866fd1..6d8053a 100644 --- a/snippets/cpp.lua +++ b/snippets/cpp.lua @@ -1,5 +1,48 @@ -# C++ LuaSnip Snippets +-- C++ LuaSnip Snippets local luasnip = require('luasnip') - luasnip.filetype_extend('cpp', { 'c' }) +local snip = luasnip.snippet +local text = luasnip.text_node +local ins = luasnip.insert_node +local dyn = luasnip.dynamic_node +local node = luasnip.snippet_node +local key = require("luasnip.nodes.key_indexer").new_key + +local snippets = { + + snip('for', { + text('for ('), + ins(1, 'auto'), + text(' '), + ins(2, 'index'), + text(' '), + ins(3, '='), + dyn(4, function(args) + local choice = args[2][1] + if choice == '=' then -- index based for loop + local var = args[1][1] + return node(nil, { + text(' '), + ins(1, '0'), + text('; ' .. var .. ' < '), + ins(2, 'count'), + text('; '), + ins(3, var .. '++'), + }) + elseif choice == ':' then -- range based for loop + return node(nil, { + text(' '), + ins(1, 'container') + }) + end + return node(nil, {}) + end, { 2, 3 }, key('var')), + text({ ') {', '\t' }), + ins(0, ''), + text({ '', '}' }) + }), + +} + +return snippets diff --git a/snippets/cpp.snippets b/snippets/cpp.snippets index 04f0314..7823570 100644 --- a/snippets/cpp.snippets +++ b/snippets/cpp.snippets @@ -2,16 +2,6 @@ extends c -snippet for Range based - for (${1:auto} ${2:value} : ${3:container}) { - $0 - } - -snippet for Index based - for (${1:int} i = ${2:0}; i < ${3:count}; i++) { - $0 - } - snippet namespace namespace ${1} { $0