Add snippet mappings & being porting snippets
This commit is contained in:
parent
781cb49cb2
commit
0c7636dfa5
@ -133,5 +133,24 @@ return {
|
||||
vim.keymap.set('i', '<C-h>', vim.lsp.buf.signature_help, opts)
|
||||
end
|
||||
})
|
||||
|
||||
-- Snippet mappings
|
||||
local luasnip = require('luasnip')
|
||||
luasnip.setup({})
|
||||
vim.keymap.set({ 'i', 's' }, '<C-j>', function()
|
||||
if luasnip.expand_or_jumpable() then
|
||||
luasnip.expand_or_jump()
|
||||
end
|
||||
end, { silent = true })
|
||||
vim.keymap.set({ 'i', 's' }, '<C-K>', 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
|
||||
}
|
||||
|
76
snippets/all.lua
Normal file
76
snippets/all.lua
Normal file
@ -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
|
7
snippets/all.snippets
Normal file
7
snippets/all.snippets
Normal file
@ -0,0 +1,7 @@
|
||||
# Global SnipMate snippets
|
||||
|
||||
snippet shrug
|
||||
¯\_(ツ)_/¯
|
||||
|
||||
snippet tableflip
|
||||
(╯°▪°)╯︵┻━┻
|
2
snippets/cpp.snippets
Normal file
2
snippets/cpp.snippets
Normal file
@ -0,0 +1,2 @@
|
||||
snippet array
|
||||
std::array<${1:T}, ${2:N}> ${3:array};${4}
|
Loading…
x
Reference in New Issue
Block a user