so.. telesnip works.. But only with the copy buffer, not inserting it directly

This commit is contained in:
pika 2024-08-19 23:30:40 +02:00
parent cf3458042f
commit df554754e6
3 changed files with 112 additions and 18 deletions

View file

@ -3,7 +3,7 @@ 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) -- Get all language directories automatically
local languages = vim.fn.readdir(base_path)
for _, lang in ipairs(languages) do
local path = base_path .. lang
@ -51,28 +51,17 @@ M.snippet_picker = function()
attach_mappings = function(_, map)
map("i", "<CR>", function(prompt_bufnr)
local selection = require("telescope.actions.state").get_selected_entry()
local snippet_content = vim.fn.readfile(selection.path)
local current_buf = vim.api.nvim_get_current_buf()
local win = vim.api.nvim_get_current_win()
local cursor = vim.api.nvim_win_get_cursor(win)
local row, col = cursor[1] - 1, cursor[2]
local snippet_content = table.concat(vim.fn.readfile(selection.path), "\n")
-- Insert snippet content at the current cursor position
vim.api.nvim_buf_set_text(current_buf, row, col, row, col, snippet_content)
-- Move cursor to the end of the inserted snippet
local new_row = row + #snippet_content
local new_col = #snippet_content[#snippet_content]
vim.api.nvim_win_set_cursor(win, { new_row, new_col })
-- Copy to clipboard
vim.fn.setreg("+", snippet_content)
vim.fn.setreg('"', snippet_content)
-- Close Telescope prompt
require("telescope.actions").close(prompt_bufnr)
-- Schedule entering insert mode
vim.schedule(function()
vim.cmd("stopinsert")
vim.cmd("startinsert!")
end)
-- Notify user
print("Snippet copied to clipboard. Press p to paste.")
end)
return true
end,