Support path_display: filename_first (#195)

* 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>
This commit is contained in:
JINNOUCHI Yasushi 2024-05-01 13:17:49 +09:00 committed by GitHub
parent 62534e2479
commit 46c5358929
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 48 additions and 37 deletions

View File

@ -49,12 +49,6 @@ end
---@param workspace_tag? string ---@param workspace_tag? string
---@return FrecencyEntryMakerInstance ---@return FrecencyEntryMakerInstance
function EntryMaker:create(filepath_formatter, workspace, workspace_tag) 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 function(file)
return { return {
filename = file.path, filename = file.path,
@ -64,63 +58,67 @@ function EntryMaker:create(filepath_formatter, workspace, workspace_tag)
---@param entry FrecencyEntry ---@param entry FrecencyEntry
---@return table ---@return table
display = function(entry) 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) return displayer(items)
end, end,
} }
end 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 ---@private
---@param entry FrecencyEntry ---@param entry FrecencyEntry
---@param workspace? string ---@param workspace? string
---@param workspace_tag? string ---@param workspace_tag? string
---@param formatter fun(filename: string): string ---@param formatter fun(filename: string): string, FrecencyTelescopePathStyle[]
---@return table[] ---@return table[], table[]
function EntryMaker:items(entry, workspace, workspace_tag, formatter) function EntryMaker:items(entry, workspace, workspace_tag, formatter)
local items = {} local items, width_items = {}, {}
if config.show_scores then if config.show_scores then
table.insert(items, { entry.score, "TelescopeFrecencyScores" }) table.insert(items, { entry.score, "TelescopeFrecencyScores" })
table.insert(width_items, { width = 5 }) -- recency score
if config.matcher == "fuzzy" then if config.matcher == "fuzzy" then
table.insert(items, { entry.index, "TelescopeFrecencyScores" }) 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" local score = (not entry.fuzzy_score or entry.fuzzy_score == 0) and "0"
or ("%.3f"):format(entry.fuzzy_score):sub(0, 5) or ("%.3f"):format(entry.fuzzy_score):sub(0, 5)
table.insert(items, { score, "TelescopeFrecencyScores" }) table.insert(items, { score, "TelescopeFrecencyScores" })
table.insert(width_items, { width = 6 }) -- fuzzy score
end end
end end
if self.web_devicons.is_enabled then 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(items, { self.web_devicons:get_icon(entry.name, entry.name:match "%a+$", { default = true }) })
table.insert(width_items, { width = 2 })
end end
if config.show_filter_column and workspace and workspace_tag then 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 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 or self.fs:relative_from_home(workspace) .. Path.path.sep
table.insert(items, { filtered, "Directory" }) table.insert(items, { filtered, "Directory" })
table.insert(width_items, { width = self:calculate_filter_column_width(workspace, workspace_tag) })
end end
table.insert(items, { formatter(entry.name), self.loaded[entry.name] and "TelescopeBufferLoaded" or "" }) local formatted_name, path_style = formatter(entry.name)
return items -- 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
return items, width_items
end end
---@private ---@private

View File

@ -257,7 +257,7 @@ function Picker:set_prompt_options(bufnr)
vim.keymap.set("i", "<S-Tab>", "pumvisible() ? '<C-p>' : ''", { buffer = bufnr, expr = true }) vim.keymap.set("i", "<S-Tab>", "pumvisible() ? '<C-p>' : ''", { buffer = bufnr, expr = true })
end end
---@alias FrecencyFilepathFormatter fun(workspace: string?): fun(filename: string): string): string ---@alias FrecencyFilepathFormatter fun(workspace: string?): fun(filename: string): string, FrecencyTelescopePathStyle[]
---@private ---@private
---@param picker_opts table ---@param picker_opts table
@ -272,6 +272,11 @@ function Picker:filepath_formatter(picker_opts)
opts.cwd = workspace or self.fs.os_homedir opts.cwd = workspace or self.fs.os_homedir
return function(filename) 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) return utils.transform_path(opts, filename)
end end
end end

View File

@ -139,9 +139,17 @@ function FrecencyPlenaryAsyncUtil.scheduler() end
---@class FrecencyTelescopeEntryDisplay ---@class FrecencyTelescopeEntryDisplay
---@field create fun(opts: FrecencyTelescopeEntryDisplayOptions): FrecencyTelescopeEntryDisplayer ---@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 ---@class FrecencyTelescopeUtils
---@field path_tail fun(path: string): string ---@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 ---@field buf_is_loaded fun(filename: string): boolean
---@class FrecencyTelescopePicker ---@class FrecencyTelescopePicker