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.

editor: Add support for pasting images in Markdown (#58588)

Khôi Tran715557fe51ce
diff --git a/crates/editor/src/clipboard.rs b/crates/editor/src/clipboard.rs
index 90818dbe313..02c4d6c818e 100644
--- a/crates/editor/src/clipboard.rs
+++ b/crates/editor/src/clipboard.rs
@@ -1,4 +1,5 @@
use super::*;
+use util::rel_path::RelPath;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ClipboardSelection {
@@ -263,6 +264,63 @@ impl Editor {
if self.read_only(cx) {
return;
}
+
+ let clipboard_image = item.entries().iter().find_map(|entry| match entry {
+ ClipboardEntry::Image(image) if !image.bytes.is_empty() => Some(image),
+ _ => None,
+ });
+
+ if let Some(image) = clipboard_image {
+ let is_markdown = {
+ let display_map = self.display_snapshot(cx);
+ let selections = self.selections.all::<MultiBufferOffset>(&display_map);
+ selections
+ .first()
+ .and_then(|s| display_map.buffer_snapshot().language_at(s.head()))
+ .map(|lang| lang.name() == "Markdown")
+ .unwrap_or(false)
+ };
+
+ if is_markdown {
+ let handled = maybe!({
+ let buffer = self.buffer().read(cx).as_singleton()?;
+ let file = buffer.read(cx).file()?;
+ let worktree_id = file.worktree_id(cx);
+ let dir_rel_path = file.path().parent()?.into_arc();
+ let worktree = self
+ .project
+ .as_ref()?
+ .read(cx)
+ .worktree_for_id(worktree_id, cx)?;
+
+ let extension = image.format.extension();
+ let snapshot = worktree.read(cx).snapshot();
+ let (filename, file_path) =
+ unused_image_path(&dir_rel_path, extension, |path| {
+ snapshot.entry_for_path(path).is_some()
+ })?;
+
+ let create_task = worktree.update(cx, |worktree, cx| {
+ worktree.create_entry(file_path, false, Some(image.bytes.clone()), cx)
+ });
+
+ cx.spawn_in(window, async move |editor, cx| {
+ create_task.await?;
+ editor.update_in(cx, |editor, window, cx| {
+ editor.insert_image_snippet(&filename, window, cx)
+ })
+ })
+ .detach_and_log_err(cx);
+
+ Some(())
+ });
+ if handled.is_some() {
+ // stop clipboard handling when the snippet is inserted
+ return;
+ }
+ }
+ }
+
let clipboard_string = item.entries().iter().find_map(|entry| match entry {
ClipboardEntry::String(s) => Some(s),
_ => None,
@@ -279,6 +337,26 @@ impl Editor {
}
}
+ fn insert_image_snippet(
+ &mut self,
+ filename: &str,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ let Some(snippet) = Snippet::parse(&format!("![$1]({filename})$0")).log_err() else {
+ return;
+ };
+ let display_map = self.display_snapshot(cx);
+ let insertion_ranges: Vec<Range<MultiBufferOffset>> = self
+ .selections
+ .all::<MultiBufferOffset>(&display_map)
+ .into_iter()
+ .map(|selection| selection.start..selection.end)
+ .collect();
+ self.insert_snippet(&insertion_ranges, snippet, window, cx)
+ .log_err();
+ }
+
pub(super) fn cut_common(
&mut self,
cut_no_selection_line: bool,
@@ -651,6 +729,26 @@ fn edit_for_markdown_paste<'a>(
(range, new_text)
}
+/// Returns a filename of the form `image.{extension}` (or `image_{N}.{extension}`
+/// if taken) that does not collide with an existing entry in `dir_rel_path`,
+/// along with the full path of the candidate file.
+fn unused_image_path(
+ dir_rel_path: &RelPath,
+ extension: &str,
+ exists: impl Fn(&RelPath) -> bool,
+) -> Option<(String, Arc<RelPath>)> {
+ let mut filename = format!("image.{extension}");
+ let mut counter = 1u32;
+ loop {
+ let candidate = dir_rel_path.join(RelPath::unix(&filename).ok()?);
+ if !exists(&candidate) {
+ return Some((filename, candidate));
+ }
+ filename = format!("image_{counter}.{extension}");
+ counter += 1;
+ }
+}
+
/// Whether `text` consists solely of a single URL, as opposed to merely
/// starting with a scheme-like prefix (e.g. a commit message like
/// `editor: Fix ...`, which `url::Url::parse` would accept).
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 43b30c471a2..bc0c36870c6 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -35641,6 +35641,273 @@ async fn test_paste_url_from_other_app_creates_markdown_link_selectively_in_mult
));
}
+#[gpui::test]
+async fn test_paste_image_in_markdown_saves_file_and_inserts_markdown(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let markdown_language = Arc::new(Language::new(
+ LanguageConfig {
+ name: "Markdown".into(),
+ ..LanguageConfig::default()
+ },
+ None,
+ ));
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/test", serde_json::json!({"test.md": ""}))
+ .await;
+ let project = Project::test(fs.clone(), [std::path::Path::new("/test")], cx).await;
+ let buffer = project
+ .update(cx, |project, cx| {
+ project.open_local_buffer("/test/test.md", cx)
+ })
+ .await
+ .unwrap();
+ buffer.update(cx, |buffer, cx| {
+ buffer.set_language(Some(markdown_language), cx);
+ });
+
+ let editor_window = cx.add_window(|window, cx| {
+ let editor = build_editor_with_project(
+ project,
+ MultiBuffer::build_from_buffer(buffer, cx),
+ window,
+ cx,
+ );
+ window.focus(&editor.focus_handle(cx), cx);
+ editor
+ });
+ cx.run_until_parked();
+ let mut cx = EditorTestContext::for_editor(editor_window, cx).await;
+
+ let png_bytes: Vec<u8> = vec![1, 2, 3, 4, 5];
+ let image = gpui::Image::from_bytes(gpui::ImageFormat::Png, png_bytes.clone());
+
+ cx.set_state("ˇ");
+ cx.update_editor(|editor, window, cx| {
+ cx.write_to_clipboard(ClipboardItem::new_image(&image));
+ editor.paste(&Paste, window, cx);
+ });
+ cx.run_until_parked();
+
+ let buffer_text = cx.buffer_text();
+ assert!(
+ buffer_text.starts_with("![](image") && buffer_text.ends_with(".png)"),
+ "expected markdown image syntax, got: {buffer_text:?}"
+ );
+
+ // Cursor should land inside [] so the user can type alt text immediately.
+ let filename_in_parens = &buffer_text["![](".len()..buffer_text.len() - 1];
+ let expected_state = format!("![ˇ]({filename_in_parens})");
+ cx.assert_editor_state(&expected_state);
+
+ let filename = &buffer_text["![](".len()..buffer_text.len() - 1];
+ assert_eq!(
+ fs.read_file_sync(format!("/test/{filename}")).unwrap(),
+ png_bytes,
+ "image file contents should match clipboard bytes"
+ );
+}
+
+#[gpui::test]
+async fn test_paste_multiple_images_in_markdown_increments_filename(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let markdown_language = Arc::new(Language::new(
+ LanguageConfig {
+ name: "Markdown".into(),
+ ..LanguageConfig::default()
+ },
+ None,
+ ));
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/test", serde_json::json!({"test.md": ""}))
+ .await;
+ let project = Project::test(fs.clone(), [std::path::Path::new("/test")], cx).await;
+ let buffer = project
+ .update(cx, |project, cx| {
+ project.open_local_buffer("/test/test.md", cx)
+ })
+ .await
+ .unwrap();
+ buffer.update(cx, |buffer, cx| {
+ buffer.set_language(Some(markdown_language), cx);
+ });
+
+ let editor_window = cx.add_window(|window, cx| {
+ let editor = build_editor_with_project(
+ project,
+ MultiBuffer::build_from_buffer(buffer, cx),
+ window,
+ cx,
+ );
+ window.focus(&editor.focus_handle(cx), cx);
+ editor
+ });
+ cx.run_until_parked();
+ let mut cx = EditorTestContext::for_editor(editor_window, cx).await;
+
+ let png_bytes_1: Vec<u8> = vec![1, 2, 3, 4, 5];
+ let png_bytes_2: Vec<u8> = vec![6, 7, 8, 9, 10];
+ let png_bytes_3: Vec<u8> = vec![11, 12, 13, 14, 15];
+ let image_1 = gpui::Image::from_bytes(gpui::ImageFormat::Png, png_bytes_1.clone());
+ let image_2 = gpui::Image::from_bytes(gpui::ImageFormat::Png, png_bytes_2.clone());
+ let image_3 = gpui::Image::from_bytes(gpui::ImageFormat::Png, png_bytes_3.clone());
+
+ // Paste first image — should produce image.png
+ cx.set_state("ˇ");
+ cx.update_editor(|editor, window, cx| {
+ cx.write_to_clipboard(ClipboardItem::new_image(&image_1));
+ editor.paste(&Paste, window, cx);
+ });
+ cx.run_until_parked();
+ let text_after_first = cx.buffer_text();
+ assert_eq!(
+ text_after_first, "![](image.png)",
+ "first paste should produce image.png"
+ );
+ assert_eq!(fs.read_file_sync("/test/image.png").unwrap(), png_bytes_1);
+
+ // Paste second image at end — should produce image_1.png
+ cx.update_editor(|editor, window, cx| {
+ cx.write_to_clipboard(ClipboardItem::new_image(&image_2));
+ editor.paste(&Paste, window, cx);
+ });
+ cx.run_until_parked();
+ let text_after_second = cx.buffer_text();
+ assert!(
+ text_after_second.contains("![](image_1.png)"),
+ "second paste should produce image_1.png, got: {text_after_second:?}"
+ );
+ assert_eq!(fs.read_file_sync("/test/image_1.png").unwrap(), png_bytes_2);
+
+ // Paste third image — should produce image_2.png
+ cx.update_editor(|editor, window, cx| {
+ cx.write_to_clipboard(ClipboardItem::new_image(&image_3));
+ editor.paste(&Paste, window, cx);
+ });
+ cx.run_until_parked();
+ let text_after_third = cx.buffer_text();
+ assert!(
+ text_after_third.contains("![](image_2.png)"),
+ "third paste should produce image_2.png, got: {text_after_third:?}"
+ );
+ assert_eq!(fs.read_file_sync("/test/image_2.png").unwrap(), png_bytes_3);
+}
+
+#[gpui::test]
+async fn test_paste_image_in_non_markdown_does_not_insert_markdown(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let rust_language = Arc::new(Language::new(
+ LanguageConfig {
+ name: "Rust".into(),
+ ..LanguageConfig::default()
+ },
+ None,
+ ));
+
+ let mut cx = EditorTestContext::new(cx).await;
+ cx.update_buffer(|buffer, cx| buffer.set_language(Some(rust_language), cx));
+ cx.set_state("ˇ");
+
+ let image = gpui::Image::from_bytes(gpui::ImageFormat::Png, vec![1, 2, 3]);
+ cx.update_editor(|editor, window, cx| {
+ cx.write_to_clipboard(ClipboardItem::new_image(&image));
+ editor.paste(&Paste, window, cx);
+ });
+
+ cx.assert_editor_state("ˇ");
+}
+
+#[gpui::test]
+async fn test_paste_image_in_markdown_without_open_file_falls_through(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let markdown_language = Arc::new(Language::new(
+ LanguageConfig {
+ name: "Markdown".into(),
+ ..LanguageConfig::default()
+ },
+ None,
+ ));
+
+ // Build an editor whose buffer has no associated file path on disk.
+ let (editor, cx) = cx.add_window_view(|window, cx| {
+ let buffer = cx.new(|cx| language::Buffer::local("", cx));
+ buffer.update(cx, |buffer, cx| {
+ buffer.set_language(Some(markdown_language), cx);
+ });
+ let multibuffer = MultiBuffer::build_from_buffer(buffer, cx);
+ build_editor(multibuffer, window, cx)
+ });
+ let mut cx = EditorTestContext::for_editor_in(editor, cx).await;
+
+ cx.set_state("ˇ");
+ let image = gpui::Image::from_bytes(gpui::ImageFormat::Png, vec![1, 2, 3]);
+ cx.update_editor(|editor, window, cx| {
+ cx.write_to_clipboard(ClipboardItem::new_image(&image));
+ editor.paste(&Paste, window, cx);
+ });
+
+ cx.assert_editor_state("ˇ");
+}
+
+#[gpui::test]
+async fn test_paste_image_in_markdown_single_file_worktree_falls_through(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let markdown_language = Arc::new(Language::new(
+ LanguageConfig {
+ name: "Markdown".into(),
+ ..LanguageConfig::default()
+ },
+ None,
+ ));
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/root", serde_json::json!({"test.md": ""}))
+ .await;
+ let project = Project::test(fs.clone(), [std::path::Path::new("/root/test.md")], cx).await;
+ let buffer = project
+ .update(cx, |project, cx| {
+ project.open_local_buffer("/root/test.md", cx)
+ })
+ .await
+ .unwrap();
+ buffer.update(cx, |buffer, cx| {
+ buffer.set_language(Some(markdown_language), cx);
+ });
+
+ let editor_window = cx.add_window(|window, cx| {
+ let editor = build_editor_with_project(
+ project,
+ MultiBuffer::build_from_buffer(buffer, cx),
+ window,
+ cx,
+ );
+ window.focus(&editor.focus_handle(cx), cx);
+ editor
+ });
+ cx.run_until_parked();
+ let mut cx = EditorTestContext::for_editor(editor_window, cx).await;
+
+ cx.set_state("ˇ");
+ let image = gpui::Image::from_bytes(gpui::ImageFormat::Png, vec![1, 2, 3]);
+ cx.update_editor(|editor, window, cx| {
+ cx.write_to_clipboard(ClipboardItem::new_image(&image));
+ editor.paste(&Paste, window, cx);
+ });
+ cx.run_until_parked();
+
+ cx.assert_editor_state("ˇ");
+ assert!(
+ fs.read_file_sync("/root/image.png").is_err(),
+ "no image file should be created for a single-file worktree"
+ );
+}
+
#[gpui::test]
async fn test_race_in_multibuffer_save(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs
index 531145bcaa7..224fb5e2040 100644
--- a/crates/gpui/src/platform.rs
+++ b/crates/gpui/src/platform.rs
@@ -2317,6 +2317,21 @@ impl ImageFormat {
}
}
+ /// Returns the file extension for this image format (without leading dot).
+ pub const fn extension(self) -> &'static str {
+ match self {
+ ImageFormat::Png => "png",
+ ImageFormat::Jpeg => "jpg",
+ ImageFormat::Webp => "webp",
+ ImageFormat::Gif => "gif",
+ ImageFormat::Svg => "svg",
+ ImageFormat::Bmp => "bmp",
+ ImageFormat::Tiff => "tiff",
+ ImageFormat::Ico => "ico",
+ ImageFormat::Pnm => "pnm",
+ }
+ }
+
/// Returns the ImageFormat for the given mime type, including known aliases.
pub fn from_mime_type(mime_type: &str) -> Option<Self> {
use strum::IntoEnumIterator;