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. value: `nil`, it uses these way below to make entries for workspace files.
It tries in order until it works successfully. It tries in order until it works successfully.
1. `rg -0.g '!.git' --files` 1. `rg -.g '!.git' --files`
2. `fdfind -0Htf` 2. `fdfind -Htf`
3. `fd -0Htf` 3. `fd -Htf`
4. Native Lua code (old way) 4. Native Lua code (old way)
If you like another commands, set them to this option, like If you like another commands, set them to this option, like
`workspace_scan_cmd = { "find", ".", "-type", "f", "-print0" }`. This command `workspace_scan_cmd = { "find", ".", "-type", "f" }`.
must use NUL characters for delimiters.
If you prefer Native Lua code, set `workspace_scan_cmd = "LUA"`. 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 async = require "plenary.async" --[[@as PlenaryAsync]]
local log = require "plenary.log" local log = require "plenary.log"
@ -74,7 +75,7 @@ function Finder:start(datetime)
local ok local ok
if cmd ~= "LUA" and self.need_scan_dir then if cmd ~= "LUA" and self.need_scan_dir then
---@type string[][] ---@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 for _, c in ipairs(cmds) do
ok = self:scan_dir_cmd(c) ok = self:scan_dir_cmd(c)
if ok then if ok then
@ -102,25 +103,52 @@ end
---@param cmd string[] ---@param cmd string[]
---@return boolean ---@return boolean
function Finder:scan_dir_cmd(cmd) function Finder:scan_dir_cmd(cmd)
local ok local function stdout(err, chunk)
---@diagnostic disable-next-line: assign-type-mismatch if not self.closed and not err and chunk then
ok, self.process = pcall(vim.system, cmd, { for name in chunk:gmatch "[^\n]+" do
cwd = self.path, local cleaned = name:gsub("^%./", "")
stdout = function(err, chunk) local fullpath = self.fs.joinpath(self.path, cleaned)
if not self.closed and not err and chunk then local entry = self.entry_maker { id = 0, count = 0, path = fullpath, score = 0 }
for name in chunk:gmatch "[^%z]+" do self.scan_tx.send(entry)
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 end
end, end
}, function() end
local function on_exit()
self.process = nil self.process = nil
self:close() self:close()
self.scan_tx.send(nil) 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 if not ok then
self.process = nil self.process = nil
end end