Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Distribute lines across cursors when pasting from external sources (#48676)
Lukas Wirth9ad8c7a2a3d8
diff --git a/crates/collab/tests/integration/integration_tests.rs b/crates/collab/tests/integration/integration_tests.rsindex 766585b3b68..413aa802a1e 100644--- a/crates/collab/tests/integration/integration_tests.rs+++ b/crates/collab/tests/integration/integration_tests.rs@@ -2379,11 +2379,11 @@ async fn test_propagate_saves_and_fs_changes(.unwrap();buffer_b.read_with(cx_b, |buffer, _| {- assert_eq!(buffer.language().unwrap().name(), "Rust".into());+ assert_eq!(buffer.language().unwrap().name(), "Rust");});buffer_c.read_with(cx_c, |buffer, _| {- assert_eq!(buffer.language().unwrap().name(), "Rust".into());+ assert_eq!(buffer.language().unwrap().name(), "Rust");});buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "i-am-b, ")], None, cx));buffer_c.update(cx_c, |buf, cx| buf.edit([(0..0, "i-am-c, ")], None, cx));@@ -2486,17 +2486,17 @@ async fn test_propagate_saves_and_fs_changes(buffer_a.read_with(cx_a, |buffer, _| {assert_eq!(buffer.file().unwrap().path().as_ref(), rel_path("file1.js"));- assert_eq!(buffer.language().unwrap().name(), "JavaScript".into());+ assert_eq!(buffer.language().unwrap().name(), "JavaScript");});buffer_b.read_with(cx_b, |buffer, _| {assert_eq!(buffer.file().unwrap().path().as_ref(), rel_path("file1.js"));- assert_eq!(buffer.language().unwrap().name(), "JavaScript".into());+ assert_eq!(buffer.language().unwrap().name(), "JavaScript");});buffer_c.read_with(cx_c, |buffer, _| {assert_eq!(buffer.file().unwrap().path().as_ref(), rel_path("file1.js"));- assert_eq!(buffer.language().unwrap().name(), "JavaScript".into());+ assert_eq!(buffer.language().unwrap().name(), "JavaScript");});let new_buffer_a = project_adiff --git a/crates/editor/src/clangd_ext.rs b/crates/editor/src/clangd_ext.rsindex a6805b8f407..c52089ca6ac 100644--- a/crates/editor/src/clangd_ext.rs+++ b/crates/editor/src/clangd_ext.rs@@ -14,7 +14,7 @@ use crate::{Editor, SwitchSourceHeader, element::register_action};use project::lsp_store::clangd_ext::CLANGD_SERVER_NAME;fn is_c_language(language: &Language) -> bool {- language.name() == "C++".into() || language.name() == "C".into()+ language.name() == "C++" || language.name() == "C"}pub fn switch_source_header(diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 44e1b4be715..192412f2681 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -13821,7 +13821,7 @@ impl Editor {let language = snapshot.language_at(selection.head());let range = selection.range();if let Some(language) = language- && language.name() == "Markdown".into()+ && language.name() == "Markdown"{edit_for_markdown_paste(&snapshot,@@ -13880,16 +13880,30 @@ impl Editor {let mut edits = Vec::new();- for selection in old_selections.iter() {+ // When pasting text without metadata (e.g. copied from an+ // external editor using multiple cursors) and the number of+ // lines matches the number of selections, distribute one+ // line per cursor instead of pasting the whole text at each.+ let lines: Vec<&str> = clipboard_text.split('\n').collect();+ let distribute_lines =+ old_selections.len() > 1 && lines.len() == old_selections.len();++ for (ix, selection) in old_selections.iter().enumerate() {let language = snapshot.language_at(selection.head());let range = selection.range();+ let text_for_cursor: &str = if distribute_lines {+ lines[ix]+ } else {+ &clipboard_text+ };+let (edit_range, edit_text) = if let Some(language) = language- && language.name() == "Markdown".into()+ && language.name() == "Markdown"{- edit_for_markdown_paste(&snapshot, range, &clipboard_text, url.clone())+ edit_for_markdown_paste(&snapshot, range, text_for_cursor, url.clone())} else {- (range, clipboard_text.clone())+ (range, Cow::Borrowed(text_for_cursor))};edits.push((edit_range, edit_text));diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 695491b6ea7..7f5a84ebd32 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -8292,6 +8292,38 @@ async fn test_paste_content_from_other_app(cx: &mut TestAppContext) {"});}+#[gpui::test]+async fn test_paste_multiline_from_other_app_into_matching_cursors(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ cx.write_to_clipboard(ClipboardItem::new_string("alpha\nbeta\ngamma".into()));++ let mut cx = EditorTestContext::new(cx).await;++ // Paste into 3 cursors: each cursor should receive one line.+ cx.set_state("ˇ one ˇ two ˇ three");+ cx.update_editor(|e, window, cx| e.paste(&Paste, window, cx));+ cx.assert_editor_state("alphaˇ one betaˇ two gammaˇ three");++ // Paste into 2 cursors: line count doesn't match, so paste entire text at each cursor.+ cx.write_to_clipboard(ClipboardItem::new_string("alpha\nbeta\ngamma".into()));+ cx.set_state("ˇ one ˇ two");+ cx.update_editor(|e, window, cx| e.paste(&Paste, window, cx));+ cx.assert_editor_state("alpha\nbeta\ngammaˇ one alpha\nbeta\ngammaˇ two");++ // Paste into a single cursor: should paste everything as-is.+ cx.write_to_clipboard(ClipboardItem::new_string("alpha\nbeta\ngamma".into()));+ cx.set_state("ˇ one");+ cx.update_editor(|e, window, cx| e.paste(&Paste, window, cx));+ cx.assert_editor_state("alpha\nbeta\ngammaˇ one");++ // Paste with selections: each selection is replaced with its corresponding line.+ cx.write_to_clipboard(ClipboardItem::new_string("xx\nyy\nzz".into()));+ cx.set_state("«aˇ» one «bˇ» two «cˇ» three");+ cx.update_editor(|e, window, cx| e.paste(&Paste, window, cx));+ cx.assert_editor_state("xxˇ one yyˇ two zzˇ three");+}+#[gpui::test]fn test_select_all(cx: &mut TestAppContext) {init_test(cx, |_| {});@@ -11304,7 +11336,11 @@ async fn test_autoclose_with_embedded_language(cx: &mut TestAppContext) {.collect::<Vec<_>>();assert_eq!(languages,- &["HTML".into(), "JavaScript".into(), "HTML".into()]+ &[+ LanguageName::from("HTML"),+ LanguageName::from("JavaScript"),+ LanguageName::from("HTML"),+ ]);});diff --git a/crates/editor/src/rust_analyzer_ext.rs b/crates/editor/src/rust_analyzer_ext.rsindex 41c062b5dfe..6ffdf1a248a 100644--- a/crates/editor/src/rust_analyzer_ext.rs+++ b/crates/editor/src/rust_analyzer_ext.rs@@ -22,7 +22,7 @@ use crate::{};fn is_rust_language(language: &Language) -> bool {- language.name() == "Rust".into()+ language.name() == "Rust"}pub fn apply_related_actions(editor: &Entity<Editor>, window: &mut Window, cx: &mut App) {diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rsindex 39af3142a46..49d871cc860 100644--- a/crates/language/src/buffer_tests.rs+++ b/crates/language/src/buffer_tests.rs@@ -267,7 +267,7 @@ async fn test_first_line_pattern(cx: &mut TestAppContext) {)).unwrap().name(),- "JavaScript".into()+ "JavaScript");}@@ -341,48 +341,48 @@ async fn test_language_for_file_with_custom_file_types(cx: &mut TestAppContext)let language = cx.read(|cx| languages.language_for_file(&file("foo.ts"), None, cx)).unwrap();- assert_eq!(language.name(), "TypeScript".into());+ assert_eq!(language.name(), "TypeScript");let language = cx.read(|cx| languages.language_for_file(&file("foo.ts.ecmascript"), None, cx)).unwrap();- assert_eq!(language.name(), "TypeScript".into());+ assert_eq!(language.name(), "TypeScript");let language = cx.read(|cx| languages.language_for_file(&file("foo.cpp"), None, cx)).unwrap();- assert_eq!(language.name(), "C++".into());+ assert_eq!(language.name(), "C++");// user configured lang extension, same length as system-providedlet language = cx.read(|cx| languages.language_for_file(&file("foo.js"), None, cx)).unwrap();- assert_eq!(language.name(), "TypeScript".into());+ assert_eq!(language.name(), "TypeScript");let language = cx.read(|cx| languages.language_for_file(&file("foo.c"), None, cx)).unwrap();- assert_eq!(language.name(), "C++".into());+ assert_eq!(language.name(), "C++");// user configured lang extension, longer than system-providedlet language = cx.read(|cx| languages.language_for_file(&file("foo.longer.ts"), None, cx)).unwrap();- assert_eq!(language.name(), "JavaScript".into());+ assert_eq!(language.name(), "JavaScript");// user configured lang extension, shorter than system-providedlet language = cx.read(|cx| languages.language_for_file(&file("foo.ecmascript"), None, cx)).unwrap();- assert_eq!(language.name(), "JavaScript".into());+ assert_eq!(language.name(), "JavaScript");// user configured glob matcheslet language = cx.read(|cx| languages.language_for_file(&file("c-plus-plus.dev"), None, cx)).unwrap();- assert_eq!(language.name(), "C++".into());+ assert_eq!(language.name(), "C++");// should match Dockerfile.* => Dockerfile, not *.dev => C++let language = cx.read(|cx| languages.language_for_file(&file("Dockerfile.dev"), None, cx)).unwrap();- assert_eq!(language.name(), "Dockerfile".into());+ assert_eq!(language.name(), "Dockerfile");}fn file(path: &str) -> Arc<dyn File> {@@ -2827,7 +2827,7 @@ fn test_language_at_with_hidden_languages(cx: &mut App) {for point in [Point::new(0, 4), Point::new(0, 16)] {let config = snapshot.language_scope_at(point).unwrap();- assert_eq!(config.language_name(), "Markdown".into());+ assert_eq!(config.language_name(), "Markdown");let language = snapshot.language_at(point).unwrap();assert_eq!(language.name().as_ref(), "Markdown");@@ -2871,7 +2871,7 @@ fn test_language_at_for_markdown_code_block(cx: &mut App) {// Test points in the code linefor point in [Point::new(1, 4), Point::new(1, 6)] {let config = snapshot.language_scope_at(point).unwrap();- assert_eq!(config.language_name(), "Rust".into());+ assert_eq!(config.language_name(), "Rust");let language = snapshot.language_at(point).unwrap();assert_eq!(language.name().as_ref(), "Rust");@@ -2880,7 +2880,7 @@ fn test_language_at_for_markdown_code_block(cx: &mut App) {// Test points in the comment line to verify it's still detected as Rustfor point in [Point::new(2, 4), Point::new(2, 6)] {let config = snapshot.language_scope_at(point).unwrap();- assert_eq!(config.language_name(), "Rust".into());+ assert_eq!(config.language_name(), "Rust");let language = snapshot.language_at(point).unwrap();assert_eq!(language.name().as_ref(), "Rust");diff --git a/crates/language/src/language_registry.rs b/crates/language/src/language_registry.rsindex 226eaf544e4..d73a44fda33 100644--- a/crates/language/src/language_registry.rs+++ b/crates/language/src/language_registry.rs@@ -87,6 +87,18 @@ impl Borrow<str> for LanguageName {}}+impl PartialEq<str> for LanguageName {+ fn eq(&self, other: &str) -> bool {+ self.0.as_ref() == other+ }+}++impl PartialEq<&str> for LanguageName {+ fn eq(&self, other: &&str) -> bool {+ self.0.as_ref() == *other+ }+}+impl std::fmt::Display for LanguageName {fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {write!(f, "{}", self.0)diff --git a/crates/markdown_preview/src/markdown_preview_view.rs b/crates/markdown_preview/src/markdown_preview_view.rsindex 9b764e20370..79bd7f33290 100644--- a/crates/markdown_preview/src/markdown_preview_view.rs+++ b/crates/markdown_preview/src/markdown_preview_view.rs@@ -259,7 +259,7 @@ impl MarkdownPreviewView {if let Some(buffer) = buffer.as_singleton()&& let Some(language) = buffer.read(cx).language(){- return language.name() == "Markdown".into();+ return language.name() == "Markdown";}false}diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rsindex 285546a9aeb..70b29635d59 100644--- a/crates/project/src/git_store.rs+++ b/crates/project/src/git_store.rs@@ -1209,7 +1209,7 @@ impl GitStore {if buffer.read(cx).language()- .is_none_or(|lang| lang.name() != "Rust".into())+ .is_none_or(|lang| lang.name() != "Rust"){return Task::ready(Err(anyhow!("no permalink available")));}diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rsindex 2394542a761..9bd0be45ae3 100644--- a/crates/project/tests/integration/project_tests.rs+++ b/crates/project/tests/integration/project_tests.rs@@ -5089,7 +5089,7 @@ async fn test_save_as(cx: &mut gpui::TestAppContext) {buffer.edit([(0..0, "abc")], None, cx);assert!(buffer.is_dirty());assert!(!buffer.has_conflict());- assert_eq!(buffer.language().unwrap().name(), "Plain Text".into());+ assert_eq!(buffer.language().unwrap().name(), "Plain Text");});project.update(cx, |project, cx| {@@ -5112,7 +5112,7 @@ async fn test_save_as(cx: &mut gpui::TestAppContext) {);assert!(!buffer.is_dirty());assert!(!buffer.has_conflict());- assert_eq!(buffer.language().unwrap().name(), "Rust".into());+ assert_eq!(buffer.language().unwrap().name(), "Rust");});let opened_buffer = projectdiff --git a/crates/remote_server/src/remote_editing_tests.rs b/crates/remote_server/src/remote_editing_tests.rsindex 45af71b920a..a744f733e72 100644--- a/crates/remote_server/src/remote_editing_tests.rs+++ b/crates/remote_server/src/remote_editing_tests.rs@@ -678,7 +678,7 @@ async fn test_remote_lsp(cx: &mut TestAppContext, server_cx: &mut TestAppContextlet buffer_id = cx.read(|cx| {let buffer = buffer.read(cx);- assert_eq!(buffer.language().unwrap().name(), "Rust".into());+ assert_eq!(buffer.language().unwrap().name(), "Rust");buffer.remote_id()});@@ -690,7 +690,7 @@ async fn test_remote_lsp(cx: &mut TestAppContext, server_cx: &mut TestAppContext.get(buffer_id).unwrap();- assert_eq!(buffer.read(cx).language().unwrap().name(), "Rust".into());+ assert_eq!(buffer.read(cx).language().unwrap().name(), "Rust");});server_cx.read(|cx| {diff --git a/crates/repl/src/repl_editor.rs b/crates/repl/src/repl_editor.rsindex dacc20b70a6..6e061c3e2e3 100644--- a/crates/repl/src/repl_editor.rs+++ b/crates/repl/src/repl_editor.rs@@ -562,7 +562,7 @@ fn runnable_ranges(cx: &mut App,) -> (Vec<Range<Point>>, Option<Point>) {if let Some(language) = buffer.language()- && language.name() == "Markdown".into()+ && language.name() == "Markdown"{return (markdown_code_blocks(buffer, range, cx), None);}diff --git a/crates/repl/src/repl_sessions_ui.rs b/crates/repl/src/repl_sessions_ui.rsindex d768d9dbede..1dc2107adde 100644--- a/crates/repl/src/repl_sessions_ui.rs+++ b/crates/repl/src/repl_sessions_ui.rs@@ -106,7 +106,7 @@ pub fn init(cx: &mut App) {let editor_handle = cx.entity().downgrade();if let Some(language) = language- && language.name() == "Python".into()+ && language.name() == "Python"&& let (Some(project_path), Some(project)) = (project_path, project){let store = ReplStore::global(cx);diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rsindex 6edd3dadbef..1e57334be59 100644--- a/crates/zed/src/zed.rs+++ b/crates/zed/src/zed.rs@@ -3666,7 +3666,7 @@ mod tests {.language_at(MultiBufferOffset(0), cx).unwrap().name(),- "Rust".into()+ "Rust");});})@@ -3814,7 +3814,7 @@ mod tests {.language_at(MultiBufferOffset(0), cx).unwrap().name(),- "Rust".into()+ "Rust")});})