From 59cd263f32c6fa0fd4f30e44e4cbdecf91fec4a3 Mon Sep 17 00:00:00 2001 From: ptdewey Date: Sat, 24 Aug 2024 20:57:14 -0400 Subject: [PATCH] refactor: added db clear option and changed db file path --- .gitignore | 1 + lua/yankbank/init.lua | 12 +++--------- lua/yankbank/persistence.lua | 8 +------- lua/yankbank/persistence/sql.lua | 14 ++++++++++---- 4 files changed, 15 insertions(+), 20 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..619c78d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +yankbank.db diff --git a/lua/yankbank/init.lua b/lua/yankbank/init.lua index aced149..cefc011 100644 --- a/lua/yankbank/init.lua +++ b/lua/yankbank/init.lua @@ -22,15 +22,10 @@ local default_opts = { keymaps = {}, } --- local plugin_path = debug.getinfo(1).source:sub(2):match("(.*/).*/.*/") or "./" - --- wrapper function for main plugin functionality ---@param opts table --- TODO: read from persistent database if sql persist is set (allow multi-session sync) local function show_yank_bank(opts) - -- Parse command arguments directly if args are provided as a string - opts = opts or default_opts - -- initialize buffer and populate bank local bufnr, display_lines, line_yank_map = menu.create_and_fill_buffer(yanks, reg_types, opts) @@ -46,11 +41,10 @@ local function show_yank_bank(opts) end -- plugin setup ----@param opts table +---@param opts? table function M.setup(opts) -- merge opts with default options table - -- opts = vim.tbl_deep_extend("force", default_opts, opts or {}) - opts = opts or default_opts + opts = vim.tbl_deep_extend("keep", opts or {}, default_opts) -- enable persistence based on opts (needs to be called before autocmd setup) yanks, reg_types = persistence.setup(opts) @@ -58,7 +52,7 @@ function M.setup(opts) -- create clipboard autocmds clipboard.setup_yank_autocmd(yanks, reg_types, opts) - -- Create user command + -- create user command vim.api.nvim_create_user_command("YankBank", function() show_yank_bank(opts) end, { desc = "Show Recent Yanks" }) diff --git a/lua/yankbank/persistence.lua b/lua/yankbank/persistence.lua index 78cda57..1d23441 100644 --- a/lua/yankbank/persistence.lua +++ b/lua/yankbank/persistence.lua @@ -7,9 +7,7 @@ local persistence = {} ---@param reg_type string ---@param opts table function M.add_entry(entry, reg_type, opts) - if not opts.persist_type then - return - elseif opts.persist_type == "sqlite" then + if opts.persist_type == "sqlite" then persistence:insert_yank(entry, reg_type) end end @@ -23,10 +21,6 @@ function M.setup(opts) return {}, {} elseif opts.persist_type == "sqlite" then persistence = require("yankbank.persistence.sql").setup(opts) - vim.api.nvim_create_user_command("YankView", function() - print(vim.inspect(persistence:get())) - end, {}) - return persistence:get_bank() else return {}, {} diff --git a/lua/yankbank/persistence/sql.lua b/lua/yankbank/persistence/sql.lua index 6753645..4c6edbe 100644 --- a/lua/yankbank/persistence/sql.lua +++ b/lua/yankbank/persistence/sql.lua @@ -2,7 +2,8 @@ local M = {} local sqlite = require("sqlite.db") -local dbdir = vim.fn.stdpath("data") .. "/databases" +-- local dbdir = vim.fn.stdpath("data") .. "/databases" +local dbdir = debug.getinfo(1).source:sub(2):match("(.*/).*/.*/.*/") or "./" local max_entries = 10 ---@class YankBankDB:sqlite_db @@ -89,9 +90,14 @@ end function M.setup(opts) max_entries = opts.max_entries - -- TODO: move database into plugin directory instead to allow easier uninstall - if vim.fn.isdirectory(dbdir) == 0 then - vim.fn.mkdir(dbdir, "p") + vim.api.nvim_create_user_command("YankBankClearDB", function() + data:remove() + end, {}) + + if opts.debug == true then + vim.api.nvim_create_user_command("YankBankViewDB", function() + print(vim.inspect(data:get())) + end, {}) end return data -- 2.51.2