Skip to content

Instantly share code, notes, and snippets.

@fira42073
Created August 12, 2024 19:04
Show Gist options
  • Save fira42073/c5bc8d4d1f60dc73722acbbf8ab55cb4 to your computer and use it in GitHub Desktop.
Save fira42073/c5bc8d4d1f60dc73722acbbf8ab55cb4 to your computer and use it in GitHub Desktop.
LuaSnip FzfLua integration
local luasnip = require("luasnip")
local fzf_lua = require("fzf-lua")
local function search_snippets()
-- Get available snippets
local snippets = luasnip.available()
-- Flatten the snippets table and prepare entries for fzf-lua
local entries = {}
for category, snippet_list in pairs(snippets) do
if type(snippet_list) == "table" then
for _, snippet in ipairs(snippet_list) do
local description = snippet.description[1] or "" -- Extract the first description if available
local entry = string.format("%s - %s (%s) : %s", snippet.trigger, snippet.name, category, description)
table.insert(entries, entry)
end
end
end
-- Use fzf-lua to search through snippets
fzf_lua.fzf_exec(entries, {
prompt = "Select Snippet> ",
actions = {
["default"] = function(selected)
if #selected > 0 then
-- Extract the trigger from the selected entry
local trigger = selected[1]:match("^(.-)%s+-")
-- Insert the trigger into the current buffer and go into insert mode
vim.api.nvim_put({ trigger }, "c", true, true)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>i", true, true, true), "n", true)
end
end,
},
})
end
vim.keymap.set("n", "<leader>fs", search_snippets, { desc = "FZF search snippets" })
@Felipe-9
Copy link

I became aware of that recently, i tried already but i get errors and it never works, im going with your implementation of that until i figure out how to get a good snippet search.

i already opened an issue there asking for help, but seeing the low activity i wonder if it will be solved

@fira42073
Copy link
Author

@Felipe-9 good luck! I've found fzf-lua to be very robust and it replaced telescope for me.

@Felipe-9
Copy link

Thank you @fira42073 i will try to make it work, i really look forward for seeing description and preview, i really depend on those for my snippets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment