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
This commit is contained in:
JINNOUCHI Yasushi 2023-11-24 10:51:36 +09:00 committed by GitHub
parent daf59744f6
commit ca5fa5326f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 0 deletions

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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?

View File

@ -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