fixed a bug where files outside of the project would show 'untitled' in the search bar

Mikayla Maki created

Change summary

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(-)

Detailed changes

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() + "/");

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| {

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<PathBuf> {
+        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
     }