@@ -114,10 +114,44 @@ local function edit_config(_)
114114 :find ()
115115end
116116
117+ local command_exists_cache = {}
118+
117119local function command_exists_on_remote (command , server )
120+ local key = server .. " :" .. command
121+ if command_exists_cache [key ] ~= nil then
122+ return command_exists_cache [key ]
123+ end
118124 local ssh_cmd = string.format (' ssh %s "which %s"' , server , command )
119- local result = vim .fn .system (ssh_cmd )
120- return result ~= " "
125+ vim .fn .system (ssh_cmd )
126+ local exists = (vim .v .shell_error == 0 )
127+ command_exists_cache [key ] = exists
128+ return exists
129+ end
130+
131+ -- Cache per-host computed find command to avoid repeated ssh which calls
132+ local _find_command_cache = {}
133+ local function get_find_command_for_host (server )
134+ if _find_command_cache [server ] ~= nil then
135+ return _find_command_cache [server ]
136+ end
137+ local cmd = nil
138+ if command_exists_on_remote (" rg" , server ) then
139+ cmd = { " ssh" , server , " -C" , " rg" , " --files" , " --color" , " never" }
140+ elseif command_exists_on_remote (" fd" , server ) then
141+ cmd = { " ssh" , server , " fd" , " --type" , " f" , " --color" , " never" }
142+ elseif command_exists_on_remote (" fdfind" , server ) then
143+ cmd = { " ssh" , server , " fdfind" , " --type" , " f" , " --color" , " never" }
144+ elseif command_exists_on_remote (" where" , server ) then
145+ cmd = { " ssh" , server , " where" , " /r" , " ." , " *" }
146+ end
147+ _find_command_cache [server ] = cmd
148+ return cmd
149+ end
150+
151+ -- Clears cached remote-find commands and existence checks
152+ local function clear_cache ()
153+ command_exists_cache = {}
154+ _find_command_cache = {}
121155end
122156
123157-- Remote find_files implementation
@@ -160,22 +194,16 @@ local function find_files(opts)
160194 local mount_point = opts .mount_point or connections .get_current_mount_point ()
161195 local current_host = connections .get_current_host ()
162196
163- local find_command = (function ()
164- if opts .find_command then
165- if type (opts .find_command ) == " function" then
166- return opts .find_command (opts )
167- end
168- return opts .find_command
169- elseif command_exists_on_remote (" rg" , current_host [" Name" ]) then
170- return { " ssh" , current_host [" Name" ], " -C" , " rg" , " --files" , " --color" , " never" }
171- elseif command_exists_on_remote (" fd" , current_host [" Name" ]) then
172- return { " ssh" , current_host [" Name" ], " fd" , " --type" , " f" , " --color" , " never" }
173- elseif command_exists_on_remote (" fdfind" , current_host [" Name" ]) then
174- return { " ssh" , current_host [" Name" ], " fdfind" , " --type" , " f" , " --color" , " never" }
175- elseif command_exists_on_remote (" where" , current_host [" Name" ]) then
176- return { " ssh" , current_host [" Name" ], " where" , " /r" , " ." , " *" }
197+ local find_command
198+ if opts .find_command then
199+ if type (opts .find_command ) == " function" then
200+ find_command = opts .find_command (opts )
201+ else
202+ find_command = opts .find_command
177203 end
178- end )()
204+ else
205+ find_command = get_find_command_for_host (current_host [" Name" ])
206+ end
179207
180208 if not find_command then
181209 vim .notify " Remote host does not support any available find commands (rg, fd, fdfind, where)."
@@ -477,18 +505,19 @@ local present, telescope = pcall(require, "telescope")
477505if present then
478506 return telescope .register_extension {
479507 exports = {
480- connect = function (_ )
481- connect (_ )
508+ connect = function (opts )
509+ connect (opts )
482510 end ,
483- edit = function (_ )
484- edit_config (_ )
511+ edit = function (opts )
512+ edit_config (opts )
485513 end ,
486514 find_files = function (opts )
487515 find_files (opts )
488516 end ,
489517 live_grep = function (opts )
490518 live_grep (opts )
491519 end ,
520+ clear_cache = clear_cache ,
492521 },
493522 }
494523else
0 commit comments