Skip to content
Draft
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
62 changes: 49 additions & 13 deletions lua/frecency/config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
local os_util = require "frecency.os_util"

---@class FrecencyOverwritableOpts
---@field disable_devicons? boolean default: false
---@field enable_prompt_mappings? boolean default: false
---@field filter_delimiter? string default: ":"
---@field hide_current_buffer? boolean default: false
---@field ignore_patterns? string[] default: { "*.git/*", "*/tmp/*", "term://*" }
---@field matcher? "default"|"fuzzy" default: "default"
---@field show_filter_column? boolean|string[] default: true
---@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|string[]> default: {}

---Type to use when users write their own config.
---@class FrecencyOpts
---@class FrecencyOpts: FrecencyOverwritableOpts
---@field recency_values? { age: integer, value: integer }[] default: see lua/frecency/config.lua
---@field auto_validate? boolean default: true
---@field bootstrap? boolean default: true
Expand All @@ -12,23 +25,12 @@ local os_util = require "frecency.os_util"
---@field debug? boolean default: false
---@field debug_timer? boolean|fun(event: string): nil default: false
---@field default_workspace? string default: nil
---@field disable_devicons? boolean default: false
---@field enable_prompt_mappings? boolean default: false
---@field filter_delimiter? string default: ":"
---@field hide_current_buffer? boolean default: false
---@field ignore_patterns? string[] default: { "*.git/*", "*/tmp/*", "term://*" }
---@field ignore_register? fun(bufnr: integer): boolean
---@field matcher? "default"|"fuzzy" default: "default"
---@field scoring_function? fun(recency: integer, fzy_score: number): number default: see lua/frecency/config.lua
---@field max_timestamps? integer default: 10
---@field path_display? table default: nil
---@field preceding? "opened"|"same_repo" default: nil
---@field show_filter_column? boolean|string[] default: true
---@field show_scores? boolean default: false
---@field show_unindexed? boolean default: true
---@field unregister_hidden? boolean default: false
---@field workspace_scan_cmd? "LUA"|string[] default: nil
---@field workspaces? table<string, string|string[]> default: {}

---@class FrecencyConfig: FrecencyRawConfig
---@field ext_config FrecencyRawConfig
Expand Down Expand Up @@ -159,7 +161,7 @@ Config.default_values = {
local config = Config.new()

---@return FrecencyRawConfig
Config.get = function()
Config.get_all = function()
return config.values
end

Expand Down Expand Up @@ -274,4 +276,38 @@ Config.setup = function(ext_config)
config.values = opts
end

---@param opts table
---@param key "disable_devicons"
---| "enable_prompt_mappings"
---| "filter_delimiter"
---| "hide_current_buffer"
---| "ignore_patterns"
---| "matcher"
---| "show_filter_column"
---| "show_scores"
---| "show_unindexed"
---| "workspace_scan_cmd"
---| "workspaces"
---@return any
Config.get = function(opts, key)
---@type table<string, true>
local overwritable = {
disable_devicons = true,
enable_prompt_mappings = true,
filter_delimiter = true,
hide_current_buffer = true,
ignore_patterns = true,
matcher = true,
show_filter_column = true,
show_scores = true,
show_unindexed = true,
workspace_scan_cmd = true,
workspaces = true,
}
if overwritable[key] and opts[key] ~= nil then
return opts[key]
end
return config[key]
end

return config
26 changes: 14 additions & 12 deletions lua/frecency/entry_maker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ local entry_display = lazy_require "telescope.pickers.entry_display" --[[@as Fre
local utils = lazy_require "telescope.utils" --[[@as FrecencyTelescopeUtils]]

---@class FrecencyEntryMaker
---@field opts FrecencyPickerOptions
---@field loaded table<string,boolean>
local EntryMaker = {}

---@param opts FrecencyPickerOptions
---@return FrecencyEntryMaker
EntryMaker.new = function()
return setmetatable({}, { __index = EntryMaker })
EntryMaker.new = function(opts)
return setmetatable({ opts = opts }, { __index = EntryMaker })
end

---@class FrecencyEntry
Expand Down Expand Up @@ -78,17 +80,17 @@ end
---@return table[]
function EntryMaker:width_items(workspaces, workspace_tag)
local width_items = {}
if config.show_scores then
if config.get(self.opts, "show_scores") then
table.insert(width_items, { width = 6 }) -- recency score
if config.matcher == "fuzzy" then
if config.get(self.opts, "matcher") == "fuzzy" then
table.insert(width_items, { width = 5 }) -- index
table.insert(width_items, { width = 6 }) -- fuzzy score
end
end
if not config.disable_devicons then
if not config.get(self.opts, "disable_devicons") then
table.insert(width_items, { width = 2 })
end
if config.show_filter_column and workspaces and #workspaces > 0 and workspace_tag then
if config.get(self.opts, "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.
Expand All @@ -113,22 +115,22 @@ function EntryMaker:items(entry, workspace, workspace_tag, formatter)
end

local items = {}
if config.show_scores then
if config.get(self.opts, "show_scores") then
table.insert(items, { format_score(entry.score), "TelescopeFrecencyScores" })
if config.matcher == "fuzzy" then
if config.get(self.opts, "matcher") == "fuzzy" then
table.insert(items, { ("%4d"):format(entry.index), "TelescopeFrecencyScores" })
table.insert(items, { format_score(entry.fuzzy_score or 0), "TelescopeFrecencyScores" })
end
end
if not config.disable_devicons then
if not config.get(self.opts, "disable_devicons") then
local basename = utils.path_tail(entry.name)
local icon, icon_highlight = web_devicons.get_icon(basename, utils.file_extension(basename), { default = false })
if not icon then
icon, icon_highlight = web_devicons.get_icon(basename, nil, { default = true })
end
table.insert(items, { icon, icon_highlight })
end
if config.show_filter_column and workspace and workspace_tag then
if config.get(self.opts, "show_filter_column") and workspace and workspace_tag then
local filtered = self:should_show_tail(workspace_tag) and utils.path_tail(workspace) .. Path.path.sep
or fs.relative_from_home(workspace) .. Path.path.sep
table.insert(items, { filtered, "Directory" })
Expand Down Expand Up @@ -164,8 +166,8 @@ end
---@private
---@param workspace_tag string
---@return boolean
function EntryMaker.should_show_tail(_, workspace_tag)
local show_filter_column = config.show_filter_column
function EntryMaker:should_show_tail(workspace_tag)
local show_filter_column = config.get(self.opts, "show_filter_column")
if show_filter_column == false then
return false
end
Expand Down
2 changes: 1 addition & 1 deletion lua/frecency/klass.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function Frecency:start(opts)
ignore_filenames = ignore_filenames,
initial_workspace_tag = opts.workspace,
})
self.picker:start(vim.tbl_extend("force", config.get(), opts))
self.picker:start(vim.tbl_extend("force", config.get_all(), opts))
timer.track "start() finish"
end

Expand Down
29 changes: 13 additions & 16 deletions lua/frecency/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ local uv = vim.loop or vim.uv
---@class FrecencyPicker
---@field private config FrecencyPickerConfig
---@field private database FrecencyDatabase
---@field private entry_maker FrecencyEntryMaker
---@field private lsp_workspaces string[]
---@field private namespace integer
---@field private state FrecencyState
Expand All @@ -43,7 +42,6 @@ Picker.new = function(database, picker_config)
local self = setmetatable({
config = picker_config,
database = database,
entry_maker = EntryMaker.new(),
lsp_workspaces = {},
namespace = vim.api.nvim_create_namespace "frecency",
}, { __index = Picker })
Expand All @@ -52,25 +50,13 @@ Picker.new = function(database, picker_config)
return self
end

---@class FrecencyPickerOptions
---@field cwd string
---@field hide_current_buffer? boolean
---@field path_display
---| "hidden"
---| "tail"
---| "absolute"
---| "smart"
---| "shorten"
---| "truncate"
---| fun(opts: FrecencyPickerOptions, path: string): string
---@field workspace? string

---@param opts table
---@param workspaces? string[]
---@param workspace_tag? string
function Picker:finder(opts, workspaces, workspace_tag)
local filepath_formatter = self:filepath_formatter(opts)
local entry_maker = self.entry_maker:create(filepath_formatter, workspaces, workspace_tag)
local em = EntryMaker.new(opts)
local entry_maker = em:create(filepath_formatter, workspaces, workspace_tag)
local need_scandir = not not (workspaces and #workspaces > 0 and config.show_unindexed)
return Finder.new(
self.database,
Expand All @@ -82,6 +68,17 @@ function Picker:finder(opts, workspaces, workspace_tag)
)
end

---@class FrecencyPickerOptions: FrecencyOverwritableOpts
---@field cwd string
---@field path_display
---| "hidden"
---| "tail"
---| "absolute"
---| "smart"
---| "shorten"
---| "truncate"
---| fun(opts: FrecencyPickerOptions, path: string): string

---@param opts? FrecencyPickerOptions
function Picker:start(opts)
opts = vim.tbl_extend("force", {
Expand Down
Loading