From ca5fa5326fc2b2ebd3269176594c4341ad720ac5 Mon Sep 17 00:00:00 2001 From: JINNOUCHI Yasushi Date: Fri, 24 Nov 2023 10:51:36 +0900 Subject: [PATCH] feat: add an command to delete an entry from DB (#152) * feat: add an command to delete an entry from DB Fix #151 * test: add tests for frecency:delete() * docs: add note for FrecencyDB --- README.md | 13 +++++++++++++ lua/frecency/database.lua | 4 ++++ lua/frecency/database/native.lua | 13 +++++++++++++ lua/frecency/database/sqlite.lua | 7 +++++++ lua/frecency/frecency.lua | 16 +++++++++++++++ lua/frecency/tests/frecency_spec.lua | 29 ++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+) diff --git a/README.md b/README.md index 60a8cb8..e58a8e6 100644 --- a/README.md +++ b/README.md @@ -263,6 +263,19 @@ The command `FrecencyValidate` can be used to clean the database when :FrecencyValidate! ``` +### Delete entries + +You can delete entries from DB by `FrecencyDelete` command. This command does +not remove the file itself, only from DB. + +```vim +" delete the current opened file +:FrecencyDelete + +" delete the supplied path +:FrecencyDelete /full/path/to/the/file +``` + ### Remove dependency for [sqlite.lua][] The former version of this plugin has used SQLite3 library to store data. When diff --git a/lua/frecency/database.lua b/lua/frecency/database.lua index 811085d..e83328b 100644 --- a/lua/frecency/database.lua +++ b/lua/frecency/database.lua @@ -25,6 +25,10 @@ function Database:unlinked_entries() end ---@return nil function Database:remove_files(files) end +---@param path string +---@return boolean +function Database:remove_entry(path) end + ---@param path string ---@param max_count integer ---@param datetime string? diff --git a/lua/frecency/database/native.lua b/lua/frecency/database/native.lua index 087891f..4f3890f 100644 --- a/lua/frecency/database/native.lua +++ b/lua/frecency/database/native.lua @@ -196,4 +196,17 @@ function Native:raw_save(tbl) assert(not async.uv.fs_close(fd)) end +---@param path string +---@return boolean +function Native:remove_entry(path) + if not self.table.records[path] then + return false + end + self.table.records[path] = nil + wait(function() + self:save() + end) + return true +end + return Native diff --git a/lua/frecency/database/sqlite.lua b/lua/frecency/database/sqlite.lua index 40801ab..755967c 100644 --- a/lua/frecency/database/sqlite.lua +++ b/lua/frecency/database/sqlite.lua @@ -145,4 +145,11 @@ function Sqlite:remove_files(ids) self.sqlite.files:remove { id = ids } end +---@param path string +---@return boolean +function Sqlite:remove_entry(path) + local exists = not not self.sqlite.files:get({ where = { path = path } })[1] + return exists and self.sqlite.files:remove { path = path } or false +end + return Sqlite diff --git a/lua/frecency/frecency.lua b/lua/frecency/frecency.lua index a5026cc..ff778c5 100644 --- a/lua/frecency/frecency.lua +++ b/lua/frecency/frecency.lua @@ -104,6 +104,12 @@ function Frecency:setup() self:migrate_database() end, { desc = "Migrate DB telescope-frecency to native code" }) + vim.api.nvim_create_user_command("FrecencyDelete", function(info) + local path_string = info.args == "" and "%:p" or info.args + local path = vim.fn.expand(path_string) --[[@as string]] + self:delete(path) + end, { nargs = "?", complete = "file", desc = "Delete entry from telescope-frecency" }) + local group = vim.api.nvim_create_augroup("TelescopeFrecency", {}) vim.api.nvim_create_autocmd({ "BufWinEnter", "BufWritePost" }, { desc = "Update database for telescope-frecency", @@ -238,6 +244,16 @@ function Frecency:migrate_database(to_sqlite, silently) end) end +---@param path string +---@return nil +function Frecency:delete(path) + if self.database:remove_entry(path) then + self:notify("successfully deleted: %s", path) + else + self:warn("failed to delete: %s", path) + end +end + ---@private ---@param fmt string ---@param ... any? diff --git a/lua/frecency/tests/frecency_spec.lua b/lua/frecency/tests/frecency_spec.lua index ec67b56..7721661 100644 --- a/lua/frecency/tests/frecency_spec.lua +++ b/lua/frecency/tests/frecency_spec.lua @@ -394,6 +394,35 @@ describe("frecency", function() end) end) end) + + describe("delete", function() + describe("when file exists", function() + with_files({ "hoge1.txt", "hoge2.txt" }, function(frecency, finder, dir) + local register = make_register(frecency, dir) + register("hoge1.txt", "2023-07-29T00:00:00+09:00") + register("hoge2.txt", "2023-07-29T00:01:00+09:00") + + it("deletes the file successfully", function() + local path = filepath(dir, "hoge2.txt") + local result + ---@diagnostic disable-next-line: duplicate-set-field + frecency.notify = function(self, fmt, ...) + vim.notify(self:message(fmt, ...)) + result = true + end + frecency:delete(path) + assert.are.same(result, true) + end) + + it("returns valid results", function() + local results = finder:get_results(nil, "2023-07-29T02:00:00+09:00") + assert.are.same({ + { count = 1, path = filepath(dir, "hoge1.txt"), score = 10 }, + }, results) + end) + end) + end) + end) end) end