diff --git a/zed/src/editor.rs b/zed/src/editor.rs index ac63f0c80d..c240ffd334 100644 --- a/zed/src/editor.rs +++ b/zed/src/editor.rs @@ -2652,7 +2652,7 @@ impl workspace::ItemView for Editor { fn save_as( &mut self, - worktree: &ModelHandle, + worktree: ModelHandle, path: &Path, cx: &mut ViewContext, ) -> Task> { @@ -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); }); }) }) diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 9e1710dac4..e2d4673a9e 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -185,7 +185,7 @@ pub trait ItemView: View { fn save(&mut self, cx: &mut ViewContext) -> Result>>; fn save_as( &mut self, - worktree: &ModelHandle, + worktree: ModelHandle, path: &Path, cx: &mut ViewContext, ) -> Task>; @@ -229,7 +229,7 @@ pub trait ItemViewHandle { fn save(&self, cx: &mut MutableAppContext) -> Result>>; fn save_as( &self, - worktree: &ModelHandle, + worktree: ModelHandle, path: &Path, cx: &mut MutableAppContext, ) -> Task>; @@ -317,7 +317,7 @@ impl ItemViewHandle for ViewHandle { fn save_as( &self, - worktree: &ModelHandle, + worktree: ModelHandle, path: &Path, cx: &mut MutableAppContext, ) -> Task> { @@ -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 } diff --git a/zed/src/worktree.rs b/zed/src/worktree.rs index 425fb5e7d8..da228ce2fd 100644 --- a/zed/src/worktree.rs +++ b/zed/src/worktree.rs @@ -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 { + match self { + Worktree::Local(worktree) => &worktree.languages, + Worktree::Remote(worktree) => &worktree.languages, + } + } + pub fn handle_add_peer( &mut self, envelope: TypedEnvelope, @@ -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> { + 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