diff --git a/AGENTS.md b/AGENTS.md index c1085f7..85c402a 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. Inline it reads as Typora's defaults: `:shortcode:` turns to its emoji as GitHub reads it (the `emojis` crate's names, GitHub's today, so `:cricket:` is the insect; read after escapes and references, so `\:smile:` turns too; inside an autolink or `` it stays text; within one DOM text node, which filtered tags do not split, the first `:name:`-shaped run decides for all: blank text or a space before it turns every shortcode, else none, so `x:smile: :smile:` stays), inline HTML's `u`, `b`/`strong`, `i`/`em`, `s`/`del`/`strike`, `code`/`kbd`, `mark`, `br` and `a href` draw as what they mean GFM's filtered tags (`script`, `style`, `title`, `textarea`, `iframe`, `xmp`, `noembed`, `noframes`, `plaintext`) show as text, and other tags, hyphenated names too, vanish, leaving their text; a tag's name runs to white space or `/`, as HTML5 reads it; attribute values split on ASCII whitespace alone and decode as HTML decodes them; each Markdown container is the element GitHub writes (`em`, `strong`, `del`, `a`), on one stack with the tags and repaired as HTML5 repairs: an end tag closes the innermost element of its name and all above it, of which only formatting elements (`a`, `b`, `code`, `em`, `i`, `s`, `strike`, `strong`, `u`) reopen, a new `a` ends the open one and an `a` without `href` links nothing; other tags are not tracked, and `
` breaks as `
` does; and a footnote reference is its number in superscript, numbered by first reference, as its definition is; one `~` still strikes. At the root, blocks read as GitHub shows them: a quote whose first line is plain text reading `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]` in any case, after escapes and references, spaces and tabs around it, with more after it, is an alert under its title (Important in the palette's violet, `syntax.keyword`, read at 4.5:1); a paragraph that is `[toc]` alone, as Typora reads it, lists every heading by depth, and a press on one reveals it as a link press does (`MarkdownDocument`'s `reveal`), the heading found where `CodeEditor` anchors carried it through each edit since the read, gone if an edit or an undo took it, and a secondary press acts on press, since a block writing back moves the list; front matter shows in a box without its fences; an HTML block shows its text with the inline tags above, white space collapsed and owed as a browser lays it, block elements breaking lines and closing what opened inside them, HTML5's start tags ending an open `p`, `fieldset` and `menu` too, and an `li` start an `li`, a `dd` or `dt` start either, as HTML5 ends them, comments dropped, the filtered tags shown as text, and an unclosed tag dropping the rest; a rule is a 2px `border_strong` line; bullets go •, ◦, ▪ by list depth, ordered lists counted. Exports read a tree as the drawing does through `MarkdownReader` (`block` gives its `MarkdownBlock`s, `inline` its text, `note` and `label` its footnotes, `headings` the `[toc]` list), one fresh stack per paragraph, heading or cell. `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. A written-back block, a mode switch or a new text is read at once up to 128 KiB, longer ones on the background executor; typing in source or split reads 300 ms after the last key, off the main thread (markdown-rs grows with the square of a paragraph's lines: 40,000 lines take 0.9 s). Texts past `LARGEST_READ` (2 MiB) are never read: no blocks, no outline. 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, and opens with the caret on the pressed character: each drawn piece keeps its written span and how it decodes (Markdown's escapes and references for text, as markdown-rs decodes them, autolink text as written, HTML's references for an HTML block's runs, none for code spans but a table cell's `\|`, cut as CommonMark cuts their end spaces), and is matched from its end, an equal character first, a drawn space next against a line end, decoded or not, so quote marks and indents at a line's start are never taken for text and an alert's text reads past its marker; text before an emoji lands at its piece's start; code, front matter, a link definition and an HTML block's runs read back line for line, LF, CRLF and CR alike, each drawn line ending its written one, a tab cut to spaces landing on the tab; a block's drawn positions count from its own start, so an edit before it keeps it alike. A secondary press opens nothing, an empty document's open on focus included: it writes back an open block, refuses the press if that changed the pressed block's kind, text or drawing (a definition's new address too), moves the caret there and emits `DocumentEvent::Pointed`, whose `DocumentTarget` names the block's kind, a link's or picture's address, and whether it lies in a table or code. `MarkdownDocument::format` toggles an `InlineFormat` at the selection as Typora's toggle does: inside that style it comes off; a selection is wrapped, its end spaces left out and the same style it partly covers taken in; a caret takes its word, or an empty pair between stop characters or on a blank line; `Clear` takes every inline mark off the blocks it touches; code, math, HTML and front matter refuse, as does a selection crossing a block, a link or another mark (`FormatRefusal`); in an open block it edits the field, which stays open, and joins its one write-back step. 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. Inline it reads as Typora's defaults: `:shortcode:` turns to its emoji as GitHub reads it (the `emojis` crate's names, GitHub's today, so `:cricket:` is the insect; read after escapes and references, so `\:smile:` turns too; inside an autolink or `` it stays text; within one DOM text node, which filtered tags do not split, the first `:name:`-shaped run decides for all: blank text or a space before it turns every shortcode, else none, so `x:smile: :smile:` stays), inline HTML's `u`, `b`/`strong`, `i`/`em`, `s`/`del`/`strike`, `code`/`kbd`, `mark`, `br` and `a href` draw as what they mean GFM's filtered tags (`script`, `style`, `title`, `textarea`, `iframe`, `xmp`, `noembed`, `noframes`, `plaintext`) show as text, and other tags, hyphenated names too, vanish, leaving their text; a tag's name runs to white space or `/`, as HTML5 reads it; attribute values split on ASCII whitespace alone and decode as HTML decodes them; each Markdown container is the element GitHub writes (`em`, `strong`, `del`, `a`), on one stack with the tags and repaired as HTML5 repairs: an end tag closes the innermost element of its name and all above it, of which only formatting elements (`a`, `b`, `code`, `em`, `i`, `s`, `strike`, `strong`, `u`) reopen, a new `a` ends the open one and an `a` without `href` links nothing; other tags are not tracked, and `
` breaks as `
` does; and a footnote reference is its number in superscript, numbered by first reference, as its definition is; one `~` still strikes. At the root, blocks read as GitHub shows them: a quote whose first line is plain text reading `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]` in any case, after escapes and references, spaces and tabs around it, with more after it, is an alert under its title (Important in the palette's violet, `syntax.keyword`, read at 4.5:1); a paragraph that is `[toc]` alone, as Typora reads it, lists every heading by depth, and a press on one reveals it as a link press does (`MarkdownDocument`'s `reveal`), the heading found where `CodeEditor` anchors carried it through each edit since the read, gone if an edit or an undo took it, and a secondary press acts on press, since a block writing back moves the list; front matter shows in a box without its fences; an HTML block shows its text with the inline tags above, white space collapsed and owed as a browser lays it, block elements breaking lines and closing what opened inside them, HTML5's start tags ending an open `p`, `fieldset` and `menu` too, and an `li` start an `li`, a `dd` or `dt` start either, as HTML5 ends them, comments dropped, the filtered tags shown as text, and an unclosed tag dropping the rest; a rule is a 2px `border_strong` line; bullets go •, ◦, ▪ by list depth, ordered lists counted. Exports read a tree as the drawing does through `MarkdownReader` (`block` gives its `MarkdownBlock`s, `inline` its text, `note` and `label` its footnotes, `headings` the `[toc]` list), one fresh stack per paragraph, heading or cell. `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. A written-back block, a mode switch or a new text is read at once up to 128 KiB, longer ones on the background executor; typing in source or split reads 300 ms after the last key, off the main thread (markdown-rs grows with the square of a paragraph's lines: 40,000 lines take 0.9 s). Texts past `LARGEST_READ` (2 MiB) are never read: no blocks, no outline. 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, and opens with the caret on the pressed character: each drawn piece keeps its written span and how it decodes (Markdown's escapes and references for text, as markdown-rs decodes them, autolink text as written, HTML's references for an HTML block's runs, none for code spans but a table cell's `\|`, cut as CommonMark cuts their end spaces), and is matched from its end, an equal character first, a drawn space next against a line end, decoded or not, so quote marks and indents at a line's start are never taken for text and an alert's text reads past its marker; text before an emoji lands at its piece's start; code, front matter, a link definition and an HTML block's runs read back line for line, LF, CRLF and CR alike, each drawn line ending its written one, a tab cut to spaces landing on the tab; a block's drawn positions count from its own start, so an edit before it keeps it alike. A secondary press opens nothing, an empty document's open on focus included: it writes back an open block, refuses the press if that changed the pressed block's kind, text or drawing (a definition's new address too), moves the caret there and emits `DocumentEvent::Pointed`, whose `DocumentTarget` names the block's kind, a link's or picture's address, and whether it lies in a table or code. `MarkdownDocument::format` toggles an `InlineFormat` at the selection as Typora's toggle does: inside that style it comes off; a selection is wrapped, its end spaces left out and the same style it partly covers taken in; a caret takes its word, or an empty pair between stop characters or on a blank line; `Clear` takes every inline mark off the blocks it touches, inline math's `$`, footnote brackets and escape backslashes too; code, math, HTML and front matter refuse, as does an end inside a node's marks or a link address, or a selection crossing a block, a link or another mark (`FormatRefusal`); past 128 KiB the toggle reads off the UI thread and lands only if text and selection are unchanged, and past the largest read it refuses; in an open block it edits the field, which stays open, and joins its one write-back step. 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/format.rs b/src/documents/document/format.rs index 94ee746..4d043bd 100644 --- a/src/documents/document/format.rs +++ b/src/documents/document/format.rs @@ -1,15 +1,14 @@ //! Inline formats toggle at the selection, as Typora's (R4.6). +mod apply; +mod clear; + use std::ops::Range; -use gpui::Context; use markdown::mdast::Node; -use super::MarkdownDocument; use crate::documents::{ - MarkdownMode, markdown::{file_tree, span, walk}, - modes::joint, tree::inside, }; @@ -40,6 +39,8 @@ pub enum FormatRefusal { Outside, /// The selection crosses a block or another mark. Across, + /// Past the largest read: source alone. + TooLong, } /// The edits and the selection after them. @@ -59,6 +60,22 @@ fn content(node: &Node) -> Range { } } +/// Room for text: a cell inside its pipes, end spaces too. +fn room(node: &Node, text: &str) -> Range { + let inner = match node { + Node::TableCell(_) => { + let whole = span(node); + let cell = &text[whole.clone()]; + whole.start + usize::from(cell.starts_with('|')) + ..whole.end - usize::from(cell.len() > 1 && cell.ends_with('|')) + } + _ => content(node), + }; + let rest = &text[inner.end..]; + let spaces = rest.len() - rest.trim_start_matches([' ', '\t']).len(); + inner.start..inner.end + spaces +} + fn block(node: &Node) -> bool { matches!( node, @@ -83,6 +100,11 @@ fn marks(node: &Node, text: &str) -> Option<[Range; 2]> { Some([whole.start..inner.start, inner.end..whole.end]) } +/// Whether `at` lies strictly inside one of `node`'s marks. +fn in_marks(node: &Node, text: &str, at: usize) -> bool { + marks(node, text).is_some_and(|holes| holes.iter().any(|hole| hole.start < at && at < hole.end)) +} + fn styled(format: InlineFormat, node: &Node) -> bool { match format { InlineFormat::Strong => matches!(node, Node::Strong(_)), @@ -95,6 +117,13 @@ fn styled(format: InlineFormat, node: &Node) -> bool { } } +fn merges(format: InlineFormat) -> bool { + matches!( + format, + InlineFormat::Strong | InlineFormat::Emphasis | InlineFormat::Strike + ) +} + /// `text[range]` without `holes`, sorted and inside it. fn cut(text: &str, range: Range, holes: &[Range]) -> String { let mut out = String::new(); @@ -156,31 +185,35 @@ fn verbatim( Ok(()) } -/// The `` and `` around `s..e` in `within`. +/// The nearest `` and `` pair around `s..e` in `within`. fn underline( nodes: &[&Node], within: &Range, s: usize, e: usize, ) -> Option<[Range; 2]> { - let tag = |node: &&&Node, name: &str| match node { - Node::Html(html) => html.value.trim().eq_ignore_ascii_case(name), - _ => false, - }; - let tags: Vec<(&Node, Range)> = nodes - .iter() - .filter(|node| matches!(node, Node::Html(_)) && within.contains(&span(node).start)) - .map(|node| (*node, span(node))) + let mut tags: Vec<(&str, Range)> = (nodes.iter()) + .filter_map(|node| match node { + Node::Html(html) => Some((html.value.trim(), span(node))), + _ => None, + }) + .filter(|(_, at)| within.start <= at.start && at.end <= within.end) .collect(); - let open = tags.iter().rfind(|(_, at)| at.end <= s)?; - let close = tags.iter().find(|(_, at)| at.start >= e)?; - let between = |from: usize, to: usize, name: &str| { - tags.iter() - .any(|(node, at)| at.start >= from && at.end <= to && tag(&node, name)) - }; - let paired = tag(&&open.0, "") && tag(&&close.0, ""); - (paired && !between(open.1.end, s, "") && !between(e, close.1.start, "")) - .then(|| [open.1.clone(), close.1.clone()]) + tags.sort_by_key(|(_, at)| at.start); + let mut opened = Vec::new(); + for (tag, at) in tags { + if tag.eq_ignore_ascii_case("") { + opened.push(at); + } else if tag.eq_ignore_ascii_case("") + && let Some(open) = opened.pop() + && open.end <= s + && e <= at.start + { + // Inner pairs close first. + return Some([open, at]); + } + } + None } /// `format` toggled at `selection` in `text`, or why not. @@ -194,21 +227,25 @@ pub(super) fn toggle( walk(&root, &mut nodes); verbatim(&nodes, &selection, format)?; if format == InlineFormat::Clear { - return clear(text, &nodes, selection); + return clear::clear(text, &nodes, selection); } let picked = &text[selection.clone()]; let s = selection.start + (picked.len() - picked.trim_start().len()); let e = (selection.end - (picked.len() - picked.trim_end().len())).max(s); - let holds = |whole: &Range| match s == e { - true => whole.start < s && s < whole.end, - false => whole.start <= s && e <= whole.end, + let holds = |node: &Node| { + let whole = span(node); + let inside = match s == e { + true => whole.start < s && s < whole.end, + false => whole.start <= s && e <= whole.end, + }; + inside && !in_marks(node, text, s) && !in_marks(node, text, e) }; let unwrap = |range: Range, holes: [Range; 2]| -> Change { let written = cut(text, range.clone(), &holes); (vec![(range, written)], after(s, &holes)..after(e, &holes)) }; let found = (nodes.iter()) - .filter(|node| styled(format, node) && holds(&span(node))) + .filter(|node| styled(format, node) && holds(node)) .min_by_key(|node| span(node).len()); if let Some(node) = found { return Ok(unwrap( @@ -216,19 +253,29 @@ pub(super) fn toggle( marks(node, text).expect("a styled node has marks"), )); } - let within = (nodes.iter()) + let rooms: Vec> = (nodes.iter()) .filter(|node| block(node)) - .map(|node| content(node)) + .map(|node| room(node, text)) + .collect(); + let within = (rooms.iter()) .filter(|range| range.start <= s && e <= range.end) - .min_by_key(|range| range.len()); + .min_by_key(|range| range.len()) + .cloned(); let Some(within) = within else { + if s != e { + let touched = (rooms.iter()).any(|range| range.start <= e && s <= range.end); + return Err(match touched { + true => FormatRefusal::Across, + false => FormatRefusal::Outside, + }); + } let line = text[..s] - .rsplit('\n') + .rsplit(['\r', '\n']) .next() .unwrap_or_default() .to_string() - + text[s..].split('\n').next().unwrap_or_default(); - return match s == e && line.trim().is_empty() { + + text[s..].split(['\r', '\n']).next().unwrap_or_default(); + return match line.trim().is_empty() { true => Ok(pair(s, format)), false => Err(FormatRefusal::Outside), }; @@ -299,7 +346,7 @@ fn pair(at: usize, format: InlineFormat) -> Change { (vec![(at..at, format!("{open}{close}"))], caret..caret) } -/// Refuses `s..e` cutting a node or landing in its marks. +/// Refuses an end in marks, or a mark split off. fn crossed( text: &str, nodes: &[&Node], @@ -308,18 +355,9 @@ fn crossed( e: usize, format: InlineFormat, ) -> Result<(), FormatRefusal> { - let merges = matches!( - format, - InlineFormat::Strong | InlineFormat::Emphasis | InlineFormat::Strike - ); for node in nodes { let whole = span(node); - let merged = merges && styled(format, node) && s <= whole.start && whole.end <= e; - if matches!(node, Node::Text(_)) - || merged - || whole.start < within.start - || whole.end > within.end - { + if matches!(node, Node::Text(_)) || whole.start < within.start || whole.end > within.end { continue; } let holes = match node { @@ -331,11 +369,12 @@ fn crossed( None => continue, }, }; - let holds = whole.start <= s && e <= whole.end; - let cuts = - whole.start < e && s < whole.end && !holds && !(s <= whole.start && whole.end <= e); - let in_mark = |at: usize| holes.iter().any(|hole| hole.start < at && at < hole.end); - if cuts || in_mark(s) || in_mark(e) { + let strictly = |at: usize| holes.iter().any(|hole| hole.start < at && at < hole.end); + let overlaps = holes.iter().any(|hole| s < hole.end && hole.start < e); + let holds_all = s <= whole.start && whole.end <= e; + // A same style cut is taken in whole. + let same = merges(format) && styled(format, node); + if strictly(s) || strictly(e) || (overlaps && !holds_all && !same) { return Err(FormatRefusal::Across); } } @@ -351,10 +390,8 @@ fn wrap( mut e: usize, format: InlineFormat, ) -> Result { - if matches!( - format, - InlineFormat::Strong | InlineFormat::Emphasis | InlineFormat::Strike - ) { + crossed(text, nodes, within, s, e, format)?; + if merges(format) { for node in nodes.iter().filter(|node| styled(format, node)) { let whole = span(node); if whole.start < e @@ -365,8 +402,8 @@ fn wrap( (s, e) = (s.min(whole.start), e.max(whole.end)); } } + crossed(text, nodes, within, s, e, format)?; } - crossed(text, nodes, within, s, e, format)?; let mut holes: Vec> = (nodes.iter()) .filter(|node| styled(format, node) && s <= span(node).start && span(node).end <= e) .flat_map(|node| marks(node, text).expect("a styled node has marks")) @@ -382,111 +419,3 @@ fn wrap( }; Ok((vec![(s..e, format!("{open}{inner}{close}"))], selection)) } - -/// Every inline mark in the blocks `selection` touches. -fn clear(text: &str, nodes: &[&Node], selection: Range) -> Result { - let blocks: Vec> = (nodes.iter()) - .filter(|node| block(node)) - .map(|node| content(node)) - .filter(|range| range.start <= selection.end && selection.start <= range.end) - .collect(); - if blocks.is_empty() { - return Err(FormatRefusal::Outside); - } - let held = |hole: &Range| { - blocks - .iter() - .any(|block| block.start <= hole.start && hole.end <= block.end) - }; - let mut holes: Vec> = (nodes.iter()) - .flat_map(|node| match node { - Node::Html(_) => vec![span(node)], - _ => marks(node, text).map(Vec::from).unwrap_or_default(), - }) - .filter(|hole| !hole.is_empty() && held(hole)) - .collect(); - holes.sort_by_key(|hole| hole.start); - let edits = (blocks.iter()) - .filter_map(|block| { - let inside: Vec> = (holes.iter()) - .filter(|hole| block.start <= hole.start && hole.end <= block.end) - .cloned() - .collect(); - (!inside.is_empty()).then(|| (block.clone(), cut(text, block.clone(), &inside))) - }) - .collect(); - Ok(( - edits, - after(selection.start, &holes)..after(selection.end, &holes), - )) -} - -impl MarkdownDocument { - /// `format` at the selection, Typora's way, or why not. - pub fn format( - &mut self, - format: InlineFormat, - cx: &mut Context, - ) -> Result<(), FormatRefusal> { - if self.mode == MarkdownMode::Preview { - return Err(FormatRefusal::Preview); - } - let text = self.editor.read(cx).text(); - let (full, offset, selection) = match &self.open { - Some(open) => { - let input = open.input.read(cx); - let (before, after) = match open.append { - true => (format!("{text}{}", joint(text)), ""), - false => ( - text[..open.range.start].to_string(), - &text[open.range.end..], - ), - }; - let picked = input.selection(); - let offset = before.len(); - let full = format!("{before}{}{after}", input.text()); - (full, offset, offset + picked.start..offset + picked.end) - } - None => (text.to_string(), 0, self.editor.read(cx).primary().range()), - }; - let (edits, selected) = toggle(&full, selection, format).inspect_err(|why| { - log::info!("markdown document: format {format:?} refused: {why:?}"); - })?; - log::info!("markdown document: format {format:?}"); - let Some(open) = &self.open else { - self.editor.update(cx, |editor, cx| { - editor.edit(edits, cx); - editor.select(std::iter::once(selected), cx); - }); - return Ok(()); - }; - let end = offset + open.input.read(cx).text().len(); - if !edits - .iter() - .all(|(range, _)| offset <= range.start && range.end <= end) - { - log::warn!("markdown document: format {format:?} reaches past the open block"); - return Err(FormatRefusal::Across); - } - let (Some(from), Some(to)) = ( - edits.iter().map(|(range, _)| range.start).min(), - edits.iter().map(|(range, _)| range.end).max(), - ) else { - return Ok(()); - }; - let mut written = String::new(); - let mut at = from; - for (range, with) in &edits { - written.push_str(&full[at..range.start]); - written.push_str(with); - at = range.end; - } - written.push_str(&full[at..to]); - open.input.update(cx, |input, cx| { - input.select(from - offset..to - offset, cx); - input.insert(&written, cx); - input.select(selected.start - offset..selected.end - offset, cx); - }); - Ok(()) - } -} diff --git a/src/documents/document/format/apply.rs b/src/documents/document/format/apply.rs new file mode 100644 index 0000000..7f8e5dc --- /dev/null +++ b/src/documents/document/format/apply.rs @@ -0,0 +1,137 @@ +//! The document's format command. + +use std::ops::Range; + +use gpui::{AppContext as _, Context}; + +use super::{Change, FormatRefusal, InlineFormat, toggle}; +use crate::documents::{ + MarkdownMode, + document::{AT_ONCE, LARGEST_READ, MarkdownDocument}, + modes::joint, +}; + +/// The text formatted, the open block's offset, the selection. +type Snapshot = (String, usize, Range); + +impl MarkdownDocument { + /// `format` at the selection, Typora's way, or why not. + /// + /// Long texts toggle off the UI thread; refusals then log. + pub fn format( + &mut self, + format: InlineFormat, + cx: &mut Context, + ) -> Result<(), FormatRefusal> { + if self.mode == MarkdownMode::Preview { + return Err(FormatRefusal::Preview); + } + let (full, offset, selection) = self.snapshot(cx); + if full.len() > LARGEST_READ { + log::info!( + "markdown document: format {format:?} refused: {} bytes", + full.len() + ); + return Err(FormatRefusal::TooLong); + } + if full.len() <= AT_ONCE { + let change = toggle(&full, selection, format).inspect_err(|why| { + log::info!("markdown document: format {format:?} refused: {why:?}"); + })?; + return self.apply(format, change, &full, offset, cx); + } + log::info!( + "markdown document: format {format:?} reads {} bytes apart", + full.len() + ); + let taken = (full.clone(), offset, selection.clone()); + self._formatting = Some(cx.spawn(async move |document, cx| { + let change = cx + .background_spawn(async move { toggle(&full, selection, format) }) + .await; + document + .update(cx, |document, cx| { + if document.snapshot(cx) != taken { + return log::warn!("markdown document: format {format:?} lapsed"); + } + match change { + // Apply logs its own refusal. + Ok(change) => document.apply(format, change, &taken.0, taken.1, cx).ok(), + Err(why) => { + log::info!("markdown document: format {format:?} refused: {why:?}"); + None + } + }; + }) + .ok(); + })); + Ok(()) + } + + /// The text a format reads: the open block's in place. + fn snapshot(&self, cx: &Context) -> Snapshot { + let text = self.editor.read(cx).text(); + let Some(open) = &self.open else { + return (text.to_string(), 0, self.editor.read(cx).primary().range()); + }; + let input = open.input.read(cx); + let (before, after) = match open.append { + true => (format!("{text}{}", joint(text)), ""), + false => ( + text[..open.range.start].to_string(), + &text[open.range.end..], + ), + }; + let picked = input.selection(); + let offset = before.len(); + let full = format!("{before}{}{after}", input.text()); + (full, offset, offset + picked.start..offset + picked.end) + } + + /// Writes `change`, into the open block if one is. + fn apply( + &mut self, + format: InlineFormat, + (edits, selected): Change, + full: &str, + offset: usize, + cx: &mut Context, + ) -> Result<(), FormatRefusal> { + log::info!("markdown document: format {format:?}"); + let Some(open) = &self.open else { + self.editor.update(cx, |editor, cx| { + editor.edit(edits, cx); + editor.select(std::iter::once(selected), cx); + }); + return Ok(()); + }; + let end = offset + open.input.read(cx).text().len(); + if !edits + .iter() + .all(|(range, _)| offset <= range.start && range.end <= end) + { + log::warn!("markdown document: format {format:?} reaches past the open block"); + return Err(FormatRefusal::Across); + } + let (Some(from), Some(to)) = ( + edits.iter().map(|(range, _)| range.start).min(), + edits.iter().map(|(range, _)| range.end).max(), + ) else { + return Ok(()); + }; + let mut written = String::new(); + let mut at = from; + for (range, with) in &edits { + written.push_str(&full[at..range.start]); + written.push_str(with); + at = range.end; + } + written.push_str(&full[at..to]); + open.input.update(cx, |input, cx| { + input.select(from - offset..to - offset, cx); + input.insert(&written, cx); + input.select(selected.start - offset..selected.end - offset, cx); + }); + Ok(()) + } +} diff --git a/src/documents/document/format/clear.rs b/src/documents/document/format/clear.rs new file mode 100644 index 0000000..60ee3f4 --- /dev/null +++ b/src/documents/document/format/clear.rs @@ -0,0 +1,79 @@ +//! Clear Format: every inline mark off the touched blocks. + +use std::ops::Range; + +use markdown::mdast::Node; + +use super::{Change, FormatRefusal, after, block, cut, marks, room}; +use crate::documents::{markdown::span, tree::inside}; + +/// The backslashes of escapes in a text node's span. +fn escapes(text: &str, whole: Range) -> Vec> { + let mut out = Vec::new(); + let mut chars = text[whole.clone()].char_indices().peekable(); + while let Some((at, ch)) = chars.next() { + if ch == '\\' + && chars + .peek() + .is_some_and(|(_, next)| next.is_ascii_punctuation()) + { + out.push(whole.start + at..whole.start + at + 1); + chars.next(); + } + } + out +} + +/// A node's inline marks, as Typora's `.md-meta`. +fn meta(node: &Node, text: &str) -> Vec> { + let whole = span(node); + match node { + Node::Html(_) => vec![whole], + Node::Text(_) => escapes(text, whole), + Node::InlineMath(_) => { + let inner = inside(text, whole.clone(), '$'); + vec![whole.start..inner.start, inner.end..whole.end] + } + Node::FootnoteReference(_) => vec![whole.start..whole.start + 2, whole.end - 1..whole.end], + _ => marks(node, text).map(Vec::from).unwrap_or_default(), + } +} + +/// Every inline mark in the blocks `selection` touches. +pub(super) fn clear( + text: &str, + nodes: &[&Node], + selection: Range, +) -> Result { + let blocks: Vec> = (nodes.iter()) + .filter(|node| block(node)) + .map(|node| room(node, text)) + .filter(|range| range.start <= selection.end && selection.start <= range.end) + .collect(); + if blocks.is_empty() { + return Err(FormatRefusal::Outside); + } + let held = |hole: &Range| { + blocks + .iter() + .any(|block| block.start <= hole.start && hole.end <= block.end) + }; + let mut holes: Vec> = (nodes.iter()) + .flat_map(|node| meta(node, text)) + .filter(|hole| !hole.is_empty() && held(hole)) + .collect(); + holes.sort_by_key(|hole| hole.start); + let edits = (blocks.iter()) + .filter_map(|block| { + let inside: Vec> = (holes.iter()) + .filter(|hole| block.start <= hole.start && hole.end <= block.end) + .cloned() + .collect(); + (!inside.is_empty()).then(|| (block.clone(), cut(text, block.clone(), &inside))) + }) + .collect(); + Ok(( + edits, + after(selection.start, &holes)..after(selection.end, &holes), + )) +} diff --git a/src/documents/document/mod.rs b/src/documents/document/mod.rs index 93c6ba2..dc1ea36 100644 --- a/src/documents/document/mod.rs +++ b/src/documents/document/mod.rs @@ -87,6 +87,7 @@ pub struct MarkdownDocument { /// Text changes counted; a read of an older one lapses. edits: u64, _parsing: Option>, + _formatting: Option>, list: ListState, open: Option, /// A top line asked for, until the view holds it. @@ -158,6 +159,7 @@ impl MarkdownDocument { parsed: Arc::default(), edits: 0, _parsing: None, + _formatting: None, list: ListState::new(1, ListAlignment::Top, overdraw), open: None, top: None, diff --git a/src/documents/document/tests/format.rs b/src/documents/document/tests/format.rs index 9019567..14f987c 100644 --- a/src/documents/document/tests/format.rs +++ b/src/documents/document/tests/format.rs @@ -1,11 +1,11 @@ //! Inline formats toggle as Typora's (R4.6). -use gpui::{Modifiers, TestAppContext}; +use gpui::{Entity, Modifiers, TestAppContext, VisualTestContext}; use super::{open, opened, press, text}; use crate::documents::{ - FormatRefusal, InlineFormat, MarkdownMode, - document::{format::toggle, visual}, + FormatRefusal, InlineFormat, LARGEST_READ, MarkdownDocument, MarkdownMode, + document::{AT_ONCE, format::toggle, visual}, }; /// `marked` toggled: `‸` is the caret, `⟨…⟩` the selection. @@ -178,3 +178,85 @@ fn preview_refuses_a_format(cx: &mut TestAppContext) { assert_eq!(refused, Err(FormatRefusal::Preview)); assert_eq!(text(&document, cx), "alpha\n"); } + +#[test] +fn an_end_in_marks_or_an_address_refuses() { + use InlineFormat::*; + let across = Err(FormatRefusal::Across); + assert_eq!(toggled("a *‸*bold** z", Strong), across); + assert_eq!(toggled("[a](ht‸tps://example.com)", Link), across); + assert_eq!(toggled("a *⟨*b** c⟩", Strong), across); + assert_eq!(toggled("*a⟨b*⟩", Strong), across); + assert_eq!(toggled("⟨alpha\n\nbeta⟩", Strong), across); +} + +#[test] +fn end_spaces_cells_and_cr_lines_take_a_pair() { + use InlineFormat::*; + assert_eq!(ok("alpha ‸", Strong), "alpha **‸**"); + assert_eq!(ok("| h |\n| - |\n| ‸ |", Strong), "| h |\n| - |\n| **‸** |"); + assert_eq!(ok("a\r‸\rb", Strong), "a\r**‸**\rb"); +} + +#[test] +fn clear_takes_math_footnote_and_escape_marks() { + use InlineFormat::Clear; + assert_eq!(ok("**a** $x‸+y$", Clear), "a x‸+y"); + assert_eq!(ok("a‸ [^n]\n\n[^n]: note", Clear), "a‸ n\n\n[^n]: note"); + assert_eq!(ok(r"a‸ \*b\*", Clear), "a‸ *b*"); +} + +#[test] +fn underline_comes_off_past_other_tags() { + use InlineFormat::Underline; + assert_eq!(ok("a
b‸c
", Underline), "a
b‸c"); + assert_eq!(ok("a b‸c d", Underline), "a b‸c d"); +} + +/// A source document of one long word, caret at 1. +fn long(cx: &mut TestAppContext, len: usize) -> (Entity, &mut VisualTestContext) { + let (document, cx) = open(&"x".repeat(len), MarkdownMode::Source, cx); + document.update(cx, |document, cx| { + document + .editor + .update(cx, |editor, cx| editor.select(std::iter::once(1..1), cx)); + }); + (document, cx) +} + +#[gpui::test] +fn a_long_text_formats_once_read_apart(cx: &mut TestAppContext) { + let (document, cx) = long(cx, AT_ONCE + 1); + document.update(cx, |document, cx| { + document.format(InlineFormat::Strong, cx).expect("taken") + }); + assert!( + !text(&document, cx).starts_with("**"), + "not on the UI thread" + ); + cx.run_until_parked(); + assert!(text(&document, cx).starts_with("**x")); +} + +#[gpui::test] +fn a_long_format_lapses_when_the_text_moves(cx: &mut TestAppContext) { + let (document, cx) = long(cx, AT_ONCE + 1); + document.update(cx, |document, cx| { + document.format(InlineFormat::Strong, cx).expect("taken"); + document + .editor + .update(cx, |editor, cx| editor.edit([(0..0, "y".to_string())], cx)); + }); + cx.run_until_parked(); + assert!( + text(&document, cx).starts_with("yx"), + "the typing stays, unformatted" + ); +} + +#[gpui::test] +fn a_text_past_the_largest_read_refuses(cx: &mut TestAppContext) { + let (document, cx) = long(cx, LARGEST_READ + 1); + let refused = document.update(cx, |document, cx| document.format(InlineFormat::Strong, cx)); + assert_eq!(refused, Err(FormatRefusal::TooLong)); +}