documents: alerts read their marker from parsed text; GFM's filtered tags show as text; block elements close what opened inside them; a TOC press relocates its heading after a write-back or a pending read and acts on press

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-13 23:42:55 -04:00
1 parent 2eec25140d
commit 98f19a8aac
7 files changed
+211 -69

No files matched your search

+20 -6
View File
@@ -1,6 +1,6 @@
use gpui::{App, Context, Focusable as _, ListOffset, ScrollStrategy, Window, px};
use super::{MarkdownDocument, Placement};
use super::{DocumentHeading, MarkdownDocument, Placement};
use crate::documents::{MarkdownMode, modes::joint};
/// Line and column, from zero, of byte `at` in `text`.
@@ -189,13 +189,27 @@ impl MarkdownDocument {
.saturating_sub(1)
}
/// Reveals the line holding byte `at` of the text.
/// Reveals the pressed heading, found again in the text.
pub(super) fn reveal_at(&mut self, at: usize, window: &mut Window, cx: &mut Context<Self>) {
let editor = self.editor.read(cx);
if at > editor.text().len() {
return log::error!("markdown document: a heading past the text");
let headings = &self.parsed.headings;
let Some(ix) = headings.iter().position(|heading| heading.at == at) else {
return log::warn!("markdown document: a heading read away");
};
let pressed = headings[ix].clone();
let same = |heading: &&DocumentHeading| {
(heading.depth, &heading.title) == (pressed.depth, &pressed.title)
};
let nth = headings[..ix].iter().filter(same).count();
if self.commit(cx).is_some() || !self.read_now(cx) {
self.reparse(true, cx);
}
let line = editor.buffer.line_of(at);
if !self.read_now(cx) {
return log::warn!("markdown document: a heading pressed while the text is read again");
}
let Some(found) = self.parsed.headings.iter().filter(same).nth(nth) else {
return log::warn!("markdown document: the pressed heading changed as one wrote");
};
let line = self.editor.read(cx).buffer.line_of(found.at);
self.reveal(line, window, cx);
}
+33 -1
View File
@@ -1,6 +1,6 @@
use gpui::{Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
use super::{MarkdownMode, open};
use super::{MarkdownMode, frames, open, type_in};
const LISTED: &str = "[toc]\n\n# A\n\ntext\n\n## B\n";
@@ -34,3 +34,35 @@ fn visual_reveals_on_a_secondary_press_and_edits_on_a_plain_one(cx: &mut TestApp
press_entry(0, Modifiers::none(), cx);
assert!(document.read_with(cx, |document, _| document.open.is_some()));
}
#[gpui::test]
fn a_press_after_an_earlier_block_grew_finds_the_heading(cx: &mut TestAppContext) {
let text = "before\n\n[toc]\n\n# A\n\ntext\n\n## B\n";
let (document, cx) = open(text, MarkdownMode::Visual, cx);
type_in(&document, Some(0), Some("before\nextra\nextra"), cx);
frames(cx);
press_entry(1, Modifiers::secondary_key(), cx);
frames(cx);
assert_eq!(
document.read_with(cx, |document, cx| document.caret(cx)),
(10, 0)
);
}
#[gpui::test]
fn a_press_while_the_text_waits_to_be_read_finds_the_heading(cx: &mut TestAppContext) {
let (document, cx) = open(LISTED, MarkdownMode::Split, cx);
document.update(cx, |document, cx| {
document.editor.update(cx, |editor, cx| {
editor.edit([(0..0, "inserted\n".repeat(20))], cx);
});
});
cx.run_until_parked();
assert!(!document.read_with(cx, |document, cx| document.read_now(cx)));
press_entry(1, Modifiers::none(), cx);
frames(cx);
assert_eq!(
document.read_with(cx, |document, cx| document.caret(cx)),
(26, 0)
);
}
+17 -3
View File
@@ -1,4 +1,6 @@
use gpui::{AnyElement, App, FontWeight, IntoElement, ParentElement, Styled, div, prelude::*};
use gpui::{
AnyElement, App, FontWeight, IntoElement, MouseButton, ParentElement, Styled, div, prelude::*,
};
use super::Draw;
use crate::{
@@ -144,13 +146,25 @@ impl Draw<'_> {
let Some(reveal) = self.on_heading.clone() else {
return entry.text_color(colors.fg_muted).into_any_element();
};
let at = heading.at;
let (at, pressed) = (heading.at, reveal.clone());
entry
.text_color(colors.link)
.tab_index(0)
.focus_ring(cx)
.cursor_pointer()
.on_click(move |_, window, cx| reveal(at, window, cx))
// On press: the layout may move before release.
.on_mouse_down(MouseButton::Left, move |event, window, cx| {
if event.modifiers.secondary() {
window.prevent_default();
cx.stop_propagation();
pressed(at, window, cx);
}
})
.on_click(move |event, window, cx| {
if !event.modifiers().secondary() {
reveal(at, window, cx);
}
})
.into_any_element()
}))
.into_any_element()
+83 -40
View File
@@ -36,7 +36,10 @@ impl AlertKind {
/// The kind `line` marks, as `[!NOTE]` in any case.
fn marked(line: &str) -> Option<Self> {
let name = line.trim().strip_prefix("[!")?.strip_suffix(']')?;
let name = line
.trim_matches([' ', '\t'])
.strip_prefix("[!")?
.strip_suffix(']')?;
[
Self::Note,
Self::Tip,
@@ -68,12 +71,10 @@ impl Reader<'_> {
let mdast::Node::Blockquote(quote) = node else {
return None;
};
let [first @ mdast::Node::Paragraph(paragraph), rest @ ..] = &quote.children[..] else {
let [mdast::Node::Paragraph(paragraph), rest @ ..] = &quote.children[..] else {
return None;
};
let line = self.source[span(first)].lines().next().unwrap_or_default();
let kind = AlertKind::marked(line)?;
let after = after_first_line(&paragraph.children);
let (kind, after) = marker(&paragraph.children)?;
if after.is_empty() && rest.is_empty() {
return None;
}
@@ -115,13 +116,16 @@ impl Reader<'_> {
let (written, after) = rest.split_at(end);
rest = after;
match html::tag(written) {
Some(tag) if !tag.closing && matches!(tag.name.as_str(), "script" | "style") => {
let closing = format!("</{}", tag.name);
let skipped = rest.to_ascii_lowercase().find(&closing);
let left = skipped.map_or("", |at| &rest[at..]);
rest = html::tag_end(left).map_or("", |end| &left[end..]);
Some(tag) if html::filtered(&tag.name) => run.words(written),
// A block closes what opened inside it.
Some(tag) if html::breaks(&tag.name) => {
run.line();
match tag.closing {
true => run.end(&tag.name),
false if tag.name != "hr" => run.start(&tag.name, None, None),
false => {}
}
}
Some(tag) if html::breaks(&tag.name) => run.line(),
// A line's end drops its space.
Some(tag) if tag.closing || tag.name == "br" => {
run.owed = run.owed.filter(|_| tag.name != "br");
@@ -165,39 +169,44 @@ impl Run {
/// Writes the white space owed, before a word or tag.
fn pay(&mut self) {
if let Some(owed) = self.owed.take() {
self.text.text.push(owed);
let Some(owed) = self.owed.take() else {
return;
};
let at = self.text.text.len();
self.text.text.push(owed);
// Reopened elements start after it.
for opened in self.open.iter_mut().filter(|opened| opened.start == at) {
opened.start = self.text.text.len();
}
}
}
/// Inlines past the first line ending.
fn after_first_line(children: &[mdast::Node]) -> Vec<mdast::Node> {
let mut found = None;
/// A plain first line naming a kind, and what follows.
fn marker(children: &[mdast::Node]) -> Option<(AlertKind, Vec<mdast::Node>)> {
let mut line = String::new();
for (at, child) in children.iter().enumerate() {
match child {
mdast::Node::Text(text) => {
if let Some((_, rest)) = text.value.split_once('\n') {
let mut kept = text.clone();
kept.value = rest.to_string();
found = Some((at + 1, (!rest.is_empty()).then_some(kept)));
break;
let head = match child {
mdast::Node::Text(text) => match text.value.find(['\n', '\r']) {
None => {
line.push_str(&text.value);
continue;
}
}
mdast::Node::Break(_) => {
found = Some((at + 1, None));
break;
}
_ => {}
}
Some(end) => {
line.push_str(&text.value[..end]);
let after = &text.value[end..];
let after = after.strip_prefix("\r\n").unwrap_or(&after[1..]);
let mut kept = text.clone();
kept.value = after.to_string();
(!after.is_empty()).then_some(mdast::Node::Text(kept))
}
},
mdast::Node::Break(_) => None,
_ => return None,
};
let after = head.into_iter().chain(children[at + 1..].iter().cloned());
return Some((AlertKind::marked(&line)?, after.collect()));
}
let Some((next, head)) = found else {
return Vec::new();
};
let head = head.map(mdast::Node::Text);
head.into_iter()
.chain(children[next..].iter().cloned())
.collect()
Some((AlertKind::marked(&line)?, Vec::new()))
}
#[cfg(test)]
@@ -239,6 +248,15 @@ mod tests {
("> [!IMPORTANT]", None),
("> [!FOO]\n> five", None),
("> text\n> [!NOTE]", None),
("> \\[!NOTE]\n> escaped", Some(AlertKind::Note)),
("> [!NOT&#69;]\n> referenced", Some(AlertKind::Note)),
("> [!NOTE] \n> hard line", Some(AlertKind::Note)),
("> [!NOTE]\rcarriage", Some(AlertKind::Note)),
("> [!NOTE]\nlazy", Some(AlertKind::Note)),
("> [!NOTE]\u{a0}\n> no-break space", None),
("> **[!TIP]**\n> strong", None),
("> <span>[!WARNING]</span>\n> tag", None),
("[!NOTE]: https://example.com\n\n> [!NOTE]\n> a link", None),
];
for (source, kind) in cases {
assert_eq!(alert(source).map(|found| found.0), kind, "{source:?}");
@@ -303,12 +321,34 @@ mod tests {
html("<div>\na <br> b</br>c</div>").expect("text").text,
"a\nb\nc"
);
let hidden = "<div>\n<script>alert(':smile:')</script>\n<!-- note -->\nshown<style>p{}</style>\n</div>";
assert_eq!(html(hidden).expect("text").text, "shown");
assert_eq!(html("<style>\np { color: red }\n</style>"), None);
let filtered = "<div>\n<script>x</script>\n<!-- note -->\nshown<style>p{}</style>\n</div>";
assert_eq!(
html(filtered).expect("text").text,
"<script>x</script> shown<style>p{}</style>"
);
let crossed = "<div>before<script>a</scriptx>b</script>after</div>";
assert_eq!(
html(crossed).expect("text").text,
"before<script>ab</script>after"
);
let split = parse("<div>\n<script>\nfirst\n\nsecond\n</script>\n</div>");
assert_eq!(text(&split), ["<script> first", "second </script>"]);
let inline = html("a <script>b</script> c").expect("text");
assert_eq!(inline.text, "a <script>b</script> c");
assert_eq!(html("<div>\na < b <i").expect("text").text, "a < b");
}
#[test]
fn a_closing_block_ends_what_opened_inside_it() {
let marked = html("<div><mark>x</div>y").expect("text");
assert_eq!(
(marked.text.as_str(), &marked.styles[..]),
("x\ny", &[(0..1, Style::Mark)][..])
);
let bold = html("<div><b>x</div>y").expect("text");
assert_eq!(bold.styles, [(0..1, Style::Strong), (2..3, Style::Strong)]);
}
#[test]
fn hostile_html_reads_in_linear_time() {
let started = web_time::Instant::now();
@@ -316,6 +356,9 @@ mod tests {
"<".repeat(1 << 17),
"<a".repeat(1 << 16),
"<!--".repeat(1 << 16),
"<b>".repeat(1 << 16),
"<b>".repeat(1 << 15) + &"</i>".repeat(1 << 15),
"<script></script>".repeat(1 << 13),
] {
let block = format!("<div>\n{source}");
parse(&block);
+16
View File
@@ -41,6 +41,22 @@ pub(crate) fn reopens(name: &str) -> bool {
)
}
/// GFM's tag filter: these tags show as text.
pub(crate) fn filtered(name: &str) -> bool {
matches!(
name,
"title"
| "textarea"
| "style"
| "xmp"
| "iframe"
| "noembed"
| "noframes"
| "script"
| "plaintext"
)
}
/// Elements a browser lays as blocks: a line each side.
pub(crate) fn breaks(name: &str) -> bool {
matches!(
+41 -18
View File
@@ -98,6 +98,8 @@ struct Opened {
struct Run {
text: Inline,
open: Vec<Opened>,
/// How many of each name are open.
named: HashMap<String, usize>,
/// Inside an autolink: its text stays as written.
verbatim: bool,
/// White space an HTML block owes the next word.
@@ -116,9 +118,28 @@ impl Run {
done
}
fn count(&self, name: &str) -> usize {
self.named.get(name).copied().unwrap_or(0)
}
/// Inside HTML `<code>`, where shortcodes stay text.
fn in_code(&self) -> bool {
self.open.iter().any(|opened| opened.name == "code")
self.count("code") > 0
}
/// Text as shown: shortcodes read, lines joined.
fn push_text(&mut self, written: &str) {
let shown = match self.verbatim || self.in_code() {
true => written.to_string(),
false => html::emoji(written),
};
let soft = shown.replace("\r\n", " ").replace(['\r', '\n'], " ");
self.text.text.push_str(&soft);
}
fn push(&mut self, opened: Opened) {
*self.named.entry(opened.name.clone()).or_default() += 1;
self.open.push(opened);
}
/// The innermost open link's address.
@@ -134,6 +155,9 @@ impl Run {
if tag.name == "br" {
return self.text.text.push('\n');
}
if html::filtered(&tag.name) {
return self.push_text(&html::text(written));
}
let Some(style) = html::inline_style(&tag.name) else {
return;
};
@@ -153,7 +177,7 @@ impl Run {
if name == "a" {
self.end("a");
}
self.open.push(Opened {
self.push(Opened {
name: name.to_string(),
start: self.text.text.len(),
style,
@@ -163,17 +187,23 @@ impl Run {
/// Ends the innermost `name`, as HTML5 repairs nesting.
fn end(&mut self, name: &str) {
let Some(at) = self.open.iter().rposition(|opened| opened.name == name) else {
if self.count(name) == 0 {
return;
};
}
let at = self
.open
.iter()
.rposition(|opened| opened.name == name)
.expect("a counted name is open");
let end = self.text.text.len();
let above = self.open.split_off(at + 1);
let ended = self.open.pop().expect("found at `at`");
close(&mut self.text, &ended, end);
for opened in above {
close(&mut self.text, &opened, end);
let closed = self.open.split_off(at);
for opened in &closed {
close(&mut self.text, opened, end);
*self.named.get_mut(&opened.name).expect("counted") -= 1;
}
for opened in closed.into_iter().skip(1) {
if html::reopens(&opened.name) {
self.open.push(Opened {
self.push(Opened {
start: end,
..opened
});
@@ -357,14 +387,7 @@ impl<'a> Reader<'a> {
fn inline_into(&self, node: &mdast::Node, run: &mut Run, images: &mut Images) {
let text = &mut run.text;
let (children, (name, style), url) = match node {
mdast::Node::Text(words) => {
let shown = match run.verbatim || run.in_code() {
true => words.value.clone(),
false => html::emoji(&words.value),
};
let soft = shown.replace("\r\n", " ").replace(['\r', '\n'], " ");
return run.text.text.push_str(&soft);
}
mdast::Node::Text(words) => return run.push_text(&words.value),
mdast::Node::Break(_) => return text.text.push('\n'),
mdast::Node::InlineCode(code) => return styled(text, &code.value, Style::Code),
mdast::Node::InlineMath(math) => return styled(text, &math.value, Style::Math),