Neovim plugin improving access to clipboard history (mirror)

refactor: lazier loading of modules

ptdewey 3701b59a cc94fbea

+52 -51
+3 -7
lua/yankbank/clipboard.lua
··· 1 1 local M = {} 2 2 3 - -- import persistence module 4 - local persistence = require("yankbank.persistence") 5 - local utils = require("yankbank.utils") 6 - 7 3 --- Function to add yanked text to table 8 4 ---@param text string 9 5 ---@param reg_type string ··· 39 35 40 36 -- trim table size if necessary 41 37 if #YB_YANKS > YB_OPTS.max_entries then 42 - local i = utils.last_zero_entry(YB_PINS) 38 + local i = require("yankbank.utils").last_zero_entry(YB_PINS) 43 39 44 40 if not i or i == 1 then 45 41 -- WARN: undefined behavior ··· 55 51 end 56 52 57 53 -- add entry to persistent store 58 - persistence.add_entry(text, reg_type, pin) 54 + require("yankbank.persistence").add_entry(text, reg_type, pin) 59 55 end 60 56 61 57 --- autocommand to listen for yank events ··· 65 61 -- get register information 66 62 local rn = vim.v.event.regname 67 63 68 - -- check changes wwere made to default register 64 + -- check changes were made to default register 69 65 if rn == "" or rn == "+" then 70 66 local reg_type = vim.fn.getregtype(rn) 71 67 local yank_text = vim.fn.getreg(rn)
+48 -44
lua/yankbank/init.lua
··· 1 1 local M = {} 2 2 3 - -- define global variables 4 - YB_YANKS = {} 5 - YB_REG_TYPES = {} 6 - YB_PINS = {} 7 - YB_OPTS = {} 3 + -- plugin setup 4 + ---@param opts? table 5 + function M.setup(opts) 6 + -- define global variables 7 + YB_YANKS = {} 8 + YB_REG_TYPES = {} 9 + YB_PINS = {} 10 + YB_OPTS = {} 8 11 9 - -- local imports 10 - local menu = require("yankbank.menu") 11 - local clipboard = require("yankbank.clipboard") 12 - local persistence = require("yankbank.persistence") 13 - local helpers = require("yankbank.helpers") 12 + -- local imports 13 + local clipboard = require("yankbank.clipboard") 14 + local persistence = require("yankbank.persistence") 14 15 15 - -- default plugin options 16 - local default_opts = { 17 - max_entries = 10, 18 - sep = "-----", 19 - focus_gain_poll = false, 20 - num_behavior = "prefix", 21 - registers = { 22 - yank_register = "+", 23 - }, 24 - keymaps = {}, 25 - persist_type = nil, 26 - db_path = nil, 27 - bind_indices = nil, 28 - } 16 + -- default plugin options 17 + local default_opts = { 18 + max_entries = 10, 19 + sep = "-----", 20 + focus_gain_poll = false, 21 + num_behavior = "prefix", 22 + registers = { 23 + yank_register = "+", 24 + }, 25 + keymaps = {}, 26 + persist_type = nil, 27 + db_path = nil, 28 + bind_indices = nil, 29 + } 29 30 30 - --- wrapper function for main plugin functionality 31 - local function show_yank_bank() 32 - YB_YANKS = persistence.get_yanks() or YB_YANKS 31 + --- wrapper function for main plugin functionality 32 + local function show_yank_bank() 33 + local menu = require("yankbank.menu") 33 34 34 - -- initialize buffer and populate bank 35 - local buf_data = menu.create_and_fill_buffer() 36 - if not buf_data then 37 - return 38 - end 35 + -- set up menu keybinds from defaults and YB_OPTS.keymaps 36 + menu.setup() 39 37 40 - -- open popup window 41 - buf_data.win_id = menu.open_window(buf_data) 38 + YB_YANKS = persistence.get_yanks() or YB_YANKS 42 39 43 - -- set popup keybinds 44 - menu.set_keymaps(buf_data) 45 - end 40 + -- initialize buffer and populate bank 41 + local buf_data = menu.create_and_fill_buffer() 42 + if not buf_data then 43 + return 44 + end 46 45 47 - -- plugin setup 48 - ---@param opts? table 49 - function M.setup(opts) 46 + -- open popup window 47 + buf_data.win_id = menu.open_window(buf_data) 48 + 49 + -- set popup keybinds 50 + menu.set_keymaps(buf_data) 51 + end 52 + 50 53 -- merge opts with default options table 51 54 YB_OPTS = vim.tbl_deep_extend("keep", opts or {}, default_opts) 52 - 53 - -- set up menu keybinds from defafaults and YB_OPTS.keymaps 54 - menu.setup() 55 55 56 56 -- enable persistence based on opts (needs to be called before autocmd setup) 57 57 YB_YANKS, YB_REG_TYPES, YB_PINS = persistence.setup() ··· 68 68 if YB_OPTS.bind_indices then 69 69 for i = 1, YB_OPTS.max_entries do 70 70 vim.keymap.set("n", YB_OPTS.bind_indices .. i, function() 71 - helpers.smart_paste(YB_YANKS[i], YB_REG_TYPES[i], true) 71 + require("yankbank.helpers").smart_paste( 72 + YB_YANKS[i], 73 + YB_REG_TYPES[i], 74 + true 75 + ) 72 76 end, { 73 77 noremap = true, 74 78 silent = true,
+1
selene.toml
··· 1 + std = "vim"