From 19c98bb5add47b805217b82fbe5de219a858a152 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Mon, 17 Oct 2022 12:58:48 -0700 Subject: [PATCH] fixed a bug where files outside of the project would show 'untitled' in the search bar --- crates/editor/src/element.rs | 5 +++-- crates/editor/src/items.rs | 22 +++++++++------------- crates/language/src/buffer.rs | 12 ++++++++++++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index afc9bdd494..b11f09fd8c 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1379,10 +1379,11 @@ impl EditorElement { let font_size = (style.text_scale_factor * self.style.text.font_size).round(); + let path = buffer.resolve_file_path(cx, true); let mut filename = None; let mut parent_path = None; - if let Some(file) = buffer.file() { - let path = file.path(); + // Can't use .and_then() because `.file_name()` and `.parent()` return references :( + if let Some(path) = path { filename = path.file_name().map(|f| f.to_string_lossy().to_string()); parent_path = path.parent().map(|p| p.to_string_lossy().to_string() + "/"); diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index e6a4eebffb..d617acc90c 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -531,21 +531,17 @@ impl Item for Editor { let buffer = multibuffer.buffer(buffer_id)?; let buffer = buffer.read(cx); - let filename = if let Some(file) = buffer.file() { - if file.path().file_name().is_none() - || self - .project + let filename = buffer + .snapshot() + .resolve_file_path( + cx, + self.project .as_ref() .map(|project| project.read(cx).visible_worktrees(cx).count() > 1) - .unwrap_or_default() - { - file.full_path(cx).to_string_lossy().to_string() - } else { - file.path().to_string_lossy().to_string() - } - } else { - "untitled".to_string() - }; + .unwrap_or_default(), + ) + .map(|path| path.to_string_lossy().to_string()) + .unwrap_or_else(|| "untitled".to_string()); let mut breadcrumbs = vec![Label::new(filename, theme.breadcrumbs.text.clone()).boxed()]; breadcrumbs.extend(symbols.into_iter().map(|symbol| { diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 274777b81c..f1717c4aac 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -2315,6 +2315,18 @@ impl BufferSnapshot { self.file.as_deref() } + pub fn resolve_file_path(&self, cx: &AppContext, include_root: bool) -> Option { + if let Some(file) = self.file() { + if file.path().file_name().is_none() || include_root { + Some(file.full_path(cx)) + } else { + Some(file.path().to_path_buf()) + } + } else { + None + } + } + pub fn file_update_count(&self) -> usize { self.file_update_count }