Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telescope integration #16

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,21 @@ vim.keymap.set("n", "<leader>y", "<cmd>YankBank<CR>", { 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.
14 changes: 14 additions & 0 deletions lua/telescope/_extensions/yankbank.lua
Original file line number Diff line number Diff line change
@@ -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,
},
})
34 changes: 33 additions & 1 deletion lua/yankbank/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand All @@ -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
46 changes: 46 additions & 0 deletions lua/yankbank/telescope.lua
Original file line number Diff line number Diff line change
@@ -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