Merge pull request #2187 from zed-industries/save-untitled-buffer-bugs
Fix newly-discovered bugs in saving untitled buffers
This commit is contained in:
commit
2982a98d1c
8 changed files with 371 additions and 346 deletions
|
@ -2244,7 +2244,7 @@ async fn test_propagate_saves_and_fs_changes(
|
||||||
});
|
});
|
||||||
|
|
||||||
// Edit the buffer as the host and concurrently save as guest B.
|
// Edit the buffer as the host and concurrently save as guest B.
|
||||||
let save_b = buffer_b.update(cx_b, |buf, cx| buf.save(cx));
|
let save_b = project_b.update(cx_b, |project, cx| project.save_buffer(buffer_b.clone(), cx));
|
||||||
buffer_a.update(cx_a, |buf, cx| buf.edit([(0..0, "hi-a, ")], None, cx));
|
buffer_a.update(cx_a, |buf, cx| buf.edit([(0..0, "hi-a, ")], None, cx));
|
||||||
save_b.await.unwrap();
|
save_b.await.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -2313,6 +2313,41 @@ async fn test_propagate_saves_and_fs_changes(
|
||||||
assert_eq!(buffer.file().unwrap().path().to_str(), Some("file1.js"));
|
assert_eq!(buffer.file().unwrap().path().to_str(), Some("file1.js"));
|
||||||
assert_eq!(&*buffer.language().unwrap().name(), "JavaScript");
|
assert_eq!(&*buffer.language().unwrap().name(), "JavaScript");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let new_buffer_a = project_a
|
||||||
|
.update(cx_a, |p, cx| p.create_buffer("", None, cx))
|
||||||
|
.unwrap();
|
||||||
|
let new_buffer_id = new_buffer_a.read_with(cx_a, |buffer, _| buffer.remote_id());
|
||||||
|
let new_buffer_b = project_b
|
||||||
|
.update(cx_b, |p, cx| p.open_buffer_by_id(new_buffer_id, cx))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
new_buffer_b.read_with(cx_b, |buffer, _| {
|
||||||
|
assert!(buffer.file().is_none());
|
||||||
|
});
|
||||||
|
|
||||||
|
new_buffer_a.update(cx_a, |buffer, cx| {
|
||||||
|
buffer.edit([(0..0, "ok")], None, cx);
|
||||||
|
});
|
||||||
|
project_a
|
||||||
|
.update(cx_a, |project, cx| {
|
||||||
|
project.save_buffer_as(new_buffer_a.clone(), "/a/file3.rs".into(), cx)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
deterministic.run_until_parked();
|
||||||
|
new_buffer_b.read_with(cx_b, |buffer_b, _| {
|
||||||
|
assert_eq!(
|
||||||
|
buffer_b.file().unwrap().path().as_ref(),
|
||||||
|
Path::new("file3.rs")
|
||||||
|
);
|
||||||
|
|
||||||
|
new_buffer_a.read_with(cx_a, |buffer_a, _| {
|
||||||
|
assert_eq!(buffer_b.saved_mtime(), buffer_a.saved_mtime());
|
||||||
|
assert_eq!(buffer_b.saved_version(), buffer_a.saved_version());
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[gpui::test(iterations = 10)]
|
#[gpui::test(iterations = 10)]
|
||||||
|
@ -2882,7 +2917,9 @@ async fn test_buffer_conflict_after_save(
|
||||||
assert!(!buf.has_conflict());
|
assert!(!buf.has_conflict());
|
||||||
});
|
});
|
||||||
|
|
||||||
buffer_b.update(cx_b, |buf, cx| buf.save(cx)).await.unwrap();
|
project_b.update(cx_b, |project, cx| project.save_buffer(buffer_b.clone(), cx))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
cx_a.foreground().forbid_parking();
|
cx_a.foreground().forbid_parking();
|
||||||
buffer_b.read_with(cx_b, |buffer_b, _| assert!(!buffer_b.is_dirty()));
|
buffer_b.read_with(cx_b, |buffer_b, _| assert!(!buffer_b.is_dirty()));
|
||||||
buffer_b.read_with(cx_b, |buf, _| {
|
buffer_b.read_with(cx_b, |buf, _| {
|
||||||
|
|
|
@ -1064,15 +1064,16 @@ async fn randomly_query_and_mutate_buffers(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
30..=39 if buffer.read_with(cx, |buffer, _| buffer.is_dirty()) => {
|
30..=39 if buffer.read_with(cx, |buffer, _| buffer.is_dirty()) => {
|
||||||
let (requested_version, save) = buffer.update(cx, |buffer, cx| {
|
let requested_version = buffer.update(cx, |buffer, cx| {
|
||||||
log::info!(
|
log::info!(
|
||||||
"{}: saving buffer {} ({:?})",
|
"{}: saving buffer {} ({:?})",
|
||||||
client.username,
|
client.username,
|
||||||
buffer.remote_id(),
|
buffer.remote_id(),
|
||||||
buffer.file().unwrap().full_path(cx)
|
buffer.file().unwrap().full_path(cx)
|
||||||
);
|
);
|
||||||
(buffer.version(), buffer.save(cx))
|
buffer.version()
|
||||||
});
|
});
|
||||||
|
let save = project.update(cx, |project, cx| project.save_buffer(buffer, cx));
|
||||||
let save = cx.background().spawn(async move {
|
let save = cx.background().spawn(async move {
|
||||||
let (saved_version, _, _) = save
|
let (saved_version, _, _) = save
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -2,12 +2,10 @@ use crate::{
|
||||||
display_map::ToDisplayPoint, link_go_to_definition::hide_link_definition,
|
display_map::ToDisplayPoint, link_go_to_definition::hide_link_definition,
|
||||||
movement::surrounding_word, persistence::DB, scroll::ScrollAnchor, Anchor, Autoscroll, Editor,
|
movement::surrounding_word, persistence::DB, scroll::ScrollAnchor, Anchor, Autoscroll, Editor,
|
||||||
Event, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, NavigationData, ToPoint as _,
|
Event, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, NavigationData, ToPoint as _,
|
||||||
FORMAT_TIMEOUT,
|
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use collections::HashSet;
|
use collections::HashSet;
|
||||||
use futures::future::try_join_all;
|
use futures::future::try_join_all;
|
||||||
use futures::FutureExt;
|
|
||||||
use gpui::{
|
use gpui::{
|
||||||
elements::*, geometry::vector::vec2f, AppContext, Entity, ModelHandle, MutableAppContext,
|
elements::*, geometry::vector::vec2f, AppContext, Entity, ModelHandle, MutableAppContext,
|
||||||
RenderContext, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle,
|
RenderContext, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle,
|
||||||
|
@ -16,7 +14,7 @@ use language::{
|
||||||
proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point,
|
proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point,
|
||||||
SelectionGoal,
|
SelectionGoal,
|
||||||
};
|
};
|
||||||
use project::{FormatTrigger, Item as _, Project, ProjectPath};
|
use project::{Item as _, Project, ProjectPath};
|
||||||
use rpc::proto::{self, update_view};
|
use rpc::proto::{self, update_view};
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
@ -610,32 +608,12 @@ impl Item for Editor {
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) -> Task<Result<()>> {
|
) -> Task<Result<()>> {
|
||||||
self.report_event("save editor", cx);
|
self.report_event("save editor", cx);
|
||||||
|
let format = self.perform_format(project.clone(), cx);
|
||||||
let buffer = self.buffer().clone();
|
let buffers = self.buffer().clone().read(cx).all_buffers();
|
||||||
let buffers = buffer.read(cx).all_buffers();
|
cx.as_mut().spawn(|mut cx| async move {
|
||||||
let mut timeout = cx.background().timer(FORMAT_TIMEOUT).fuse();
|
format.await?;
|
||||||
let format = project.update(cx, |project, cx| {
|
project
|
||||||
project.format(buffers, true, FormatTrigger::Save, cx)
|
.update(&mut cx, |project, cx| project.save_buffers(buffers, cx))
|
||||||
});
|
|
||||||
cx.spawn(|_, mut cx| async move {
|
|
||||||
let transaction = futures::select_biased! {
|
|
||||||
_ = timeout => {
|
|
||||||
log::warn!("timed out waiting for formatting");
|
|
||||||
None
|
|
||||||
}
|
|
||||||
transaction = format.log_err().fuse() => transaction,
|
|
||||||
};
|
|
||||||
|
|
||||||
buffer
|
|
||||||
.update(&mut cx, |buffer, cx| {
|
|
||||||
if let Some(transaction) = transaction {
|
|
||||||
if !buffer.is_singleton() {
|
|
||||||
buffer.push_transaction(&transaction.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer.save(cx)
|
|
||||||
})
|
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
@ -1159,7 +1137,6 @@ fn path_for_file<'a>(
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use gpui::MutableAppContext;
|
use gpui::MutableAppContext;
|
||||||
use language::RopeFingerprint;
|
|
||||||
use std::{
|
use std::{
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
|
@ -1205,17 +1182,6 @@ mod tests {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save(
|
|
||||||
&self,
|
|
||||||
_: u64,
|
|
||||||
_: language::Rope,
|
|
||||||
_: clock::Global,
|
|
||||||
_: project::LineEnding,
|
|
||||||
_: &mut MutableAppContext,
|
|
||||||
) -> gpui::Task<anyhow::Result<(clock::Global, RopeFingerprint, SystemTime)>> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn std::any::Any {
|
fn as_any(&self) -> &dyn std::any::Any {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
mod anchor;
|
mod anchor;
|
||||||
|
|
||||||
pub use anchor::{Anchor, AnchorRangeExt};
|
pub use anchor::{Anchor, AnchorRangeExt};
|
||||||
use anyhow::Result;
|
|
||||||
use clock::ReplicaId;
|
use clock::ReplicaId;
|
||||||
use collections::{BTreeMap, Bound, HashMap, HashSet};
|
use collections::{BTreeMap, Bound, HashMap, HashSet};
|
||||||
use futures::{channel::mpsc, SinkExt};
|
use futures::{channel::mpsc, SinkExt};
|
||||||
|
@ -1288,20 +1287,6 @@ impl MultiBuffer {
|
||||||
.map(|state| state.buffer.clone())
|
.map(|state| state.buffer.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
|
|
||||||
let mut save_tasks = Vec::new();
|
|
||||||
for BufferState { buffer, .. } in self.buffers.borrow().values() {
|
|
||||||
save_tasks.push(buffer.update(cx, |buffer, cx| buffer.save(cx)));
|
|
||||||
}
|
|
||||||
|
|
||||||
cx.spawn(|_, _| async move {
|
|
||||||
for save in save_tasks {
|
|
||||||
save.await?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_completion_trigger<T>(&self, position: T, text: &str, cx: &AppContext) -> bool
|
pub fn is_completion_trigger<T>(&self, position: T, text: &str, cx: &AppContext) -> bool
|
||||||
where
|
where
|
||||||
T: ToOffset,
|
T: ToOffset,
|
||||||
|
|
|
@ -214,15 +214,6 @@ pub trait File: Send + Sync {
|
||||||
|
|
||||||
fn is_deleted(&self) -> bool;
|
fn is_deleted(&self) -> bool;
|
||||||
|
|
||||||
fn save(
|
|
||||||
&self,
|
|
||||||
buffer_id: u64,
|
|
||||||
text: Rope,
|
|
||||||
version: clock::Global,
|
|
||||||
line_ending: LineEnding,
|
|
||||||
cx: &mut MutableAppContext,
|
|
||||||
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>>;
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn Any;
|
fn as_any(&self) -> &dyn Any;
|
||||||
|
|
||||||
fn to_proto(&self) -> rpc::proto::File;
|
fn to_proto(&self) -> rpc::proto::File;
|
||||||
|
@ -529,33 +520,6 @@ impl Buffer {
|
||||||
self.file.as_ref()
|
self.file.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(
|
|
||||||
&mut self,
|
|
||||||
cx: &mut ModelContext<Self>,
|
|
||||||
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
|
|
||||||
let file = if let Some(file) = self.file.as_ref() {
|
|
||||||
file
|
|
||||||
} else {
|
|
||||||
return Task::ready(Err(anyhow!("buffer has no file")));
|
|
||||||
};
|
|
||||||
let text = self.as_rope().clone();
|
|
||||||
let version = self.version();
|
|
||||||
let save = file.save(
|
|
||||||
self.remote_id(),
|
|
||||||
text,
|
|
||||||
version,
|
|
||||||
self.line_ending(),
|
|
||||||
cx.as_mut(),
|
|
||||||
);
|
|
||||||
cx.spawn(|this, mut cx| async move {
|
|
||||||
let (version, fingerprint, mtime) = save.await?;
|
|
||||||
this.update(&mut cx, |this, cx| {
|
|
||||||
this.did_save(version.clone(), fingerprint, mtime, None, cx);
|
|
||||||
});
|
|
||||||
Ok((version, fingerprint, mtime))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn saved_version(&self) -> &clock::Global {
|
pub fn saved_version(&self) -> &clock::Global {
|
||||||
&self.saved_version
|
&self.saved_version
|
||||||
}
|
}
|
||||||
|
@ -585,16 +549,11 @@ impl Buffer {
|
||||||
version: clock::Global,
|
version: clock::Global,
|
||||||
fingerprint: RopeFingerprint,
|
fingerprint: RopeFingerprint,
|
||||||
mtime: SystemTime,
|
mtime: SystemTime,
|
||||||
new_file: Option<Arc<dyn File>>,
|
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) {
|
) {
|
||||||
self.saved_version = version;
|
self.saved_version = version;
|
||||||
self.saved_version_fingerprint = fingerprint;
|
self.saved_version_fingerprint = fingerprint;
|
||||||
self.saved_mtime = mtime;
|
self.saved_mtime = mtime;
|
||||||
if let Some(new_file) = new_file {
|
|
||||||
self.file = Some(new_file);
|
|
||||||
self.file_update_count += 1;
|
|
||||||
}
|
|
||||||
cx.emit(Event::Saved);
|
cx.emit(Event::Saved);
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
@ -661,14 +620,10 @@ impl Buffer {
|
||||||
new_file: Arc<dyn File>,
|
new_file: Arc<dyn File>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Task<()> {
|
) -> Task<()> {
|
||||||
let old_file = if let Some(file) = self.file.as_ref() {
|
|
||||||
file
|
|
||||||
} else {
|
|
||||||
return Task::ready(());
|
|
||||||
};
|
|
||||||
let mut file_changed = false;
|
let mut file_changed = false;
|
||||||
let mut task = Task::ready(());
|
let mut task = Task::ready(());
|
||||||
|
|
||||||
|
if let Some(old_file) = self.file.as_ref() {
|
||||||
if new_file.path() != old_file.path() {
|
if new_file.path() != old_file.path() {
|
||||||
file_changed = true;
|
file_changed = true;
|
||||||
}
|
}
|
||||||
|
@ -691,6 +646,9 @@ impl Buffer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
file_changed = true;
|
||||||
|
};
|
||||||
|
|
||||||
if file_changed {
|
if file_changed {
|
||||||
self.file_update_count += 1;
|
self.file_update_count += 1;
|
||||||
|
|
|
@ -12,7 +12,7 @@ use clock::ReplicaId;
|
||||||
use collections::{hash_map, BTreeMap, HashMap, HashSet};
|
use collections::{hash_map, BTreeMap, HashMap, HashSet};
|
||||||
use futures::{
|
use futures::{
|
||||||
channel::{mpsc, oneshot},
|
channel::{mpsc, oneshot},
|
||||||
future::Shared,
|
future::{try_join_all, Shared},
|
||||||
AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt,
|
AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt,
|
||||||
};
|
};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
|
@ -28,8 +28,8 @@ use language::{
|
||||||
range_from_lsp, range_to_lsp, Anchor, Bias, Buffer, CachedLspAdapter, CharKind, CodeAction,
|
range_from_lsp, range_to_lsp, Anchor, Bias, Buffer, CachedLspAdapter, CharKind, CodeAction,
|
||||||
CodeLabel, Completion, Diagnostic, DiagnosticEntry, DiagnosticSet, Event as BufferEvent,
|
CodeLabel, Completion, Diagnostic, DiagnosticEntry, DiagnosticSet, Event as BufferEvent,
|
||||||
File as _, Language, LanguageRegistry, LanguageServerName, LocalFile, OffsetRangeExt,
|
File as _, Language, LanguageRegistry, LanguageServerName, LocalFile, OffsetRangeExt,
|
||||||
Operation, Patch, PointUtf16, TextBufferSnapshot, ToOffset, ToPointUtf16, Transaction,
|
Operation, Patch, PointUtf16, RopeFingerprint, TextBufferSnapshot, ToOffset, ToPointUtf16,
|
||||||
Unclipped,
|
Transaction, Unclipped,
|
||||||
};
|
};
|
||||||
use lsp::{
|
use lsp::{
|
||||||
DiagnosticSeverity, DiagnosticTag, DocumentHighlightKind, LanguageServer, LanguageString,
|
DiagnosticSeverity, DiagnosticTag, DocumentHighlightKind, LanguageServer, LanguageString,
|
||||||
|
@ -59,7 +59,7 @@ use std::{
|
||||||
atomic::{AtomicUsize, Ordering::SeqCst},
|
atomic::{AtomicUsize, Ordering::SeqCst},
|
||||||
Arc,
|
Arc,
|
||||||
},
|
},
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant, SystemTime},
|
||||||
};
|
};
|
||||||
use terminal::{Terminal, TerminalBuilder};
|
use terminal::{Terminal, TerminalBuilder};
|
||||||
use util::{debug_panic, defer, post_inc, ResultExt, TryFutureExt as _};
|
use util::{debug_panic, defer, post_inc, ResultExt, TryFutureExt as _};
|
||||||
|
@ -554,11 +554,13 @@ impl Project {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let languages = Arc::new(LanguageRegistry::test());
|
let mut languages = LanguageRegistry::test();
|
||||||
|
languages.set_executor(cx.background());
|
||||||
let http_client = client::test::FakeHttpClient::with_404_response();
|
let http_client = client::test::FakeHttpClient::with_404_response();
|
||||||
let client = cx.update(|cx| client::Client::new(http_client.clone(), cx));
|
let client = cx.update(|cx| client::Client::new(http_client.clone(), cx));
|
||||||
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
|
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
|
||||||
let project = cx.update(|cx| Project::local(client, user_store, languages, fs, cx));
|
let project =
|
||||||
|
cx.update(|cx| Project::local(client, user_store, Arc::new(languages), fs, cx));
|
||||||
for path in root_paths {
|
for path in root_paths {
|
||||||
let (tree, _) = project
|
let (tree, _) = project
|
||||||
.update(cx, |project, cx| {
|
.update(cx, |project, cx| {
|
||||||
|
@ -1426,11 +1428,41 @@ impl Project {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn save_buffers(
|
||||||
|
&self,
|
||||||
|
buffers: HashSet<ModelHandle<Buffer>>,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) -> Task<Result<()>> {
|
||||||
|
cx.spawn(|this, mut cx| async move {
|
||||||
|
let save_tasks = buffers
|
||||||
|
.into_iter()
|
||||||
|
.map(|buffer| this.update(&mut cx, |this, cx| this.save_buffer(buffer, cx)));
|
||||||
|
try_join_all(save_tasks).await?;
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save_buffer(
|
||||||
|
&self,
|
||||||
|
buffer: ModelHandle<Buffer>,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
|
||||||
|
let Some(file) = File::from_dyn(buffer.read(cx).file()) else {
|
||||||
|
return Task::ready(Err(anyhow!("buffer doesn't have a file")));
|
||||||
|
};
|
||||||
|
let worktree = file.worktree.clone();
|
||||||
|
let path = file.path.clone();
|
||||||
|
worktree.update(cx, |worktree, cx| match worktree {
|
||||||
|
Worktree::Local(worktree) => worktree.save_buffer(buffer, path, false, cx),
|
||||||
|
Worktree::Remote(worktree) => worktree.save_buffer(buffer, cx),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn save_buffer_as(
|
pub fn save_buffer_as(
|
||||||
&mut self,
|
&mut self,
|
||||||
buffer: ModelHandle<Buffer>,
|
buffer: ModelHandle<Buffer>,
|
||||||
abs_path: PathBuf,
|
abs_path: PathBuf,
|
||||||
cx: &mut ModelContext<Project>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Task<Result<()>> {
|
) -> Task<Result<()>> {
|
||||||
let worktree_task = self.find_or_create_local_worktree(&abs_path, true, cx);
|
let worktree_task = self.find_or_create_local_worktree(&abs_path, true, cx);
|
||||||
let old_path =
|
let old_path =
|
||||||
|
@ -1443,11 +1475,11 @@ impl Project {
|
||||||
}
|
}
|
||||||
let (worktree, path) = worktree_task.await?;
|
let (worktree, path) = worktree_task.await?;
|
||||||
worktree
|
worktree
|
||||||
.update(&mut cx, |worktree, cx| {
|
.update(&mut cx, |worktree, cx| match worktree {
|
||||||
worktree
|
Worktree::Local(worktree) => {
|
||||||
.as_local_mut()
|
worktree.save_buffer(buffer.clone(), path.into(), true, cx)
|
||||||
.unwrap()
|
}
|
||||||
.save_buffer_as(buffer.clone(), path, cx)
|
Worktree::Remote(_) => panic!("cannot remote buffers as new files"),
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
this.update(&mut cx, |this, cx| {
|
this.update(&mut cx, |this, cx| {
|
||||||
|
@ -1789,20 +1821,22 @@ impl Project {
|
||||||
while let Some(()) = subscription.next().await {
|
while let Some(()) = subscription.next().await {
|
||||||
if let Some(project) = project.upgrade(&cx) {
|
if let Some(project) = project.upgrade(&cx) {
|
||||||
project.update(&mut cx, |project, cx| {
|
project.update(&mut cx, |project, cx| {
|
||||||
let mut buffers_without_language = Vec::new();
|
let mut plain_text_buffers = Vec::new();
|
||||||
let mut buffers_with_unknown_injections = Vec::new();
|
let mut buffers_with_unknown_injections = Vec::new();
|
||||||
for buffer in project.opened_buffers.values() {
|
for buffer in project.opened_buffers.values() {
|
||||||
if let Some(handle) = buffer.upgrade(cx) {
|
if let Some(handle) = buffer.upgrade(cx) {
|
||||||
let buffer = &handle.read(cx);
|
let buffer = &handle.read(cx);
|
||||||
if buffer.language().is_none() {
|
if buffer.language().is_none()
|
||||||
buffers_without_language.push(handle);
|
|| buffer.language() == Some(&*language::PLAIN_TEXT)
|
||||||
|
{
|
||||||
|
plain_text_buffers.push(handle);
|
||||||
} else if buffer.contains_unknown_injections() {
|
} else if buffer.contains_unknown_injections() {
|
||||||
buffers_with_unknown_injections.push(handle);
|
buffers_with_unknown_injections.push(handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for buffer in buffers_without_language {
|
for buffer in plain_text_buffers {
|
||||||
project.assign_language_to_buffer(&buffer, cx);
|
project.assign_language_to_buffer(&buffer, cx);
|
||||||
project.register_buffer_with_language_server(&buffer, cx);
|
project.register_buffer_with_language_server(&buffer, cx);
|
||||||
}
|
}
|
||||||
|
@ -2818,59 +2852,30 @@ impl Project {
|
||||||
trigger: FormatTrigger,
|
trigger: FormatTrigger,
|
||||||
cx: &mut ModelContext<Project>,
|
cx: &mut ModelContext<Project>,
|
||||||
) -> Task<Result<ProjectTransaction>> {
|
) -> Task<Result<ProjectTransaction>> {
|
||||||
let mut local_buffers = Vec::new();
|
if self.is_local() {
|
||||||
let mut remote_buffers = None;
|
let mut buffers_with_paths_and_servers = buffers
|
||||||
for buffer_handle in buffers {
|
.into_iter()
|
||||||
|
.filter_map(|buffer_handle| {
|
||||||
let buffer = buffer_handle.read(cx);
|
let buffer = buffer_handle.read(cx);
|
||||||
if let Some(file) = File::from_dyn(buffer.file()) {
|
let file = File::from_dyn(buffer.file())?;
|
||||||
if let Some(buffer_abs_path) = file.as_local().map(|f| f.abs_path(cx)) {
|
let buffer_abs_path = file.as_local()?.abs_path(cx);
|
||||||
if let Some((_, server)) = self.language_server_for_buffer(buffer, cx) {
|
let (_, server) = self.language_server_for_buffer(buffer, cx)?;
|
||||||
local_buffers.push((buffer_handle, buffer_abs_path, server.clone()));
|
Some((buffer_handle, buffer_abs_path, server.clone()))
|
||||||
}
|
})
|
||||||
} else {
|
.collect::<Vec<_>>();
|
||||||
remote_buffers.get_or_insert(Vec::new()).push(buffer_handle);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Task::ready(Ok(Default::default()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let remote_buffers = self.remote_id().zip(remote_buffers);
|
|
||||||
let client = self.client.clone();
|
|
||||||
|
|
||||||
cx.spawn(|this, mut cx| async move {
|
cx.spawn(|this, mut cx| async move {
|
||||||
let mut project_transaction = ProjectTransaction::default();
|
|
||||||
|
|
||||||
if let Some((project_id, remote_buffers)) = remote_buffers {
|
|
||||||
let response = client
|
|
||||||
.request(proto::FormatBuffers {
|
|
||||||
project_id,
|
|
||||||
trigger: trigger as i32,
|
|
||||||
buffer_ids: remote_buffers
|
|
||||||
.iter()
|
|
||||||
.map(|buffer| buffer.read_with(&cx, |buffer, _| buffer.remote_id()))
|
|
||||||
.collect(),
|
|
||||||
})
|
|
||||||
.await?
|
|
||||||
.transaction
|
|
||||||
.ok_or_else(|| anyhow!("missing transaction"))?;
|
|
||||||
project_transaction = this
|
|
||||||
.update(&mut cx, |this, cx| {
|
|
||||||
this.deserialize_project_transaction(response, push_to_history, cx)
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not allow multiple concurrent formatting requests for the
|
// Do not allow multiple concurrent formatting requests for the
|
||||||
// same buffer.
|
// same buffer.
|
||||||
this.update(&mut cx, |this, _| {
|
this.update(&mut cx, |this, _| {
|
||||||
local_buffers
|
buffers_with_paths_and_servers
|
||||||
.retain(|(buffer, _, _)| this.buffers_being_formatted.insert(buffer.id()));
|
.retain(|(buffer, _, _)| this.buffers_being_formatted.insert(buffer.id()));
|
||||||
});
|
});
|
||||||
|
|
||||||
let _cleanup = defer({
|
let _cleanup = defer({
|
||||||
let this = this.clone();
|
let this = this.clone();
|
||||||
let mut cx = cx.clone();
|
let mut cx = cx.clone();
|
||||||
let local_buffers = &local_buffers;
|
let local_buffers = &buffers_with_paths_and_servers;
|
||||||
move || {
|
move || {
|
||||||
this.update(&mut cx, |this, _| {
|
this.update(&mut cx, |this, _| {
|
||||||
for (buffer, _, _) in local_buffers {
|
for (buffer, _, _) in local_buffers {
|
||||||
|
@ -2880,8 +2885,10 @@ impl Project {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
for (buffer, buffer_abs_path, language_server) in &local_buffers {
|
let mut project_transaction = ProjectTransaction::default();
|
||||||
let (format_on_save, formatter, tab_size) = buffer.read_with(&cx, |buffer, cx| {
|
for (buffer, buffer_abs_path, language_server) in &buffers_with_paths_and_servers {
|
||||||
|
let (format_on_save, formatter, tab_size) =
|
||||||
|
buffer.read_with(&cx, |buffer, cx| {
|
||||||
let settings = cx.global::<Settings>();
|
let settings = cx.global::<Settings>();
|
||||||
let language_name = buffer.language().map(|language| language.name());
|
let language_name = buffer.language().map(|language| language.name());
|
||||||
(
|
(
|
||||||
|
@ -2938,6 +2945,33 @@ impl Project {
|
||||||
|
|
||||||
Ok(project_transaction)
|
Ok(project_transaction)
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
let remote_id = self.remote_id();
|
||||||
|
let client = self.client.clone();
|
||||||
|
cx.spawn(|this, mut cx| async move {
|
||||||
|
let mut project_transaction = ProjectTransaction::default();
|
||||||
|
if let Some(project_id) = remote_id {
|
||||||
|
let response = client
|
||||||
|
.request(proto::FormatBuffers {
|
||||||
|
project_id,
|
||||||
|
trigger: trigger as i32,
|
||||||
|
buffer_ids: buffers
|
||||||
|
.iter()
|
||||||
|
.map(|buffer| buffer.read_with(&cx, |buffer, _| buffer.remote_id()))
|
||||||
|
.collect(),
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
.transaction
|
||||||
|
.ok_or_else(|| anyhow!("missing transaction"))?;
|
||||||
|
project_transaction = this
|
||||||
|
.update(&mut cx, |this, cx| {
|
||||||
|
this.deserialize_project_transaction(response, push_to_history, cx)
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
Ok(project_transaction)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn format_via_lsp(
|
async fn format_via_lsp(
|
||||||
|
@ -4429,6 +4463,7 @@ impl Project {
|
||||||
renamed_buffers.push((cx.handle(), old_path));
|
renamed_buffers.push((cx.handle(), old_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if new_file != *old_file {
|
||||||
if let Some(project_id) = self.remote_id() {
|
if let Some(project_id) = self.remote_id() {
|
||||||
self.client
|
self.client
|
||||||
.send(proto::UpdateBufferFile {
|
.send(proto::UpdateBufferFile {
|
||||||
|
@ -4438,8 +4473,10 @@ impl Project {
|
||||||
})
|
})
|
||||||
.log_err();
|
.log_err();
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.file_updated(Arc::new(new_file), cx).detach();
|
buffer.file_updated(Arc::new(new_file), cx).detach();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
buffers_to_delete.push(*buffer_id);
|
buffers_to_delete.push(*buffer_id);
|
||||||
|
@ -5151,8 +5188,9 @@ impl Project {
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let (saved_version, fingerprint, mtime) =
|
let (saved_version, fingerprint, mtime) = this
|
||||||
buffer.update(&mut cx, |buffer, cx| buffer.save(cx)).await?;
|
.update(&mut cx, |this, cx| this.save_buffer(buffer, cx))
|
||||||
|
.await?;
|
||||||
Ok(proto::BufferSaved {
|
Ok(proto::BufferSaved {
|
||||||
project_id,
|
project_id,
|
||||||
buffer_id,
|
buffer_id,
|
||||||
|
@ -6022,7 +6060,7 @@ impl Project {
|
||||||
.and_then(|buffer| buffer.upgrade(cx));
|
.and_then(|buffer| buffer.upgrade(cx));
|
||||||
if let Some(buffer) = buffer {
|
if let Some(buffer) = buffer {
|
||||||
buffer.update(cx, |buffer, cx| {
|
buffer.update(cx, |buffer, cx| {
|
||||||
buffer.did_save(version, fingerprint, mtime, None, cx);
|
buffer.did_save(version, fingerprint, mtime, cx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -243,8 +243,8 @@ async fn test_managing_language_servers(
|
||||||
);
|
);
|
||||||
|
|
||||||
// Save notifications are reported to all servers.
|
// Save notifications are reported to all servers.
|
||||||
toml_buffer
|
project
|
||||||
.update(cx, |buffer, cx| buffer.save(cx))
|
.update(cx, |project, cx| project.save_buffer(toml_buffer, cx))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -2083,12 +2083,13 @@ async fn test_save_file(cx: &mut gpui::TestAppContext) {
|
||||||
.update(cx, |p, cx| p.open_local_buffer("/dir/file1", cx))
|
.update(cx, |p, cx| p.open_local_buffer("/dir/file1", cx))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
buffer
|
buffer.update(cx, |buffer, cx| {
|
||||||
.update(cx, |buffer, cx| {
|
|
||||||
assert_eq!(buffer.text(), "the old contents");
|
assert_eq!(buffer.text(), "the old contents");
|
||||||
buffer.edit([(0..0, "a line of text.\n".repeat(10 * 1024))], None, cx);
|
buffer.edit([(0..0, "a line of text.\n".repeat(10 * 1024))], None, cx);
|
||||||
buffer.save(cx)
|
});
|
||||||
})
|
|
||||||
|
project
|
||||||
|
.update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -2112,11 +2113,12 @@ async fn test_save_in_single_file_worktree(cx: &mut gpui::TestAppContext) {
|
||||||
.update(cx, |p, cx| p.open_local_buffer("/dir/file1", cx))
|
.update(cx, |p, cx| p.open_local_buffer("/dir/file1", cx))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
buffer
|
buffer.update(cx, |buffer, cx| {
|
||||||
.update(cx, |buffer, cx| {
|
|
||||||
buffer.edit([(0..0, "a line of text.\n".repeat(10 * 1024))], None, cx);
|
buffer.edit([(0..0, "a line of text.\n".repeat(10 * 1024))], None, cx);
|
||||||
buffer.save(cx)
|
});
|
||||||
})
|
|
||||||
|
project
|
||||||
|
.update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -2130,6 +2132,20 @@ async fn test_save_as(cx: &mut gpui::TestAppContext) {
|
||||||
fs.insert_tree("/dir", json!({})).await;
|
fs.insert_tree("/dir", json!({})).await;
|
||||||
|
|
||||||
let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
|
let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
|
||||||
|
|
||||||
|
let languages = project.read_with(cx, |project, _| project.languages().clone());
|
||||||
|
languages.register(
|
||||||
|
"/some/path",
|
||||||
|
LanguageConfig {
|
||||||
|
name: "Rust".into(),
|
||||||
|
path_suffixes: vec!["rs".into()],
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
tree_sitter_rust::language(),
|
||||||
|
None,
|
||||||
|
|_| Default::default(),
|
||||||
|
);
|
||||||
|
|
||||||
let buffer = project.update(cx, |project, cx| {
|
let buffer = project.update(cx, |project, cx| {
|
||||||
project.create_buffer("", None, cx).unwrap()
|
project.create_buffer("", None, cx).unwrap()
|
||||||
});
|
});
|
||||||
|
@ -2137,23 +2153,30 @@ async fn test_save_as(cx: &mut gpui::TestAppContext) {
|
||||||
buffer.edit([(0..0, "abc")], None, cx);
|
buffer.edit([(0..0, "abc")], None, cx);
|
||||||
assert!(buffer.is_dirty());
|
assert!(buffer.is_dirty());
|
||||||
assert!(!buffer.has_conflict());
|
assert!(!buffer.has_conflict());
|
||||||
|
assert_eq!(buffer.language().unwrap().name().as_ref(), "Plain Text");
|
||||||
});
|
});
|
||||||
project
|
project
|
||||||
.update(cx, |project, cx| {
|
.update(cx, |project, cx| {
|
||||||
project.save_buffer_as(buffer.clone(), "/dir/file1".into(), cx)
|
project.save_buffer_as(buffer.clone(), "/dir/file1.rs".into(), cx)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(fs.load(Path::new("/dir/file1")).await.unwrap(), "abc");
|
assert_eq!(fs.load(Path::new("/dir/file1.rs")).await.unwrap(), "abc");
|
||||||
|
|
||||||
|
cx.foreground().run_until_parked();
|
||||||
buffer.read_with(cx, |buffer, cx| {
|
buffer.read_with(cx, |buffer, cx| {
|
||||||
assert_eq!(buffer.file().unwrap().full_path(cx), Path::new("dir/file1"));
|
assert_eq!(
|
||||||
|
buffer.file().unwrap().full_path(cx),
|
||||||
|
Path::new("dir/file1.rs")
|
||||||
|
);
|
||||||
assert!(!buffer.is_dirty());
|
assert!(!buffer.is_dirty());
|
||||||
assert!(!buffer.has_conflict());
|
assert!(!buffer.has_conflict());
|
||||||
|
assert_eq!(buffer.language().unwrap().name().as_ref(), "Rust");
|
||||||
});
|
});
|
||||||
|
|
||||||
let opened_buffer = project
|
let opened_buffer = project
|
||||||
.update(cx, |project, cx| {
|
.update(cx, |project, cx| {
|
||||||
project.open_local_buffer("/dir/file1", cx)
|
project.open_local_buffer("/dir/file1.rs", cx)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -2462,7 +2485,6 @@ async fn test_buffer_is_dirty(cx: &mut gpui::TestAppContext) {
|
||||||
buffer.version(),
|
buffer.version(),
|
||||||
buffer.as_rope().fingerprint(),
|
buffer.as_rope().fingerprint(),
|
||||||
buffer.file().unwrap().mtime(),
|
buffer.file().unwrap().mtime(),
|
||||||
None,
|
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -2682,11 +2704,11 @@ async fn test_buffer_line_endings(cx: &mut gpui::TestAppContext) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Save a file with windows line endings. The file is written correctly.
|
// Save a file with windows line endings. The file is written correctly.
|
||||||
buffer2
|
buffer2.update(cx, |buffer, cx| {
|
||||||
.update(cx, |buffer, cx| {
|
|
||||||
buffer.set_text("one\ntwo\nthree\nfour\n", cx);
|
buffer.set_text("one\ntwo\nthree\nfour\n", cx);
|
||||||
buffer.save(cx)
|
});
|
||||||
})
|
project
|
||||||
|
.update(cx, |project, cx| project.save_buffer(buffer2, cx))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
@ -20,6 +20,7 @@ use gpui::{
|
||||||
executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
|
executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
|
||||||
Task,
|
Task,
|
||||||
};
|
};
|
||||||
|
use language::File as _;
|
||||||
use language::{
|
use language::{
|
||||||
proto::{
|
proto::{
|
||||||
deserialize_fingerprint, deserialize_version, serialize_fingerprint, serialize_line_ending,
|
deserialize_fingerprint, deserialize_version, serialize_fingerprint, serialize_line_ending,
|
||||||
|
@ -724,34 +725,69 @@ impl LocalWorktree {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_buffer_as(
|
pub fn save_buffer(
|
||||||
&self,
|
&self,
|
||||||
buffer_handle: ModelHandle<Buffer>,
|
buffer_handle: ModelHandle<Buffer>,
|
||||||
path: impl Into<Arc<Path>>,
|
path: Arc<Path>,
|
||||||
|
has_changed_file: bool,
|
||||||
cx: &mut ModelContext<Worktree>,
|
cx: &mut ModelContext<Worktree>,
|
||||||
) -> Task<Result<()>> {
|
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
|
||||||
|
let handle = cx.handle();
|
||||||
let buffer = buffer_handle.read(cx);
|
let buffer = buffer_handle.read(cx);
|
||||||
|
|
||||||
|
let rpc = self.client.clone();
|
||||||
|
let buffer_id = buffer.remote_id();
|
||||||
|
let project_id = self.share.as_ref().map(|share| share.project_id);
|
||||||
|
|
||||||
let text = buffer.as_rope().clone();
|
let text = buffer.as_rope().clone();
|
||||||
let fingerprint = text.fingerprint();
|
let fingerprint = text.fingerprint();
|
||||||
let version = buffer.version();
|
let version = buffer.version();
|
||||||
let save = self.write_file(path, text, buffer.line_ending(), cx);
|
let save = self.write_file(path, text, buffer.line_ending(), cx);
|
||||||
let handle = cx.handle();
|
|
||||||
cx.as_mut().spawn(|mut cx| async move {
|
cx.as_mut().spawn(|mut cx| async move {
|
||||||
let entry = save.await?;
|
let entry = save.await?;
|
||||||
let file = File {
|
|
||||||
|
if has_changed_file {
|
||||||
|
let new_file = Arc::new(File {
|
||||||
entry_id: entry.id,
|
entry_id: entry.id,
|
||||||
worktree: handle,
|
worktree: handle,
|
||||||
path: entry.path,
|
path: entry.path,
|
||||||
mtime: entry.mtime,
|
mtime: entry.mtime,
|
||||||
is_local: true,
|
is_local: true,
|
||||||
is_deleted: false,
|
is_deleted: false,
|
||||||
};
|
|
||||||
|
|
||||||
buffer_handle.update(&mut cx, |buffer, cx| {
|
|
||||||
buffer.did_save(version, fingerprint, file.mtime, Some(Arc::new(file)), cx);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
if let Some(project_id) = project_id {
|
||||||
|
rpc.send(proto::UpdateBufferFile {
|
||||||
|
project_id,
|
||||||
|
buffer_id,
|
||||||
|
file: Some(new_file.to_proto()),
|
||||||
|
})
|
||||||
|
.log_err();
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer_handle.update(&mut cx, |buffer, cx| {
|
||||||
|
if has_changed_file {
|
||||||
|
buffer.file_updated(new_file, cx).detach();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(project_id) = project_id {
|
||||||
|
rpc.send(proto::BufferSaved {
|
||||||
|
project_id,
|
||||||
|
buffer_id,
|
||||||
|
version: serialize_version(&version),
|
||||||
|
mtime: Some(entry.mtime.into()),
|
||||||
|
fingerprint: serialize_fingerprint(fingerprint),
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer_handle.update(&mut cx, |buffer, cx| {
|
||||||
|
buffer.did_save(version.clone(), fingerprint, entry.mtime, cx);
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok((version, fingerprint, entry.mtime))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1085,6 +1121,39 @@ impl RemoteWorktree {
|
||||||
self.disconnected = true;
|
self.disconnected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn save_buffer(
|
||||||
|
&self,
|
||||||
|
buffer_handle: ModelHandle<Buffer>,
|
||||||
|
cx: &mut ModelContext<Worktree>,
|
||||||
|
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
|
||||||
|
let buffer = buffer_handle.read(cx);
|
||||||
|
let buffer_id = buffer.remote_id();
|
||||||
|
let version = buffer.version();
|
||||||
|
let rpc = self.client.clone();
|
||||||
|
let project_id = self.project_id;
|
||||||
|
cx.as_mut().spawn(|mut cx| async move {
|
||||||
|
let response = rpc
|
||||||
|
.request(proto::SaveBuffer {
|
||||||
|
project_id,
|
||||||
|
buffer_id,
|
||||||
|
version: serialize_version(&version),
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
let version = deserialize_version(response.version);
|
||||||
|
let fingerprint = deserialize_fingerprint(&response.fingerprint)?;
|
||||||
|
let mtime = response
|
||||||
|
.mtime
|
||||||
|
.ok_or_else(|| anyhow!("missing mtime"))?
|
||||||
|
.into();
|
||||||
|
|
||||||
|
buffer_handle.update(&mut cx, |buffer, cx| {
|
||||||
|
buffer.did_save(version.clone(), fingerprint, mtime, cx);
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok((version, fingerprint, mtime))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn update_from_remote(&mut self, update: proto::UpdateWorktree) {
|
pub fn update_from_remote(&mut self, update: proto::UpdateWorktree) {
|
||||||
if let Some(updates_tx) = &self.updates_tx {
|
if let Some(updates_tx) = &self.updates_tx {
|
||||||
updates_tx
|
updates_tx
|
||||||
|
@ -1859,57 +1928,6 @@ impl language::File for File {
|
||||||
self.is_deleted
|
self.is_deleted
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save(
|
|
||||||
&self,
|
|
||||||
buffer_id: u64,
|
|
||||||
text: Rope,
|
|
||||||
version: clock::Global,
|
|
||||||
line_ending: LineEnding,
|
|
||||||
cx: &mut MutableAppContext,
|
|
||||||
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
|
|
||||||
self.worktree.update(cx, |worktree, cx| match worktree {
|
|
||||||
Worktree::Local(worktree) => {
|
|
||||||
let rpc = worktree.client.clone();
|
|
||||||
let project_id = worktree.share.as_ref().map(|share| share.project_id);
|
|
||||||
let fingerprint = text.fingerprint();
|
|
||||||
let save = worktree.write_file(self.path.clone(), text, line_ending, cx);
|
|
||||||
cx.background().spawn(async move {
|
|
||||||
let entry = save.await?;
|
|
||||||
if let Some(project_id) = project_id {
|
|
||||||
rpc.send(proto::BufferSaved {
|
|
||||||
project_id,
|
|
||||||
buffer_id,
|
|
||||||
version: serialize_version(&version),
|
|
||||||
mtime: Some(entry.mtime.into()),
|
|
||||||
fingerprint: serialize_fingerprint(fingerprint),
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
Ok((version, fingerprint, entry.mtime))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Worktree::Remote(worktree) => {
|
|
||||||
let rpc = worktree.client.clone();
|
|
||||||
let project_id = worktree.project_id;
|
|
||||||
cx.foreground().spawn(async move {
|
|
||||||
let response = rpc
|
|
||||||
.request(proto::SaveBuffer {
|
|
||||||
project_id,
|
|
||||||
buffer_id,
|
|
||||||
version: serialize_version(&version),
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
let version = deserialize_version(response.version);
|
|
||||||
let fingerprint = deserialize_fingerprint(&response.fingerprint)?;
|
|
||||||
let mtime = response
|
|
||||||
.mtime
|
|
||||||
.ok_or_else(|| anyhow!("missing mtime"))?
|
|
||||||
.into();
|
|
||||||
Ok((version, fingerprint, mtime))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn Any {
|
fn as_any(&self) -> &dyn Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue