Skip to content

Commit

Permalink
Support path_display: filename_first (#195)
Browse files Browse the repository at this point in the history
* feat:support path style

* chore: add types and comments to be readable

* fix: do not use the logic without 'filename_first'

---------

Co-authored-by: zhaogang <zhaogang@dustess.com>
  • Loading branch information
delphinus and zhaogang committed May 1, 2024
1 parent 62534e2 commit 46c5358
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
68 changes: 33 additions & 35 deletions lua/frecency/entry_maker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ end
---@param workspace_tag? string
---@return FrecencyEntryMakerInstance
function EntryMaker:create(filepath_formatter, workspace, workspace_tag)
local displayer = entry_display.create {
separator = "",
hl_chars = { [Path.path.sep] = "TelescopePathSeparator" },
items = self:displayer_items(workspace, workspace_tag),
}

return function(file)
return {
filename = file.path,
Expand All @@ -64,63 +58,67 @@ 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))
local items, width_items = self:items(entry, workspace, workspace_tag, filepath_formatter(workspace))

local displayer = entry_display.create {
separator = "",
hl_chars = { [Path.path.sep] = "TelescopePathSeparator" },
items = width_items,
}

return displayer(items)
end,
}
end
end

---@private
---@param workspace? string
---@param workspace_tag? string
---@return table[]
function EntryMaker:displayer_items(workspace, workspace_tag)
local items = {}
if config.show_scores then
table.insert(items, { width = 5 }) -- recency score
if config.matcher == "fuzzy" then
table.insert(items, { width = 5 }) -- index
table.insert(items, { width = 6 }) -- fuzzy score
end
end
if self.web_devicons.is_enabled then
table.insert(items, { width = 2 })
end
if config.show_filter_column and workspace and workspace_tag then
table.insert(items, { width = self:calculate_filter_column_width(workspace, workspace_tag) })
end
table.insert(items, { remaining = true })
return items
end

---@private
---@param entry FrecencyEntry
---@param workspace? string
---@param workspace_tag? string
---@param formatter fun(filename: string): string
---@return table[]
---@param formatter fun(filename: string): string, FrecencyTelescopePathStyle[]
---@return table[], table[]
function EntryMaker:items(entry, workspace, workspace_tag, formatter)
local items = {}
local items, width_items = {}, {}
if config.show_scores then
table.insert(items, { entry.score, "TelescopeFrecencyScores" })
table.insert(width_items, { width = 5 }) -- recency score
if config.matcher == "fuzzy" then
table.insert(items, { entry.index, "TelescopeFrecencyScores" })
table.insert(width_items, { width = 5 }) -- index
local score = (not entry.fuzzy_score or entry.fuzzy_score == 0) and "0"
or ("%.3f"):format(entry.fuzzy_score):sub(0, 5)
table.insert(items, { score, "TelescopeFrecencyScores" })
table.insert(width_items, { width = 6 }) -- fuzzy score
end
end
if self.web_devicons.is_enabled then
table.insert(items, { self.web_devicons:get_icon(entry.name, entry.name:match "%a+$", { default = true }) })
table.insert(width_items, { width = 2 })
end
if config.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 self.fs:relative_from_home(workspace) .. Path.path.sep
table.insert(items, { filtered, "Directory" })
table.insert(width_items, { width = self:calculate_filter_column_width(workspace, workspace_tag) })
end
local formatted_name, path_style = formatter(entry.name)
-- NOTE: this means it is formatted with the option: filename_first
if path_style and type(path_style) == "table" and #path_style > 0 then
local index = path_style[1][1]
local filename = formatted_name:sub(1, index[1])
local parent_path = formatted_name:sub(index[1] + 2, index[2])
local hl = path_style[1][2]

table.insert(items, { filename, self.loaded[entry.name] and "TelescopeBufferLoaded" or "" })
table.insert(items, { parent_path, hl })
table.insert(width_items, { width = #filename + 1 })
table.insert(width_items, { remaining = true })
else
table.insert(items, { formatted_name, self.loaded[entry.name] and "TelescopeBufferLoaded" or "" })
table.insert(width_items, { remaining = true })
end
table.insert(items, { formatter(entry.name), self.loaded[entry.name] and "TelescopeBufferLoaded" or "" })
return items
return items, width_items
end

---@private
Expand Down
7 changes: 6 additions & 1 deletion lua/frecency/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function Picker:set_prompt_options(bufnr)
vim.keymap.set("i", "<S-Tab>", "pumvisible() ? '<C-p>' : ''", { buffer = bufnr, expr = true })
end

---@alias FrecencyFilepathFormatter fun(workspace: string?): fun(filename: string): string): string
---@alias FrecencyFilepathFormatter fun(workspace: string?): fun(filename: string): string, FrecencyTelescopePathStyle[]

---@private
---@param picker_opts table
Expand All @@ -272,6 +272,11 @@ function Picker:filepath_formatter(picker_opts)
opts.cwd = workspace or self.fs.os_homedir

return function(filename)
local path_display = config_values.path_display
if type(path_display) == "table" and path_display.filename_first then
opts.path_display = path_display
end

return utils.transform_path(opts, filename)
end
end
Expand Down
10 changes: 9 additions & 1 deletion lua/frecency/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,17 @@ function FrecencyPlenaryAsyncUtil.scheduler() end
---@class FrecencyTelescopeEntryDisplay
---@field create fun(opts: FrecencyTelescopeEntryDisplayOptions): FrecencyTelescopeEntryDisplayer

---@class FrecencyTelescopePathStyleIndex
---@field [1] integer start index of the path
---@field [2] integer finish index of the path

---@class FrecencyTelescopePathStyle
---@field [1] FrecencyTelescopePathStyleIndex
---@field [2] string highlight name

---@class FrecencyTelescopeUtils
---@field path_tail fun(path: string): string
---@field transform_path fun(opts: table, path: string): string
---@field transform_path fun(opts: table, path: string): string, FrecencyTelescopePathStyle[]
---@field buf_is_loaded fun(filename: string): boolean

---@class FrecencyTelescopePicker
Expand Down

0 comments on commit 46c5358

Please sign in to comment.