diff --git a/AGENTS.md b/AGENTS.md index b5e3824..a6bdd9c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,7 +13,7 @@ Ely GPUI Component. A component library for GPUI, in light and dark. - Web views: `wry` 0.57 (Apache-2.0 OR MIT, default features off, macOS only) lays a child WKWebView over the window. - Terminal: `alacritty_terminal` 0.26 (Apache-2.0, default features off, native only) for the grid, its parser and the pseudo-terminal; `futures` carries its events. `vte` 0.15 (Apache-2.0 OR MIT), the parser it re-exports, reads `AnsiText`'s codes alone, so colored output builds for wasm32. - Diffs: `similar` 3.2 (Apache-2.0) for line and word diffs and three-way merges; its `unicode` feature splits words at punctuation. -- Markdown: `markdown` 1.0.0 (markdown-rs, MIT) reads GitHub's flavor and math into a syntax tree with byte positions for `MarkdownRenderer`, `MarkdownEditor`, the highlights, block ranges and outline. `MarkdownRenderer` draws display math through `typography::Latex`, a ```` ```mermaid ```` block through `documents::Diagram`, and pictures from the owner's folder (`base`) or `data:` addresses (`base64` 0.22, MIT OR Apache-2.0); web pictures stay unloaded. Inline math shows as TeX: gpui text carries no inline pictures. `MarkdownDocument` holds a whole file in one `CodeEditor` (text and undo, colored by `CodeEditor::highlighter` with `markdown_highlights`): visual draws the file's top-level blocks in a gpui `list`, a pressed block opening as its source in a `TextInput` whose typing is the document's text at once and whose write-back is one undo step; source is the editor; preview draws without presses. Texts up to 128 KiB are read at once, longer ones on the background executor, and a new read keeps the list's blocks alike at both ends. One caret serves every mode: the editor's selection, which a written-back block moves; `set_caret` and `set_top_line` set caret and view apart; a top line asked before its text is read waits for the read, lands on its block, then a frame later at its share of the block's height; a switch between source and the list keeps the top line, one between visual and preview keeps the list's scroll as it is. A block opens only when the read is of the text as it is now. A pressed block keeps its identity across the write-back of the one before it. Blocks are Tab stops that Enter or Space opens; preview takes no presses, undo or redo. `CodeEditor` keeps at most 64 MiB of undo and redo text, dropping the oldest undo and then the farthest redo after every record, composition, undo and redo. `BlockData::from_markdown` and its export still read with `pulldown-cmark` 0.13 (MIT, default features off); it adds GitHub's alerts, a callout's form, and reads each field's inline Markdown from the source by offset, as written. +- Markdown: `markdown` 1.0.0 (markdown-rs, MIT) reads GitHub's flavor and math into a syntax tree with byte positions for `MarkdownRenderer`, `MarkdownEditor`, the highlights, block ranges and outline. `MarkdownRenderer` draws display math through `typography::Latex`, a ```` ```mermaid ```` block through `documents::Diagram`, and pictures from the owner's folder (`base`) or `data:` addresses (`base64` 0.22, MIT OR Apache-2.0); web pictures stay unloaded. Inline math shows as TeX: gpui text carries no inline pictures. `MarkdownDocument` holds a whole file in one `CodeEditor` (text and undo, colored by `CodeEditor::highlighter` with `markdown_highlights`): visual draws the file's top-level blocks in a gpui `list`, a pressed block opening as its source in a `TextInput` whose typing is the document's text at once and whose write-back is one undo step; source is the editor; preview draws without presses. Texts up to 128 KiB are read at once, longer ones on the background executor, and a new read keeps the list's blocks alike at both ends. One caret serves every mode: the editor's selection, which a written-back block moves; `set_caret` and `set_top_line` set caret and view apart; a top line asked (numbered, so a late frame callback finishes only its own ask; new text places it again, clamped to the text as it then is) waits for the read, lands on its block, then a frame later at its share of the block's height; `reveal` is such an ask with the caret at its line; a switch between source and the list keeps the top line, one between visual and preview keeps the list's scroll as it is. A block opens only when the read is of the text as it is now. A pressed block keeps its identity across the write-back of the one before it. Blocks are Tab stops that Enter or Space opens; preview takes no presses, undo or redo. `CodeEditor` keeps at most 64 MiB of undo and redo text, dropping the oldest undo and then the farthest redo after every record, composition, undo and redo. `BlockData::from_markdown` and its export still read with `pulldown-cmark` 0.13 (MIT, default features off); it adds GitHub's alerts, a callout's form, and reads each field's inline Markdown from the source by offset, as written. - Pictures: `image` 0.25 (MIT OR Apache-2.0), the crate gpui decodes with, default features off; it turns decoded frames and rims map tiles. - Math and diagrams: RaTeX 0.1.14 (`ratex-parser`, `ratex-layout`, `ratex-svg` with `embed-fonts`, `ratex-types`; MIT, KaTeX's fonts OFL) typesets `Latex` to SVG with outlined glyphs; `mermaid-rs-renderer` 0.3.1 (MIT, default features off) draws `Diagram`. Both go through gpui's `SvgRenderer` at the window's scale, loaded as gpui assets off the main thread; `usvg` 0.46, gpui's own, reads a diagram's size. A RaTeX or Mermaid panic on parsed input becomes the element's error. Each element holds its picture through `primitives::held`: a new source, or the element leaving the window, removes the old asset from gpui's cache and drops its image from the atlas after the frame. - Maps: Natural Earth's 110m countries (public domain). `assets/maps/countries.geojson`, trimmed by `scripts/countries.py` to an ISO code, a name and positions to 0.01°, backs `maps::WorldMap`; the gallery's tiles are cut from the same file, z0 to z3, by `scripts/tiles.py` with Pillow. diff --git a/src/documents/document/mod.rs b/src/documents/document/mod.rs index ace25a2..dc50239 100644 --- a/src/documents/document/mod.rs +++ b/src/documents/document/mod.rs @@ -35,6 +35,15 @@ pub enum DocumentEvent { Parsed, } +/// A top line asked, numbered so late callbacks know theirs. +#[derive(Clone, Copy, Debug)] +struct Placement { + line: usize, + ask: u64, + /// Scrolled; its frame callback finishes it. + placing: bool, +} + /// The block open as its source, in its own field. struct Open { range: Range, @@ -56,8 +65,8 @@ pub struct MarkdownDocument { list: ListState, open: Option, /// A top line asked for, until the view holds it. - top: Option, - placing: bool, + top: Option, + asks: u64, /// Its own write-backs whose changes are still to come. writes: usize, focus: FocusHandle, @@ -106,7 +115,7 @@ impl MarkdownDocument { list: ListState::new(1, ListAlignment::Top, overdraw), open: None, top: None, - placing: false, + asks: 0, writes: 0, focus: cx.focus_handle(), on_link: None, @@ -203,6 +212,7 @@ impl MarkdownDocument { log::error!("markdown document: the text changed under the open block"); self.drop_open(cx); } + self.replace_top(); self.edits += 1; cx.emit(DocumentEvent::Changed); self.reparse(cx); diff --git a/src/documents/document/place.rs b/src/documents/document/place.rs index 9ce01fc..0df7aaf 100644 --- a/src/documents/document/place.rs +++ b/src/documents/document/place.rs @@ -1,6 +1,6 @@ use gpui::{App, Context, Focusable as _, ListOffset, ScrollStrategy, Window, px}; -use super::MarkdownDocument; +use super::{MarkdownDocument, Placement}; use crate::documents::{MarkdownMode, modes::joint}; /// Line and column, from zero, of byte `at` in `text`. @@ -48,10 +48,10 @@ impl MarkdownDocument { /// The first line in view; block lines share its height. pub fn top_line(&self, cx: &App) -> usize { - if let Some(line) = self.top { - return line; - } let editor = self.editor.read(cx); + if let Some(top) = self.top { + return top.line.min(editor.buffer.lines() - 1); + } match self.mode { MarkdownMode::Source | MarkdownMode::Split => editor.top_line().unwrap_or(0), MarkdownMode::Visual | MarkdownMode::Preview => { @@ -74,23 +74,43 @@ impl MarkdownDocument { /// Scrolls `line` to the top; the caret stays. pub fn set_top_line(&mut self, line: usize, window: &mut Window, cx: &mut Context) { - let lines = self.editor.read(cx).buffer.lines(); - self.top = Some(line.min(lines - 1)); + self.asks += 1; + self.top = Some(Placement { + line, + ask: self.asks, + placing: false, + }); self.place_top(window, cx); } + /// A placement in flight goes stale; it is placed again. + pub(super) fn replace_top(&mut self) { + if let Some(top) = &mut self.top { + self.asks += 1; + (top.ask, top.placing) = (self.asks, false); + } + } + /// Moves the view to the asked top once read. pub(super) fn place_top(&mut self, window: &mut Window, cx: &mut Context) { - let Some(line) = self.top.filter(|_| !self.placing) else { + let Some(Placement { + line, + ask, + placing: false, + }) = self.top + else { return; }; + let editor = self.editor.read(cx); + let line = line.min(editor.buffer.lines() - 1); if matches!(self.mode, MarkdownMode::Source | MarkdownMode::Split) { - let scroll = self.editor.read(cx).scroll.clone(); - scroll.scroll_to_item_strict(line, ScrollStrategy::Top); - self.placing = true; + editor + .scroll + .scroll_to_item_strict(line, ScrollStrategy::Top); + self.placing(ask); // The editor's offset holds it from the next frame. - return cx.on_next_frame(window, move |document, window, cx| { - document.placed(line, None, window, cx) + return cx.on_next_frame(window, move |document, _, cx| { + document.placed(ask, None, cx) }); } if !self.read_now(cx) { @@ -110,25 +130,24 @@ impl MarkdownDocument { item_ix, offset_in_item: px(0.), }); - self.placing = true; + self.placing(ask); cx.notify(); // A block's height is known once drawn. - cx.on_next_frame(window, move |document, window, cx| { - document.placed(line, Some((item_ix, into)), window, cx) + cx.on_next_frame(window, move |document, _, cx| { + document.placed(ask, Some((item_ix, into)), cx) }); } - /// A frame after placing `line`; a newer ask goes next. - fn placed( - &mut self, - line: usize, - within: Option<(usize, f32)>, - window: &mut Window, - cx: &mut Context, - ) { - self.placing = false; - if self.top != Some(line) { - return self.place_top(window, cx); + fn placing(&mut self, ask: u64) { + if let Some(top) = self.top.as_mut().filter(|top| top.ask == ask) { + top.placing = true; + } + } + + /// A frame after placing ask `ask`; stale asks do nothing. + fn placed(&mut self, ask: u64, within: Option<(usize, f32)>, cx: &mut Context) { + if self.top.is_none_or(|top| top.ask != ask) { + return; } if let Some((item_ix, into)) = within && let Some(bounds) = self.list.bounds_for_item(item_ix) @@ -150,16 +169,10 @@ impl MarkdownDocument { .saturating_sub(1) } - /// Line `line` in view and focus, caret at start. + /// Line `line` at top and focused, caret at start. pub fn reveal(&mut self, line: usize, window: &mut Window, cx: &mut Context) { self.set_caret(line, 0, cx); - let at = self.editor.read(cx).primary().head; - match self.mode { - MarkdownMode::Source | MarkdownMode::Split => {} - MarkdownMode::Visual | MarkdownMode::Preview => { - self.list.scroll_to_reveal_item(self.block_at(at)) - } - } + self.set_top_line(line, window, cx); let focus = self.focus_handle(cx); window.focus(&focus, cx); } diff --git a/src/documents/document/tests.rs b/src/documents/document/tests/mod.rs similarity index 72% rename from src/documents/document/tests.rs rename to src/documents/document/tests/mod.rs index 502042c..534c72c 100644 --- a/src/documents/document/tests.rs +++ b/src/documents/document/tests/mod.rs @@ -7,7 +7,7 @@ use crate::documents::MarkdownMode; const TEXT: &str = "# Lift\n\nBlend toward white.\n\n- one\n- two\n"; -fn open<'a>( +pub(super) fn open<'a>( text: &str, mode: MarkdownMode, cx: &'a mut TestAppContext, @@ -32,16 +32,18 @@ fn press(ix: usize, modifiers: Modifiers, cx: &mut VisualTestContext) { cx.run_until_parked(); } -/// A few frames, so deferred scrolls land. -fn frames(cx: &mut VisualTestContext) { - for _ in 0..3 { +/// Frames with their next-frame callbacks, so scrolls land. +pub(super) fn frames(cx: &mut VisualTestContext) { + for _ in 0..4 { cx.run_until_parked(); cx.update(|window, _| window.refresh()); + cx.run_until_parked(); + cx.update(|window, cx| window.simulate_next_frame(cx)); } cx.run_until_parked(); } -fn text(document: &Entity, cx: &mut VisualTestContext) -> String { +pub(super) fn text(document: &Entity, cx: &mut VisualTestContext) -> String { document.read_with(cx, |document, cx| document.text(cx)) } @@ -145,32 +147,6 @@ fn down_at_a_blocks_last_line_opens_the_next(cx: &mut TestAppContext) { assert_eq!(opened, "Blend toward white."); } -#[gpui::test] -fn reveal_puts_the_caret_on_the_line(cx: &mut TestAppContext) { - let (document, cx) = open(TEXT, MarkdownMode::Source, cx); - document.update_in(cx, |document, window, cx| document.reveal(4, window, cx)); - assert_eq!( - document.read_with(cx, |document, cx| document.caret(cx)), - (4, 0) - ); - cx.simulate_input("x"); - assert!( - text(&document, cx).contains( - " -x- one" - ), - "keys land on the line" - ); - let outline: Vec<(String, usize)> = document.read_with(cx, |document, cx| { - document - .outline(cx) - .into_iter() - .map(|(heading, line)| (heading.title, line)) - .collect() - }); - assert_eq!(outline, [("Lift".to_string(), 0)]); -} - #[gpui::test] fn reset_starts_undo_afresh(cx: &mut TestAppContext) { let (document, cx) = open(TEXT, MarkdownMode::Visual, cx); @@ -253,7 +229,7 @@ fn down_past_the_last_block_keeps_it_open(cx: &mut TestAppContext) { } /// Opens block `ix` as shown, its field set to `typed`. -fn type_in( +pub(super) fn type_in( document: &Entity, ix: Option, typed: Option<&str>, @@ -268,7 +244,7 @@ fn type_in( cx.run_until_parked(); } -fn opened(document: &Entity, cx: &mut VisualTestContext) -> String { +pub(super) fn opened(document: &Entity, cx: &mut VisualTestContext) -> String { document.read_with(cx, |document, cx| { let open = document.open.as_ref().expect("a block is open"); open.input.read(cx).text().to_string() @@ -348,30 +324,6 @@ fn an_added_block_reports_its_caret_after_the_joint(cx: &mut TestAppContext) { } } -#[gpui::test] -fn caret_and_view_set_apart_and_carry_across_modes(cx: &mut TestAppContext) { - let long = (0..60).map(|n| format!("Line {n}\n\n")).collect::(); - let (document, cx) = open(&long, MarkdownMode::Source, cx); - document.update(cx, |document, cx| document.set_caret(4, 3, cx)); - document.update_in(cx, |document, window, cx| { - document.set_top_line(40, window, cx) - }); - frames(cx); - let source = document.read_with(cx, |document, cx| document.top_line(cx)); - assert_eq!(source, 40, "the source scrolled"); - let caret = document.read_with(cx, |document, cx| document.caret(cx)); - assert_eq!(caret, (4, 3), "the view moved alone"); - document.update_in(cx, |document, window, cx| { - document.set_mode(MarkdownMode::Visual, window, cx) - }); - frames(cx); - let (caret, top) = document.read_with(cx, |document, cx| { - (document.caret(cx), document.top_line(cx)) - }); - assert_eq!(caret, (4, 3), "one caret for every mode"); - assert_eq!(top, 40, "the view stays at its line"); -} - #[gpui::test] fn the_keyboard_reaches_and_opens_a_block(cx: &mut TestAppContext) { let (document, cx) = open(TEXT, MarkdownMode::Visual, cx); @@ -413,73 +365,4 @@ fn a_press_after_a_write_back_waits_for_the_long_read(cx: &mut TestAppContext) { assert_eq!(opened(&document, cx), "B", "once read, B opens whole"); } -/// A fenced block of `lines` lines, then a paragraph. -fn fenced(lines: usize) -> String { - let body: String = (0..lines).map(|n| format!("line {n}\n")).collect(); - format!("```\n{body}```\n\nAfter.\n") -} - -#[gpui::test] -fn a_top_line_inside_a_tall_block_survives_modes(cx: &mut TestAppContext) { - let (document, cx) = open(&fenced(100), MarkdownMode::Source, cx); - document.update_in(cx, |document, window, cx| { - document.set_top_line(40, window, cx) - }); - frames(cx); - document.update_in(cx, |document, window, cx| { - document.set_mode(MarkdownMode::Visual, window, cx) - }); - frames(cx); - let top = document.read_with(cx, |document, cx| document.top_line(cx)); - assert_eq!(top, 40); -} - -#[gpui::test] -fn visual_to_preview_keeps_the_offset_in_a_block(cx: &mut TestAppContext) { - let (document, cx) = open(&fenced(100), MarkdownMode::Visual, cx); - document.update(cx, |document, cx| { - document.list.scroll_to(gpui::ListOffset { - item_ix: 0, - offset_in_item: px(500.0), - }); - cx.notify(); - }); - frames(cx); - let before = document.read_with(cx, |document, _| document.list.logical_scroll_top()); - document.update_in(cx, |document, window, cx| { - document.set_mode(MarkdownMode::Preview, window, cx) - }); - frames(cx); - let after = document.read_with(cx, |document, _| document.list.logical_scroll_top()); - assert_eq!( - (after.item_ix, after.offset_in_item), - (before.item_ix, before.offset_in_item) - ); - assert_eq!(after.offset_in_item, px(500.0)); -} - -#[gpui::test] -fn a_top_line_asked_with_new_text_lands_once_read(cx: &mut TestAppContext) { - let (document, cx) = open("old", MarkdownMode::Visual, cx); - let fresh: String = (0..100).map(|n| format!("Paragraph {n}\n\n")).collect(); - document.update_in(cx, |document, window, cx| { - document.reset(fresh, cx); - document.set_top_line(40, window, cx); - }); - frames(cx); - let top = document.read_with(cx, |document, cx| document.top_line(cx)); - assert_eq!(top, 40); -} - -#[gpui::test] -fn a_second_top_line_asked_while_placing_wins(cx: &mut TestAppContext) { - let fresh: String = (0..100).map(|n| format!("Paragraph {n}\n\n")).collect(); - let (document, cx) = open(&fresh, MarkdownMode::Source, cx); - document.update_in(cx, |document, window, cx| { - document.set_mode(MarkdownMode::Visual, window, cx); - document.set_top_line(60, window, cx); - }); - frames(cx); - let top = document.read_with(cx, |document, cx| document.top_line(cx)); - assert_eq!(top, 60); -} +mod place; diff --git a/src/documents/document/tests/place.rs b/src/documents/document/tests/place.rs new file mode 100644 index 0000000..18ce4b4 --- /dev/null +++ b/src/documents/document/tests/place.rs @@ -0,0 +1,182 @@ +use gpui::{Entity, TestAppContext, VisualTestContext, px}; + +use super::{AT_ONCE, MarkdownDocument, MarkdownMode, TEXT, frames, open, text}; + +/// The view's top line once no ask waits. +fn landed(document: &Entity, cx: &mut VisualTestContext) -> usize { + frames(cx); + document.read_with(cx, |document, cx| { + assert!(document.top.is_none(), "the ask finished"); + document.top_line(cx) + }) +} + +/// A hundred paragraphs: line `2n` is the `n`th. +fn paragraphs() -> String { + (0..100).map(|n| format!("Paragraph {n}\n\n")).collect() +} + +/// A fenced block of `lines` lines, then a paragraph. +fn fenced(lines: usize) -> String { + let body: String = (0..lines).map(|n| format!("line {n}\n")).collect(); + format!("```\n{body}```\n\nAfter.\n") +} + +#[gpui::test] +fn caret_and_view_set_apart_and_carry_across_modes(cx: &mut TestAppContext) { + let long = (0..60).map(|n| format!("Line {n}\n\n")).collect::(); + let (document, cx) = open(&long, MarkdownMode::Source, cx); + document.update(cx, |document, cx| document.set_caret(4, 3, cx)); + document.update_in(cx, |document, window, cx| { + document.set_top_line(40, window, cx) + }); + frames(cx); + let source = document.read_with(cx, |document, cx| document.top_line(cx)); + assert_eq!(source, 40, "the source scrolled"); + let caret = document.read_with(cx, |document, cx| document.caret(cx)); + assert_eq!(caret, (4, 3), "the view moved alone"); + document.update_in(cx, |document, window, cx| { + document.set_mode(MarkdownMode::Visual, window, cx) + }); + frames(cx); + let (caret, top) = document.read_with(cx, |document, cx| { + (document.caret(cx), document.top_line(cx)) + }); + assert_eq!(caret, (4, 3), "one caret for every mode"); + assert_eq!(top, 40, "the view stays at its line"); +} + +#[gpui::test] +fn a_top_line_inside_a_tall_block_survives_modes(cx: &mut TestAppContext) { + let (document, cx) = open(&fenced(100), MarkdownMode::Source, cx); + document.update_in(cx, |document, window, cx| { + document.set_top_line(40, window, cx) + }); + frames(cx); + document.update_in(cx, |document, window, cx| { + document.set_mode(MarkdownMode::Visual, window, cx) + }); + assert_eq!(landed(&document, cx), 40); +} + +#[gpui::test] +fn visual_to_preview_keeps_the_offset_in_a_block(cx: &mut TestAppContext) { + let (document, cx) = open(&fenced(100), MarkdownMode::Visual, cx); + document.update(cx, |document, cx| { + document.list.scroll_to(gpui::ListOffset { + item_ix: 0, + offset_in_item: px(500.0), + }); + cx.notify(); + }); + frames(cx); + let before = document.read_with(cx, |document, _| document.list.logical_scroll_top()); + document.update_in(cx, |document, window, cx| { + document.set_mode(MarkdownMode::Preview, window, cx) + }); + frames(cx); + let after = document.read_with(cx, |document, _| document.list.logical_scroll_top()); + assert_eq!( + (after.item_ix, after.offset_in_item), + (before.item_ix, before.offset_in_item) + ); + assert_eq!(after.offset_in_item, px(500.0)); +} + +#[gpui::test] +fn a_top_line_asked_with_new_text_lands_once_read(cx: &mut TestAppContext) { + let (document, cx) = open("old", MarkdownMode::Visual, cx); + let fresh: String = (0..100).map(|n| format!("Paragraph {n}\n\n")).collect(); + document.update_in(cx, |document, window, cx| { + document.reset(fresh, cx); + document.set_top_line(40, window, cx); + }); + assert_eq!(landed(&document, cx), 40); +} + +#[gpui::test] +fn a_second_top_line_asked_while_placing_wins(cx: &mut TestAppContext) { + let fresh: String = (0..100).map(|n| format!("Paragraph {n}\n\n")).collect(); + let (document, cx) = open(&fresh, MarkdownMode::Source, cx); + document.update_in(cx, |document, window, cx| { + document.set_mode(MarkdownMode::Visual, window, cx); + document.set_top_line(60, window, cx); + }); + assert_eq!(landed(&document, cx), 60); +} + +#[gpui::test] +fn a_switch_before_the_frame_keeps_the_ask(cx: &mut TestAppContext) { + let (document, cx) = open(¶graphs(), MarkdownMode::Source, cx); + document.update_in(cx, |document, window, cx| { + document.set_top_line(40, window, cx); + document.set_mode(MarkdownMode::Visual, window, cx); + }); + assert_eq!(landed(&document, cx), 40, "source to visual"); + document.update_in(cx, |document, window, cx| { + document.set_top_line(60, window, cx); + document.set_mode(MarkdownMode::Source, window, cx); + }); + assert_eq!(landed(&document, cx), 60, "visual to source"); +} + +#[gpui::test] +fn new_text_under_an_ask_places_it_again(cx: &mut TestAppContext) { + let (document, cx) = open(¶graphs(), MarkdownMode::Visual, cx); + document.update_in(cx, |document, window, cx| { + document.set_top_line(40, window, cx); + document.reset(fenced(100), cx); + document.set_top_line(40, window, cx); + }); + assert_eq!(landed(&document, cx), 40); +} + +#[gpui::test] +fn an_ask_past_a_shorter_text_lands_on_its_last_line(cx: &mut TestAppContext) { + let (document, cx) = open("old", MarkdownMode::Visual, cx); + let long = "Prose.\n\n".repeat(AT_ONCE / 8 + 1); + document.update_in(cx, |document, window, cx| { + document.reset(long, cx); + document.set_top_line(40, window, cx); + document.reset("short", cx); + }); + assert_eq!(landed(&document, cx), 0); +} + +#[gpui::test] +fn reveal_after_new_text_lands_once_read(cx: &mut TestAppContext) { + let (document, cx) = open("old", MarkdownMode::Visual, cx); + document.update_in(cx, |document, window, cx| { + document.reset(paragraphs(), cx); + document.reveal(80, window, cx); + }); + assert_eq!(landed(&document, cx), 80); + let caret = document.read_with(cx, |document, cx| document.caret(cx)); + assert_eq!(caret, (80, 0)); +} + +#[gpui::test] +fn reveal_puts_the_caret_on_the_line(cx: &mut TestAppContext) { + let (document, cx) = open(TEXT, MarkdownMode::Source, cx); + document.update_in(cx, |document, window, cx| document.reveal(4, window, cx)); + assert_eq!( + document.read_with(cx, |document, cx| document.caret(cx)), + (4, 0) + ); + cx.simulate_input("x"); + assert!( + text(&document, cx).contains( + " +x- one" + ), + "keys land on the line" + ); + let outline: Vec<(String, usize)> = document.read_with(cx, |document, cx| { + document + .outline(cx) + .into_iter() + .map(|(heading, line)| (heading.title, line)) + .collect() + }); + assert_eq!(outline, [("Lift".to_string(), 0)]); +}