Neovim plugin improving access to clipboard history (mirror)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: switched yanks and reg_types to global variables for wip api

ptdewey eca193db e93fd66c

+82 -68
+1 -1
README.md
··· 64 64 | focus_gain_poll | boolean | `nil` | 65 65 | registers | table container for register overrides | `{ }` | 66 66 | registers.yank_register | default register to yank from popup to | `"+"` | 67 - | persist_type | string defining persistence type "memory" or "sqlite" | `"memory"` | 67 + | persist_type | string defining persistence type "sqlite" or nil | `nil` | 68 68 69 69 70 70 #### Example Configuration
+30
lua/yankbank/api.lua
··· 1 + local M = {} 2 + 3 + -- TODO: figure out how api should get 'yanks' and 'reg_types' 4 + -- - possibly rewrite these to be global variables 5 + 6 + --- DOC: 7 + ---@param i integer 8 + ---@return table 9 + function M.get_entry(i) 10 + -- TODO: figure out how tables are being populated for the api 11 + return { 12 + yank_text = YANKS[i], 13 + reg_type = REG_TYPES[i], 14 + } 15 + end 16 + 17 + --- DOC: 18 + ---@return table 19 + function M.get_all() 20 + local out = {} 21 + for i, v in ipairs(YANKS) do 22 + table.insert(out, { 23 + yank_text = v, 24 + reg_type = REG_TYPES[i], 25 + }) 26 + end 27 + return out 28 + end 29 + 30 + return M
+14 -18
lua/yankbank/clipboard.lua
··· 3 3 -- import persistence module 4 4 local persistence = require("yankbank.persistence") 5 5 6 - -- Function to add yanked text to table 7 - ---@param yanks table 8 - ---@param reg_types table 6 + --- Function to add yanked text to table 9 7 ---@param text string 10 8 ---@param reg_type string 11 9 ---@param opts table 12 - function M.add_yank(yanks, reg_types, text, reg_type, opts) 10 + function M.add_yank(text, reg_type, opts) 13 11 -- avoid adding empty strings 14 12 if text == "" and text == " " and text == "\n" then 15 13 return 16 14 end 17 15 18 16 -- check for duplicate values already inserted 19 - for i, entry in ipairs(yanks) do 17 + for i, entry in ipairs(YANKS) do 20 18 if entry == text then 21 19 -- remove matched entry so it can be inserted at 1st position 22 - table.remove(yanks, i) 23 - table.remove(reg_types, i) 20 + table.remove(YANKS, i) 21 + table.remove(REG_TYPES, i) 24 22 break 25 23 end 26 24 end 27 25 28 26 -- add entry to bank 29 - table.insert(yanks, 1, text) 30 - table.insert(reg_types, 1, reg_type) 27 + table.insert(YANKS, 1, text) 28 + table.insert(REG_TYPES, 1, reg_type) 31 29 32 30 -- trim table size if necessary 33 - if #yanks > opts.max_entries then 34 - table.remove(yanks) 35 - table.remove(reg_types) 31 + if #YANKS > opts.max_entries then 32 + table.remove(YANKS) 33 + table.remove(REG_TYPES) 36 34 end 37 35 38 36 -- add entry to persistent store 39 37 persistence.add_entry(text, reg_type, opts) 40 38 end 41 39 42 - -- autocommand to listen for yank events 43 - ---@param yanks table 44 - ---@param reg_types table 40 + --- autocommand to listen for yank events 45 41 ---@param opts table 46 - function M.setup_yank_autocmd(yanks, reg_types, opts) 42 + function M.setup_yank_autocmd(opts) 47 43 vim.api.nvim_create_autocmd("TextYankPost", { 48 44 callback = function() 49 45 -- get register information ··· 62 58 return 63 59 end 64 60 65 - M.add_yank(yanks, reg_types, yank_text, reg_type, opts) 61 + M.add_yank(yank_text, reg_type, opts) 66 62 end 67 63 end, 68 64 }) ··· 83 79 return 84 80 end 85 81 86 - M.add_yank(yanks, reg_types, yank_text, reg_type, opts) 82 + M.add_yank(yank_text, reg_type, opts) 87 83 end, 88 84 }) 89 85 end
+8 -10
lua/yankbank/data.lua
··· 1 1 local M = {} 2 2 3 - ---reformat yanks table for popup 4 - ---@param yanks table 5 - ---@param sep string 3 + --- reformat yanks table for popup 4 + ---@param opts table 6 5 ---@return table, table 7 - function M.get_display_lines(yanks, sep) 6 + function M.get_display_lines(opts) 8 7 local display_lines = {} 9 8 local line_yank_map = {} 10 9 local yank_num = 0 11 10 12 11 -- calculate the maximum width needed for the yank numbers 13 - local max_digits = #tostring(#yanks) 12 + local max_digits = #tostring(#YANKS) 14 13 15 14 -- assumes yanks is table of strings 16 - for i, yank in ipairs(yanks) do 15 + for i, yank in ipairs(YANKS) do 17 16 yank_num = yank_num + 1 18 17 19 - -- FIX: there were changes here, might need further changes 20 18 local yank_lines = yank 21 19 if type(yank) == "string" then 22 20 -- remove trailing newlines ··· 51 49 table.insert(line_yank_map, i) 52 50 end 53 51 54 - if i < #yanks then 52 + if i < #YANKS then 55 53 -- Add a visual separator between yanks, aligned with the yank content 56 - if sep ~= "" then 54 + if opts.sep ~= "" then 57 55 table.insert( 58 56 display_lines, 59 - string.rep(" ", max_digits + 2) .. sep 57 + string.rep(" ", max_digits + 2) .. opts.sep 60 58 ) 61 59 end 62 60 table.insert(line_yank_map, false)
+3 -7
lua/yankbank/helpers.lua
··· 1 1 local M = {} 2 2 3 - -- navigate to the next numbered item 3 + --- navigate to the next numbered item 4 4 ---@param steps integer 5 5 function M.next_numbered_item(steps) 6 6 steps = steps or 1 -- Default to 1 if no steps are provided ··· 23 23 vim.api.nvim_win_set_cursor(0, { last_entry, 0 }) 24 24 end 25 25 26 - -- navigate to the previous numbered item 26 + --- navigate to the previous numbered item 27 27 ---@param steps integer 28 28 function M.prev_numbered_item(steps) 29 29 steps = steps or 1 -- Default to 1 if no steps are provided ··· 43 43 vim.api.nvim_win_set_cursor(0, { 1, 0 }) 44 44 end 45 45 46 - -- customized paste function that functions like 'p' 46 + --- customized paste function that functions like 'p' 47 47 ---@param text string|table 48 48 ---@param reg_type string 49 49 function M.smart_paste(text, reg_type) ··· 61 61 lines = text 62 62 end 63 63 64 - -- remove last newline character to replicate base put behavior 65 - -- if lines[#lines] == "" then 66 - -- table.remove(lines) 67 - -- end 68 64 vim.api.nvim_put(lines, reg_type, true, true) 69 65 end 70 66
+15 -9
lua/yankbank/init.lua
··· 5 5 local clipboard = require("yankbank.clipboard") 6 6 local persistence = require("yankbank.persistence") 7 7 8 - -- initialize yanks tables 9 - local yanks = {} 10 - local reg_types = {} 8 + -- initialize tables 9 + YANKS = {} 10 + REG_TYPES = {} 11 11 12 12 -- default plugin options 13 13 local default_opts = { ··· 18 18 registers = { 19 19 yank_register = "+", 20 20 }, 21 - persist_type = "memory", 21 + persist_type = nil, 22 22 keymaps = {}, 23 23 } 24 24 25 25 --- wrapper function for main plugin functionality 26 26 ---@param opts table 27 27 local function show_yank_bank(opts) 28 - yanks = persistence.get_yanks(opts) or yanks 28 + YANKS = persistence.get_yanks(opts) or YANKS 29 29 30 30 -- initialize buffer and populate bank 31 31 local bufnr, display_lines, line_yank_map = 32 - menu.create_and_fill_buffer(yanks, reg_types, opts) 32 + menu.create_and_fill_buffer(opts) 33 33 34 34 -- handle empty bank case 35 35 if not bufnr or not display_lines or not line_yank_map then ··· 38 38 39 39 -- open window and set keybinds 40 40 local win_id = menu.open_window(bufnr, display_lines) 41 - menu.set_keymaps(win_id, bufnr, yanks, reg_types, line_yank_map, opts) 41 + menu.set_keymaps(win_id, bufnr, line_yank_map, opts) 42 42 end 43 43 44 44 -- plugin setup ··· 48 48 opts = vim.tbl_deep_extend("keep", opts or {}, default_opts) 49 49 50 50 -- enable persistence based on opts (needs to be called before autocmd setup) 51 - yanks, reg_types = persistence.setup(opts) 51 + YANKS, REG_TYPES = persistence.setup(opts) 52 52 53 53 -- create clipboard autocmds 54 - clipboard.setup_yank_autocmd(yanks, reg_types, opts) 54 + clipboard.setup_yank_autocmd(opts) 55 55 56 56 -- create user command 57 57 vim.api.nvim_create_user_command("YankBank", function() 58 58 show_yank_bank(opts) 59 59 end, { desc = "Show Recent Yanks" }) 60 + 61 + -- TODO: remove 62 + local api = require("yankbank.api") 63 + vim.api.nvim_create_user_command("YankBankApi", function() 64 + print(vim.inspect(api.get_all())) 65 + end, {}) 60 66 end 61 67 62 68 return M
+9 -23
lua/yankbank/menu.lua
··· 1 1 local M = {} 2 2 3 - -- import clipboard functions 4 - -- local clipboard = require("yankbank.clipboard") 5 3 local data = require("yankbank.data") 6 4 local helpers = require("yankbank.helpers") 7 5 8 6 ---create new buffer and reformat yank table for ui 9 - ---@param yanks table 10 - ---@param reg_types table 11 7 ---@param opts table 12 8 ---@return integer? 13 9 ---@return table? 14 10 ---@return table? 15 - function M.create_and_fill_buffer(yanks, reg_types, opts) 16 - -- check the content of the system clipboard register 17 - -- TODO: this could be replaced with some sort of polling of the + register 18 - -- local text = vim.fn.getreg("+") 19 - -- local most_recent_yank = yanks[1] or "" 20 - -- local reg_type = vim.fn.getregtype("+") 21 - -- clipboard.add_yank(yanks, reg_types, text, reg_type, opts) 22 - 23 - -- stop if yank table is empty 24 - if #yanks == 0 and #reg_types then 11 + function M.create_and_fill_buffer(opts) 12 + -- stop if yanks or register types table is empty 13 + if #YANKS == 0 or #REG_TYPES == 0 then 25 14 print("No yanks to show.") 26 15 return nil, nil, nil 27 16 end ··· 33 22 local current_filetype = vim.bo.filetype 34 23 vim.api.nvim_set_option_value("filetype", current_filetype, { buf = bufnr }) 35 24 36 - -- TODO: need to update yanks from bank file before get_display_lines 37 - local display_lines, line_yank_map = data.get_display_lines(yanks, opts.sep) 25 + local display_lines, line_yank_map = data.get_display_lines(opts) 38 26 39 27 -- replace current buffer contents with updated table 40 28 vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, display_lines) ··· 88 76 return win_id 89 77 end 90 78 91 - ---Set key mappings for the popup window 79 + --- Set key mappings for the popup window 92 80 ---@param win_id integer 93 81 ---@param bufnr integer 94 - ---@param yanks table 95 - ---@param reg_types table 96 82 ---@param line_yank_map table 97 83 ---@param opts table 98 - function M.set_keymaps(win_id, bufnr, yanks, reg_types, line_yank_map, opts) 84 + function M.set_keymaps(win_id, bufnr, line_yank_map, opts) 99 85 -- Key mappings for selection and closing the popup 100 86 local map_opts = { noremap = true, silent = true, buffer = bufnr } 101 87 ··· 177 163 local yankIndex = line_yank_map[cursor] 178 164 if yankIndex then 179 165 -- retrieve the full yank, including all lines 180 - local text = yanks[yankIndex] 166 + local text = YANKS[yankIndex] 181 167 182 168 -- close window upon selection 183 169 vim.api.nvim_win_close(win_id, true) 184 - helpers.smart_paste(text, reg_types[yankIndex]) 170 + helpers.smart_paste(text, REG_TYPES[yankIndex]) 185 171 else 186 172 print("Error: Invalid selection") 187 173 end ··· 192 178 local cursor = vim.api.nvim_win_get_cursor(win_id)[1] 193 179 local yankIndex = line_yank_map[cursor] 194 180 if yankIndex then 195 - local text = yanks[yankIndex] 181 + local text = YANKS[yankIndex] 196 182 -- NOTE: possibly change this to '"' if not using system clipboard 197 183 -- - make this an option 198 184 vim.fn.setreg(opts.registers.yank_register, text)
+2
lua/yankbank/persistence/sql.lua
··· 90 90 91 91 vim.api.nvim_create_user_command("YankBankClearDB", function() 92 92 data:remove() 93 + YANKS = {} 94 + REG_TYPES = {} 93 95 end, {}) 94 96 95 97 if opts.debug == true then