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, SharedString, Task, TextStyleRefinement, WeakEntity,
15};
16
17use language::{CursorShape, Point};
18use markdown::{Markdown, MarkdownElement, MarkdownStyle};
19use release_channel::ReleaseChannel;
20use remote::{
21 ConnectionIdentifier, RemoteClient, RemoteConnection, RemoteConnectionOptions, RemotePlatform,
22 SshConnectionOptions,
23};
24use semver::Version;
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<Version>,
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.clone(),
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 .as_ref()
502 .map(|v| format!("{}", v))
503 .unwrap_or("unknown".to_string()),
504 platform.os,
505 platform.arch,
506 )
507 })
508 })
509 }
510
511 fn get_download_url(
512 &self,
513 platform: RemotePlatform,
514 release_channel: ReleaseChannel,
515 version: Option<Version>,
516 cx: &mut AsyncApp,
517 ) -> Task<Result<Option<String>>> {
518 cx.spawn(async move |cx| {
519 AutoUpdater::get_remote_server_release_url(
520 release_channel,
521 version,
522 platform.os,
523 platform.arch,
524 cx,
525 )
526 .await
527 })
528 }
529}
530
531impl RemoteClientDelegate {
532 fn update_status(&self, status: Option<&str>, cx: &mut AsyncApp) {
533 self.window
534 .update(cx, |_, _, cx| {
535 self.ui.update(cx, |modal, cx| {
536 modal.set_status(status.map(|s| s.to_string()), cx);
537 })
538 })
539 .ok();
540 }
541}
542
543pub fn connect(
544 unique_identifier: ConnectionIdentifier,
545 connection_options: RemoteConnectionOptions,
546 ui: Entity<RemoteConnectionPrompt>,
547 window: &mut Window,
548 cx: &mut App,
549) -> Task<Result<Option<Entity<RemoteClient>>>> {
550 let window = window.window_handle();
551 let known_password = match &connection_options {
552 RemoteConnectionOptions::Ssh(ssh_connection_options) => ssh_connection_options
553 .password
554 .as_deref()
555 .and_then(|pw| pw.try_into().ok()),
556 _ => None,
557 };
558 let (tx, rx) = oneshot::channel();
559 ui.update(cx, |ui, _cx| ui.set_cancellation_tx(tx));
560
561 let delegate = Arc::new(RemoteClientDelegate {
562 window,
563 ui: ui.downgrade(),
564 known_password,
565 });
566
567 cx.spawn(async move |cx| {
568 let connection = remote::connect(connection_options, delegate.clone(), cx).await?;
569 cx.update(|cx| remote::RemoteClient::new(unique_identifier, connection, rx, delegate, cx))?
570 .await
571 })
572}
573
574pub async fn open_remote_project(
575 connection_options: RemoteConnectionOptions,
576 paths: Vec<PathBuf>,
577 app_state: Arc<AppState>,
578 open_options: workspace::OpenOptions,
579 cx: &mut AsyncApp,
580) -> Result<()> {
581 let created_new_window = open_options.replace_window.is_none();
582 let window = if let Some(window) = open_options.replace_window {
583 window
584 } else {
585 let workspace_position = cx
586 .update(|cx| {
587 // todo: These paths are wrong they may have column and line information
588 workspace::remote_workspace_position_from_db(connection_options.clone(), &paths, cx)
589 })?
590 .await
591 .context("fetching ssh workspace position from db")?;
592
593 let mut options =
594 cx.update(|cx| (app_state.build_window_options)(workspace_position.display, cx))?;
595 options.window_bounds = workspace_position.window_bounds;
596
597 cx.open_window(options, |window, cx| {
598 let project = project::Project::local(
599 app_state.client.clone(),
600 app_state.node_runtime.clone(),
601 app_state.user_store.clone(),
602 app_state.languages.clone(),
603 app_state.fs.clone(),
604 None,
605 cx,
606 );
607 cx.new(|cx| {
608 let mut workspace = Workspace::new(None, project, app_state.clone(), window, cx);
609 workspace.centered_layout = workspace_position.centered_layout;
610 workspace
611 })
612 })?
613 };
614
615 loop {
616 let (cancel_tx, cancel_rx) = oneshot::channel();
617 let delegate = window.update(cx, {
618 let paths = paths.clone();
619 let connection_options = connection_options.clone();
620 move |workspace, window, cx| {
621 window.activate_window();
622 workspace.toggle_modal(window, cx, |window, cx| {
623 RemoteConnectionModal::new(&connection_options, paths, window, cx)
624 });
625
626 let ui = workspace
627 .active_modal::<RemoteConnectionModal>(cx)?
628 .read(cx)
629 .prompt
630 .clone();
631
632 ui.update(cx, |ui, _cx| {
633 ui.set_cancellation_tx(cancel_tx);
634 });
635
636 Some(Arc::new(RemoteClientDelegate {
637 window: window.window_handle(),
638 ui: ui.downgrade(),
639 known_password: if let RemoteConnectionOptions::Ssh(options) =
640 &connection_options
641 {
642 options
643 .password
644 .as_deref()
645 .and_then(|pw| EncryptedPassword::try_from(pw).ok())
646 } else {
647 None
648 },
649 }))
650 }
651 })?;
652
653 let Some(delegate) = delegate else { break };
654
655 let remote_connection =
656 match remote::connect(connection_options.clone(), delegate.clone(), cx).await {
657 Ok(connection) => connection,
658 Err(e) => {
659 window
660 .update(cx, |workspace, _, cx| {
661 if let Some(ui) = workspace.active_modal::<RemoteConnectionModal>(cx) {
662 ui.update(cx, |modal, cx| modal.finished(cx))
663 }
664 })
665 .ok();
666 log::error!("Failed to open project: {e:#}");
667 let response = window
668 .update(cx, |_, window, cx| {
669 window.prompt(
670 PromptLevel::Critical,
671 match connection_options {
672 RemoteConnectionOptions::Ssh(_) => "Failed to connect over SSH",
673 RemoteConnectionOptions::Wsl(_) => "Failed to connect to WSL",
674 },
675 Some(&format!("{e:#}")),
676 &["Retry", "Cancel"],
677 cx,
678 )
679 })?
680 .await;
681
682 if response == Ok(0) {
683 continue;
684 }
685
686 if created_new_window {
687 window
688 .update(cx, |_, window, _| window.remove_window())
689 .ok();
690 }
691 break;
692 }
693 };
694
695 let (paths, paths_with_positions) =
696 determine_paths_with_positions(&remote_connection, paths.clone()).await;
697
698 let opened_items = cx
699 .update(|cx| {
700 workspace::open_remote_project_with_new_connection(
701 window,
702 remote_connection,
703 cancel_rx,
704 delegate.clone(),
705 app_state.clone(),
706 paths.clone(),
707 cx,
708 )
709 })?
710 .await;
711
712 window
713 .update(cx, |workspace, _, cx| {
714 if let Some(ui) = workspace.active_modal::<RemoteConnectionModal>(cx) {
715 ui.update(cx, |modal, cx| modal.finished(cx))
716 }
717 })
718 .ok();
719
720 match opened_items {
721 Err(e) => {
722 log::error!("Failed to open project: {e:#}");
723 let response = window
724 .update(cx, |_, window, cx| {
725 window.prompt(
726 PromptLevel::Critical,
727 match connection_options {
728 RemoteConnectionOptions::Ssh(_) => "Failed to connect over SSH",
729 RemoteConnectionOptions::Wsl(_) => "Failed to connect to WSL",
730 },
731 Some(&format!("{e:#}")),
732 &["Retry", "Cancel"],
733 cx,
734 )
735 })?
736 .await;
737 if response == Ok(0) {
738 continue;
739 }
740
741 if created_new_window {
742 window
743 .update(cx, |_, window, _| window.remove_window())
744 .ok();
745 }
746 }
747
748 Ok(items) => {
749 for (item, path) in items.into_iter().zip(paths_with_positions) {
750 let Some(item) = item else {
751 continue;
752 };
753 let Some(row) = path.row else {
754 continue;
755 };
756 if let Some(active_editor) = item.downcast::<Editor>() {
757 window
758 .update(cx, |_, window, cx| {
759 active_editor.update(cx, |editor, cx| {
760 let row = row.saturating_sub(1);
761 let col = path.column.unwrap_or(0).saturating_sub(1);
762 editor.go_to_singleton_buffer_point(
763 Point::new(row, col),
764 window,
765 cx,
766 );
767 });
768 })
769 .ok();
770 }
771 }
772 }
773 }
774
775 window
776 .update(cx, |workspace, _, cx| {
777 if let Some(client) = workspace.project().read(cx).remote_client() {
778 ExtensionStore::global(cx)
779 .update(cx, |store, cx| store.register_remote_client(client, cx));
780 }
781 })
782 .ok();
783
784 break;
785 }
786
787 // Already showed the error to the user
788 Ok(())
789}
790
791pub(crate) async fn determine_paths_with_positions(
792 remote_connection: &Arc<dyn RemoteConnection>,
793 mut paths: Vec<PathBuf>,
794) -> (Vec<PathBuf>, Vec<PathWithPosition>) {
795 let mut paths_with_positions = Vec::<PathWithPosition>::new();
796 for path in &mut paths {
797 if let Some(path_str) = path.to_str() {
798 let path_with_position = PathWithPosition::parse_str(&path_str);
799 if path_with_position.row.is_some() {
800 if !path_exists(&remote_connection, &path).await {
801 *path = path_with_position.path.clone();
802 paths_with_positions.push(path_with_position);
803 continue;
804 }
805 }
806 }
807 paths_with_positions.push(PathWithPosition::from_path(path.clone()))
808 }
809 (paths, paths_with_positions)
810}
811
812async fn path_exists(connection: &Arc<dyn RemoteConnection>, path: &Path) -> bool {
813 let Ok(command) = connection.build_command(
814 Some("test".to_string()),
815 &["-e".to_owned(), path.to_string_lossy().to_string()],
816 &Default::default(),
817 None,
818 None,
819 ) else {
820 return false;
821 };
822 let Ok(mut child) = util::command::new_smol_command(command.program)
823 .args(command.args)
824 .envs(command.env)
825 .spawn()
826 else {
827 return false;
828 };
829 child.status().await.is_ok_and(|status| status.success())
830}