diff --git a/README.md b/README.md index 212e8ac..bf6d725 100644 --- a/README.md +++ b/README.md @@ -109,3 +109,21 @@ vim.keymap.set("n", "y", "YankBank", { noremap = true }) - [nvim-neoclip](https://github.com/AckslD/nvim-neoclip.lua) - [yanky.nvim](https://github.com/gbprod/yanky.nvim) +## Telescope Integration + +This plugin includes integration with [Telescope](https://github.com/nvim-telescope/telescope.nvim). To use the Telescope picker for YankBank, follow these steps: + +1. Ensure you have Telescope installed and set up in your Neovim configuration. +2. Load the YankBank extension in Telescope by adding the following line to your Neovim configuration: + +```lua +require('telescope').load_extension('yankbank') +``` + +3. You can now use the YankBank picker with the following command: + +```vim +:Telescope yankbank +``` + +This will open a Telescope picker showing your recent yanks, allowing you to search and select yanks easily. diff --git a/lua/telescope/_extensions/yankbank.lua b/lua/telescope/_extensions/yankbank.lua new file mode 100644 index 0000000..1ab87ec --- /dev/null +++ b/lua/telescope/_extensions/yankbank.lua @@ -0,0 +1,14 @@ +local telescope = require("telescope") +local yankbank = require("yankbank.telescope") + +local has_telescope, telescope = pcall(require, "telescope") + +if not has_telescope then + error("yankbank-nvim requires nvim-telescope/telescope.nvim") +end + +return telescope.register_extension({ + exports = { + yankbank = require("yankbank.telescope").yankbank, + }, +}) diff --git a/lua/yankbank/init.lua b/lua/yankbank/init.lua index b9d16fc..0b38b03 100644 --- a/lua/yankbank/init.lua +++ b/lua/yankbank/init.lua @@ -4,6 +4,7 @@ local M = {} -- local imports local menu = require("yankbank.menu") local clipboard = require("yankbank.clipboard") +local telescope = require("yankbank.telescope") -- initialize yanks tables local yanks = {} @@ -40,6 +41,7 @@ local function show_yank_bank(opts) -- open window and set keybinds local win_id = menu.open_window(bufnr, display_lines) menu.set_keymaps(win_id, bufnr, yanks, reg_types, line_yank_map, opts) + return end -- plugin setup @@ -50,9 +52,39 @@ function M.setup(opts) clipboard.setup_yank_autocmd(yanks, reg_types, opts) -- Create user command - vim.api.nvim_create_user_command("YankBank", function() + vim.api.nvim_create_user_command("YankBank", function(args) show_yank_bank(opts) end, { desc = "Show Recent Yanks" }) + + -- Add a command for Telescope integration + vim.api.nvim_create_user_command("TelescopeYankBank", function() + require("yankbank.telescope").yankbank(yanks, reg_types) + end, { desc = "Show Recent Yanks with Telescope" }) end +-- Expose yanks and reg_types for external access +M.yanks = yanks +M.reg_types = reg_types + +function M.setup(opts) + opts = opts or default_opts + + -- create clipboard autocmds + clipboard.setup_yank_autocmd(yanks, reg_types, opts) + + -- Create user command + vim.api.nvim_create_user_command("YankBank", function(args) + show_yank_bank(opts) + end, { desc = "Show Recent Yanks" }) + + -- Add a command for Telescope integration + vim.api.nvim_create_user_command("TelescopeYankBank", function() + require("yankbank.telescope").yankbank(yanks, reg_types) + end, { desc = "Show Recent Yanks with Telescope" }) +end + +-- Expose yanks and reg_types for external access +M.yanks = yanks +M.reg_types = reg_types + return M diff --git a/lua/yankbank/telescope.lua b/lua/yankbank/telescope.lua new file mode 100644 index 0000000..fccdf3f --- /dev/null +++ b/lua/yankbank/telescope.lua @@ -0,0 +1,46 @@ +local pickers = require("telescope.pickers") +local finders = require("telescope.finders") +local sorters = require("telescope.sorters") +local actions = require("telescope.actions") +local action_state = require("telescope.actions.state") + +local M = {} + +function M.yankbank(opts) + local plug = require("yankbank") + + local yanks = plug.yanks + local reg_types = plug.reg_types + + pickers + .new({}, { + prompt_title = "YankBank", + finder = finders.new_table({ + results = yanks, + entry_maker = function(entry) + return { + value = entry, + display = entry:gsub("\n", "↵"), + ordinal = entry, + } + end, + }), + sorter = sorters.get_generic_fuzzy_sorter(), + attach_mappings = function(prompt_bufnr, map) + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + if selection then + local text = selection.value + local index = vim.tbl_index(yanks, text) + local reg_type = reg_types[index] + require("yankbank.helpers").smart_paste(text, reg_type) + end + end) + return true + end, + }) + :find() +end + +return M