Skip to content

Commit

Permalink
hoge
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Nov 24, 2024
1 parent 6b813a0 commit 299b67d
Showing 1 changed file with 58 additions and 46 deletions.
104 changes: 58 additions & 46 deletions lua/frecency/finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,38 @@ local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]]
---@field private seen table<string, boolean>
---@field private process VimSystemObj?
---@field private state FrecencyState
local Finder = {}
local Finder = {
---@type fun(): string[]?
cmd = (function()
local candidates = {
{ "fdfind", "-Htf", "-E", ".git" },
{ "fd", "-Htf", "-E", ".git" },
{ "rg", "-.g", "!.git", "--files" },
}
---@type string[]?
local cache
return function()
if not cache then
for _, candidate in ipairs(candidates) do
if vim.system then
if pcall(vim.system, candidate[1], { "--version" }) then
cache = candidate
break
end
elseif
pcall(function()
Job:new { command = candidate[1], args = { "--version" } }
end)
then
cache = candidate
break
end
end
end
return cache
end
end)(),
}

---@class FrecencyFinderConfig
---@field chunk_size? integer default: 1000
Expand Down Expand Up @@ -77,40 +108,15 @@ end
---@param epoch? integer
---@return nil
function Finder:start(epoch)
local ok
---@type table<string, boolean>
local results = {}
if config.workspace_scan_cmd ~= "LUA" and self.need_scan_dir then
local function detect_cmd()
local cmd
for _, c in ipairs {
{ "fdfind", "-Htf", "-E", ".git" },
{ "fd", "-Htf", "-E", ".git" },
{ "rg", "-.g", "!.git", "--files" },
} do
if vim.system then
if pcall(vim.system, { c[1], "--version" }) then
cmd = c
break
end
else
if pcall(function()
Job:new { command = c[1], args = { "--version" } }
end) then
cmd = c
break
end
end
end
return cmd
end

---@type string[][]
local cmds = config.workspace_scan_cmd and { config.workspace_scan_cmd }
or { { "fdfind", "-Htf", "-E", ".git" }, { "fd", "-Htf", "-E", ".git" }, { "rg", "-.g", "!.git", "--files" } }
for _, c in ipairs(cmds) do
ok = self:scan_dir_cmd(c)
if ok then
log.debug("scan_dir_cmd: " .. vim.inspect(c))
break
local cmd = config.workspace_scan_cmd --[=[@as string[]]=]
or Finder.cmd()
if cmd then
for _, path in ipairs(self.paths) do
log.debug(("scan_dir_cmd: %s: %s"):format(vim.inspect(cmd), path))
results[path] = self:scan_dir_cmd(path, cmd)
end
end
end
Expand All @@ -123,22 +129,27 @@ function Finder:start(epoch)
self.tx.send(entry)
end
self.tx.send(nil)
if self.need_scan_dir and not ok then
log.debug "scan_dir_lua"
async.util.scheduler()
self:scan_dir_lua()
if self.need_scan_dir then
for path, result in pairs(results) do
if not result then
log.debug("scan_dir_lua: " .. path)
async.util.scheduler()
self:scan_dir_lua(path)
end
end
end
end)()
end

---@param path string
---@param cmd string[]
---@return boolean
function Finder:scan_dir_cmd(cmd)
function Finder:scan_dir_cmd(path, cmd)
local function stdout(err, chunk)
if not self.closed and not err and chunk then
for name in chunk:gmatch "[^\n]+" do
local cleaned = name:gsub("^%./", "")
local fullpath = os_util.join_path(self.path, cleaned)
local fullpath = os_util.join_path(path, cleaned)
local entry = self.entry_maker { id = 0, count = 0, path = fullpath, score = 0 }
self.scan_tx.send(entry)
end
Expand All @@ -155,7 +166,7 @@ function Finder:scan_dir_cmd(cmd)
if vim.system then
---@diagnostic disable-next-line: assign-type-mismatch
ok, self.process = pcall(vim.system, cmd, {
cwd = self.path,
cwd = path,
text = true,
stdout = stdout,
}, on_exit)
Expand All @@ -170,7 +181,7 @@ function Finder:scan_dir_cmd(cmd)
end
log.debug { cmd = cmd[1], args = args }
local job = Job:new {
cwd = self.path,
cwd = path,
command = cmd[1],
args = args,
on_stdout = stdout,
Expand All @@ -187,14 +198,15 @@ function Finder:scan_dir_cmd(cmd)
end

---@async
---@param path string
---@return nil
function Finder:scan_dir_lua()
function Finder:scan_dir_lua(path)
local count = 0
for name in fs.scan_dir(self.path) do
for name in fs.scan_dir(path) do
if self.closed then
break
end
local fullpath = os_util.join_path(self.path, name)
local fullpath = os_util.join_path(path, name)
local entry = self.entry_maker { id = 0, count = 0, path = fullpath, score = 0 }
self.scan_tx.send(entry)
count = count + 1
Expand Down Expand Up @@ -237,7 +249,7 @@ end
---@param process_result fun(entry: FrecencyEntry): nil
---@param entries FrecencyEntry[]
---@return boolean?
function Finder:process_table(process_result, entries)
function Finder.process_table(_, process_result, entries)
for _, entry in ipairs(entries) do
if process_result(entry) then
return true
Expand Down

0 comments on commit 299b67d

Please sign in to comment.