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: Fix crash when mentioning a file with a line break in its name (#65693)

Smit Barmase9535c9d29d80
diff --git a/crates/acp_thread/src/mention.rs b/crates/acp_thread/src/mention.rs
index b4304a61a2c..226e649c135 100644
--- a/crates/acp_thread/src/mention.rs
+++ b/crates/acp_thread/src/mention.rs
@@ -8,6 +8,7 @@ use std::{
ops::RangeInclusive,
path::{Path, PathBuf},
};
+use text::LineEnding;
use ui::{App, IconName, SharedString};
use url::Url;
use urlencoding::decode;
@@ -590,7 +591,10 @@ pub struct MentionLink<'a>(&'a MentionUri);
impl fmt::Display for MentionLink<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "[@{}]({})", self.0.name(), self.0.to_uri())
+ // Match the buffer's line ending normalization, so the link's length matches the inserted text.
+ let mut name = self.0.name();
+ LineEnding::normalize(&mut name);
+ write!(f, "[@{name}]({})", self.0.to_uri())
}
}
@@ -1789,4 +1793,23 @@ mod tests {
assert_eq!(root_file.disambiguated_name(1), "README.md");
assert_eq!(root_file.disambiguated_name(5), "README.md");
}
+
+ #[test]
+ fn test_link_normalizes_line_endings_in_name() {
+ for (file_name, expected_link_name) in [
+ ("a\r\nb.txt", "a\nb.txt"),
+ ("a\rb.txt", "a\nb.txt"),
+ ("a\r\rb.txt", "a\n\nb.txt"),
+ ("a\nb.txt", "a\nb.txt"),
+ ] {
+ let uri = MentionUri::File {
+ abs_path: PathBuf::from(path!("/project")).join(file_name),
+ };
+ assert_eq!(uri.name(), file_name);
+ assert_eq!(
+ uri.as_link().to_string(),
+ format!("[@{expected_link_name}]({})", uri.to_uri())
+ );
+ }
+ }
}
diff --git a/crates/agent_ui/src/message_editor.rs b/crates/agent_ui/src/message_editor.rs
index 717f9f14f0d..ad5eca5cdf6 100644
--- a/crates/agent_ui/src/message_editor.rs
+++ b/crates/agent_ui/src/message_editor.rs
@@ -5208,6 +5208,40 @@ mod tests {
);
}
+ #[gpui::test]
+ async fn test_paste_external_file_path_with_line_breaks_in_name(cx: &mut TestAppContext) {
+ init_test(cx);
+ let (message_editor, editor, mut cx) =
+ setup_paste_test_message_editor(json!({"a\r\nb.txt": "content"}), cx).await;
+ paste_external_paths(
+ &message_editor,
+ vec![PathBuf::from(path!("/project/a\r\nb.txt"))],
+ &mut cx,
+ );
+
+ let expected_uri = MentionUri::File {
+ abs_path: path!("/project/a\r\nb.txt").into(),
+ }
+ .to_uri()
+ .to_string();
+
+ editor.update(&mut cx, |editor, cx| {
+ assert_eq!(editor.text(cx), format!("[@a\nb.txt]({expected_uri}) "));
+ });
+
+ let contents = mention_contents(&message_editor, &mut cx).await;
+ let [(uri, Mention::Text { content, .. })] = contents.as_slice() else {
+ panic!("Unexpected mentions");
+ };
+ assert_eq!(content, "content");
+ assert_eq!(
+ uri,
+ &MentionUri::File {
+ abs_path: path!("/project/a\r\nb.txt").into(),
+ }
+ );
+ }
+
#[gpui::test]
async fn test_paste_external_directory_path_inserts_directory_mention(cx: &mut TestAppContext) {
init_test(cx);
diff --git a/crates/agent_ui/src/ui/mention_crease.rs b/crates/agent_ui/src/ui/mention_crease.rs
index 17890ef167e..1bbb53146e2 100644
--- a/crates/agent_ui/src/ui/mention_crease.rs
+++ b/crates/agent_ui/src/ui/mention_crease.rs
@@ -1,4 +1,4 @@
-use std::{path::PathBuf, time::Duration};
+use std::{borrow::Cow, path::PathBuf, time::Duration};
use acp_thread::MentionUri;
use agent_client_protocol::schema::v2 as acp;
@@ -11,7 +11,7 @@ use language::Buffer;
use rope::Point;
use settings::Settings;
use theme_settings::ThemeSettings;
-use ui::{ButtonLike, TintColor, Tooltip, prelude::*};
+use ui::{ButtonLike, TintColor, Tooltip, prelude::*, utils::replace_control_characters};
use workspace::{OpenOptions, Workspace};
use crate::open_abs_path_at_point;
@@ -35,10 +35,14 @@ impl MentionCrease {
icon: impl Into<SharedString>,
label: impl Into<SharedString>,
) -> Self {
+ let mut label = label.into();
+ if let Cow::Owned(replaced) = replace_control_characters(&label) {
+ label = replaced.into();
+ }
Self {
id: id.into(),
icon: icon.into(),
- label: label.into(),
+ label,
mention_uri: None,
workspace: None,
is_toggled: false,