1use std::{
2 path::{Path, PathBuf},
3 sync::Arc,
4};
5
6use anyhow::{Context as _, Result};
7use askpass::EncryptedPassword;
8use auto_update::AutoUpdater;
9use editor::Editor;
10use extension_host::ExtensionStore;
11use futures::channel::oneshot;
12use gpui::{
13 AnyWindowHandle, App, AsyncApp, DismissEvent, Entity, EventEmitter, Focusable, FontFeatures,
14 ParentElement as _, PromptLevel, Render, SemanticVersion, SharedString, Task,
15 TextStyleRefinement, WeakEntity,
16};
17
18use language::{CursorShape, Point};
19use markdown::{Markdown, MarkdownElement, MarkdownStyle};
20use release_channel::ReleaseChannel;
21use remote::{
22 ConnectionIdentifier, RemoteClient, RemoteConnection, RemoteConnectionOptions, RemotePlatform,
23 SshConnectionOptions,
24};
25pub use settings::SshConnection;
26use settings::{ExtendingVec, RegisterSetting, Settings, WslConnection};
27use theme::ThemeSettings;
28use ui::{
29 ActiveTheme, Color, CommonAnimationExt, Context, Icon, IconName, IconSize, InteractiveElement,
30 IntoElement, Label, LabelCommon, Styled, Window, prelude::*,
31};
32use util::paths::PathWithPosition;
33use workspace::{AppState, ModalView, Workspace};
34
35#[derive(RegisterSetting)]
36pub struct SshSettings {
37 pub ssh_connections: ExtendingVec<SshConnection>,
38 pub wsl_connections: ExtendingVec<WslConnection>,
39 /// Whether to read ~/.ssh/config for ssh connection sources.
40 pub read_ssh_config: bool,
41}
42
43impl SshSettings {
44 pub fn ssh_connections(&self) -> impl Iterator<Item = SshConnection> + use<> {
45 self.ssh_connections.clone().0.into_iter()
46 }
47
48 pub fn wsl_connections(&self) -> impl Iterator<Item = WslConnection> + use<> {
49 self.wsl_connections.clone().0.into_iter()
50 }
51
52 pub fn fill_connection_options_from_settings(&self, options: &mut SshConnectionOptions) {
53 for conn in self.ssh_connections() {
54 if conn.host == options.host
55 && conn.username == options.username
56 && conn.port == options.port
57 {
58 options.nickname = conn.nickname;
59 options.upload_binary_over_ssh = conn.upload_binary_over_ssh.unwrap_or_default();
60 options.args = Some(conn.args);
61 options.port_forwards = conn.port_forwards;
62 break;
63 }
64 }
65 }
66
67 pub fn connection_options_for(
68 &self,
69 host: String,
70 port: Option<u16>,
71 username: Option<String>,
72 ) -> SshConnectionOptions {
73 let mut options = SshConnectionOptions {
74 host,
75 port,
76 username,
77 ..Default::default()
78 };
79 self.fill_connection_options_from_settings(&mut options);
80 options
81 }
82}
83
84#[derive(Clone, PartialEq)]
85pub enum Connection {
86 Ssh(SshConnection),
87 Wsl(WslConnection),
88}
89
90impl From<Connection> for RemoteConnectionOptions {
91 fn from(val: Connection) -> Self {
92 match val {
93 Connection::Ssh(conn) => RemoteConnectionOptions::Ssh(conn.into()),
94 Connection::Wsl(conn) => RemoteConnectionOptions::Wsl(conn.into()),
95 }
96 }
97}
98
99impl From<SshConnection> for Connection {
100 fn from(val: SshConnection) -> Self {
101 Connection::Ssh(val)
102 }
103}
104
105impl From<WslConnection> for Connection {
106 fn from(val: WslConnection) -> Self {
107 Connection::Wsl(val)
108 }
109}
110
111impl Settings for SshSettings {
112 fn from_settings(content: &settings::SettingsContent) -> Self {
113 let remote = &content.remote;
114 Self {
115 ssh_connections: remote.ssh_connections.clone().unwrap_or_default().into(),
116 wsl_connections: remote.wsl_connections.clone().unwrap_or_default().into(),
117 read_ssh_config: remote.read_ssh_config.unwrap(),
118 }
119 }
120}
121
122pub struct RemoteConnectionPrompt {
123 connection_string: SharedString,
124 nickname: Option<SharedString>,
125 is_wsl: bool,
126 status_message: Option<SharedString>,
127 prompt: Option<(Entity<Markdown>, oneshot::Sender<EncryptedPassword>)>,
128 cancellation: Option<oneshot::Sender<()>>,
129 editor: Entity<Editor>,
130}
131
132impl Drop for RemoteConnectionPrompt {
133 fn drop(&mut self) {
134 if let Some(cancel) = self.cancellation.take() {
135 cancel.send(()).ok();
136 }
137 }
138}
139
140pub struct RemoteConnectionModal {
141 pub prompt: Entity<RemoteConnectionPrompt>,
142 paths: Vec<PathBuf>,
143 finished: bool,
144}
145
146impl RemoteConnectionPrompt {
147 pub(crate) fn new(
148 connection_string: String,
149 nickname: Option<String>,
150 is_wsl: bool,
151 window: &mut Window,
152 cx: &mut Context<Self>,
153 ) -> Self {
154 Self {
155 connection_string: connection_string.into(),
156 nickname: nickname.map(|nickname| nickname.into()),
157 is_wsl,
158 editor: cx.new(|cx| Editor::single_line(window, cx)),
159 status_message: None,
160 cancellation: None,
161 prompt: None,
162 }
163 }
164
165 pub fn set_cancellation_tx(&mut self, tx: oneshot::Sender<()>) {
166 self.cancellation = Some(tx);
167 }
168
169 fn set_prompt(
170 &mut self,
171 prompt: String,
172 tx: oneshot::Sender<EncryptedPassword>,
173 window: &mut Window,
174 cx: &mut Context<Self>,
175 ) {
176 let theme = ThemeSettings::get_global(cx);
177
178 let refinement = TextStyleRefinement {
179 font_family: Some(theme.buffer_font.family.clone()),
180 font_features: Some(FontFeatures::disable_ligatures()),
181 font_size: Some(theme.buffer_font_size(cx).into()),
182 color: Some(cx.theme().colors().editor_foreground),
183 background_color: Some(gpui::transparent_black()),
184 ..Default::default()
185 };
186
187 self.editor.update(cx, |editor, cx| {
188 if prompt.contains("yes/no") {
189 editor.set_masked(false, cx);
190 } else {
191 editor.set_masked(true, cx);
192 }
193 editor.set_text_style_refinement(refinement);
194 editor.set_cursor_shape(CursorShape::Block, cx);
195 });
196
197 let markdown = cx.new(|cx| Markdown::new_text(prompt.into(), cx));
198 self.prompt = Some((markdown, tx));
199 self.status_message.take();
200 window.focus(&self.editor.focus_handle(cx));
201 cx.notify();
202 }
203
204 pub fn set_status(&mut self, status: Option<String>, cx: &mut Context<Self>) {
205 self.status_message = status.map(|s| s.into());
206 cx.notify();
207 }
208
209 pub fn confirm(&mut self, window: &mut Window, cx: &mut Context<Self>) {
210 if let Some((_, tx)) = self.prompt.take() {
211 self.status_message = Some("Connecting".into());
212
213 self.editor.update(cx, |editor, cx| {
214 let pw = editor.text(cx);
215 if let Ok(secure) = EncryptedPassword::try_from(pw.as_ref()) {
216 tx.send(secure).ok();
217 }
218 editor.clear(window, cx);
219 });
220 }
221 }
222}
223
224impl Render for RemoteConnectionPrompt {
225 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
226 let theme = ThemeSettings::get_global(cx);
227
228 let mut text_style = window.text_style();
229 let refinement = TextStyleRefinement {
230 font_family: Some(theme.buffer_font.family.clone()),
231 font_features: Some(FontFeatures::disable_ligatures()),
232 font_size: Some(theme.buffer_font_size(cx).into()),
233 color: Some(cx.theme().colors().editor_foreground),
234 background_color: Some(gpui::transparent_black()),
235 ..Default::default()
236 };
237
238 text_style.refine(&refinement);
239 let markdown_style = MarkdownStyle {
240 base_text_style: text_style,
241 selection_background_color: cx.theme().colors().element_selection_background,
242 ..Default::default()
243 };
244
245 v_flex()
246 .key_context("PasswordPrompt")
247 .py_2()
248 .px_3()
249 .size_full()
250 .text_buffer(cx)
251 .when_some(self.status_message.clone(), |el, status_message| {
252 el.child(
253 h_flex()
254 .gap_1()
255 .child(
256 Icon::new(IconName::ArrowCircle)
257 .size(IconSize::Medium)
258 .with_rotate_animation(2),
259 )
260 .child(
261 div()
262 .text_ellipsis()
263 .overflow_x_hidden()
264 .child(format!("{}…", status_message)),
265 ),
266 )
267 })
268 .when_some(self.prompt.as_ref(), |el, prompt| {
269 el.child(
270 div()
271 .size_full()
272 .overflow_hidden()
273 .child(MarkdownElement::new(prompt.0.clone(), markdown_style))
274 .child(self.editor.clone()),
275 )
276 .when(window.capslock().on, |el| {
277 el.child(Label::new("⚠️ ⇪ is on"))
278 })
279 })
280 }
281}
282
283impl RemoteConnectionModal {
284 pub fn new(
285 connection_options: &RemoteConnectionOptions,
286 paths: Vec<PathBuf>,
287 window: &mut Window,
288 cx: &mut Context<Self>,
289 ) -> Self {
290 let (connection_string, nickname, is_wsl) = match connection_options {
291 RemoteConnectionOptions::Ssh(options) => {
292 (options.connection_string(), options.nickname.clone(), false)
293 }
294 RemoteConnectionOptions::Wsl(options) => (options.distro_name.clone(), None, true),
295 };
296 Self {
297 prompt: cx.new(|cx| {
298 RemoteConnectionPrompt::new(connection_string, nickname, is_wsl, window, cx)
299 }),
300 finished: false,
301 paths,
302 }
303 }
304
305 fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
306 self.prompt
307 .update(cx, |prompt, cx| prompt.confirm(window, cx))
308 }
309
310 pub fn finished(&mut self, cx: &mut Context<Self>) {
311 self.finished = true;
312 cx.emit(DismissEvent);
313 }
314
315 fn dismiss(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
316 if let Some(tx) = self
317 .prompt
318 .update(cx, |prompt, _cx| prompt.cancellation.take())
319 {
320 tx.send(()).ok();
321 }
322 self.finished(cx);
323 }
324}
325
326pub(crate) struct SshConnectionHeader {
327 pub(crate) connection_string: SharedString,
328 pub(crate) paths: Vec<PathBuf>,
329 pub(crate) nickname: Option<SharedString>,
330 pub(crate) is_wsl: bool,
331}
332
333impl RenderOnce for SshConnectionHeader {
334 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
335 let theme = cx.theme();
336
337 let mut header_color = theme.colors().text;
338 header_color.fade_out(0.96);
339
340 let (main_label, meta_label) = if let Some(nickname) = self.nickname {
341 (nickname, Some(format!("({})", self.connection_string)))
342 } else {
343 (self.connection_string, None)
344 };
345
346 let icon = match self.is_wsl {
347 true => IconName::Linux,
348 false => IconName::Server,
349 };
350
351 h_flex()
352 .px(DynamicSpacing::Base12.rems(cx))
353 .pt(DynamicSpacing::Base08.rems(cx))
354 .pb(DynamicSpacing::Base04.rems(cx))
355 .rounded_t_sm()
356 .w_full()
357 .gap_1p5()
358 .child(Icon::new(icon).size(IconSize::Small))
359 .child(
360 h_flex()
361 .gap_1()
362 .overflow_x_hidden()
363 .child(
364 div()
365 .max_w_96()
366 .overflow_x_hidden()
367 .text_ellipsis()
368 .child(Headline::new(main_label).size(HeadlineSize::XSmall)),
369 )
370 .children(
371 meta_label.map(|label| {
372 Label::new(label).color(Color::Muted).size(LabelSize::Small)
373 }),
374 )
375 .child(div().overflow_x_hidden().text_ellipsis().children(
376 self.paths.into_iter().map(|path| {
377 Label::new(path.to_string_lossy().into_owned())
378 .size(LabelSize::Small)
379 .color(Color::Muted)
380 }),
381 )),
382 )
383 }
384}
385
386impl Render for RemoteConnectionModal {
387 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl ui::IntoElement {
388 let nickname = self.prompt.read(cx).nickname.clone();
389 let connection_string = self.prompt.read(cx).connection_string.clone();
390 let is_wsl = self.prompt.read(cx).is_wsl;
391
392 let theme = cx.theme().clone();
393 let body_color = theme.colors().editor_background;
394
395 v_flex()
396 .elevation_3(cx)
397 .w(rems(34.))
398 .border_1()
399 .border_color(theme.colors().border)
400 .key_context("SshConnectionModal")
401 .track_focus(&self.focus_handle(cx))
402 .on_action(cx.listener(Self::dismiss))
403 .on_action(cx.listener(Self::confirm))
404 .child(
405 SshConnectionHeader {
406 paths: self.paths.clone(),
407 connection_string,
408 nickname,
409 is_wsl,
410 }
411 .render(window, cx),
412 )
413 .child(
414 div()
415 .w_full()
416 .rounded_b_lg()
417 .bg(body_color)
418 .border_t_1()
419 .border_color(theme.colors().border_variant)
420 .child(self.prompt.clone()),
421 )
422 }
423}
424
425impl Focusable for RemoteConnectionModal {
426 fn focus_handle(&self, cx: &gpui::App) -> gpui::FocusHandle {
427 self.prompt.read(cx).editor.focus_handle(cx)
428 }
429}
430
431impl EventEmitter<DismissEvent> for RemoteConnectionModal {}
432
433impl ModalView for RemoteConnectionModal {
434 fn on_before_dismiss(
435 &mut self,
436 _window: &mut Window,
437 _: &mut Context<Self>,
438 ) -> workspace::DismissDecision {
439 workspace::DismissDecision::Dismiss(self.finished)
440 }
441
442 fn fade_out_background(&self) -> bool {
443 true
444 }
445}
446
447#[derive(Clone)]
448pub struct RemoteClientDelegate {
449 window: AnyWindowHandle,
450 ui: WeakEntity<RemoteConnectionPrompt>,
451 known_password: Option<EncryptedPassword>,
452}
453
454impl remote::RemoteClientDelegate for RemoteClientDelegate {
455 fn ask_password(
456 &self,
457 prompt: String,
458 tx: oneshot::Sender<EncryptedPassword>,
459 cx: &mut AsyncApp,
460 ) {
461 let mut known_password = self.known_password.clone();
462 if let Some(password) = known_password.take() {
463 tx.send(password).ok();
464 } else {
465 self.window
466 .update(cx, |_, window, cx| {
467 self.ui.update(cx, |modal, cx| {
468 modal.set_prompt(prompt, tx, window, cx);
469 })
470 })
471 .ok();
472 }
473 }
474
475 fn set_status(&self, status: Option<&str>, cx: &mut AsyncApp) {
476 self.update_status(status, cx)
477 }
478
479 fn download_server_binary_locally(
480 &self,
481 platform: RemotePlatform,
482 release_channel: ReleaseChannel,
483 version: Option<SemanticVersion>,
484 cx: &mut AsyncApp,
485 ) -> Task<anyhow::Result<PathBuf>> {
486 let this = self.clone();
487 cx.spawn(async move |cx| {
488 AutoUpdater::download_remote_server_release(
489 release_channel,
490 version,
491 platform.os,
492 platform.arch,
493 move |status, cx| this.set_status(Some(status), cx),
494 cx,
495 )
496 .await
497 .with_context(|| {
498 format!(
499 "Downloading remote server binary (version: {}, os: {}, arch: {})",
500 version
501 .map(|v| format!("{}", v))
502 .unwrap_or("unknown".to_string()),
503 platform.os,
504 platform.arch,
505 )
506 })
507 })
508 }
509
510 fn get_download_url(
511 &self,
512 platform: RemotePlatform,
513 release_channel: ReleaseChannel,
514 version: Option<SemanticVersion>,
515 cx: &mut AsyncApp,
516 ) -> Task<Result<Option<String>>> {
517 cx.spawn(async move |cx| {
518 AutoUpdater::get_remote_server_release_url(
519 release_channel,
520 version,
521 platform.os,
522 platform.arch,
523 cx,
524 )
525 .await
526 })
527 }
528}
529
530impl RemoteClientDelegate {
531 fn update_status(&self, status: Option<&str>, cx: &mut AsyncApp) {
532 self.window
533 .update(cx, |_, _, cx| {
534 self.ui.update(cx, |modal, cx| {
535 modal.set_status(status.map(|s| s.to_string()), cx);
536 })
537 })
538 .ok();
539 }
540}
541
542pub fn connect(
543 unique_identifier: ConnectionIdentifier,
544 connection_options: RemoteConnectionOptions,
545 ui: Entity<RemoteConnectionPrompt>,
546 window: &mut Window,
547 cx: &mut App,
548) -> Task<Result<Option<Entity<RemoteClient>>>> {
549 let window = window.window_handle();
550 let known_password = match &connection_options {
551 RemoteConnectionOptions::Ssh(ssh_connection_options) => ssh_connection_options
552 .password
553 .as_deref()
554 .and_then(|pw| pw.try_into().ok()),
555 _ => None,
556 };
557 let (tx, rx) = oneshot::channel();
558 ui.update(cx, |ui, _cx| ui.set_cancellation_tx(tx));
559
560 let delegate = Arc::new(RemoteClientDelegate {
561 window,
562 ui: ui.downgrade(),
563 known_password,
564 });
565
566 cx.spawn(async move |cx| {
567 let connection = remote::connect(connection_options, delegate.clone(), cx).await?;
568 cx.update(|cx| remote::RemoteClient::new(unique_identifier, connection, rx, delegate, cx))?
569 .await
570 })
571}
572
573pub async fn open_remote_project(
574 connection_options: RemoteConnectionOptions,
575 paths: Vec<PathBuf>,
576 app_state: Arc<AppState>,
577 open_options: workspace::OpenOptions,
578 cx: &mut AsyncApp,
579) -> Result<()> {
580 let created_new_window = open_options.replace_window.is_none();
581 let window = if let Some(window) = open_options.replace_window {
582 window
583 } else {
584 let workspace_position = cx
585 .update(|cx| {
586 // todo: These paths are wrong they may have column and line information
587 workspace::remote_workspace_position_from_db(connection_options.clone(), &paths, cx)
588 })?
589 .await
590 .context("fetching ssh workspace position from db")?;
591
592 let mut options =
593 cx.update(|cx| (app_state.build_window_options)(workspace_position.display, cx))?;
594 options.window_bounds = workspace_position.window_bounds;
595
596 cx.open_window(options, |window, cx| {
597 let project = project::Project::local(
598 app_state.client.clone(),
599 app_state.node_runtime.clone(),
600 app_state.user_store.clone(),
601 app_state.languages.clone(),
602 app_state.fs.clone(),
603 None,
604 cx,
605 );
606 cx.new(|cx| {
607 let mut workspace = Workspace::new(None, project, app_state.clone(), window, cx);
608 workspace.centered_layout = workspace_position.centered_layout;
609 workspace
610 })
611 })?
612 };
613
614 loop {
615 let (cancel_tx, cancel_rx) = oneshot::channel();
616 let delegate = window.update(cx, {
617 let paths = paths.clone();
618 let connection_options = connection_options.clone();
619 move |workspace, window, cx| {
620 window.activate_window();
621 workspace.toggle_modal(window, cx, |window, cx| {
622 RemoteConnectionModal::new(&connection_options, paths, window, cx)
623 });
624
625 let ui = workspace
626 .active_modal::<RemoteConnectionModal>(cx)?
627 .read(cx)
628 .prompt
629 .clone();
630
631 ui.update(cx, |ui, _cx| {
632 ui.set_cancellation_tx(cancel_tx);
633 });
634
635 Some(Arc::new(RemoteClientDelegate {
636 window: window.window_handle(),
637 ui: ui.downgrade(),
638 known_password: if let RemoteConnectionOptions::Ssh(options) =
639 &connection_options
640 {
641 options
642 .password
643 .as_deref()
644 .and_then(|pw| EncryptedPassword::try_from(pw).ok())
645 } else {
646 None
647 },
648 }))
649 }
650 })?;
651
652 let Some(delegate) = delegate else { break };
653
654 let remote_connection =
655 match remote::connect(connection_options.clone(), delegate.clone(), cx).await {
656 Ok(connection) => connection,
657 Err(e) => {
658 window
659 .update(cx, |workspace, _, cx| {
660 if let Some(ui) = workspace.active_modal::<RemoteConnectionModal>(cx) {
661 ui.update(cx, |modal, cx| modal.finished(cx))
662 }
663 })
664 .ok();
665 log::error!("Failed to open project: {e:#}");
666 let response = window
667 .update(cx, |_, window, cx| {
668 window.prompt(
669 PromptLevel::Critical,
670 match connection_options {
671 RemoteConnectionOptions::Ssh(_) => "Failed to connect over SSH",
672 RemoteConnectionOptions::Wsl(_) => "Failed to connect to WSL",
673 },
674 Some(&format!("{e:#}")),
675 &["Retry", "Cancel"],
676 cx,
677 )
678 })?
679 .await;
680
681 if response == Ok(0) {
682 continue;
683 }
684
685 if created_new_window {
686 window
687 .update(cx, |_, window, _| window.remove_window())
688 .ok();
689 }
690 break;
691 }
692 };
693
694 let (paths, paths_with_positions) =
695 determine_paths_with_positions(&remote_connection, paths.clone()).await;
696
697 let opened_items = cx
698 .update(|cx| {
699 workspace::open_remote_project_with_new_connection(
700 window,
701 remote_connection,
702 cancel_rx,
703 delegate.clone(),
704 app_state.clone(),
705 paths.clone(),
706 cx,
707 )
708 })?
709 .await;
710
711 window
712 .update(cx, |workspace, _, cx| {
713 if let Some(ui) = workspace.active_modal::<RemoteConnectionModal>(cx) {
714 ui.update(cx, |modal, cx| modal.finished(cx))
715 }
716 })
717 .ok();
718
719 match opened_items {
720 Err(e) => {
721 log::error!("Failed to open project: {e:#}");
722 let response = window
723 .update(cx, |_, window, cx| {
724 window.prompt(
725 PromptLevel::Critical,
726 match connection_options {
727 RemoteConnectionOptions::Ssh(_) => "Failed to connect over SSH",
728 RemoteConnectionOptions::Wsl(_) => "Failed to connect to WSL",
729 },
730 Some(&format!("{e:#}")),
731 &["Retry", "Cancel"],
732 cx,
733 )
734 })?
735 .await;
736 if response == Ok(0) {
737 continue;
738 }
739
740 if created_new_window {
741 window
742 .update(cx, |_, window, _| window.remove_window())
743 .ok();
744 }
745 }
746
747 Ok(items) => {
748 for (item, path) in items.into_iter().zip(paths_with_positions) {
749 let Some(item) = item else {
750 continue;
751 };
752 let Some(row) = path.row else {
753 continue;
754 };
755 if let Some(active_editor) = item.downcast::<Editor>() {
756 window
757 .update(cx, |_, window, cx| {
758 active_editor.update(cx, |editor, cx| {
759 let row = row.saturating_sub(1);
760 let col = path.column.unwrap_or(0).saturating_sub(1);
761 editor.go_to_singleton_buffer_point(
762 Point::new(row, col),
763 window,
764 cx,
765 );
766 });
767 })
768 .ok();
769 }
770 }
771 }
772 }
773
774 window
775 .update(cx, |workspace, _, cx| {
776 if let Some(client) = workspace.project().read(cx).remote_client() {
777 ExtensionStore::global(cx)
778 .update(cx, |store, cx| store.register_remote_client(client, cx));
779 }
780 })
781 .ok();
782
783 break;
784 }
785
786 // Already showed the error to the user
787 Ok(())
788}
789
790pub(crate) async fn determine_paths_with_positions(
791 remote_connection: &Arc<dyn RemoteConnection>,
792 mut paths: Vec<PathBuf>,
793) -> (Vec<PathBuf>, Vec<PathWithPosition>) {
794 let mut paths_with_positions = Vec::<PathWithPosition>::new();
795 for path in &mut paths {
796 if let Some(path_str) = path.to_str() {
797 let path_with_position = PathWithPosition::parse_str(&path_str);
798 if path_with_position.row.is_some() {
799 if !path_exists(&remote_connection, &path).await {
800 *path = path_with_position.path.clone();
801 paths_with_positions.push(path_with_position);
802 continue;
803 }
804 }
805 }
806 paths_with_positions.push(PathWithPosition::from_path(path.clone()))
807 }
808 (paths, paths_with_positions)
809}
810
811async fn path_exists(connection: &Arc<dyn RemoteConnection>, path: &Path) -> bool {
812 let Ok(command) = connection.build_command(
813 Some("test".to_string()),
814 &["-e".to_owned(), path.to_string_lossy().to_string()],
815 &Default::default(),
816 None,
817 None,
818 ) else {
819 return false;
820 };
821 let Ok(mut child) = util::command::new_smol_command(command.program)
822 .args(command.args)
823 .envs(command.env)
824 .spawn()
825 else {
826 return false;
827 };
828 child.status().await.is_ok_and(|status| status.success())
829}