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 Nov 24, 2024
1 parent 44d1c7b commit 7501f1e
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 89 deletions.
4 changes: 2 additions & 2 deletions lua/frecency/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 @@ -57,7 +57,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
42 changes: 29 additions & 13 deletions lua/frecency/entry_maker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ end
---@alias FrecencyEntryMakerInstance fun(file: FrecencyFile): FrecencyEntry

---@param filepath_formatter FrecencyFilepathFormatter
---@param workspace? string
---@param workspaces? string[]
---@param workspace_tag? string
---@return FrecencyEntryMakerInstance
function EntryMaker:create(filepath_formatter, workspace, workspace_tag)
function EntryMaker:create(filepath_formatter, workspaces, workspace_tag)
-- NOTE: entry_display.create calls non API-fast functions. We cannot call
-- in entry_maker because it will be called in a Lua loop.
local displayer = entry_display.create {
separator = "",
hl_chars = { [Path.path.sep] = "TelescopePathSeparator" },
items = self:width_items(workspace, workspace_tag),
items = self:width_items(workspaces, workspace_tag),
}

-- set loaded buffers for highlight
Expand All @@ -66,18 +66,28 @@ function EntryMaker:create(filepath_formatter, workspace, workspace_tag)
---@param entry FrecencyEntry
---@return table
display = function(entry)
local items = self:items(entry, workspace, workspace_tag, filepath_formatter(workspace))
---@type string
local matched
if workspaces then
for _, workspace in ipairs(workspaces) do
if entry.name:find(workspace, 1, true) then
matched = workspace
break
end
end
end
local items = self:items(entry, matched, workspace_tag, filepath_formatter(matched))
return displayer(items)
end,
}
end
end

---@private
---@param workspace? string
---@param workspaces? string[]
---@param workspace_tag? string
---@return table[]
function EntryMaker:width_items(workspace, workspace_tag)
function EntryMaker:width_items(workspaces, workspace_tag)
local width_items = {}
if config.show_scores then
table.insert(width_items, { width = 5 }) -- recency score
Expand All @@ -89,8 +99,8 @@ function EntryMaker:width_items(workspace, workspace_tag)
if not config.disable_devicons then
table.insert(width_items, { width = 2 })
end
if config.show_filter_column and workspace and workspace_tag then
table.insert(width_items, { width = self:calculate_filter_column_width(workspace, workspace_tag) })
if config.show_filter_column and workspaces and #workspaces > 0 and workspace_tag then
table.insert(width_items, { width = self:calculate_filter_column_width(workspaces, workspace_tag) })
end
-- TODO: This is a stopgap measure to detect placeholders.
table.insert(width_items, {})
Expand Down Expand Up @@ -146,18 +156,24 @@ function EntryMaker:items(entry, workspace, workspace_tag, formatter)
end

---@private
---@param workspace string
---@param workspaces string[]
---@param workspace_tag string
---@return integer
function EntryMaker:calculate_filter_column_width(workspace, workspace_tag)
return self:should_show_tail(workspace_tag) and #(utils.path_tail(workspace)) + 1
or #(fs.relative_from_home(workspace)) + 1
function EntryMaker:calculate_filter_column_width(workspaces, workspace_tag)
local longest = ""
for _, workspace in ipairs(workspaces) do
if not longest or #workspace > #longest then
longest = workspace
end
end
return self:should_show_tail(workspace_tag) and #(utils.path_tail(longest)) + 1
or #(fs.relative_from_home(longest)) + 1
end

---@private
---@param workspace_tag string
---@return boolean
function EntryMaker:should_show_tail(workspace_tag)
function EntryMaker.should_show_tail(_, workspace_tag)
local show_filter_column = config.show_filter_column
local filters = type(show_filter_column) == "table" and show_filter_column or { "LSP", "CWD" }
return vim.tbl_contains(filters, workspace_tag)
Expand Down
Loading

0 comments on commit 7501f1e

Please sign in to comment.