Skip to content

Commit

Permalink
hoge
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Sep 11, 2024
1 parent 4872f1d commit 041d045
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
14 changes: 7 additions & 7 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,18 @@ 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 = self:items(entry, workspaces, workspace_tag, filepath_formatter(workspaces))
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,7 +89,7 @@ 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
if config.show_filter_column and workspaces 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.
Expand Down
45 changes: 23 additions & 22 deletions lua/frecency/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local uv = vim.loop or vim.uv
---@field private lsp_workspaces string[]
---@field private namespace integer
---@field private state FrecencyState
---@field private workspace string?
---@field private workspaces string[]?
---@field private workspace_tag_regex string
local Picker = {}

Expand Down Expand Up @@ -64,20 +64,20 @@ end
---| "shorten"
---| "truncate"
---| fun(opts: FrecencyPickerOptions, path: string): string
---@field workspace? string
---@field workspace? string|string[]

---@param opts table
---@param workspace? string
---@param workspaces? string[]
---@param workspace_tag? string
function Picker:finder(opts, workspace, workspace_tag)
function Picker:finder(opts, workspaces, workspace_tag)
local filepath_formatter = self:filepath_formatter(opts)
local entry_maker = self.entry_maker:create(filepath_formatter, workspace, workspace_tag)
local need_scandir = not not (workspace and config.show_unindexed)
local entry_maker = self.entry_maker:create(filepath_formatter, workspaces, workspace_tag)
local need_scandir = not not (workspaces and #workspaces > 0 and config.show_unindexed)
return Finder.new(
self.database,
entry_maker,
need_scandir,
workspace,
workspaces,
self.state,
{ ignore_filenames = self.config.ignore_filenames }
)
Expand All @@ -91,11 +91,11 @@ function Picker:start(opts)
return self:default_path_display(picker_opts, path)
end,
}, telescope_config.values, opts or {}) --[[@as FrecencyPickerOptions]]
self.workspace = self:get_workspace(opts.cwd, self.config.initial_workspace_tag or config.default_workspace)
log.debug { workspace = self.workspace }
self.workspaces = self:get_workspaces(opts.cwd, self.config.initial_workspace_tag or config.default_workspace)
log.debug { workspaces = self.workspaces }

self.state = State.new()
local finder = self:finder(opts, self.workspace, self.config.initial_workspace_tag or config.default_workspace)
local finder = self:finder(opts, self.workspaces, self.config.initial_workspace_tag or config.default_workspace)
local picker = pickers.new(opts, {
prompt_title = "Frecency",
finder = finder,
Expand Down Expand Up @@ -140,7 +140,7 @@ end
function Picker:workspace_tags()
local tags = vim.tbl_keys(config.workspaces)
table.insert(tags, "CWD")
if self:get_lsp_workspace() then
if self:get_lsp_workspaces() then
table.insert(tags, "LSP")
end
return tags
Expand All @@ -152,7 +152,7 @@ end
---@return string
function Picker:default_path_display(opts, path)
local filename = Path:new(path):make_relative(opts.cwd)
if not self.workspace then
if not self.workspaces or #self.workspaces == 0 then
if vim.startswith(filename, fs.os_homedir) then
filename = "~" .. Path.path.sep .. fs.relative_from_home(filename)
elseif filename ~= path then
Expand All @@ -165,26 +165,27 @@ end
---@private
---@param cwd string
---@param tag? string
---@return string?
function Picker:get_workspace(cwd, tag)
---@return string[]?
function Picker:get_workspaces(cwd, tag)
if not tag then
return nil
elseif config.workspaces[tag] then
return config.workspaces[tag]
local w = config.workspaces[tag]
return type(w) == "table" and w or { w }
elseif tag == "LSP" then
return self:get_lsp_workspace()
return self:get_lsp_workspaces()
elseif tag == "CWD" then
return cwd
return { cwd }
end
end

---@private
---@return string?
function Picker:get_lsp_workspace()
---@return string[]?
function Picker:get_lsp_workspaces()
if vim.tbl_isempty(self.lsp_workspaces) then
self.lsp_workspaces = vim.api.nvim_buf_call(self.config.editing_bufnr, vim.lsp.buf.list_workspace_folders)
end
return self.lsp_workspaces[1]
return self.lsp_workspaces
end

---@private
Expand All @@ -196,9 +197,9 @@ function Picker:on_input_filter_cb(picker_opts)
local start, finish, tag = prompt:find(self.workspace_tag_regex)
local opts = { prompt = start and prompt:sub(finish + 1) or prompt }
if prompt == "" then
workspace = self:get_workspace(picker_opts.cwd, self.config.initial_workspace_tag or config.default_workspace)
workspace = self:get_workspaces(picker_opts.cwd, self.config.initial_workspace_tag or config.default_workspace)
else
workspace = self:get_workspace(picker_opts.cwd, tag) or self.workspace
workspace = self:get_workspaces(picker_opts.cwd, tag) or self.workspace
end
local picker = self.state:get()
if picker then
Expand Down

0 comments on commit 041d045

Please sign in to comment.