From 3e2541d396d25c37d6fc4281e6d40fd853250e85 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 5 Jan 2024 14:28:36 -0700 Subject: [PATCH] Expand snippet on tabs. Add C++ snippets. --- users/jrpotter/lang/cxx.nix | 5 ++ users/jrpotter/neovim/config/cxx/snippets.lua | 57 +++++++++++++++++++ users/jrpotter/neovim/config/utils/cmp.lua | 12 +++- .../jrpotter/neovim/config/utils/luasnip.lua | 3 +- 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 users/jrpotter/neovim/config/cxx/snippets.lua diff --git a/users/jrpotter/lang/cxx.nix b/users/jrpotter/lang/cxx.nix index 445de89..0ec0ea0 100644 --- a/users/jrpotter/lang/cxx.nix +++ b/users/jrpotter/lang/cxx.nix @@ -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 = '' diff --git a/users/jrpotter/neovim/config/cxx/snippets.lua b/users/jrpotter/neovim/config/cxx/snippets.lua new file mode 100644 index 0000000..80173de --- /dev/null +++ b/users/jrpotter/neovim/config/cxx/snippets.lua @@ -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 = '<>' } + ) + ), +} diff --git a/users/jrpotter/neovim/config/utils/cmp.lua b/users/jrpotter/neovim/config/utils/cmp.lua index 295083b..06fa7ae 100644 --- a/users/jrpotter/neovim/config/utils/cmp.lua +++ b/users/jrpotter/neovim/config/utils/cmp.lua @@ -38,11 +38,19 @@ function M.setup() }, mapping = { [''] = 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' }), [''] = 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' }), [''] = cmp.mapping(function(fallback) diff --git a/users/jrpotter/neovim/config/utils/luasnip.lua b/users/jrpotter/neovim/config/utils/luasnip.lua index 49b77b1..f54e0cd 100644 --- a/users/jrpotter/neovim/config/utils/luasnip.lua +++ b/users/jrpotter/neovim/config/utils/luasnip.lua @@ -40,11 +40,12 @@ function M.setup() }, [types.insertNode] = { passive = { - hl_group = 'DiagnosticHint', + hl_group = 'DiagnosticVirtualTextWarn', }, }, [types.choiceNode] = { active = { + hl_group = 'DiagnosticVirtualTextHint', virt_text = { { '⧨', 'DiagnosticHint' } }, }, },