From 4872f1d2c1e79a117d505df7416b9151fa542dec Mon Sep 17 00:00:00 2001 From: delphinus Date: Mon, 9 Sep 2024 01:13:46 +0900 Subject: [PATCH] feat: enable to use multi dirs for each workspace --- lua/frecency/config.lua | 4 ++-- lua/frecency/database.lua | 19 ++++++++++++++++--- lua/frecency/finder.lua | 8 ++++---- lua/frecency/klass.lua | 5 +++-- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lua/frecency/config.lua b/lua/frecency/config.lua index 73cd75c0..81afdf73 100644 --- a/lua/frecency/config.lua +++ b/lua/frecency/config.lua @@ -24,7 +24,7 @@ local os_util = require "frecency.os_util" ---@field show_scores? boolean default: false ---@field show_unindexed? boolean default: true ---@field workspace_scan_cmd? "LUA"|string[] default: nil ----@field workspaces? table default: {} +---@field workspaces? table default: {} ---@class FrecencyConfig: FrecencyRawConfig ---@field ext_config FrecencyRawConfig @@ -55,7 +55,7 @@ local Config = {} ---@field show_scores boolean default: false ---@field show_unindexed boolean default: true ---@field workspace_scan_cmd? "LUA"|string[] default: nil ----@field workspaces table default: {} +---@field workspaces table default: {} ---@return FrecencyConfig Config.new = function() diff --git a/lua/frecency/database.lua b/lua/frecency/database.lua index 233d5237..1c1110b7 100644 --- a/lua/frecency/database.lua +++ b/lua/frecency/database.lua @@ -170,14 +170,27 @@ function Database:update(path, epoch) end ---@async ----@param workspace? string +---@param workspaces? string[] ---@param epoch? integer ---@return FrecencyDatabaseEntry[] -function Database:get_entries(workspace, epoch) +function Database:get_entries(workspaces, epoch) local now = epoch or os.time() + ---@param path string + ---@return boolean + local function in_workspace(path) + if not workspaces then + return true + end + for _, workspace in ipairs(workspaces) do + if fs.starts_with(path, workspace) then + return true + end + end + return false + end local items = {} for path, record in pairs(self.tbl.records) do - if fs.starts_with(path, workspace) then + if in_workspace(path) then table.insert(items, { path = path, count = record.count, diff --git a/lua/frecency/finder.lua b/lua/frecency/finder.lua index e405e107..08deb533 100644 --- a/lua/frecency/finder.lua +++ b/lua/frecency/finder.lua @@ -252,13 +252,13 @@ function Finder:process_channel(process_result, entries, rx, start_index) end end ----@param workspace? string +---@param workspaces? string[] ---@param epoch? integer ---@return FrecencyFile[] -function Finder:get_results(workspace, epoch) - log.debug { workspace = workspace or "NONE" } +function Finder:get_results(workspaces, epoch) + log.debug { workspaces = workspaces or "NONE" } timer.track "fetching start" - local files = self.database:get_entries(workspace, epoch) + local files = self.database:get_entries(workspaces, epoch) timer.track "fetching finish" for _, file in ipairs(files) do file.score = file.ages and recency.calculate(file.count, file.ages) or 0 diff --git a/lua/frecency/klass.lua b/lua/frecency/klass.lua index 0635c72e..332231e5 100644 --- a/lua/frecency/klass.lua +++ b/lua/frecency/klass.lua @@ -204,7 +204,7 @@ end ---@field limit? integer default: 100 ---@field order? FrecencyQueryOrder default: "score" ---@field record? boolean default: false ----@field workspace? string default: nil +---@field workspace? string|string[] default: nil ---@class FrecencyQueryEntry ---@field count integer @@ -223,6 +223,7 @@ function Frecency:query(opts, epoch) order = "score", record = false, }, opts or {}) + local workspaces=type(opts.workspace)=='table'and opts.workspace or type(opts.workspace)=='string'and {opts.workspace}or nil ---@param entry FrecencyDatabaseEntry local entries = vim.tbl_map(function(entry) return { @@ -231,7 +232,7 @@ function Frecency:query(opts, epoch) score = entry.ages and recency.calculate(entry.count, entry.ages) or 0, timestamps = entry.timestamps, } - end, self.database:get_entries(opts.workspace, epoch)) + end, self.database:get_entries(workspaces, epoch)) table.sort(entries, self:query_sorter(opts.order, opts.direction)) local results = opts.record and entries or vim.tbl_map(function(entry) return entry.path