Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

agent_ui: Show base name for duplicate file mentions in the Agent Panel (#59845)

Yash Patodkar6e085b2fdc0a
diff --git a/crates/agent_ui/src/mention_set.rs b/crates/agent_ui/src/mention_set.rs
index cf3bf02b78c..506c68cc080 100644
--- a/crates/agent_ui/src/mention_set.rs
+++ b/crates/agent_ui/src/mention_set.rs
@@ -697,23 +697,41 @@ impl MentionSet {
}
}
-/// Computes disambiguated labels for a set of mentions. When multiple mentions
-/// share the same base name, their labels include extra context (additional
-/// parent path components for files/directories, source for skills) so the user
-/// can tell them apart. Driven by [`util::disambiguate::compute_disambiguation_details`],
-/// which is the same utility used for buffer tab titles and the sidebar.
+/// Computes disambiguated labels for a set of mentions, so that mentions sharing
+/// a base name get extra context (parent path components, skill source) to tell
+/// them apart. Same approach as buffer tab titles and the sidebar.
fn compute_disambiguated_labels<'a>(
mentions: impl Iterator<Item = (CreaseId, &'a MentionUri)>,
) -> HashMap<CreaseId, SharedString> {
- let mentions: Vec<_> = mentions.collect();
+ let (ids, uris): (Vec<CreaseId>, Vec<&MentionUri>) = mentions.unzip();
+ ids.into_iter()
+ .zip(disambiguated_labels_for_uris(&uris))
+ .collect()
+}
+
+/// Labels for each URI, in input order. Duplicate URIs are collapsed first, so a
+/// mention added twice keeps its base name instead of being escalated to its
+/// full path by the collision-resolution loop.
+fn disambiguated_labels_for_uris(uris: &[&MentionUri]) -> Vec<SharedString> {
+ let mut seen: HashSet<&MentionUri> = HashSet::default();
+ let unique_uris: Vec<&MentionUri> = uris
+ .iter()
+ .copied()
+ .filter(|&uri| seen.insert(uri))
+ .collect();
+
let details =
- util::disambiguate::compute_disambiguation_details(&mentions, |(_, uri), detail| {
+ util::disambiguate::compute_disambiguation_details(&unique_uris, |uri, detail| {
uri.disambiguated_name(detail)
});
- mentions
- .into_iter()
- .zip(details)
- .map(|((id, uri), detail)| (id, uri.disambiguated_name(detail).into()))
+
+ let uri_to_detail: HashMap<&MentionUri, usize> = unique_uris.into_iter().zip(details).collect();
+
+ uris.iter()
+ .map(|uri| {
+ let detail = uri_to_detail.get(uri).copied().unwrap_or(0);
+ uri.disambiguated_name(detail).into()
+ })
.collect()
}
@@ -824,6 +842,27 @@ mod tests {
assert!(!is_raster_image_path(Path::new("/tmp/notes.txt")));
assert!(!is_raster_image_path(Path::new("/tmp/README")));
}
+
+ #[test]
+ fn test_disambiguated_labels_dedupe_identical_uris() {
+ // Mentioning the same file twice must not escalate the duplicates to
+ // their full path. Distinct files sharing a base name still disambiguate.
+ let foo_a = MentionUri::File {
+ abs_path: path!("/project/a/foo.rs").into(),
+ };
+
+ let foo_b = MentionUri::File {
+ abs_path: path!("/project/b/foo.rs").into(),
+ };
+
+ let uris = vec![&foo_a, &foo_a, &foo_b];
+ let labels = disambiguated_labels_for_uris(&uris);
+
+ assert_eq!(labels[0].as_ref(), "a/foo.rs");
+ assert_eq!(labels[2].as_ref(), "b/foo.rs");
+ // The duplicate keeps the same label rather than escalating to full path.
+ assert_eq!(labels[1].as_ref(), "a/foo.rs");
+ }
}
/// Inserts a list of images into the editor as context mentions.