mirror of
https://github.com/kristoferssolo/telescope-frecency.nvim.git
synced 2025-10-21 20:10:38 +00:00
fix: avoid calling non API-fast funcs in Lua loop (#197)
This commit is contained in:
parent
f8a89691c8
commit
6b6565e658
@ -49,6 +49,14 @@ 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)
|
||||||
|
-- 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),
|
||||||
|
}
|
||||||
|
|
||||||
return function(file)
|
return function(file)
|
||||||
return {
|
return {
|
||||||
filename = file.path,
|
filename = file.path,
|
||||||
@ -58,14 +66,7 @@ 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, width_items = self:items(entry, workspace, workspace_tag, filepath_formatter(workspace))
|
local 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,
|
||||||
}
|
}
|
||||||
@ -73,34 +74,55 @@ function EntryMaker:create(filepath_formatter, workspace, workspace_tag)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
---@param entry FrecencyEntry
|
|
||||||
---@param workspace? string
|
---@param workspace? string
|
||||||
---@param workspace_tag? string
|
---@param workspace_tag? string
|
||||||
---@param formatter fun(filename: string): string, FrecencyTelescopePathStyle[]
|
---@return table[]
|
||||||
---@return table[], table[]
|
function EntryMaker:width_items(workspace, workspace_tag)
|
||||||
function EntryMaker:items(entry, workspace, workspace_tag, formatter)
|
local width_items = {}
|
||||||
local items, width_items = {}, {}
|
|
||||||
if config.show_scores then
|
if config.show_scores then
|
||||||
table.insert(items, { entry.score, "TelescopeFrecencyScores" })
|
|
||||||
table.insert(width_items, { width = 5 }) -- recency score
|
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(width_items, { width = 5 }) -- index
|
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
|
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(width_items, { width = 2 })
|
table.insert(width_items, { width = 2 })
|
||||||
end
|
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) })
|
||||||
|
end
|
||||||
|
-- TODO: This is a stopgap measure to detect placeholders.
|
||||||
|
table.insert(width_items, {})
|
||||||
|
table.insert(width_items, {})
|
||||||
|
table.insert(width_items, {})
|
||||||
|
return width_items
|
||||||
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
|
---@param entry FrecencyEntry
|
||||||
|
---@param workspace? string
|
||||||
|
---@param workspace_tag? string
|
||||||
|
---@param formatter fun(filename: string): string, FrecencyTelescopePathStyle[]
|
||||||
|
---@return table[]
|
||||||
|
function EntryMaker:items(entry, workspace, workspace_tag, formatter)
|
||||||
|
local items = {}
|
||||||
|
if config.show_scores then
|
||||||
|
table.insert(items, { entry.score, "TelescopeFrecencyScores" })
|
||||||
|
if config.matcher == "fuzzy" then
|
||||||
|
table.insert(items, { entry.index, "TelescopeFrecencyScores" })
|
||||||
|
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" })
|
||||||
|
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 }) })
|
||||||
|
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
|
||||||
local formatted_name, path_style = formatter(entry.name)
|
local formatted_name, path_style = formatter(entry.name)
|
||||||
-- NOTE: this means it is formatted with the option: filename_first
|
-- NOTE: this means it is formatted with the option: filename_first
|
||||||
@ -110,15 +132,12 @@ function EntryMaker:items(entry, workspace, workspace_tag, formatter)
|
|||||||
local parent_path = formatted_name:sub(index[1] + 2, index[2])
|
local parent_path = formatted_name:sub(index[1] + 2, index[2])
|
||||||
local hl = path_style[1][2]
|
local hl = path_style[1][2]
|
||||||
|
|
||||||
table.insert(items, { filename, self.loaded[entry.name] and "TelescopeBufferLoaded" or "" })
|
table.insert(items, { filename .. " ", self.loaded[entry.name] and "TelescopeBufferLoaded" or "" })
|
||||||
table.insert(items, { parent_path, hl })
|
table.insert(items, { parent_path, hl })
|
||||||
table.insert(width_items, { width = #filename + 1 })
|
|
||||||
table.insert(width_items, { remaining = true })
|
|
||||||
else
|
else
|
||||||
table.insert(items, { formatted_name, self.loaded[entry.name] and "TelescopeBufferLoaded" or "" })
|
table.insert(items, { formatted_name, self.loaded[entry.name] and "TelescopeBufferLoaded" or "" })
|
||||||
table.insert(width_items, { remaining = true })
|
|
||||||
end
|
end
|
||||||
return items, width_items
|
return items
|
||||||
end
|
end
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user