Skip to content

Commit

Permalink
feat: enable to use multi dirs for each workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Sep 8, 2024
1 parent f67baca commit 4872f1d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lua/frecency/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> default: {}
---@field workspaces? table<string, string|string[]> default: {}

---@class FrecencyConfig: FrecencyRawConfig
---@field ext_config FrecencyRawConfig
Expand Down Expand Up @@ -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<string, string> default: {}
---@field workspaces table<string, string|string[]> default: {}

---@return FrecencyConfig
Config.new = function()
Expand Down
19 changes: 16 additions & 3 deletions lua/frecency/database.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions lua/frecency/finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lua/frecency/klass.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 4872f1d

Please sign in to comment.