MarkdownDocument: numbered top-line asks, clamped when placed, reveal waits for its read; tests run the frame callbacks
CI / platforms (windows-latest) (push) Failing after 9s
CI / site (push) Successful in 24s
CI / crate (push) Failing after 11m58s
CI / platforms (ubuntu-24.04) (push) Canceled after 0s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-12 02:02:56 -04:00
1 parent 433c38fb2d
commit abac8b41b3
5 files changed
+253 -165

No files matched your search

+13 -3
View File
@@ -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<usize>,
@@ -56,8 +65,8 @@ pub struct MarkdownDocument {
list: ListState,
open: Option<Open>,
/// A top line asked for, until the view holds it.
top: Option<usize>,
placing: bool,
top: Option<Placement>,
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);
+47 -34
View File
@@ -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<Self>) {
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<Self>) {
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>,
) {
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<Self>) {
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>) {
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);
}
@@ -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<MarkdownDocument>, cx: &mut VisualTestContext) -> String {
pub(super) fn text(document: &Entity<MarkdownDocument>, 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<MarkdownDocument>,
ix: Option<usize>,
typed: Option<&str>,
@@ -268,7 +244,7 @@ fn type_in(
cx.run_until_parked();
}
fn opened(document: &Entity<MarkdownDocument>, cx: &mut VisualTestContext) -> String {
pub(super) fn opened(document: &Entity<MarkdownDocument>, 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::<String>();
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;
+182
View File
@@ -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<MarkdownDocument>, 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::<String>();
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(&paragraphs(), 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(&paragraphs(), 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)]);
}