From 0c7636dfa52b0046ccd7f678bc59f389e56eb11e Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Tue, 5 Mar 2024 23:36:41 +0000 Subject: [PATCH] Add snippet mappings & being porting snippets --- lua/plugins/completions.lua | 19 ++++++++++ snippets/all.lua | 76 +++++++++++++++++++++++++++++++++++++ snippets/all.snippets | 7 ++++ snippets/cpp.snippets | 2 + 4 files changed, 104 insertions(+) create mode 100644 snippets/all.lua create mode 100644 snippets/all.snippets create mode 100644 snippets/cpp.snippets diff --git a/lua/plugins/completions.lua b/lua/plugins/completions.lua index f27b3fe..4312fc6 100644 --- a/lua/plugins/completions.lua +++ b/lua/plugins/completions.lua @@ -133,5 +133,24 @@ return { vim.keymap.set('i', '', vim.lsp.buf.signature_help, opts) end }) + + -- Snippet mappings + local luasnip = require('luasnip') + luasnip.setup({}) + vim.keymap.set({ 'i', 's' }, '', function() + if luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + end + end, { silent = true }) + vim.keymap.set({ 'i', 's' }, '', function() + if luasnip.jumpable(-1) then + luasnip.jump(-1) + end + end, { silent = true }) + + -- Load snippets + local opts = { paths = vim.fn.stdpath('config') .. '/snippets' } + require('luasnip.loaders.from_snipmate').lazy_load(opts) + require('luasnip.loaders.from_lua').lazy_load(opts) end } diff --git a/snippets/all.lua b/snippets/all.lua new file mode 100644 index 0000000..2191629 --- /dev/null +++ b/snippets/all.lua @@ -0,0 +1,76 @@ +# Global LuaSnip snippets + +local luasnip = require('luasnip') +local snip = luasnip.snippet +-- local sn = luasnip.snippet_node +local text = luasnip.text_node +-- local isn = luasnip.indent_snippet_node +-- local t = luasnip.text_node +local insert = luasnip.insert_node +local func = luasnip.function_node +-- local c = luasnip.choice_node +-- local d = luasnip.dynamic_node +-- local r = luasnip.restore_node +-- local events = require("luasnip.util.events") +-- local ai = require("luasnip.nodes.absolute_indexer") +-- local extras = require("luasnip.extras") +-- local l = extras.lambda +-- local rep = extras.rep +-- local p = extras.partial +-- local m = extras.match +-- local n = extras.nonempty +-- local dl = extras.dynamic_lambda +-- local fmt = require("luasnip.extras.fmt").fmt +-- local fmta = require("luasnip.extras.fmt").fmta +-- local conds = require("luasnip.extras.expand_conditions") +-- local postfix = require("luasnip.extras.postfix").postfix +-- local types = require("luasnip.util.types") +-- local parse = require("luasnip.util.parser").parse_snippet +-- local ms = luasnip.multi_snippet +-- local k = require("luasnip.nodes.key_indexer").new_key + +local snippets = {} + +local function comment_prefix() + -- Get value of commentstring buffer option + local split = string.find(vim.bo.commentstring, '%s', 1, true) + if not split then + return '' + end + -- Slice commentstring prefix + local prefix = string.sub(vim.bo.commentstring, 1, split - 1) + local line = vim.api.nvim_get_current_line() + local cursor = vim.api.nvim_win_get_cursor(vim.api.nvim_get_current_win()) + -- Check if the current line already has prefix before the cursor + if string.find(string.sub(line, 1, cursor[2] + 1), prefix, 1, true) then + return '' + end + return prefix .. ' ' +end + +local function comment_suffix() + -- Get value of commentstring buffer option + local split = string.find(vim.bo.commentstring, '%s', 1, true) + if not split then + return '' + end + -- Slice commentstring suffix + local suffix = string.sub(vim.bo.commentstring, split + 2, -1) + if string.len(suffix) == 0 then + return '' + end + return ' ' .. suffix +end + +for _, name in ipairs({ 'fixme', 'todo', 'hack', 'warn', 'note' }) do + table.insert(snippets, + snip(name, { + func(comment_prefix), + text(string.upper(name) .. ': '), + insert(0), + func(comment_suffix), + }) + ) +end + +return snippets diff --git a/snippets/all.snippets b/snippets/all.snippets new file mode 100644 index 0000000..7fa16d3 --- /dev/null +++ b/snippets/all.snippets @@ -0,0 +1,7 @@ +# Global SnipMate snippets + +snippet shrug + ¯\_(ツ)_/¯ + +snippet tableflip + (╯°▪°)╯︵┻━┻ diff --git a/snippets/cpp.snippets b/snippets/cpp.snippets new file mode 100644 index 0000000..8c61765 --- /dev/null +++ b/snippets/cpp.snippets @@ -0,0 +1,2 @@ +snippet array + std::array<${1:T}, ${2:N}> ${3:array};${4}