Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Limit `CopyHighlightJson` to selection (#46555)
Ian Chamberlain361428a4509b
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 6d9ee235f01..76ec95928dc 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -25043,7 +25043,7 @@ impl Editor {fn copy_highlight_json(&mut self,_: &CopyHighlightJson,- window: &mut Window,+ _: &mut Window,cx: &mut Context<Self>,) {#[derive(Serialize)]@@ -25053,23 +25053,19 @@ impl Editor {}let snapshot = self.buffer.read(cx).snapshot(cx);- let range = self- .selected_text_range(false, window, cx)- .and_then(|selection| {- if selection.range.is_empty() {- None- } else {- Some(- snapshot.offset_utf16_to_offset(MultiBufferOffsetUtf16(OffsetUtf16(- selection.range.start,- )))- ..snapshot.offset_utf16_to_offset(MultiBufferOffsetUtf16(OffsetUtf16(- selection.range.end,- ))),- )- }- })- .unwrap_or_else(|| MultiBufferOffset(0)..snapshot.len());+ let mut selection = self.selections.newest::<Point>(&self.display_snapshot(cx));+ let max_point = snapshot.max_point();++ let range = if self.selections.line_mode() {+ selection.start = Point::new(selection.start.row, 0);+ selection.end = cmp::min(max_point, Point::new(selection.end.row + 1, 0));+ selection.goal = SelectionGoal::None;+ selection.range()+ } else if selection.is_empty() {+ Point::new(0, 0)..max_point+ } else {+ selection.range()+ };let chunks = snapshot.chunks(range, true);let mut lines = Vec::new();@@ -25114,6 +25110,10 @@ impl Editor {}}+ if line.iter().any(|chunk| !chunk.text.is_empty()) {+ lines.push(line);+ }+let Some(lines) = serde_json::to_string_pretty(&lines).log_err() else {return;};diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 3fcd0f08fd5..dc84e826f6a 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -19253,6 +19253,212 @@ fn test_highlighted_ranges(cx: &mut TestAppContext) {});}+#[gpui::test]+async fn test_copy_highlight_json(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let mut cx = EditorTestContext::new(cx).await;+ cx.set_state(indoc! {"+ fn main() {+ let x = 1;ˇ+ }+ "});+ setup_rust_syntax_highlighting(&mut cx);++ cx.update_editor(|editor, window, cx| {+ editor.copy_highlight_json(&CopyHighlightJson, window, cx);+ });++ let clipboard_json: serde_json::Value =+ serde_json::from_str(&cx.read_from_clipboard().unwrap().text().unwrap()).unwrap();+ assert_eq!(+ clipboard_json,+ json!([+ [+ {"text": "fn", "highlight": "keyword"},+ {"text": " ", "highlight": null},+ {"text": "main", "highlight": "function"},+ {"text": "()", "highlight": "punctuation.bracket"},+ {"text": " ", "highlight": null},+ {"text": "{", "highlight": "punctuation.bracket"},+ ],+ [+ {"text": " ", "highlight": null},+ {"text": "let", "highlight": "keyword"},+ {"text": " ", "highlight": null},+ {"text": "x", "highlight": "variable"},+ {"text": " ", "highlight": null},+ {"text": "=", "highlight": "operator"},+ {"text": " ", "highlight": null},+ {"text": "1", "highlight": "number"},+ {"text": ";", "highlight": "punctuation.delimiter"},+ ],+ [+ {"text": "}", "highlight": "punctuation.bracket"},+ ],+ ])+ );+}++#[gpui::test]+async fn test_copy_highlight_json_selected_range(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let mut cx = EditorTestContext::new(cx).await;+ cx.set_state(indoc! {"+ fn main() {+ «let x = 1;+ let yˇ» = 2;+ }+ "});+ setup_rust_syntax_highlighting(&mut cx);++ cx.update_editor(|editor, window, cx| {+ editor.copy_highlight_json(&CopyHighlightJson, window, cx);+ });++ let clipboard_json: serde_json::Value =+ serde_json::from_str(&cx.read_from_clipboard().unwrap().text().unwrap()).unwrap();+ assert_eq!(+ clipboard_json,+ json!([+ [+ {"text": "let", "highlight": "keyword"},+ {"text": " ", "highlight": null},+ {"text": "x", "highlight": "variable"},+ {"text": " ", "highlight": null},+ {"text": "=", "highlight": "operator"},+ {"text": " ", "highlight": null},+ {"text": "1", "highlight": "number"},+ {"text": ";", "highlight": "punctuation.delimiter"},+ ],+ [+ {"text": " ", "highlight": null},+ {"text": "let", "highlight": "keyword"},+ {"text": " ", "highlight": null},+ {"text": "y", "highlight": "variable"},+ ],+ ])+ );+}++#[gpui::test]+async fn test_copy_highlight_json_selected_line_range(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let mut cx = EditorTestContext::new(cx).await;++ cx.set_state(indoc! {"+ fn main() {+ «let x = 1;+ let yˇ» = 2;+ }+ "});+ setup_rust_syntax_highlighting(&mut cx);++ cx.update_editor(|editor, window, cx| {+ editor.selections.set_line_mode(true);+ editor.copy_highlight_json(&CopyHighlightJson, window, cx);+ });++ let clipboard_json: serde_json::Value =+ serde_json::from_str(&cx.read_from_clipboard().unwrap().text().unwrap()).unwrap();+ assert_eq!(+ clipboard_json,+ json!([+ [+ {"text": " ", "highlight": null},+ {"text": "let", "highlight": "keyword"},+ {"text": " ", "highlight": null},+ {"text": "x", "highlight": "variable"},+ {"text": " ", "highlight": null},+ {"text": "=", "highlight": "operator"},+ {"text": " ", "highlight": null},+ {"text": "1", "highlight": "number"},+ {"text": ";", "highlight": "punctuation.delimiter"},+ ],+ [+ {"text": " ", "highlight": null},+ {"text": "let", "highlight": "keyword"},+ {"text": " ", "highlight": null},+ {"text": "y", "highlight": "variable"},+ {"text": " ", "highlight": null},+ {"text": "=", "highlight": "operator"},+ {"text": " ", "highlight": null},+ {"text": "2", "highlight": "number"},+ {"text": ";", "highlight": "punctuation.delimiter"},+ ],+ ])+ );+}++#[gpui::test]+async fn test_copy_highlight_json_single_line(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let mut cx = EditorTestContext::new(cx).await;++ cx.set_state(indoc! {"+ fn main() {+ let ˇx = 1;+ let y = 2;+ }+ "});+ setup_rust_syntax_highlighting(&mut cx);++ cx.update_editor(|editor, window, cx| {+ editor.selections.set_line_mode(true);+ editor.copy_highlight_json(&CopyHighlightJson, window, cx);+ });++ let clipboard_json: serde_json::Value =+ serde_json::from_str(&cx.read_from_clipboard().unwrap().text().unwrap()).unwrap();+ assert_eq!(+ clipboard_json,+ json!([+ [+ {"text": " ", "highlight": null},+ {"text": "let", "highlight": "keyword"},+ {"text": " ", "highlight": null},+ {"text": "x", "highlight": "variable"},+ {"text": " ", "highlight": null},+ {"text": "=", "highlight": "operator"},+ {"text": " ", "highlight": null},+ {"text": "1", "highlight": "number"},+ {"text": ";", "highlight": "punctuation.delimiter"},+ ]+ ])+ );+}++fn setup_rust_syntax_highlighting(cx: &mut EditorTestContext) {+ let syntax = SyntaxTheme::new_test(vec![+ ("keyword", Hsla::red()),+ ("function", Hsla::blue()),+ ("variable", Hsla::green()),+ ("number", Hsla::default()),+ ("operator", Hsla::default()),+ ("punctuation.bracket", Hsla::default()),+ ("punctuation.delimiter", Hsla::default()),+ ]);++ let language = rust_lang();+ language.set_theme(&syntax);++ cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));+ cx.executor().run_until_parked();+ cx.update_editor(|editor, window, cx| {+ editor.set_style(+ EditorStyle {+ syntax: Arc::new(syntax),+ ..Default::default()+ },+ window,+ cx,+ );+ });+}+#[gpui::test]async fn test_following(cx: &mut TestAppContext) {init_test(cx, |_| {});