open new buffer (#11203)

Release Notes:

- Allow creating new untitled buffers in remote projects

TODO:
- Add a Test
- Fix version number check
This commit is contained in:
Conrad Irwin 2024-04-30 16:09:43 -06:00 committed by GitHub
parent 28bcc95468
commit 3752ed294d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 276 additions and 111 deletions

View file

@ -74,6 +74,8 @@ use tracing::{
};
use util::http::IsahcHttpClient;
use self::connection_pool::VersionedMessage;
pub const RECONNECT_TIMEOUT: Duration = Duration::from_secs(30);
// kubernetes gives terminated pods 10s to shutdown gracefully. After they're gone, we can clean up old resources.
@ -465,6 +467,9 @@ impl Server {
.add_request_handler(user_handler(
forward_mutating_project_request::<proto::ApplyCompletionAdditionalEdits>,
))
.add_request_handler(user_handler(
forward_versioned_mutating_project_request::<proto::OpenNewBuffer>,
))
.add_request_handler(user_handler(
forward_mutating_project_request::<proto::ResolveCompletionDocumentation>,
))
@ -505,7 +510,7 @@ impl Server {
forward_mutating_project_request::<proto::OnTypeFormatting>,
))
.add_request_handler(user_handler(
forward_mutating_project_request::<proto::SaveBuffer>,
forward_versioned_mutating_project_request::<proto::SaveBuffer>,
))
.add_request_handler(user_handler(
forward_mutating_project_request::<proto::BlameBuffer>,
@ -2677,6 +2682,7 @@ where
T: EntityMessage + RequestMessage,
{
let project_id = ProjectId::from_proto(request.remote_entity_id());
let host_connection_id = session
.db()
.await
@ -2690,6 +2696,45 @@ where
Ok(())
}
/// forward a project request to the host. These requests are disallowed
/// for guests.
async fn forward_versioned_mutating_project_request<T>(
request: T,
response: Response<T>,
session: UserSession,
) -> Result<()>
where
T: EntityMessage + RequestMessage + VersionedMessage,
{
let project_id = ProjectId::from_proto(request.remote_entity_id());
let host_connection_id = session
.db()
.await
.host_for_mutating_project_request(project_id, session.connection_id, session.user_id())
.await?;
if let Some(host_version) = session
.connection_pool()
.await
.connection(host_connection_id)
.map(|c| c.zed_version)
{
if let Some(min_required_version) = request.required_host_version() {
if min_required_version > host_version {
return Err(anyhow!(ErrorCode::RemoteUpgradeRequired
.with_tag("required", &min_required_version.to_string())))?;
}
}
}
let payload = session
.peer
.forward_request(session.connection_id, host_connection_id, request)
.await?;
response.send(payload)?;
Ok(())
}
/// Notify other participants that a new buffer has been created
async fn create_buffer_for_peer(
request: proto::CreateBufferForPeer,

View file

@ -21,7 +21,7 @@ struct ConnectedPrincipal {
connection_ids: HashSet<ConnectionId>,
}
#[derive(Debug, Serialize)]
#[derive(Copy, Clone, Debug, Serialize, PartialOrd, PartialEq, Eq, Ord)]
pub struct ZedVersion(pub SemanticVersion);
impl fmt::Display for ZedVersion {
@ -34,6 +34,32 @@ impl ZedVersion {
pub fn can_collaborate(&self) -> bool {
self.0 >= SemanticVersion::new(0, 129, 2)
}
pub fn with_save_as() -> ZedVersion {
ZedVersion(SemanticVersion::new(0, 134, 0))
}
}
pub trait VersionedMessage {
fn required_host_version(&self) -> Option<ZedVersion> {
None
}
}
impl VersionedMessage for proto::SaveBuffer {
fn required_host_version(&self) -> Option<ZedVersion> {
if self.new_path.is_some() {
Some(ZedVersion::with_save_as())
} else {
None
}
}
}
impl VersionedMessage for proto::OpenNewBuffer {
fn required_host_version(&self) -> Option<ZedVersion> {
Some(ZedVersion::with_save_as())
}
}
#[derive(Serialize)]
@ -50,6 +76,10 @@ impl ConnectionPool {
self.channels.clear();
}
pub fn connection(&mut self, connection_id: ConnectionId) -> Option<&Connection> {
self.connections.get(&connection_id)
}
#[instrument(skip(self))]
pub fn add_connection(
&mut self,

View file

@ -398,3 +398,33 @@ async fn test_save_as_remote(cx1: &mut gpui::TestAppContext, cx2: &mut gpui::Tes
"remote\nremote\nremote"
);
}
#[gpui::test]
async fn test_new_file_remote(cx1: &mut gpui::TestAppContext, cx2: &mut gpui::TestAppContext) {
let (server, client1) = TestServer::start1(cx1).await;
// Creating a project with a path that does exist should not fail
let (dev_server, remote_workspace) =
create_remote_project(&server, client1.app_state.clone(), cx1, cx2).await;
let mut cx = VisualTestContext::from_window(remote_workspace.into(), cx1);
cx.simulate_keystrokes("cmd-n");
cx.simulate_input("new!");
cx.simulate_keystrokes("cmd-shift-s");
cx.simulate_input("2.txt");
cx.simulate_keystrokes("enter");
cx.executor().run_until_parked();
let title = remote_workspace
.update(&mut cx, |ws, cx| {
ws.active_item(cx).unwrap().tab_description(0, &cx).unwrap()
})
.unwrap();
assert_eq!(title, "2.txt");
let path = Path::new("/remote/2.txt");
assert_eq!(dev_server.fs().load(&path).await.unwrap(), "new!");
}

View file

@ -2450,7 +2450,8 @@ async fn test_propagate_saves_and_fs_changes(
});
let new_buffer_a = project_a
.update(cx_a, |p, cx| p.create_buffer("", None, cx))
.update(cx_a, |p, cx| p.create_buffer(cx))
.await
.unwrap();
let new_buffer_id = new_buffer_a.read_with(cx_a, |buffer, _| buffer.remote_id());