snippets: load my own snippets

- Renames the snippets folder to my_snippets.
- Loads my snippets.
- Creates two snippet example available for all filetypes.
- Creates three snippets for markdown filetype.
- Adds the telescope luasnip plugin.
- Sets a keymaping to fuzzy search all available snippets (<leader>ss).

Co-Authored-by: iGor milhit <igor.milhit@unige.ch>
iGor milhit 2023-06-09 13:00:41 +02:00
parent 402e5a9308
commit c9af70e82e
Signed by: igor
GPG Key ID: 692D97C3D0228A99
5 changed files with 78 additions and 1 deletions

View File

@ -195,6 +195,8 @@ require('lazy').setup({
end, end,
}, },
{ 'benfowler/telescope-luasnip.nvim' },
{ {
-- Highlight, edit, and navigate code -- Highlight, edit, and navigate code
'nvim-treesitter/nvim-treesitter', 'nvim-treesitter/nvim-treesitter',
@ -320,6 +322,7 @@ require('telescope').setup {
-- Enable telescope fzf native, if installed -- Enable telescope fzf native, if installed
pcall(require('telescope').load_extension, 'fzf') pcall(require('telescope').load_extension, 'fzf')
pcall(require('telescope').load_extension, 'luasnip')
-- See `:help telescope.builtin` -- See `:help telescope.builtin`
vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' }) vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
@ -339,6 +342,7 @@ vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { de
vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' }) vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' }) vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
vim.keymap.set('n', '<leader>sb', require('telescope.builtin').buffers, { desc = '[S]earch [B]uffers' }) vim.keymap.set('n', '<leader>sb', require('telescope.builtin').buffers, { desc = '[S]earch [B]uffers' })
vim.keymap.set('n', '<leader>ss', require'telescope'.extensions.luasnip.luasnip , { desc = '[S]earch [S]nippets' })
-- [[ Configure Treesitter ]] -- [[ Configure Treesitter ]]
-- See `:help nvim-treesitter` -- See `:help nvim-treesitter`
@ -519,9 +523,12 @@ luasnip.config.setup {
snippets = { snippets = {
markdown = {}, markdown = {},
}, },
luasnip.filetype_extend("pandoc", {"markdown"}) luasnip.filetype_extend("pandoc", {"markdown"}),
} }
-- Load snippets from ~/.config/nvim/LuaSnip/
require("luasnip.loaders.from_lua").load({paths = "~/.config/nvim/my_snippets/"})
cmp.setup { cmp.setup {
snippet = { snippet = {
expand = function(args) expand = function(args)

View File

@ -0,0 +1,14 @@
return {
-- A snippet that expands the trigger "hi" into the string "Hello, world!".
require("luasnip").snippet(
{ trig = "hi" },
{ t("Hello, world!") }
),
-- To return multiple snippets, use one `return` statement per snippet file
-- and return a table of Lua snippets.
require("luasnip").snippet(
{ trig = "foo" },
{ t("Another snippet.") }
)
}

View File

@ -0,0 +1,56 @@
local ls = require("luasnip")
local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
-- Function to get the datetime
-- with daylight saving time
-- according to the locale
local get_date = function()
return os.date("%Y-%m-%dT%H:%M:%S%z")
end
-- Function to get an id based on the datetime
local get_id = function()
return os.date("%Y%m%d%H%M%S")
end
return {
s(
{
trig = "datetime",
dscr = "Insert datetime with locale daylight saving"
},
{
f(get_date, {})
}
),
s(
{
trig = "id",
dscr = "Insert an id based on the datetime"
},
{
f(get_id, {})
}
),
s(
{
trig = "yaml",
dscr = "Insert a YAML frontmatter for markdown"
},
{
t { "---", "title: " },
i(1, "title"),
t{ "", "date: " },
f(get_date, {}),
t{ "", "id: " },
f(get_id, {}),
t{ "","tags: [" },
i(2, "tags"),
t{ "]", "---", "" },
i(0),
}
)
}