From c7a86079fc39c3bfd234ce5b7eca77204f10b7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Mon, 12 Oct 2026 10:06:57 -0400 Subject: [PATCH] documents: the selected Markdown, in source or an open block --- src/documents/document/place.rs | 10 ++++++++++ src/documents/document/tests/place.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/documents/document/place.rs b/src/documents/document/place.rs index 202f6c1..4ba8935 100644 --- a/src/documents/document/place.rs +++ b/src/documents/document/place.rs @@ -28,6 +28,16 @@ impl MarkdownDocument { point(&self.text(cx), open.range.start + joined + input.cursor()) } + /// The selected Markdown: the source's, or the open block's. + pub fn selected(&self, cx: &App) -> String { + let Some(open) = &self.open else { + let editor = self.editor.read(cx); + return editor.text()[editor.primary().range()].to_string(); + }; + let input = open.input.read(cx); + input.text()[input.selection()].to_string() + } + /// The caret to `line` and `column`, clamped to text. pub fn set_caret(&mut self, line: usize, column: usize, cx: &mut Context) { self.commit(cx); diff --git a/src/documents/document/tests/place.rs b/src/documents/document/tests/place.rs index 4e6613b..cdbddc8 100644 --- a/src/documents/document/tests/place.rs +++ b/src/documents/document/tests/place.rs @@ -198,3 +198,30 @@ fn a_reset_keeps_the_caret_and_the_top_line(cx: &mut TestAppContext) { }); assert_eq!((caret, top), ((40, 2), 30)); } + +#[gpui::test] +fn the_selection_reads_in_source_and_in_an_open_block(cx: &mut TestAppContext) { + let (document, cx) = open("# One\n\nTwo words.\n", MarkdownMode::Source, cx); + document.update(cx, |document, cx| { + document + .editor + .update(cx, |editor, cx| editor.select(std::iter::once(2..5), cx)) + }); + assert_eq!( + document.read_with(cx, |document, cx| document.selected(cx)), + "One" + ); + document.update_in(cx, |document, window, cx| { + document.set_mode(MarkdownMode::Visual, window, cx) + }); + frames(cx); + super::type_in(&document, Some(1), None, cx); + document.update(cx, |document, cx| { + let open = document.open.as_ref().expect("a block is open"); + open.input.update(cx, |input, cx| input.select(4..9, cx)); + }); + assert_eq!( + document.read_with(cx, |document, cx| document.selected(cx)), + "words" + ); +}