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 11 ---@param opts table 12 12 function M.add_yank(yanks, reg_types, text, reg_type, opts) 13 13 -- avoid adding empty strings 14 - -- TODO: could block adding single characters here 15 - if text == "" or text == " " or text == "\n" then 14 + if text == "" and text == " " and text == "\n" then 16 15 return 17 16 end 18 17 19 - -- do not update with duplicate values 20 - for _, entry in ipairs(yanks) do 18 + -- check for duplicate values already inserted 19 + for i, entry in ipairs(yanks) do 21 20 if entry == text then 22 - return 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 23 25 end 24 26 end 25 27 26 28 -- add entry to bank 27 29 table.insert(yanks, 1, text) 28 30 table.insert(reg_types, 1, reg_type) 31 + 32 + -- trim table size if necessary 29 33 if #yanks > opts.max_entries then 30 34 table.remove(yanks) 31 35 table.remove(reg_types) ··· 57 61 if #yank_text <= 1 then 58 62 return 59 63 end 64 + 60 65 M.add_yank(yanks, reg_types, yank_text, reg_type, opts) 61 66 end 62 67 end,
+4
lua/yankbank/persistence.lua
··· 23 23 return {}, {} 24 24 elseif opts.persist_type == "sqlite" then 25 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 + 26 30 return persistence:get_bank() 27 31 else 28 32 return {}, {}
+28 -8
lua/yankbank/persistence/sql.lua
··· 25 25 function data:insert_yank(yank_text, reg_type) 26 26 -- attempt to remove entry if count > 0 (to move potential duplicate) 27 27 if self:count() > 0 then 28 - self:remove({ yank_text = yank_text }) 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) 29 34 end 30 35 31 - -- insert entry 32 - self:insert({ 33 - yank_text = yank_text, 34 - reg_type = reg_type, 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) 36 43 37 44 -- attempt to trim database size 38 45 self:trim_size() ··· 42 49 function data:trim_size() 43 50 if self:count() > max_entries then 44 51 -- remove the oldest entry 45 - self:remove({ yank_text = self:get()[1].yank_text }) 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 46 67 end 47 68 end 48 69 ··· 61 82 end 62 83 63 84 -- FIX: correctly handle multiple sessions open at once 64 - -- - fetch database state each time YankBank command is called? 65 85 66 86 --- set up database persistence 67 87 ---@param opts table