From 423721e92d20219597bc4403a9de1ad4cb29bb71 Mon Sep 17 00:00:00 2001 From: iGor milhit Date: Fri, 9 Jun 2023 13:00:41 +0200 Subject: [PATCH] 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 (ss). Co-Authored-by: iGor milhit --- init.lua | 9 +++- my_snippets/all.lua | 14 ++++++ {snippets => my_snippets}/boilerplate.html | 0 my_snippets/markdown.lua | 56 ++++++++++++++++++++++ {snippets => my_snippets}/template.md | 0 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 my_snippets/all.lua rename {snippets => my_snippets}/boilerplate.html (100%) create mode 100644 my_snippets/markdown.lua rename {snippets => my_snippets}/template.md (100%) diff --git a/init.lua b/init.lua index e43be48..7ec03aa 100644 --- a/init.lua +++ b/init.lua @@ -195,6 +195,8 @@ require('lazy').setup({ end, }, + { 'benfowler/telescope-luasnip.nvim' }, + { -- Highlight, edit, and navigate code 'nvim-treesitter/nvim-treesitter', @@ -320,6 +322,7 @@ require('telescope').setup { -- Enable telescope fzf native, if installed pcall(require('telescope').load_extension, 'fzf') +pcall(require('telescope').load_extension, 'luasnip') -- See `:help telescope.builtin` vim.keymap.set('n', '?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' }) @@ -339,6 +342,7 @@ vim.keymap.set('n', 'sw', require('telescope.builtin').grep_string, { de vim.keymap.set('n', 'sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' }) vim.keymap.set('n', 'sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' }) vim.keymap.set('n', 'sb', require('telescope.builtin').buffers, { desc = '[S]earch [B]uffers' }) +vim.keymap.set('n', 'ss', require'telescope'.extensions.luasnip.luasnip , { desc = '[S]earch [S]nippets' }) -- [[ Configure Treesitter ]] -- See `:help nvim-treesitter` @@ -519,9 +523,12 @@ luasnip.config.setup { snippets = { 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 { snippet = { expand = function(args) diff --git a/my_snippets/all.lua b/my_snippets/all.lua new file mode 100644 index 0000000..0a11b6a --- /dev/null +++ b/my_snippets/all.lua @@ -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.") } + ) +} diff --git a/snippets/boilerplate.html b/my_snippets/boilerplate.html similarity index 100% rename from snippets/boilerplate.html rename to my_snippets/boilerplate.html diff --git a/my_snippets/markdown.lua b/my_snippets/markdown.lua new file mode 100644 index 0000000..b3d7972 --- /dev/null +++ b/my_snippets/markdown.lua @@ -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), + } + ) +} diff --git a/snippets/template.md b/my_snippets/template.md similarity index 100% rename from snippets/template.md rename to my_snippets/template.md