Skip to content

Commit 6fb9609

Browse files
committed
fix: file search error when other tools are available
This PR will look for alternative tools even if the preferred one is set. This should fix sudo-tee#31
1 parent 6d6e918 commit 6fb9609

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

lua/opencode/ui/completion/files.lua

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local config = require('opencode.config').get()
22
local M = {}
33

4+
local last_successful_tool = nil
5+
46
local function should_keep(ignore_patterns)
57
return function(path)
68
for _, pattern in ipairs(ignore_patterns) do
@@ -17,29 +19,45 @@ local function run_systemlist(cmd)
1719
return ok and vim.v.shell_error == 0 and result or nil
1820
end
1921

22+
local function try_tool(tool, args, pattern, max, ignore_patterns)
23+
if vim.fn.executable(tool) then
24+
local result = run_systemlist(tool .. string.format(args, pattern, max))
25+
if result then
26+
return vim.tbl_filter(should_keep(ignore_patterns), result)
27+
end
28+
end
29+
return nil
30+
end
31+
2032
---@param pattern string
2133
---@return string[]
2234
local function find_files_fast(pattern)
2335
pattern = vim.fn.shellescape(pattern) or '.'
2436
local file_config = config.ui.completion.file_sources
25-
local cli_tool = file_config.preferred_cli_tool or 'fd'
37+
local cli_tool = last_successful_tool or file_config.preferred_cli_tool or 'fd'
2638
local max = file_config.max_files or 10
39+
local ignore_patterns = file_config.ignore_patterns or {}
2740

41+
local tools_order = { 'fd', 'fdfind', 'rg', 'git' }
2842
local commands = {
2943
fd = ' --type f --type l --full-path --color=never -E .git -E node_modules -i %s --max-results %d 2>/dev/null',
3044
fdfind = ' --type f --type l --color=never -E .git -E node_modules --full-path -i %s --max-results %d 2>/dev/null',
3145
rg = ' --files --no-messages --color=never | grep -i %s 2>/dev/null | head -%d',
3246
git = ' ls-files --cached --others --exclude-standard | grep -i %s | head -%d',
3347
}
3448

35-
local tools_to_try = commands[cli_tool] and { [cli_tool] = commands[cli_tool] } or commands
49+
if cli_tool and commands[cli_tool] then
50+
tools_order = vim.tbl_filter(function(t)
51+
return t ~= cli_tool
52+
end, tools_order)
53+
table.insert(tools_order, 1, cli_tool)
54+
end
3655

37-
for tool, args in pairs(tools_to_try) do
38-
if vim.fn.executable(tool) then
39-
local result = run_systemlist(tool .. string.format(args, pattern, max))
40-
if result then
41-
return vim.tbl_filter(should_keep(file_config.ignore_patterns or {}), result)
42-
end
56+
for _, tool in ipairs(tools_order) do
57+
local result = try_tool(tool, commands[tool], pattern, max, ignore_patterns)
58+
if result then
59+
last_successful_tool = tool
60+
return result
4361
end
4462
end
4563
vim.notify('No suitable file search tool found. Please install fd, rg, or git.', vim.log.levels.WARN)

0 commit comments

Comments
 (0)