11local config = require (' opencode.config' ).get ()
22local M = {}
33
4+ local last_successful_tool = nil
5+
46local 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
1820end
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[]
2234local 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