fix: add fallback for v0.9 that has no vim.system (#159)

* fix: add fallback for v0.9 that has no vim.system

* docs: remove note for NUL character delimiters
This commit is contained in:
JINNOUCHI Yasushi 2024-01-01 01:24:46 +09:00 committed by GitHub
parent da7c724e3c
commit b57930bfaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 21 deletions

View File

@ -215,14 +215,13 @@ See [default configuration](https://github.com/nvim-telescope/telescope.nvim#tel
value: `nil`, it uses these way below to make entries for workspace files.
It tries in order until it works successfully.
1. `rg -0.g '!.git' --files`
2. `fdfind -0Htf`
3. `fd -0Htf`
1. `rg -.g '!.git' --files`
2. `fdfind -Htf`
3. `fd -Htf`
4. Native Lua code (old way)
If you like another commands, set them to this option, like
`workspace_scan_cmd = { "find", ".", "-type", "f", "-print0" }`. This command
must use NUL characters for delimiters.
`workspace_scan_cmd = { "find", ".", "-type", "f" }`.
If you prefer Native Lua code, set `workspace_scan_cmd = "LUA"`.

View File

@ -1,3 +1,4 @@
local Job = require "plenary.job"
local async = require "plenary.async" --[[@as PlenaryAsync]]
local log = require "plenary.log"
@ -74,7 +75,7 @@ function Finder:start(datetime)
local ok
if cmd ~= "LUA" and self.need_scan_dir then
---@type string[][]
local cmds = cmd and { cmd } or { { "rg", "-0.g", "!.git", "--files" }, { "fdfind", "-0Htf" }, { "fd", "-0Htf" } }
local cmds = cmd and { cmd } or { { "rg", "-.g", "!.git", "--files" }, { "fdfind", "-Htf" }, { "fd", "-Htf" } }
for _, c in ipairs(cmds) do
ok = self:scan_dir_cmd(c)
if ok then
@ -102,25 +103,52 @@ end
---@param cmd string[]
---@return boolean
function Finder:scan_dir_cmd(cmd)
local ok
---@diagnostic disable-next-line: assign-type-mismatch
ok, self.process = pcall(vim.system, cmd, {
cwd = self.path,
stdout = function(err, chunk)
if not self.closed and not err and chunk then
for name in chunk:gmatch "[^%z]+" do
local cleaned = name:gsub("^%./", "")
local fullpath = self.fs.joinpath(self.path, cleaned)
local entry = self.entry_maker { id = 0, count = 0, path = fullpath, score = 0 }
self.scan_tx.send(entry)
end
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 = self.fs.joinpath(self.path, cleaned)
local entry = self.entry_maker { id = 0, count = 0, path = fullpath, score = 0 }
self.scan_tx.send(entry)
end
end,
}, function()
end
end
local function on_exit()
self.process = nil
self:close()
self.scan_tx.send(nil)
end)
end
local ok
if vim.system then
---@diagnostic disable-next-line: assign-type-mismatch
ok, self.process = pcall(vim.system, cmd, {
cwd = self.path,
text = true,
stdout = stdout,
}, on_exit)
else
-- for Neovim v0.9.x
ok, self.process = pcall(function()
local args = {}
for i, arg in ipairs(cmd) do
if i > 1 then
table.insert(args, arg)
end
end
log.debug { cmd = cmd[1], args = args }
local job = Job:new {
cwd = self.path,
command = cmd[1],
args = args,
on_stdout = stdout,
on_exit = on_exit,
}
job:start()
return job.handle
end)
end
if not ok then
self.process = nil
end