diff --git a/lua/frecency/config.lua b/lua/frecency/config.lua index df59769e..73cd75c0 100644 --- a/lua/frecency/config.lua +++ b/lua/frecency/config.lua @@ -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: ":" @@ -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: ":" @@ -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, @@ -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 = ":", diff --git a/lua/frecency/database/table.lua b/lua/frecency/database/table.lua index 5fbdf145..c3d519ef 100644 --- a/lua/frecency/database/table.lua +++ b/lua/frecency/database/table.lua @@ -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]] @@ -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 diff --git a/lua/frecency/klass.lua b/lua/frecency/klass.lua index 0e3d172e..0635c72e 100644 --- a/lua/frecency/klass.lua +++ b/lua/frecency/klass.lua @@ -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 @@ -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, @@ -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 diff --git a/lua/frecency/timer.lua b/lua/frecency/timer.lua index 1dc5f49f..aa4453b2 100644 --- a/lua/frecency/timer.lua +++ b/lua/frecency/timer.lua @@ -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