nvim/lua/telesnip/init.lua

72 lines
1.9 KiB
Lua

local M = {}
local function get_snippets()
local snippets = {}
local base_path = vim.fn.stdpath("config") .. "/lua/telesnip/snippets/"
local languages = vim.fn.readdir(base_path)
for _, lang in ipairs(languages) do
local path = base_path .. lang
local files = vim.fn.glob(path .. "/*", false, true)
for _, file in ipairs(files) do
local snippet_name = vim.fn.fnamemodify(file, ":t")
table.insert(snippets, {
name = snippet_name,
path = file,
language = lang,
})
end
end
return snippets
end
M.snippet_picker = function()
local snippets = get_snippets()
local opts = require("telescope.themes").get_dropdown({})
require("telescope.pickers")
.new(opts, {
prompt_title = "Snippets",
finder = require("telescope.finders").new_table({
results = snippets,
entry_maker = function(entry)
return {
value = entry,
display = entry.name,
ordinal = entry.name,
path = entry.path,
}
end,
}),
sorter = require("telescope.config").values.generic_sorter(opts),
previewer = require("telescope.previewers").new_buffer_previewer({
define_preview = function(self, entry)
local lines = vim.fn.readfile(entry.path)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
local ext = vim.fn.fnamemodify(entry.path, ":e")
vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", ext)
end,
}),
attach_mappings = function(_, map)
map("i", "<CR>", function(prompt_bufnr)
local selection = require("telescope.actions.state").get_selected_entry()
local snippet_content = table.concat(vim.fn.readfile(selection.path), "\n")
-- Copy to clipboard
vim.fn.setreg("+", snippet_content)
vim.fn.setreg('"', snippet_content)
-- Close Telescope prompt
require("telescope.actions").close(prompt_bufnr)
-- Notify user
print("Snippet copied to clipboard. Press p to paste.")
end)
return true
end,
})
:find()
end
return M