Remove File::select_language

We want to make File a trait object defined in the buffer crate to decouple buffer from worktree, and this method is in the way.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo 2021-10-01 17:48:02 -06:00 committed by Max Brunsfeld
parent 90cc01a50a
commit 7728467790
3 changed files with 33 additions and 19 deletions

View file

@ -2652,7 +2652,7 @@ impl workspace::ItemView for Editor {
fn save_as(
&mut self,
worktree: &ModelHandle<Worktree>,
worktree: ModelHandle<Worktree>,
path: &Path,
cx: &mut ViewContext<Self>,
) -> Task<Result<()>> {
@ -2670,9 +2670,16 @@ impl workspace::ItemView for Editor {
cx.spawn(|buffer, mut cx| async move {
save_as.await.map(|new_file| {
let language = worktree.read_with(&cx, |worktree, cx| {
worktree
.languages()
.select_language(new_file.full_path(cx))
.cloned()
});
buffer.update(&mut cx, |buffer, cx| {
buffer.set_language(new_file.select_language(cx), cx);
buffer.did_save(version, new_file.mtime, Some(new_file), cx);
buffer.set_language(language, cx);
});
})
})

View file

@ -185,7 +185,7 @@ pub trait ItemView: View {
fn save(&mut self, cx: &mut ViewContext<Self>) -> Result<Task<Result<()>>>;
fn save_as(
&mut self,
worktree: &ModelHandle<Worktree>,
worktree: ModelHandle<Worktree>,
path: &Path,
cx: &mut ViewContext<Self>,
) -> Task<anyhow::Result<()>>;
@ -229,7 +229,7 @@ pub trait ItemViewHandle {
fn save(&self, cx: &mut MutableAppContext) -> Result<Task<Result<()>>>;
fn save_as(
&self,
worktree: &ModelHandle<Worktree>,
worktree: ModelHandle<Worktree>,
path: &Path,
cx: &mut MutableAppContext,
) -> Task<anyhow::Result<()>>;
@ -317,7 +317,7 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
fn save_as(
&self,
worktree: &ModelHandle<Worktree>,
worktree: ModelHandle<Worktree>,
path: &Path,
cx: &mut MutableAppContext,
) -> Task<anyhow::Result<()>> {
@ -768,7 +768,7 @@ impl Workspace {
Ok((worktree, path)) => {
handle
.update(&mut cx, |_, cx| {
item.save_as(&worktree, &path, cx.as_mut())
item.save_as(worktree, &path, cx.as_mut())
})
.await
}

View file

@ -5,7 +5,7 @@ use crate::{
editor::{self, Buffer, History, Operation, Rope},
fs::{self, Fs},
fuzzy::CharBag,
language::{Language, LanguageRegistry},
language::LanguageRegistry,
rpc::{self, proto, Status},
time::{self, ReplicaId},
util::{Bias, TryFutureExt},
@ -288,6 +288,13 @@ impl Worktree {
}
}
pub fn languages(&self) -> &Arc<LanguageRegistry> {
match self {
Worktree::Local(worktree) => &worktree.languages,
Worktree::Remote(worktree) => &worktree.languages,
}
}
pub fn handle_add_peer(
&mut self,
envelope: TypedEnvelope<proto::AddPeer>,
@ -856,8 +863,12 @@ impl LocalWorktree {
let (file, contents) = this
.update(&mut cx, |this, cx| this.as_local().unwrap().load(&path, cx))
.await?;
let language = this.read_with(&cx, |this, cx| {
this.languages()
.select_language(file.full_path(cx))
.cloned()
});
let buffer = cx.add_model(|cx| {
let language = file.select_language(cx);
Buffer::from_history(0, History::new(contents.into()), Some(file), language, cx)
});
this.update(&mut cx, |this, _| {
@ -1261,7 +1272,11 @@ impl RemoteWorktree {
.upgrade(&cx)
.ok_or_else(|| anyhow!("worktree was closed"))?;
let file = File::new(entry.id, this.clone(), entry.path, entry.mtime);
let language = cx.read(|cx| file.select_language(cx));
let language = this.read_with(&cx, |this, cx| {
this.languages()
.select_language(file.full_path(cx))
.cloned()
});
let remote_buffer = response.buffer.ok_or_else(|| anyhow!("empty buffer"))?;
let buffer_id = remote_buffer.id as usize;
let buffer = cx.add_model(|cx| {
@ -1819,20 +1834,12 @@ impl File {
self.path.clone()
}
pub fn abs_path(&self, cx: &AppContext) -> PathBuf {
self.worktree.read(cx).abs_path.join(&self.path)
}
pub fn select_language(&self, cx: &AppContext) -> Option<Arc<Language>> {
pub fn full_path(&self, cx: &AppContext) -> PathBuf {
let worktree = self.worktree.read(cx);
let mut full_path = PathBuf::new();
full_path.push(worktree.root_name());
full_path.push(&self.path);
let languages = match self.worktree.read(cx) {
Worktree::Local(worktree) => &worktree.languages,
Worktree::Remote(worktree) => &worktree.languages,
};
languages.select_language(&full_path).cloned()
full_path
}
/// Returns the last component of this handle's absolute path. If this handle refers to the root