Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add JSON output for query() #262

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lua/frecency/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local os_util = require "frecency.os_util"
---@field db_safe_mode? boolean default: true
---@field db_validate_threshold? integer default: 10
---@field debug? boolean default: false
---@field debug_timer? boolean default: false
---@field default_workspace? string default: nil
---@field disable_devicons? boolean default: false
---@field filter_delimiter? string default: ":"
Expand Down Expand Up @@ -39,6 +40,7 @@ local Config = {}
---@field db_safe_mode boolean default: true
---@field db_validate_threshold integer default: 10
---@field debug boolean default: false
---@field debug_timer boolean default: false
---@field default_workspace? string default: nil
---@field disable_devicons boolean default: false
---@field filter_delimiter string default: ":"
Expand Down Expand Up @@ -66,6 +68,7 @@ Config.new = function()
db_safe_mode = true,
db_validate_threshold = true,
debug = true,
debug_timer = true,
default_workspace = true,
disable_devicons = true,
filter_delimiter = true,
Expand Down Expand Up @@ -106,6 +109,7 @@ Config.default_values = {
db_safe_mode = true,
db_validate_threshold = 10,
debug = false,
debug_timer = false,
default_workspace = nil,
disable_devicons = false,
filter_delimiter = ":",
Expand Down
12 changes: 11 additions & 1 deletion lua/frecency/database/table.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local log = require "frecency.log"
local timer = require "frecency.timer"
local wait = require "frecency.wait"
local lazy_require = require "frecency.lazy_require"
local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]]

Expand All @@ -24,7 +26,15 @@ end
---@param key string
function Table:__index(key)
if key == "records" and not rawget(self, "is_ready") then
Table.wait_ready(self)
local is_async = not not coroutine.running()
if is_async then
Table.wait_ready(self)
else
log.debug "need wait() for wait_ready()"
wait(function()
Table.wait_ready(self)
end)
end
end
return vim.F.if_nil(rawget(self, key), Table[key])
end
Expand Down
8 changes: 5 additions & 3 deletions lua/frecency/klass.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ end

---@class FrecencyQueryOpts
---@field direction? "asc"|"desc" default: "desc"
---@field json? boolean default: false
---@field limit? integer default: 100
---@field order? FrecencyQueryOrder default: "score"
---@field record? boolean default: false
Expand All @@ -213,10 +214,11 @@ end

---@param opts? FrecencyQueryOpts
---@param epoch? integer
---@return FrecencyQueryEntry[]|string[]
---@return string|FrecencyQueryEntry[]|string[]
function Frecency:query(opts, epoch)
opts = vim.tbl_extend("force", {
direction = "desc",
json = false,
limit = 100,
order = "score",
record = false,
Expand All @@ -235,9 +237,9 @@ function Frecency:query(opts, epoch)
return entry.path
end, entries)
if #results > opts.limit then
return vim.list_slice(results, 1, opts.limit)
results = vim.list_slice(results, 1, opts.limit)
end
return results
return opts.json and vim.json.encode(results) or results
end

---@private
Expand Down
3 changes: 3 additions & 0 deletions lua/frecency/timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ function M.track(event)
return stats._stats.times[key] and make_key((num or 1) + 1) or key
end
stats.track(make_key())
if config.debug_timer then
log.debug(event)
end
end
end

Expand Down
Loading