Neovim plugin improving access to clipboard history (mirror)

fix: #17 error on yanking double quoted text

ptdewey 4e12df3a d6340a53

+42 -13
+10 -5
lua/yankbank/clipboard.lua
··· 11 ---@param opts table 12 function M.add_yank(yanks, reg_types, text, reg_type, opts) 13 -- avoid adding empty strings 14 - -- TODO: could block adding single characters here 15 - if text == "" or text == " " or text == "\n" then 16 return 17 end 18 19 - -- do not update with duplicate values 20 - for _, entry in ipairs(yanks) do 21 if entry == text then 22 - return 23 end 24 end 25 26 -- add entry to bank 27 table.insert(yanks, 1, text) 28 table.insert(reg_types, 1, reg_type) 29 if #yanks > opts.max_entries then 30 table.remove(yanks) 31 table.remove(reg_types) ··· 57 if #yank_text <= 1 then 58 return 59 end 60 M.add_yank(yanks, reg_types, yank_text, reg_type, opts) 61 end 62 end,
··· 11 ---@param opts table 12 function M.add_yank(yanks, reg_types, text, reg_type, opts) 13 -- avoid adding empty strings 14 + if text == "" and text == " " and text == "\n" then 15 return 16 end 17 18 + -- check for duplicate values already inserted 19 + for i, entry in ipairs(yanks) do 20 if entry == text then 21 + -- remove matched entry so it can be inserted at 1st position 22 + table.remove(yanks, i) 23 + table.remove(reg_types, i) 24 + break 25 end 26 end 27 28 -- add entry to bank 29 table.insert(yanks, 1, text) 30 table.insert(reg_types, 1, reg_type) 31 + 32 + -- trim table size if necessary 33 if #yanks > opts.max_entries then 34 table.remove(yanks) 35 table.remove(reg_types) ··· 61 if #yank_text <= 1 then 62 return 63 end 64 + 65 M.add_yank(yanks, reg_types, yank_text, reg_type, opts) 66 end 67 end,
+4
lua/yankbank/persistence.lua
··· 23 return {}, {} 24 elseif opts.persist_type == "sqlite" then 25 persistence = require("yankbank.persistence.sql").setup(opts) 26 return persistence:get_bank() 27 else 28 return {}, {}
··· 23 return {}, {} 24 elseif opts.persist_type == "sqlite" then 25 persistence = require("yankbank.persistence.sql").setup(opts) 26 + vim.api.nvim_create_user_command("YankView", function() 27 + print(vim.inspect(persistence:get())) 28 + end, {}) 29 + 30 return persistence:get_bank() 31 else 32 return {}, {}
+28 -8
lua/yankbank/persistence/sql.lua
··· 25 function data:insert_yank(yank_text, reg_type) 26 -- attempt to remove entry if count > 0 (to move potential duplicate) 27 if self:count() > 0 then 28 - self:remove({ yank_text = yank_text }) 29 end 30 31 - -- insert entry 32 - self:insert({ 33 - yank_text = yank_text, 34 - reg_type = reg_type, 35 - }) 36 37 -- attempt to trim database size 38 self:trim_size() ··· 42 function data:trim_size() 43 if self:count() > max_entries then 44 -- remove the oldest entry 45 - self:remove({ yank_text = self:get()[1].yank_text }) 46 end 47 end 48 ··· 61 end 62 63 -- FIX: correctly handle multiple sessions open at once 64 - -- - fetch database state each time YankBank command is called? 65 66 --- set up database persistence 67 ---@param opts table
··· 25 function data:insert_yank(yank_text, reg_type) 26 -- attempt to remove entry if count > 0 (to move potential duplicate) 27 if self:count() > 0 then 28 + db:with_open(function() 29 + db:eval( 30 + "DELETE FROM bank WHERE yank_text = :yank_text", 31 + { yank_text = yank_text } 32 + ) 33 + end) 34 end 35 36 + -- insert entry using the eval method with parameterized query to avoid error on 'data:insert()' 37 + db:with_open(function() 38 + db:eval( 39 + "INSERT INTO bank (yank_text, reg_type) VALUES (:yank_text, :reg_type)", 40 + { yank_text = yank_text, reg_type = reg_type } 41 + ) 42 + end) 43 44 -- attempt to trim database size 45 self:trim_size() ··· 49 function data:trim_size() 50 if self:count() > max_entries then 51 -- remove the oldest entry 52 + local oldest_entry = db:with_open(function() 53 + return db:select( 54 + "bank", 55 + { order_by = { asc = "rowid" }, limit = { 1 } } 56 + )[1] 57 + end) 58 + 59 + if oldest_entry then 60 + db:with_open(function() 61 + db:eval( 62 + "DELETE FROM bank WHERE yank_text = :yank_text", 63 + { yank_text = oldest_entry.yank_text } 64 + ) 65 + end) 66 + end 67 end 68 end 69 ··· 82 end 83 84 -- FIX: correctly handle multiple sessions open at once 85 86 --- set up database persistence 87 ---@param opts table