From da13d028a3963928bad6d574f327ebe5a3ecbc7e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sat, 22 Jan 2022 22:19:04 -0700 Subject: [PATCH] Send File protos as part of Buffer protos Use the File proto to build the File associated with the buffer rather than relying on the local entry. --- crates/language/src/buffer.rs | 3 ++ crates/project/src/worktree.rs | 58 ++++++++++++++++------ crates/rpc/proto/zed.proto | 90 ++++++++++++++++++---------------- crates/rpc/src/proto.rs | 1 + 4 files changed, 95 insertions(+), 57 deletions(-) diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 9ff432ae8675b11c1050e3b3d1326e4af5d8e13c..1d3b8a8e6df267051e2a24d22eb0defbf4ec13c0 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -189,6 +189,8 @@ pub trait File { fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext); fn as_any(&self) -> &dyn Any; + + fn to_proto(&self) -> rpc::proto::File; } pub trait LocalFile: File { @@ -352,6 +354,7 @@ impl Buffer { pub fn to_proto(&self) -> proto::Buffer { proto::Buffer { id: self.remote_id(), + file: self.file.as_ref().map(|f| f.to_proto()), visible_text: self.text.text(), deleted_text: self.text.deleted_text(), undo_map: self diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index 4fb359ba1bc6c7e52da7ddadd11ddffafe30660c..ff3cb1ea82cfc9f171167dc1f46a17c76536a32c 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -840,11 +840,6 @@ impl RemoteWorktree { let path: Arc = Arc::from(path); let path_string = path.to_string_lossy().to_string(); cx.spawn_weak(move |this, mut cx| async move { - let entry = this - .upgrade(&cx) - .ok_or_else(|| anyhow!("worktree was closed"))? - .read_with(&cx, |tree, _| tree.entry_for_path(&path).cloned()) - .ok_or_else(|| anyhow!("file does not exist"))?; let response = rpc .request(proto::OpenBuffer { project_id, @@ -856,17 +851,15 @@ impl RemoteWorktree { let this = this .upgrade(&cx) .ok_or_else(|| anyhow!("worktree was closed"))?; - let file = File { - entry_id: Some(entry.id), - worktree: this.clone(), - path: entry.path, - mtime: entry.mtime, - is_local: false, - }; - let remote_buffer = response.buffer.ok_or_else(|| anyhow!("empty buffer"))?; - Ok(cx.add_model(|cx| { - Buffer::from_proto(replica_id, remote_buffer, Some(Box::new(file)), cx).unwrap() - })) + let mut remote_buffer = response.buffer.ok_or_else(|| anyhow!("empty buffer"))?; + let file = remote_buffer + .file + .take() + .map(|proto| cx.read(|cx| File::from_proto(proto, this.clone(), cx))) + .transpose()? + .map(|file| Box::new(file) as Box); + + Ok(cx.add_model(|cx| Buffer::from_proto(replica_id, remote_buffer, file, cx).unwrap())) }) } @@ -1499,6 +1492,15 @@ impl language::File for File { fn as_any(&self) -> &dyn Any { self } + + fn to_proto(&self) -> rpc::proto::File { + rpc::proto::File { + worktree_id: self.worktree.id() as u64, + entry_id: self.entry_id.map(|entry_id| entry_id as u64), + path: self.path.to_string_lossy().into(), + mtime: Some(self.mtime.into()), + } + } } impl language::LocalFile for File { @@ -1521,6 +1523,30 @@ impl language::LocalFile for File { } impl File { + pub fn from_proto( + proto: rpc::proto::File, + worktree: ModelHandle, + cx: &AppContext, + ) -> Result { + let worktree_id = worktree + .read(cx) + .as_remote() + .ok_or_else(|| anyhow!("not remote"))? + .id(); + + if worktree_id.to_proto() != proto.worktree_id { + return Err(anyhow!("worktree id does not match file")); + } + + Ok(Self { + worktree, + path: Path::new(&proto.path).into(), + mtime: proto.mtime.ok_or_else(|| anyhow!("no timestamp"))?.into(), + entry_id: proto.entry_id.map(|entry_id| entry_id as usize), + is_local: false, + }) + } + pub fn from_dyn(file: Option<&dyn language::File>) -> Option<&Self> { file.and_then(|f| f.as_any().downcast_ref()) } diff --git a/crates/rpc/proto/zed.proto b/crates/rpc/proto/zed.proto index 21df9ec8ce70aa0701ed2c949e1ddc55b73a7012..26fa227dde05f59dbad537273e6d11dde7825bdf 100644 --- a/crates/rpc/proto/zed.proto +++ b/crates/rpc/proto/zed.proto @@ -33,25 +33,26 @@ message Envelope { OpenBufferResponse open_buffer_response = 25; CloseBuffer close_buffer = 26; UpdateBuffer update_buffer = 27; - SaveBuffer save_buffer = 28; - BufferSaved buffer_saved = 29; - FormatBuffer format_buffer = 30; - - GetChannels get_channels = 31; - GetChannelsResponse get_channels_response = 32; - JoinChannel join_channel = 33; - JoinChannelResponse join_channel_response = 34; - LeaveChannel leave_channel = 35; - SendChannelMessage send_channel_message = 36; - SendChannelMessageResponse send_channel_message_response = 37; - ChannelMessageSent channel_message_sent = 38; - GetChannelMessages get_channel_messages = 39; - GetChannelMessagesResponse get_channel_messages_response = 40; - - UpdateContacts update_contacts = 41; - - GetUsers get_users = 42; - GetUsersResponse get_users_response = 43; + UpdateBufferFile update_buffer_file = 28; + SaveBuffer save_buffer = 29; + BufferSaved buffer_saved = 30; + FormatBuffer format_buffer = 31; + + GetChannels get_channels = 32; + GetChannelsResponse get_channels_response = 33; + JoinChannel join_channel = 34; + JoinChannelResponse join_channel_response = 35; + LeaveChannel leave_channel = 36; + SendChannelMessage send_channel_message = 37; + SendChannelMessageResponse send_channel_message_response = 38; + ChannelMessageSent channel_message_sent = 39; + GetChannelMessages get_channel_messages = 40; + GetChannelMessagesResponse get_channel_messages_response = 41; + + UpdateContacts update_contacts = 42; + + GetUsers get_users = 43; + GetUsersResponse get_users_response = 44; } } @@ -88,9 +89,9 @@ message JoinProject { } message JoinProjectResponse { - uint32 replica_id = 2; - repeated Worktree worktrees = 3; - repeated Collaborator collaborators = 4; + uint32 replica_id = 1; + repeated Worktree worktrees = 2; + repeated Collaborator collaborators = 3; } message LeaveProject { @@ -150,7 +151,13 @@ message CloseBuffer { message UpdateBuffer { uint64 project_id = 1; uint64 buffer_id = 2; - repeated Operation operations = 4; + repeated Operation operations = 3; +} + +message UpdateBufferFile { + uint64 project_id = 1; + uint64 buffer_id = 2; + File file = 3; } message SaveBuffer { @@ -177,11 +184,11 @@ message UpdateDiagnosticSummary { } message DiagnosticSummary { - string path = 3; - uint32 error_count = 4; - uint32 warning_count = 5; - uint32 info_count = 6; - uint32 hint_count = 7; + string path = 1; + uint32 error_count = 2; + uint32 warning_count = 3; + uint32 info_count = 4; + uint32 hint_count = 5; } message DiskBasedDiagnosticsUpdating { @@ -272,7 +279,7 @@ message Worktree { message File { uint64 worktree_id = 1; - uint64 entry_id = 2; + optional uint64 entry_id = 2; string path = 3; Timestamp mtime = 4; } @@ -289,15 +296,16 @@ message Entry { message Buffer { uint64 id = 1; - string visible_text = 2; - string deleted_text = 3; - repeated BufferFragment fragments = 4; - repeated UndoMapEntry undo_map = 5; - repeated VectorClockEntry version = 6; - repeated SelectionSet selections = 7; - repeated Diagnostic diagnostics = 8; - uint32 lamport_timestamp = 9; - repeated Operation deferred_operations = 10; + optional File file = 2; + string visible_text = 3; + string deleted_text = 4; + repeated BufferFragment fragments = 5; + repeated UndoMapEntry undo_map = 6; + repeated VectorClockEntry version = 7; + repeated SelectionSet selections = 8; + repeated Diagnostic diagnostics = 9; + uint32 lamport_timestamp = 10; + repeated Operation deferred_operations = 11; } message BufferFragment { @@ -306,7 +314,7 @@ message BufferFragment { uint32 lamport_timestamp = 3; uint32 insertion_offset = 4; uint32 len = 5; - bool visible = 6; + bool visible = 6; repeated VectorClockEntry deletions = 7; repeated VectorClockEntry max_undos = 8; } @@ -390,8 +398,8 @@ message Operation { message UpdateSelections { uint32 replica_id = 1; - uint32 lamport_timestamp = 3; - repeated Selection selections = 4; + uint32 lamport_timestamp = 2; + repeated Selection selections = 3; } } diff --git a/crates/rpc/src/proto.rs b/crates/rpc/src/proto.rs index 8860bc5f0549b0a4341e5fe85526c299a5f1fa24..0b27ca7e3697e49dbc21c98ed6089b0478d7dd3f 100644 --- a/crates/rpc/src/proto.rs +++ b/crates/rpc/src/proto.rs @@ -157,6 +157,7 @@ messages!( UnregisterWorktree, UnshareProject, UpdateBuffer, + UpdateBufferFile, UpdateContacts, UpdateDiagnosticSummary, UpdateWorktree,