remote: Support line and column numbers for remote paths (#40410)

Closes #40297
Closes https://github.com/zed-industries/zed/issues/40367

Release Notes:

- Improved line and column number handling for paths in remotes

---------

Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Lukas Wirth
2025-10-17 10:50:22 +00:00
committed by GitHub
co-authored by Max Brunsfeld
parent 45a0d08535
commit ca1f843a0b
9 changed files with 238 additions and 126 deletions
+112 -63
View File
@@ -1,4 +1,7 @@
use std::{path::PathBuf, sync::Arc};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use anyhow::{Context as _, Result};
use askpass::EncryptedPassword;
@@ -12,11 +15,11 @@ use gpui::{
TextStyleRefinement, WeakEntity,
};
use language::CursorShape;
use language::{CursorShape, Point};
use markdown::{Markdown, MarkdownElement, MarkdownStyle};
use release_channel::ReleaseChannel;
use remote::{
ConnectionIdentifier, RemoteClient, RemoteConnectionOptions, RemotePlatform,
ConnectionIdentifier, RemoteClient, RemoteConnection, RemoteConnectionOptions, RemotePlatform,
SshConnectionOptions,
};
pub use settings::SshConnection;
@@ -26,6 +29,7 @@ use ui::{
ActiveTheme, Color, CommonAnimationExt, Context, Icon, IconName, IconSize, InteractiveElement,
IntoElement, Label, LabelCommon, Styled, Window, prelude::*,
};
use util::paths::PathWithPosition;
use workspace::{AppState, ModalView, Workspace};
pub struct SshSettings {
@@ -533,34 +537,6 @@ impl RemoteClientDelegate {
}
}
pub fn connect_over_ssh(
unique_identifier: ConnectionIdentifier,
connection_options: SshConnectionOptions,
ui: Entity<RemoteConnectionPrompt>,
window: &mut Window,
cx: &mut App,
) -> Task<Result<Option<Entity<RemoteClient>>>> {
let window = window.window_handle();
let known_password = connection_options
.password
.as_deref()
.and_then(|pw| EncryptedPassword::try_from(pw).ok());
let (tx, rx) = oneshot::channel();
ui.update(cx, |ui, _cx| ui.set_cancellation_tx(tx));
remote::RemoteClient::ssh(
unique_identifier,
connection_options,
rx,
Arc::new(RemoteClientDelegate {
window,
ui: ui.downgrade(),
known_password,
}),
cx,
)
}
pub fn connect(
unique_identifier: ConnectionIdentifier,
connection_options: RemoteConnectionOptions,
@@ -579,17 +555,17 @@ pub fn connect(
let (tx, rx) = oneshot::channel();
ui.update(cx, |ui, _cx| ui.set_cancellation_tx(tx));
remote::RemoteClient::new(
unique_identifier,
connection_options,
rx,
Arc::new(RemoteClientDelegate {
window,
ui: ui.downgrade(),
known_password,
}),
cx,
)
let delegate = Arc::new(RemoteClientDelegate {
window,
ui: ui.downgrade(),
known_password,
});
cx.spawn(async move |cx| {
let connection = remote::connect(connection_options, delegate.clone(), cx).await?;
cx.update(|cx| remote::RemoteClient::new(unique_identifier, connection, rx, delegate, cx))?
.await
})
}
pub async fn open_remote_project(
@@ -604,6 +580,7 @@ pub async fn open_remote_project(
} else {
let workspace_position = cx
.update(|cx| {
// todo: These paths are wrong they may have column and line information
workspace::remote_workspace_position_from_db(connection_options.clone(), &paths, cx)
})?
.await
@@ -671,11 +648,16 @@ pub async fn open_remote_project(
let Some(delegate) = delegate else { break };
let did_open_project = cx
let remote_connection =
remote::connect(connection_options.clone(), delegate.clone(), cx).await?;
let (paths, paths_with_positions) =
determine_paths_with_positions(&remote_connection, paths.clone()).await;
let opened_items = cx
.update(|cx| {
workspace::open_remote_project_with_new_connection(
window,
connection_options.clone(),
remote_connection,
cancel_rx,
delegate.clone(),
app_state.clone(),
@@ -693,25 +675,51 @@ pub async fn open_remote_project(
})
.ok();
if let Err(e) = did_open_project {
log::error!("Failed to open project: {e:?}");
let response = window
.update(cx, |_, window, cx| {
window.prompt(
PromptLevel::Critical,
match connection_options {
RemoteConnectionOptions::Ssh(_) => "Failed to connect over SSH",
RemoteConnectionOptions::Wsl(_) => "Failed to connect to WSL",
},
Some(&e.to_string()),
&["Retry", "Ok"],
cx,
)
})?
.await;
if response == Ok(0) {
continue;
match opened_items {
Err(e) => {
log::error!("Failed to open project: {e:?}");
let response = window
.update(cx, |_, window, cx| {
window.prompt(
PromptLevel::Critical,
match connection_options {
RemoteConnectionOptions::Ssh(_) => "Failed to connect over SSH",
RemoteConnectionOptions::Wsl(_) => "Failed to connect to WSL",
},
Some(&e.to_string()),
&["Retry", "Ok"],
cx,
)
})?
.await;
if response == Ok(0) {
continue;
}
}
Ok(items) => {
for (item, path) in items.into_iter().zip(paths_with_positions) {
let Some(item) = item else {
continue;
};
let Some(row) = path.row else {
continue;
};
if let Some(active_editor) = item.downcast::<Editor>() {
window
.update(cx, |_, window, cx| {
active_editor.update(cx, |editor, cx| {
let row = row.saturating_sub(1);
let col = path.column.unwrap_or(0).saturating_sub(1);
editor.go_to_singleton_buffer_point(
Point::new(row, col),
window,
cx,
);
});
})
.ok();
}
}
}
}
@@ -730,3 +738,44 @@ pub async fn open_remote_project(
// Already showed the error to the user
Ok(())
}
pub(crate) async fn determine_paths_with_positions(
remote_connection: &Arc<dyn RemoteConnection>,
mut paths: Vec<PathBuf>,
) -> (Vec<PathBuf>, Vec<PathWithPosition>) {
let mut paths_with_positions = Vec::<PathWithPosition>::new();
for path in &mut paths {
if let Some(path_str) = path.to_str() {
let path_with_position = PathWithPosition::parse_str(&path_str);
if path_with_position.row.is_some() {
if !path_exists(&remote_connection, &path).await {
*path = path_with_position.path.clone();
paths_with_positions.push(path_with_position);
continue;
}
}
}
paths_with_positions.push(PathWithPosition::from_path(path.clone()))
}
(paths, paths_with_positions)
}
async fn path_exists(connection: &Arc<dyn RemoteConnection>, path: &Path) -> bool {
let Ok(command) = connection.build_command(
Some("test".to_string()),
&["-e".to_owned(), path.to_string_lossy().to_string()],
&Default::default(),
None,
None,
) else {
return false;
};
let Ok(mut child) = util::command::new_smol_command(command.program)
.args(command.args)
.envs(command.env)
.spawn()
else {
return false;
};
child.status().await.is_ok_and(|status| status.success())
}
+41 -4
View File
@@ -1,7 +1,8 @@
use crate::{
remote_connections::{
Connection, RemoteConnectionModal, RemoteConnectionPrompt, SshConnection,
SshConnectionHeader, SshSettings, connect, connect_over_ssh, open_remote_project,
SshConnectionHeader, SshSettings, connect, determine_paths_with_positions,
open_remote_project,
},
ssh_config::parse_ssh_config_hosts,
};
@@ -13,6 +14,7 @@ use gpui::{
FocusHandle, Focusable, PromptLevel, ScrollHandle, Subscription, Task, WeakEntity, Window,
canvas,
};
use language::Point;
use log::info;
use paths::{global_ssh_config_file, user_ssh_config_file};
use picker::Picker;
@@ -233,6 +235,15 @@ impl ProjectPicker {
.read_with(cx, |workspace, _| workspace.app_state().clone())
.ok()?;
let remote_connection = project
.read_with(cx, |project, cx| {
project.remote_client()?.read(cx).connection()
})
.ok()??;
let (paths, paths_with_positions) =
determine_paths_with_positions(&remote_connection, paths).await;
cx.update(|_, cx| {
let fs = app_state.fs.clone();
update_settings_file(fs, cx, {
@@ -278,12 +289,38 @@ impl ProjectPicker {
})
.log_err()?;
open_remote_project_with_existing_connection(
let items = open_remote_project_with_existing_connection(
connection, project, paths, app_state, window, cx,
)
.await
.log_err();
if let Some(items) = items {
for (item, path) in items.into_iter().zip(paths_with_positions) {
let Some(item) = item else {
continue;
};
let Some(row) = path.row else {
continue;
};
if let Some(active_editor) = item.downcast::<Editor>() {
window
.update(cx, |_, window, cx| {
active_editor.update(cx, |editor, cx| {
let row = row.saturating_sub(1);
let col = path.column.unwrap_or(0).saturating_sub(1);
editor.go_to_singleton_buffer_point(
Point::new(row, col),
window,
cx,
);
});
})
.ok();
}
}
}
this.update(cx, |_, cx| {
cx.emit(DismissEvent);
})
@@ -671,9 +708,9 @@ impl RemoteServerProjects {
)
});
let connection = connect_over_ssh(
let connection = connect(
ConnectionIdentifier::setup(),
connection_options.clone(),
RemoteConnectionOptions::Ssh(connection_options.clone()),
ssh_prompt.clone(),
window,
cx,