assistant2: Correctly display context files outside project worktrees (#27806)

Agus Zubiaga created

We were displaying empty pills for files that weren't inside one of the
project worktrees

Release Notes:

- N/A

Change summary

crates/assistant2/src/context.rs       |  4 ++--
crates/assistant2/src/context_store.rs | 19 ++++++++++++-------
crates/assistant2/src/context_strip.rs |  6 +++---
3 files changed, 17 insertions(+), 12 deletions(-)

Detailed changes

crates/assistant2/src/context.rs 🔗

@@ -156,7 +156,7 @@ impl AssistantContext {
 impl FileContext {
     pub fn snapshot(&self, cx: &App) -> Option<ContextSnapshot> {
         let buffer = self.context_buffer.buffer.read(cx);
-        let path = buffer_path_log_err(buffer)?;
+        let path = buffer_path_log_err(buffer, cx)?;
         let full_path: SharedString = path.to_string_lossy().into_owned().into();
         let name = match path.file_name() {
             Some(name) => name.to_string_lossy().into_owned().into(),
@@ -231,7 +231,7 @@ impl SymbolContext {
     pub fn snapshot(&self, cx: &App) -> Option<ContextSnapshot> {
         let buffer = self.context_symbol.buffer.read(cx);
         let name = self.context_symbol.id.name.clone();
-        let path = buffer_path_log_err(buffer)?
+        let path = buffer_path_log_err(buffer, cx)?
             .to_string_lossy()
             .into_owned()
             .into();

crates/assistant2/src/context_store.rs 🔗

@@ -472,7 +472,7 @@ impl ContextStore {
             let found_file_context = self.context.iter().find(|context| match &context {
                 AssistantContext::File(file_context) => {
                     let buffer = file_context.context_buffer.buffer.read(cx);
-                    if let Some(file_path) = buffer_path_log_err(buffer) {
+                    if let Some(file_path) = buffer_path_log_err(buffer, cx) {
                         *file_path == *path
                     } else {
                         false
@@ -545,7 +545,7 @@ impl ContextStore {
             .filter_map(|context| match context {
                 AssistantContext::File(file) => {
                     let buffer = file.context_buffer.buffer.read(cx);
-                    buffer_path_log_err(buffer).map(|p| p.to_path_buf())
+                    buffer_path_log_err(buffer, cx).map(|p| p.to_path_buf())
                 }
                 AssistantContext::Directory(_)
                 | AssistantContext::Symbol(_)
@@ -620,9 +620,14 @@ fn collect_buffer_info_and_text(
     (buffer_info, text_task)
 }
 
-pub fn buffer_path_log_err(buffer: &Buffer) -> Option<Arc<Path>> {
+pub fn buffer_path_log_err(buffer: &Buffer, cx: &App) -> Option<Arc<Path>> {
     if let Some(file) = buffer.file() {
-        Some(file.path().clone())
+        let mut path = file.path().clone();
+
+        if path.as_os_str().is_empty() {
+            path = file.full_path(cx).into();
+        }
+        Some(path)
     } else {
         log::error!("Buffer that had a path unexpectedly no longer has a path.");
         None
@@ -708,7 +713,7 @@ pub fn refresh_context_store_text(
                         || changed_buffers.iter().any(|buffer| {
                             let buffer = buffer.read(cx);
 
-                            buffer_path_log_err(&buffer).map_or(false, |path| {
+                            buffer_path_log_err(&buffer, cx).map_or(false, |path| {
                                 path.starts_with(&directory_context.path.path)
                             })
                         });
@@ -857,7 +862,7 @@ fn refresh_context_buffer(
     cx: &App,
 ) -> Option<impl Future<Output = ContextBuffer> + use<>> {
     let buffer = context_buffer.buffer.read(cx);
-    let path = buffer_path_log_err(buffer)?;
+    let path = buffer_path_log_err(buffer, cx)?;
     if buffer.version.changed_since(&context_buffer.version) {
         let (buffer_info, text_task) = collect_buffer_info_and_text(
             path,
@@ -877,7 +882,7 @@ fn refresh_context_symbol(
     cx: &App,
 ) -> Option<impl Future<Output = ContextSymbol> + use<>> {
     let buffer = context_symbol.buffer.read(cx);
-    let path = buffer_path_log_err(buffer)?;
+    let path = buffer_path_log_err(buffer, cx)?;
     let project_path = buffer.project_path(cx)?;
     if buffer.version.changed_since(&context_symbol.buffer_version) {
         let (buffer_info, text_task) = collect_buffer_info_and_text(

crates/assistant2/src/context_strip.rs 🔗

@@ -92,12 +92,12 @@ impl ContextStrip {
         let active_buffer_entity = editor.buffer().read(cx).as_singleton()?;
         let active_buffer = active_buffer_entity.read(cx);
 
-        let path = active_buffer.file()?.path();
+        let path = active_buffer.file()?.full_path(cx);
 
         if self
             .context_store
             .read(cx)
-            .will_include_buffer(active_buffer.remote_id(), path)
+            .will_include_buffer(active_buffer.remote_id(), &path)
             .is_some()
         {
             return None;
@@ -108,7 +108,7 @@ impl ContextStrip {
             None => path.to_string_lossy().into_owned().into(),
         };
 
-        let icon_path = FileIcons::get_icon(path, cx);
+        let icon_path = FileIcons::get_icon(&path, cx);
 
         Some(SuggestedContext::File {
             name,