Compare commits
4
Commits
1d0354592b
...
0558f0c890
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0558f0c890
|
||
|
|
892e095025
|
||
|
|
b37031b00b
|
||
|
|
58e212d4ec
|
No files matched your search
@@ -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 `<code>` 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 `</br>` breaks as `<br>` 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 its nodes, not its bytes: in release, 128 KiB of one-letter paragraphs takes 1.7 s, 16 KiB 42 ms, while 128 KiB of prose takes 20 ms; a block-heavy text under the cutoff can still hold the UI thread past 100 ms, a known limit). 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 listLine truncated
|
||||
- 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 `<code>` 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 `</br>` breaks as `<br>` 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 its nodes, not its bytes: in release, 128 KiB of one-letter paragraphs takes 1.7 s, 16 KiB 42 ms, while 128 KiB of prose takes 20 ms; a block-heavy text under the cutoff can still hold the UI thread past 100 ms, a known limit). 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 listLine truncated
|
||||
- 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.
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
//! Edits read off the UI thread: formats and block commands.
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
use gpui::{AppContext as _, Context};
|
||||
|
||||
use super::{
|
||||
DocumentEvent, LARGEST_READ, MarkdownDocument,
|
||||
block::{self, BlockCommand},
|
||||
format::{Change, EditRefusal, InlineFormat, toggle},
|
||||
};
|
||||
use crate::documents::{MarkdownMode, modes::joint};
|
||||
|
||||
/// The text read, the open block's offset, the selection.
|
||||
type Snapshot = (String, usize, Range<usize>);
|
||||
|
||||
impl MarkdownDocument {
|
||||
/// `format` at the selection, read apart; refusals by event.
|
||||
pub fn format(
|
||||
&mut self,
|
||||
format: InlineFormat,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Result<(), EditRefusal> {
|
||||
self.apart(
|
||||
format!("format {format:?}"),
|
||||
move |full, selection| toggle(full, selection, format),
|
||||
cx,
|
||||
)
|
||||
}
|
||||
|
||||
/// `command` at the caret, read apart; refusals by event.
|
||||
pub fn block(
|
||||
&mut self,
|
||||
command: BlockCommand,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Result<(), EditRefusal> {
|
||||
self.apart(
|
||||
format!("block {command:?}"),
|
||||
move |full, selection| block::run(full, selection, command),
|
||||
cx,
|
||||
)
|
||||
}
|
||||
|
||||
/// Reads `read` off the UI thread, then writes its change.
|
||||
fn apart(
|
||||
&mut self,
|
||||
name: String,
|
||||
read: impl FnOnce(&str, Range<usize>) -> Result<Change, EditRefusal> + Send + 'static,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Result<(), EditRefusal> {
|
||||
if self.mode == MarkdownMode::Preview {
|
||||
return Err(EditRefusal::Preview);
|
||||
}
|
||||
let (full, offset, selection) = self.snapshot(cx);
|
||||
if full.len() > LARGEST_READ {
|
||||
log::info!("markdown document: {name} refused: {} bytes", full.len());
|
||||
return Err(EditRefusal::TooLong);
|
||||
}
|
||||
if self.editing {
|
||||
log::info!("markdown document: {name} refused: one runs");
|
||||
return Err(EditRefusal::Busy);
|
||||
}
|
||||
log::info!("markdown document: {name} reads {} bytes apart", full.len());
|
||||
let taken = (full.clone(), offset, selection.clone());
|
||||
self.editing = true;
|
||||
cx.spawn(async move |document, cx| {
|
||||
let change = cx
|
||||
.background_spawn(async move { read(&full, selection) })
|
||||
.await;
|
||||
document
|
||||
.update(cx, |document, cx| {
|
||||
document.editing = false;
|
||||
let done = if document.mode == MarkdownMode::Preview {
|
||||
Err(EditRefusal::Preview)
|
||||
} else if document.snapshot(cx) != taken {
|
||||
Err(EditRefusal::Changed)
|
||||
} else {
|
||||
change
|
||||
.and_then(|change| document.apply(&name, change, &taken.0, taken.1, cx))
|
||||
};
|
||||
if let Err(why) = done {
|
||||
log::info!("markdown document: {name} refused: {why:?}");
|
||||
cx.emit(DocumentEvent::EditRefused(why));
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The text an edit reads: the open block's in place.
|
||||
fn snapshot(&self, cx: &Context<Self>) -> 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 while it stays inside.
|
||||
fn apply(
|
||||
&mut self,
|
||||
name: &str,
|
||||
(edits, selected): Change,
|
||||
full: &str,
|
||||
offset: usize,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Result<(), EditRefusal> {
|
||||
log::info!("markdown document: {name}");
|
||||
if let Some(open) = &self.open {
|
||||
let end = offset + open.input.read(cx).text().len();
|
||||
let grown = (edits.iter().map(|(_, with)| with.len()).sum::<usize>() + end)
|
||||
.checked_sub(edits.iter().map(|(range, _)| range.len()).sum::<usize>());
|
||||
let inside = edits
|
||||
.iter()
|
||||
.all(|(range, _)| offset <= range.start && range.end <= end)
|
||||
&& grown.is_some_and(|grown| offset <= selected.start && selected.end <= grown);
|
||||
if inside {
|
||||
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);
|
||||
});
|
||||
return Ok(());
|
||||
}
|
||||
log::info!("markdown document: {name} reaches past the open block");
|
||||
self.commit(cx);
|
||||
if self.editor.read(cx).text() != full {
|
||||
log::error!("markdown document: {name}: the write-back is not the text read");
|
||||
return Err(EditRefusal::Changed);
|
||||
}
|
||||
}
|
||||
self.editor.update(cx, |editor, cx| {
|
||||
editor.edit(edits, cx);
|
||||
editor.select(std::iter::once(selected), cx);
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,446 @@
|
||||
//! Block commands at the caret, as Typora's Paragraph menu (R4.7).
|
||||
|
||||
mod check;
|
||||
mod heading;
|
||||
mod insert;
|
||||
mod list;
|
||||
mod quote;
|
||||
mod split;
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use super::format::{Change, EditRefusal};
|
||||
use crate::documents::markdown::{file_tree, span, walk};
|
||||
|
||||
/// A command of Typora's Paragraph menu.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum BlockCommand {
|
||||
/// Levels 1 to 6.
|
||||
Heading(u8),
|
||||
Paragraph,
|
||||
/// A level up; a paragraph turns level 6.
|
||||
Promote,
|
||||
/// A level down; level 6 turns a paragraph.
|
||||
Demote,
|
||||
Quote,
|
||||
OrderedList,
|
||||
BulletList,
|
||||
TaskList,
|
||||
Rule,
|
||||
CodeBlock,
|
||||
MathBlock,
|
||||
Toc,
|
||||
FrontMatter,
|
||||
Footnote,
|
||||
LinkDefinition,
|
||||
ParagraphBefore,
|
||||
ParagraphAfter,
|
||||
Delete,
|
||||
}
|
||||
|
||||
/// `command` at `selection` in `text`, or why not.
|
||||
pub(super) fn run(
|
||||
text: &str,
|
||||
selection: Range<usize>,
|
||||
command: BlockCommand,
|
||||
) -> Result<Change, EditRefusal> {
|
||||
let root = file_tree(text);
|
||||
let here = Here::new(text, &root, selection);
|
||||
match command {
|
||||
BlockCommand::Heading(_)
|
||||
| BlockCommand::Paragraph
|
||||
| BlockCommand::Promote
|
||||
| BlockCommand::Demote => heading::run(&here, command),
|
||||
BlockCommand::Quote => quote::run(&here),
|
||||
BlockCommand::OrderedList | BlockCommand::BulletList | BlockCommand::TaskList => {
|
||||
list::run(&here, command)
|
||||
}
|
||||
BlockCommand::FrontMatter => insert::front_matter(&here),
|
||||
BlockCommand::ParagraphBefore => insert::paragraph(&here, true),
|
||||
BlockCommand::ParagraphAfter => insert::paragraph(&here, false),
|
||||
BlockCommand::Delete => insert::delete(&here),
|
||||
_ => insert::run(&here, command),
|
||||
}
|
||||
}
|
||||
|
||||
/// The caret's place in the parsed text.
|
||||
pub(super) struct Here<'a> {
|
||||
pub(super) text: &'a str,
|
||||
pub(super) selection: Range<usize>,
|
||||
/// Root down to the deepest block at the selection's start.
|
||||
pub(super) path: Vec<&'a Node>,
|
||||
pub(super) root: &'a Node,
|
||||
/// Every node, in document order.
|
||||
pub(super) nodes: Vec<&'a Node>,
|
||||
}
|
||||
|
||||
impl<'a> Here<'a> {
|
||||
fn new(text: &'a str, root: &'a Node, selection: Range<usize>) -> Self {
|
||||
let mut nodes = Vec::new();
|
||||
walk(root, &mut nodes);
|
||||
Here {
|
||||
text,
|
||||
path: path(text, root, selection.start),
|
||||
selection,
|
||||
root,
|
||||
nodes,
|
||||
}
|
||||
}
|
||||
|
||||
/// The caret's block, unless the path ends on a container.
|
||||
pub(super) fn leaf(&self) -> Option<&'a Node> {
|
||||
let last = *self.path.last()?;
|
||||
(!holds(last)).then_some(last)
|
||||
}
|
||||
|
||||
/// The containers around `path[..upto]`.
|
||||
pub(super) fn containers(&self, upto: usize) -> Vec<&'a Node> {
|
||||
self.path[..upto]
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|node| container(node))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Whether the caret's line holds nothing past its prefix.
|
||||
pub(super) fn blank(&self) -> bool {
|
||||
let line = line_start(self.text, self.selection.start);
|
||||
let containers = self.containers(self.path.len());
|
||||
inside(self.text, line, &containers)
|
||||
.is_some_and(|at| self.text[at..line_end(self.text, line)].trim().is_empty())
|
||||
}
|
||||
|
||||
/// Root down to the deepest block at `at`.
|
||||
pub(super) fn path_at(&self, at: usize) -> Vec<&'a Node> {
|
||||
path(self.text, self.root, at)
|
||||
}
|
||||
|
||||
/// The common holder's index and the blocks the selection touches.
|
||||
pub(super) fn blocks(&self) -> Option<(usize, Vec<&'a Node>)> {
|
||||
let end = self.path_at(self.selection.end);
|
||||
let mut k = 0;
|
||||
while k + 1 < self.path.len()
|
||||
&& k + 1 < end.len()
|
||||
&& std::ptr::eq(self.path[k + 1], end[k + 1])
|
||||
{
|
||||
k += 1;
|
||||
}
|
||||
if !holds(self.path[k]) {
|
||||
return Some((k - 1, vec![self.path[k]]));
|
||||
}
|
||||
let (s, e) = (self.selection.start, self.selection.end);
|
||||
let kids: Vec<&Node> = (self.path[k].children().map(Vec::as_slice))
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.filter(|kid| span(kid).start <= e && s <= span(kid).end)
|
||||
.collect();
|
||||
(!kids.is_empty()).then_some((k, kids))
|
||||
}
|
||||
|
||||
/// Whether a paragraph ending on the line before joins `block`.
|
||||
pub(super) fn joins_before(&self, block: &Range<usize>, gone: &[&Node]) -> bool {
|
||||
let line = line_start(self.text, block.start);
|
||||
let Some(previous) = prev_line(self.text, line) else {
|
||||
return false;
|
||||
};
|
||||
let ended = (self.nodes.iter()).any(|node| {
|
||||
matches!(node, Node::Paragraph(_)) && line_start(self.text, span(node).end) == previous
|
||||
});
|
||||
ended && !self.opens(line, gone)
|
||||
}
|
||||
|
||||
/// Whether the line after `block` would join it as text.
|
||||
pub(super) fn joins_after(&self, block: &Range<usize>) -> bool {
|
||||
let Some(next) = next_line(self.text, block.end) else {
|
||||
return false;
|
||||
};
|
||||
let starts = (self.nodes.iter()).any(|node| {
|
||||
let at = span(node).start;
|
||||
line_start(self.text, at) == next
|
||||
&& match node {
|
||||
Node::Paragraph(_) => true,
|
||||
Node::ThematicBreak(_) => self.text[at..].starts_with('-'),
|
||||
_ => false,
|
||||
}
|
||||
});
|
||||
starts && !self.opens(next, &[])
|
||||
}
|
||||
|
||||
/// Whether `line` opens a container that stays and interrupts.
|
||||
fn opens(&self, line: usize, gone: &[&Node]) -> bool {
|
||||
let on = |node: &Node| line_start(self.text, span(node).start) == line;
|
||||
let left = |node: &Node| !gone.iter().any(|gone| std::ptr::eq(*gone, node));
|
||||
(self.nodes.iter()).any(|node| match node {
|
||||
Node::List(list) if left(node) => list
|
||||
.children
|
||||
.iter()
|
||||
.any(|item| on(item) && (!list.ordered || list.start == Some(1))),
|
||||
Node::Blockquote(_) => on(node) && left(node),
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Blockquotes, list items and footnote definitions.
|
||||
pub(super) fn container(node: &Node) -> bool {
|
||||
matches!(
|
||||
node,
|
||||
Node::Blockquote(_) | Node::ListItem(_) | Node::FootnoteDefinition(_)
|
||||
)
|
||||
}
|
||||
|
||||
/// Whether `node` is task item `holder`'s first text.
|
||||
pub(super) fn task_head(holder: &Node, node: &Node) -> bool {
|
||||
matches!(holder, Node::ListItem(item)
|
||||
if item.checked.is_some()
|
||||
&& item.children.first().is_some_and(|kid| std::ptr::eq(kid, node)))
|
||||
}
|
||||
|
||||
/// Where a heading-closing `#` run starts in `line`.
|
||||
pub(super) fn closing(line: &str) -> Option<usize> {
|
||||
let line = line.trim_end_matches([' ', '\t']);
|
||||
let kept = line.trim_end_matches('#');
|
||||
(kept.len() < line.len() && kept.ends_with([' ', '\t'])).then_some(kept.len())
|
||||
}
|
||||
|
||||
/// The byte to escape so `line` reads as prose.
|
||||
pub(super) fn unblocked(line: &str) -> Result<Option<usize>, EditRefusal> {
|
||||
let prose = |line: &str| {
|
||||
let root = file_tree(line);
|
||||
matches!(
|
||||
root.children().and_then(|kids| kids.first()),
|
||||
Some(Node::Paragraph(_))
|
||||
)
|
||||
};
|
||||
if prose(line) {
|
||||
return Ok(None);
|
||||
}
|
||||
let mark = line.find(|ch: char| ch.is_ascii_punctuation());
|
||||
match mark.filter(|mark| prose(&format!("{}\\{}", &line[..*mark], &line[*mark..]))) {
|
||||
Some(mark) => Ok(Some(mark)),
|
||||
None => Err(check::misread("a line stays a block")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the path goes on below `node`.
|
||||
fn holds(node: &Node) -> bool {
|
||||
container(node) || matches!(node, Node::Root(_) | Node::List(_))
|
||||
}
|
||||
|
||||
/// Root down to the deepest block at `at`.
|
||||
fn path<'a>(text: &str, root: &'a Node, at: usize) -> Vec<&'a Node> {
|
||||
let mut path = vec![root];
|
||||
while let Some(node) = path.last().copied().filter(|node| holds(node)) {
|
||||
let kids = node.children().map(Vec::as_slice).unwrap_or_default();
|
||||
let found = (kids.iter())
|
||||
.find(|kid| span(kid).contains(&at) || span(kid).end == at)
|
||||
.or_else(|| {
|
||||
// A caret before a block on its line is inside.
|
||||
(kids.iter()).find(|kid| {
|
||||
let start = span(kid).start;
|
||||
at < start && !text[at..start].contains(['\n', '\r'])
|
||||
})
|
||||
});
|
||||
let Some(kid) = found else {
|
||||
break;
|
||||
};
|
||||
path.push(kid);
|
||||
}
|
||||
path
|
||||
}
|
||||
|
||||
pub(super) fn line_start(text: &str, at: usize) -> usize {
|
||||
text[..at].rfind(['\n', '\r']).map_or(0, |at| at + 1)
|
||||
}
|
||||
|
||||
pub(super) fn line_end(text: &str, at: usize) -> usize {
|
||||
at + text[at..].find(['\n', '\r']).unwrap_or(text.len() - at)
|
||||
}
|
||||
|
||||
/// The start of the line after `at`'s.
|
||||
pub(super) fn next_line(text: &str, at: usize) -> Option<usize> {
|
||||
let end = line_end(text, at);
|
||||
let rest = &text[end..];
|
||||
let ending = if rest.starts_with("\r\n") { 2 } else { 1 };
|
||||
(end < text.len()).then_some(end + ending)
|
||||
}
|
||||
|
||||
/// The start of the line before `at`'s.
|
||||
pub(super) fn prev_line(text: &str, at: usize) -> Option<usize> {
|
||||
let start = line_start(text, at);
|
||||
let ending = if text[..start].ends_with("\r\n") {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
(start > 0).then(|| line_start(text, start - ending))
|
||||
}
|
||||
|
||||
/// The text's own line ending.
|
||||
pub(super) fn eol(text: &str) -> &'static str {
|
||||
match (text.contains("\r\n"), text.contains('\r')) {
|
||||
(true, _) => "\r\n",
|
||||
(false, true) => "\r",
|
||||
_ => "\n",
|
||||
}
|
||||
}
|
||||
|
||||
/// Columns before `at` on its line, tabs stopping at four.
|
||||
pub(super) fn column(text: &str, at: usize) -> usize {
|
||||
advance(0, &text[line_start(text, at)..at])
|
||||
}
|
||||
|
||||
fn advance(col: usize, run: &str) -> usize {
|
||||
run.chars().fold(col, |col, ch| match ch {
|
||||
'\t' => col / 4 * 4 + 4,
|
||||
_ => col + 1,
|
||||
})
|
||||
}
|
||||
|
||||
/// A list marker's or footnote label's bytes, then its spaces.
|
||||
pub(super) fn marker(text: &str, node: &Node) -> (usize, usize) {
|
||||
let start = span(node).start;
|
||||
let line = &text[start..line_end(text, start)];
|
||||
let mark = match node {
|
||||
Node::FootnoteDefinition(_) => line.find("]:").map_or(line.len(), |at| at + 2),
|
||||
_ if line.starts_with(['-', '*', '+']) => 1,
|
||||
_ => line.find(['.', ')']).map_or(line.len(), |at| at + 1),
|
||||
};
|
||||
let after = &line[mark..];
|
||||
let spaces = after.len() - after.trim_start_matches([' ', '\t']).len();
|
||||
(mark, spaces)
|
||||
}
|
||||
|
||||
/// The column a container's continuation lines start at.
|
||||
pub(super) fn indent(text: &str, node: &Node) -> usize {
|
||||
let start = span(node).start;
|
||||
let base = column(text, start);
|
||||
let (mark, spaces) = marker(text, node);
|
||||
if matches!(node, Node::FootnoteDefinition(_)) {
|
||||
return base + 4;
|
||||
}
|
||||
let after = advance(base + mark, &text[start + mark..start + mark + spaces]) - base - mark;
|
||||
let blank = text[start + mark..line_end(text, start)].trim().is_empty();
|
||||
match blank || !(1..=4).contains(&after) {
|
||||
true => base + mark + 1,
|
||||
false => base + mark + after,
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a container's text starts on its first line.
|
||||
pub(super) fn opener(text: &str, node: &Node) -> usize {
|
||||
let start = span(node).start;
|
||||
let (mark, spaces) = marker(text, node);
|
||||
let width = advance(
|
||||
column(text, start + mark),
|
||||
&text[start + mark..start + mark + spaces],
|
||||
) - column(text, start + mark);
|
||||
match (1..=4).contains(&width) {
|
||||
true => start + mark + spaces,
|
||||
false => start + mark + spaces.min(1),
|
||||
}
|
||||
}
|
||||
|
||||
/// Where `line`'s text starts inside `containers`; none when lazy.
|
||||
pub(super) fn inside(text: &str, line: usize, containers: &[&Node]) -> Option<usize> {
|
||||
let end = line_end(text, line);
|
||||
let mut at = line;
|
||||
for node in containers {
|
||||
if text[at..end].trim().is_empty() {
|
||||
return Some(end);
|
||||
}
|
||||
if matches!(node, Node::Blockquote(_)) {
|
||||
let lead = text[at..end].len() - text[at..end].trim_start_matches(' ').len();
|
||||
if lead > 3 || !text[at + lead..end].starts_with('>') {
|
||||
return None;
|
||||
}
|
||||
at += lead + 1;
|
||||
at += usize::from(text[at..end].starts_with([' ', '\t']));
|
||||
continue;
|
||||
}
|
||||
let start = span(node).start;
|
||||
if line <= start && start < end {
|
||||
at = opener(text, node);
|
||||
continue;
|
||||
}
|
||||
let want = indent(text, node);
|
||||
let mut col = column(text, at);
|
||||
while col < want {
|
||||
let ch = text[at..end]
|
||||
.chars()
|
||||
.next()
|
||||
.filter(|ch| matches!(ch, ' ' | '\t'))?;
|
||||
col = advance(col, &text[at..at + 1]);
|
||||
at += ch.len_utf8();
|
||||
}
|
||||
}
|
||||
Some(at)
|
||||
}
|
||||
|
||||
/// The prefix a new line takes inside `containers`.
|
||||
pub(super) fn prefix(text: &str, containers: &[&Node]) -> String {
|
||||
let mut out = String::new();
|
||||
for (ix, node) in containers.iter().enumerate() {
|
||||
if matches!(node, Node::Blockquote(_)) {
|
||||
out.push_str("> ");
|
||||
continue;
|
||||
}
|
||||
let start = span(node).start;
|
||||
let outer = inside(text, line_start(text, start), &containers[..ix]).unwrap_or(start);
|
||||
let width = indent(text, node).saturating_sub(column(text, outer.min(start)));
|
||||
out.push_str(&" ".repeat(width));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Where byte `at` lies once `edits`, sorted, are written.
|
||||
pub(super) fn shifted(at: usize, edits: &[(Range<usize>, String)]) -> usize {
|
||||
let mut moved = at;
|
||||
for (range, with) in edits {
|
||||
if range.end <= at {
|
||||
moved = moved + with.len() - range.len();
|
||||
} else if range.start < at {
|
||||
moved -= at - range.start;
|
||||
}
|
||||
}
|
||||
moved
|
||||
}
|
||||
|
||||
/// `edits`, sorted, and the caret mapped through them.
|
||||
pub(super) fn change(mut edits: Vec<(Range<usize>, String)>, caret: Range<usize>) -> Change {
|
||||
edits.sort_by_key(|(range, _)| range.start);
|
||||
assert!(
|
||||
edits
|
||||
.windows(2)
|
||||
.all(|pair| pair[0].0.end <= pair[1].0.start),
|
||||
"block edits overlap"
|
||||
);
|
||||
let caret = shifted(caret.start, &edits)..shifted(caret.end, &edits);
|
||||
(edits, caret)
|
||||
}
|
||||
|
||||
/// `edits`, sorted, with the caret already where it lands.
|
||||
pub(super) fn placed(mut edits: Vec<(Range<usize>, String)>, caret: usize) -> Change {
|
||||
edits.sort_by_key(|(range, _)| range.start);
|
||||
assert!(
|
||||
edits
|
||||
.windows(2)
|
||||
.all(|pair| pair[0].0.end <= pair[1].0.start),
|
||||
"block edits overlap"
|
||||
);
|
||||
(edits, caret..caret)
|
||||
}
|
||||
|
||||
/// Why `node` takes no block command at the caret.
|
||||
pub(super) fn refusal(node: &Node) -> EditRefusal {
|
||||
match node {
|
||||
Node::Code(_) => EditRefusal::Code,
|
||||
Node::Math(_) => EditRefusal::Math,
|
||||
Node::Html(_) => EditRefusal::Html,
|
||||
Node::Table(_) => EditRefusal::Table,
|
||||
Node::Yaml(_) | Node::Toml(_) => EditRefusal::FrontMatter,
|
||||
_ => EditRefusal::Outside,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
//! A command's result read back: its text and formats kept.
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use crate::documents::{
|
||||
MarkdownReader, Style,
|
||||
document::format::EditRefusal,
|
||||
markdown::{file_tree, span, walk},
|
||||
};
|
||||
|
||||
const KEPT: u16 = bit(Style::Code) | bit(Style::Math);
|
||||
const CODE: u16 = 1 << 9;
|
||||
const MATH: u16 = 1 << 10;
|
||||
const HTML: u16 = 1 << 11;
|
||||
|
||||
/// Refuses a result that reads as something else.
|
||||
pub(super) fn misread(why: &str) -> EditRefusal {
|
||||
log::warn!("block command: {why}");
|
||||
EditRefusal::Misread
|
||||
}
|
||||
|
||||
/// `text` with sorted `edits` written, and its parse.
|
||||
pub(super) fn reread(text: &str, edits: &[(Range<usize>, String)]) -> (String, Node) {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
let mut at = 0;
|
||||
for (range, with) in edits {
|
||||
out.push_str(&text[at..range.start]);
|
||||
out.push_str(with);
|
||||
at = range.end;
|
||||
}
|
||||
out.push_str(&text[at..]);
|
||||
let root = file_tree(&out);
|
||||
(out, root)
|
||||
}
|
||||
|
||||
const fn bit(style: Style) -> u16 {
|
||||
1 << style as u16
|
||||
}
|
||||
|
||||
/// A parsed document read as the drawing reads it.
|
||||
pub(super) struct Shown<'a> {
|
||||
root: &'a Node,
|
||||
reader: MarkdownReader<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Shown<'a> {
|
||||
pub(super) fn new(source: &'a str, root: &'a Node) -> Self {
|
||||
let reader = MarkdownReader::new(source, root);
|
||||
Self { root, reader }
|
||||
}
|
||||
|
||||
/// Each shown character, with its formats.
|
||||
pub(super) fn all(&self) -> Vec<(char, u16)> {
|
||||
let mut out = Vec::new();
|
||||
self.read(self.root, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
fn read(&self, node: &Node, out: &mut Vec<(char, u16)>) {
|
||||
let (value, format) = match node {
|
||||
Node::Paragraph(_) | Node::Heading(_) | Node::TableCell(_) => {
|
||||
return out.extend(self.inline(node));
|
||||
}
|
||||
Node::Code(code) => (code.value.as_str(), CODE),
|
||||
Node::Math(math) => (math.value.as_str(), MATH),
|
||||
Node::Html(html) => (html.value.as_str(), HTML),
|
||||
_ => {
|
||||
for kid in node.children().map(Vec::as_slice).unwrap_or_default() {
|
||||
self.read(kid, out);
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
out.extend(value.chars().map(|ch| (ch, format)));
|
||||
}
|
||||
|
||||
/// `node`'s drawn text; white space counts only in code.
|
||||
pub(super) fn inline(&self, node: &Node) -> Vec<(char, u16)> {
|
||||
let kids = node.children().map(Vec::as_slice).unwrap_or_default();
|
||||
let inline = self.reader.inline(kids);
|
||||
let mut formats = vec![0u16; inline.text.len()];
|
||||
for (range, style) in &inline.styles {
|
||||
for format in &mut formats[range.clone()] {
|
||||
*format |= bit(*style);
|
||||
}
|
||||
}
|
||||
(inline.text.char_indices())
|
||||
.map(|(at, ch)| (ch, formats[at]))
|
||||
.filter(|(ch, format)| !ch.is_whitespace() || format & KEPT != 0)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The level (0 a paragraph) and characters at `at`.
|
||||
pub(super) fn at(&self, at: usize) -> Option<(u8, Vec<(char, u16)>)> {
|
||||
let mut nodes = Vec::new();
|
||||
walk(self.root, &mut nodes);
|
||||
nodes.into_iter().find_map(|node| {
|
||||
let level = match node {
|
||||
Node::Paragraph(_) => 0,
|
||||
Node::Heading(heading) => heading.depth,
|
||||
_ => return None,
|
||||
};
|
||||
let whole = span(node);
|
||||
(whole.start <= at && at <= whole.end).then(|| (level, self.inline(node)))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
//! Heading levels and paragraphs, as Typora's `changeBlock`.
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use super::{
|
||||
BlockCommand, Here, change, check, closing, eol, insert, inside, line_end, line_start,
|
||||
next_line, prefix, prev_line, refusal, unblocked,
|
||||
};
|
||||
use crate::documents::{
|
||||
document::format::{Change, EditRefusal},
|
||||
markdown::span,
|
||||
};
|
||||
|
||||
/// The level `command` asks at `level`; 0 is a paragraph.
|
||||
fn target(level: u8, command: BlockCommand) -> u8 {
|
||||
match command {
|
||||
BlockCommand::Heading(n) => {
|
||||
assert!((1..=6).contains(&n), "a heading level is 1 to 6");
|
||||
n
|
||||
}
|
||||
BlockCommand::Promote if level == 0 => 6,
|
||||
BlockCommand::Promote => (level - 1).max(1),
|
||||
BlockCommand::Demote if level == 0 || level == 6 => 0,
|
||||
BlockCommand::Demote => level + 1,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn run(here: &Here, command: BlockCommand) -> Result<Change, EditRefusal> {
|
||||
let text = here.text;
|
||||
let caret = here.selection.clone();
|
||||
let leaf = here.leaf();
|
||||
let level = match leaf {
|
||||
Some(Node::Paragraph(_)) => 0,
|
||||
Some(Node::Heading(heading)) => heading.depth,
|
||||
Some(node @ (Node::Math(_) | Node::Html(_) | Node::Table(_)))
|
||||
if !matches!(command, BlockCommand::Promote | BlockCommand::Demote) =>
|
||||
{
|
||||
// Typora gives these an empty paragraph after.
|
||||
log::info!("block command: {:?} takes a paragraph after", refusal(node));
|
||||
return insert::paragraph(here, false);
|
||||
}
|
||||
Some(node) => return Err(refusal(node)),
|
||||
None if here.blank() => 0,
|
||||
None => return Err(EditRefusal::Outside),
|
||||
};
|
||||
let goal = target(level, command);
|
||||
if goal == level {
|
||||
return Ok((Vec::new(), caret));
|
||||
}
|
||||
let containers = here.containers(here.path.len());
|
||||
let hashes = "#".repeat(usize::from(goal)) + " ";
|
||||
let Some(node) = leaf else {
|
||||
let line = line_start(text, caret.start);
|
||||
let at = inside(text, line, &containers).expect("a blank line holds its prefix");
|
||||
return Ok(change(vec![(at..at, hashes)], caret));
|
||||
};
|
||||
let whole = span(node);
|
||||
let edits = match (level, goal) {
|
||||
(0, _) if task_head(here, &whole) => return Err(EditRefusal::Outside),
|
||||
(0, _) => to_heading(here, &containers, &whole, hashes),
|
||||
(_, 0) => to_paragraph(here, node, &containers)?,
|
||||
_ => relevel(here, node, &containers, goal),
|
||||
};
|
||||
let change = change(edits, caret);
|
||||
// Every shown character stays; the caret's block is `goal`.
|
||||
let (written, after) = check::reread(text, &change.0);
|
||||
let before = check::Shown::new(text, here.root);
|
||||
let now = check::Shown::new(&written, &after);
|
||||
let placed = match now.at(change.1.start) {
|
||||
Some((level, _)) => level == goal,
|
||||
// An empty paragraph has no node.
|
||||
None => goal == 0 && before.inline(node).is_empty(),
|
||||
};
|
||||
let kept = placed && now.all() == before.all();
|
||||
match kept {
|
||||
true => Ok(change),
|
||||
false => Err(check::misread("a heading change would reread otherwise")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `node` is an ATX heading, `#` led.
|
||||
fn atx(text: &str, node: &Node) -> bool {
|
||||
text[span(node)].trim_start().starts_with('#')
|
||||
}
|
||||
|
||||
/// Whether the caret is on a task item's first line.
|
||||
fn task_head(here: &Here, whole: &Range<usize>) -> bool {
|
||||
let first = line_start(here.text, whole.start) == line_start(here.text, here.selection.start);
|
||||
let leaf = here.path[here.path.len() - 1];
|
||||
first && super::task_head(here.path[here.path.len() - 2], leaf)
|
||||
}
|
||||
|
||||
/// The caret's line led by `hashes`; lazy lines kept in.
|
||||
fn to_heading(
|
||||
here: &Here,
|
||||
containers: &[&Node],
|
||||
whole: &Range<usize>,
|
||||
hashes: String,
|
||||
) -> Vec<(Range<usize>, String)> {
|
||||
let text = here.text;
|
||||
let lead = prefix(text, containers);
|
||||
let first = line_start(text, here.selection.start);
|
||||
let mut edits = Vec::new();
|
||||
let mut line = first;
|
||||
loop {
|
||||
let at = match line_start(text, whole.start) == line {
|
||||
true => Some(whole.start),
|
||||
false => inside(text, line, containers),
|
||||
};
|
||||
let content = |from: usize| {
|
||||
from + text[from..].len() - text[from..].trim_start_matches([' ', '\t']).len()
|
||||
};
|
||||
let (at, lazy) = match at {
|
||||
Some(at) => (content(at), ""),
|
||||
None => (content(line), lead.as_str()),
|
||||
};
|
||||
if line == first {
|
||||
edits.push((at..at, format!("{lazy}{hashes}")));
|
||||
if let Some(run) = closing(&text[at..line_end(text, at)]) {
|
||||
edits.push((at + run..at + run, "\\".to_string()));
|
||||
}
|
||||
} else if !lazy.is_empty() {
|
||||
edits.push((at..at, lazy.to_string()));
|
||||
}
|
||||
match next_line(text, line) {
|
||||
Some(next) if next < whole.end => line = next,
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
edits
|
||||
}
|
||||
|
||||
/// A heading's marks off; a blank line where text joins.
|
||||
fn to_paragraph(
|
||||
here: &Here,
|
||||
node: &Node,
|
||||
containers: &[&Node],
|
||||
) -> Result<Vec<(Range<usize>, String)>, EditRefusal> {
|
||||
let text = here.text;
|
||||
let whole = span(node);
|
||||
let blank = format!("{}{}", prefix(text, containers).trim_end(), eol(text));
|
||||
let mut edits = Vec::new();
|
||||
if here.joins_before(&whole, &[]) {
|
||||
let line = line_start(text, whole.start);
|
||||
edits.push((line..line, blank.clone()));
|
||||
}
|
||||
let kids = node.children().map(Vec::as_slice).unwrap_or_default();
|
||||
match atx(text, node) {
|
||||
true => match (kids.first(), kids.last()) {
|
||||
(Some(first), Some(last)) => {
|
||||
let start = span(first).start;
|
||||
edits.push((whole.start..start, String::new()));
|
||||
// The body kept: closing hashes go.
|
||||
let end = line_end(text, start).min(span(last).end);
|
||||
if let Some(mark) = unblocked(&text[start..end])? {
|
||||
edits.push((start + mark..start + mark, "\\".to_string()));
|
||||
}
|
||||
edits.push((span(last).end..whole.end, String::new()));
|
||||
}
|
||||
_ => edits.push((whole.clone(), String::new())),
|
||||
},
|
||||
false => {
|
||||
// The underline line goes with the break before it.
|
||||
let under = line_start(text, whole.end);
|
||||
let ending = text[..under].len() - text[..under].trim_end_matches(['\r', '\n']).len();
|
||||
edits.push((under - ending.min(2)..whole.end, String::new()));
|
||||
}
|
||||
}
|
||||
if here.joins_after(&whole) {
|
||||
let next = next_line(text, whole.end).expect("a joining line follows");
|
||||
edits.push((next..next, blank.clone()));
|
||||
}
|
||||
Ok(edits)
|
||||
}
|
||||
|
||||
/// A heading moved to level `goal`.
|
||||
fn relevel(
|
||||
here: &Here,
|
||||
node: &Node,
|
||||
containers: &[&Node],
|
||||
goal: u8,
|
||||
) -> Vec<(Range<usize>, String)> {
|
||||
let text = here.text;
|
||||
let whole = span(node);
|
||||
if atx(text, node) {
|
||||
let at = whole.start + text[whole.clone()].find('#').expect("an ATX heading has #");
|
||||
let run = text[at..].len() - text[at..].trim_start_matches('#').len();
|
||||
return vec![(at..at + run, "#".repeat(usize::from(goal)))];
|
||||
}
|
||||
let under = line_start(text, whole.end);
|
||||
let marks = &text[under..whole.end];
|
||||
let at = under + marks.find(['=', '-']).expect("a setext underline");
|
||||
let run = marks.trim_end().len() - (at - under);
|
||||
if goal <= 2 {
|
||||
let mark = if goal == 1 { "=" } else { "-" };
|
||||
return vec![(at..at + run, mark.repeat(run))];
|
||||
}
|
||||
// Levels past 2 are ATX: one line, the underline gone.
|
||||
let mut edits = vec![(
|
||||
whole.start..whole.start,
|
||||
"#".repeat(usize::from(goal)) + " ",
|
||||
)];
|
||||
let mut line = line_start(text, whole.start);
|
||||
while let Some(next) = next_line(text, line).filter(|next| *next < under) {
|
||||
let end = line_end(text, line);
|
||||
let from = inside(text, next, containers).unwrap_or(next);
|
||||
let from = from + text[from..].len() - text[from..].trim_start_matches([' ', '\t']).len();
|
||||
edits.push((end..from, " ".to_string()));
|
||||
line = next;
|
||||
}
|
||||
let last = prev_line(text, under).expect("a setext heading has text");
|
||||
if let Some(run) = closing(&text[last..line_end(text, last)]) {
|
||||
edits.push((last + run..last + run, "\\".to_string()));
|
||||
}
|
||||
let ending = text[..under].len() - text[..under].trim_end_matches(['\r', '\n']).len();
|
||||
edits.push((under - ending.min(2)..whole.end, String::new()));
|
||||
edits
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
//! Blocks put in and taken out, as Typora's `insertBlock`.
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use super::{
|
||||
BlockCommand, Here, container, eol, inside, line_end, line_start, next_line, placed, prefix,
|
||||
prev_line, refusal,
|
||||
split::{Room, content, split},
|
||||
task_head,
|
||||
};
|
||||
use crate::documents::{
|
||||
document::format::{Change, EditRefusal},
|
||||
markdown::span,
|
||||
};
|
||||
|
||||
/// Whether `line` holds text past `containers`' prefix.
|
||||
fn text_on(text: &str, line: usize, containers: &[&Node]) -> bool {
|
||||
let at = content(text, line, containers);
|
||||
at < line_end(text, line)
|
||||
}
|
||||
|
||||
/// The selection's text, line by line, prefixes cut.
|
||||
fn picked(here: &Here, containers: &[&Node]) -> Vec<String> {
|
||||
let text = here.text;
|
||||
let (s, e) = (here.selection.start, here.selection.end);
|
||||
let mut out = Vec::new();
|
||||
let mut at = s;
|
||||
while at < e {
|
||||
out.push(text[at..line_end(text, at).min(e)].to_string());
|
||||
match next_line(text, at).filter(|next| *next < e) {
|
||||
Some(next) => at = inside(text, next, containers).unwrap_or(next).min(e),
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The smallest footnote number not taken.
|
||||
fn free_note(here: &Here) -> usize {
|
||||
let used: HashSet<&str> = (here.nodes.iter())
|
||||
.filter_map(|node| match node {
|
||||
Node::FootnoteDefinition(note) => Some(note.identifier.as_str()),
|
||||
Node::FootnoteReference(note) => Some(note.identifier.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
(1..)
|
||||
.find(|n: &usize| !used.contains(n.to_string().as_str()))
|
||||
.expect("a free number")
|
||||
}
|
||||
|
||||
/// `command`'s lines and the caret's line and byte.
|
||||
fn lines(
|
||||
here: &Here,
|
||||
command: BlockCommand,
|
||||
body: Vec<String>,
|
||||
) -> (Vec<String>, Option<(usize, usize)>) {
|
||||
let fenced = |fence: String, body: Vec<String>| {
|
||||
let mut lines = vec![fence.clone()];
|
||||
lines.extend(if body.is_empty() {
|
||||
vec![String::new()]
|
||||
} else {
|
||||
body
|
||||
});
|
||||
lines.push(fence);
|
||||
(lines, Some((1, 0)))
|
||||
};
|
||||
match command {
|
||||
BlockCommand::CodeBlock => {
|
||||
let longest = (body.iter())
|
||||
.flat_map(|line| line.split(|ch| ch != '`'))
|
||||
.map(str::len)
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
fenced("`".repeat(longest.max(2) + 1), body)
|
||||
}
|
||||
BlockCommand::MathBlock => fenced("$$".to_string(), body),
|
||||
BlockCommand::Toc => (vec!["[toc]".to_string()], Some((0, 5))),
|
||||
BlockCommand::Footnote => {
|
||||
let line = format!("[^{}]: ", free_note(here));
|
||||
let end = line.len();
|
||||
(vec![line], Some((0, end)))
|
||||
}
|
||||
BlockCommand::LinkDefinition => {
|
||||
let first = body.first().map_or("", |line| line.trim());
|
||||
if first.contains(['/', '\\']) {
|
||||
let address: String = first.chars().take(160).collect();
|
||||
return (vec![format!("[]: {address}")], Some((0, 1)));
|
||||
}
|
||||
let label: String = (first.split_whitespace().next().unwrap_or_default())
|
||||
.chars()
|
||||
.take(20)
|
||||
.collect();
|
||||
let line = format!("[{label}]: ");
|
||||
let caret = if label.is_empty() { 1 } else { line.len() };
|
||||
(vec![line], Some((0, caret)))
|
||||
}
|
||||
_ => (vec!["---".to_string()], None),
|
||||
}
|
||||
}
|
||||
|
||||
/// `lines` joined inside `lead`, and the caret's byte in them.
|
||||
fn joined(
|
||||
lines: &[String],
|
||||
caret: Option<(usize, usize)>,
|
||||
nl: &str,
|
||||
lead: &str,
|
||||
) -> (String, Option<usize>) {
|
||||
let mut out = String::new();
|
||||
let mut at = None;
|
||||
for (ix, line) in lines.iter().enumerate() {
|
||||
if ix > 0 {
|
||||
out.push_str(nl);
|
||||
out.push_str(lead);
|
||||
}
|
||||
if let Some((row, byte)) = caret
|
||||
&& row == ix
|
||||
{
|
||||
at = Some(out.len() + byte);
|
||||
}
|
||||
out.push_str(line);
|
||||
}
|
||||
(out, at)
|
||||
}
|
||||
|
||||
/// Whether a paragraph or ATX heading splits at the caret.
|
||||
fn splits(text: &str, node: &Node) -> bool {
|
||||
match node {
|
||||
Node::Paragraph(_) => true,
|
||||
Node::Heading(_) => text[span(node)].trim_start().starts_with('#'),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn run(here: &Here, command: BlockCommand) -> Result<Change, EditRefusal> {
|
||||
let text = here.text;
|
||||
let leaf = here.leaf();
|
||||
let containers = here.containers(here.path.len() - usize::from(leaf.is_some()));
|
||||
let lead = prefix(text, &containers);
|
||||
let room = Room {
|
||||
nl: eol(text),
|
||||
blank: lead.trim_end().to_string(),
|
||||
lead,
|
||||
containers,
|
||||
};
|
||||
let (lines, caret) = lines(here, command, picked(here, &room.containers));
|
||||
let (block, inner) = joined(&lines, caret, room.nl, &room.lead);
|
||||
let rule = inner.is_none();
|
||||
match leaf {
|
||||
Some(node) if splits(text, node) => {
|
||||
if here.selection.end > span(node).end {
|
||||
return Err(EditRefusal::Across);
|
||||
}
|
||||
split(here, node, &room, &block, inner)
|
||||
}
|
||||
Some(node) if !here.selection.is_empty() => Err(refusal(node)),
|
||||
Some(node) => {
|
||||
let at = line_end(text, span(node).end);
|
||||
let next =
|
||||
next_line(text, at).is_some_and(|next| text_on(text, next, &room.containers));
|
||||
let gap = room.gap();
|
||||
let tail = match rule {
|
||||
true => gap.clone(),
|
||||
false => String::new(),
|
||||
};
|
||||
let sep = if next {
|
||||
format!("{}{}", room.nl, room.blank)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let caret = at + gap.len() + inner.unwrap_or(block.len() + tail.len());
|
||||
Ok(placed(
|
||||
vec![(at..at, format!("{gap}{block}{tail}{sep}"))],
|
||||
caret,
|
||||
))
|
||||
}
|
||||
None if here.blank() && here.selection.is_empty() => {
|
||||
let line = line_start(text, here.selection.start);
|
||||
let at = inside(text, line, &room.containers).expect("a blank line holds its prefix");
|
||||
let end = line_end(text, line);
|
||||
let prev =
|
||||
prev_line(text, line).is_some_and(|prev| text_on(text, prev, &room.containers));
|
||||
let next =
|
||||
next_line(text, line).is_some_and(|next| text_on(text, next, &room.containers));
|
||||
let head = if prev {
|
||||
format!("{}{}", room.nl, room.lead)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let tail = if rule { room.gap() } else { String::new() };
|
||||
let sep = if next {
|
||||
format!("{}{}", room.nl, room.blank)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let caret = at + head.len() + inner.unwrap_or(block.len() + tail.len());
|
||||
Ok(placed(
|
||||
vec![(at..end, format!("{head}{block}{tail}{sep}"))],
|
||||
caret,
|
||||
))
|
||||
}
|
||||
None => Err(EditRefusal::Outside),
|
||||
}
|
||||
}
|
||||
|
||||
/// An empty line before or after the caret's block.
|
||||
pub(super) fn paragraph(here: &Here, before: bool) -> Result<Change, EditRefusal> {
|
||||
let text = here.text;
|
||||
let leaf = here.leaf().ok_or(EditRefusal::Outside)?;
|
||||
let whole = span(leaf);
|
||||
let containers = here.containers(here.path.len() - 1);
|
||||
let lead = prefix(text, &containers);
|
||||
let room = Room {
|
||||
nl: eol(text),
|
||||
blank: lead.trim_end().to_string(),
|
||||
lead,
|
||||
containers,
|
||||
};
|
||||
let gap = room.gap();
|
||||
let holder = here.path[here.path.len() - 2];
|
||||
if before && task_head(holder, leaf) {
|
||||
return Err(EditRefusal::Outside);
|
||||
}
|
||||
// A holder's first line keeps its opener: text moves down.
|
||||
if before
|
||||
&& container(holder)
|
||||
&& !matches!(holder, Node::Blockquote(_))
|
||||
&& line_start(text, span(holder).start) == line_start(text, whole.start)
|
||||
{
|
||||
let down = format!("{}{}", room.nl, room.lead);
|
||||
return Ok(placed(vec![(whole.start..whole.start, down)], whole.start));
|
||||
}
|
||||
if before {
|
||||
let mut edits = Vec::new();
|
||||
let mut shift = 0;
|
||||
if here.joins_before(&whole, &[]) {
|
||||
let line = line_start(text, whole.start);
|
||||
let blank = format!("{}{}", room.blank, room.nl);
|
||||
shift = blank.len();
|
||||
edits.push((line..line, blank));
|
||||
}
|
||||
edits.push((whole.start..whole.start, gap));
|
||||
return Ok(placed(edits, whole.start + shift));
|
||||
}
|
||||
let at = line_end(text, whole.end);
|
||||
let next = next_line(text, at).is_some_and(|next| text_on(text, next, &room.containers));
|
||||
let sep = if next {
|
||||
format!("{}{}", room.nl, room.blank)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
Ok(placed(
|
||||
vec![(at..at, format!("{gap}{sep}"))],
|
||||
at + gap.len(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Front matter at the top, unless there is some.
|
||||
pub(super) fn front_matter(here: &Here) -> Result<Change, EditRefusal> {
|
||||
let first = here.root.children().and_then(|kids| kids.first());
|
||||
if matches!(first, Some(Node::Yaml(_) | Node::Toml(_))) {
|
||||
return Err(EditRefusal::Present);
|
||||
}
|
||||
let nl = eol(here.text);
|
||||
let tail = match here.text.is_empty() || here.text.starts_with(['\n', '\r']) {
|
||||
true => "",
|
||||
false => nl,
|
||||
};
|
||||
Ok(placed(
|
||||
vec![(0..0, format!("---{nl}{nl}---{nl}{tail}"))],
|
||||
3 + nl.len(),
|
||||
))
|
||||
}
|
||||
|
||||
/// The caret's block gone; a lone paragraph takes its holder.
|
||||
pub(super) fn delete(here: &Here) -> Result<Change, EditRefusal> {
|
||||
let text = here.text;
|
||||
let leaf = here.leaf().ok_or(EditRefusal::Outside)?;
|
||||
let mut ix = here.path.len() - 1;
|
||||
if matches!(leaf, Node::Paragraph(_))
|
||||
&& let Some(parent) = here.path.get(ix - 1)
|
||||
&& container(parent)
|
||||
&& parent.children().is_some_and(|kids| kids.len() == 1)
|
||||
{
|
||||
ix -= 1;
|
||||
}
|
||||
let whole = span(here.path[ix]);
|
||||
let line = line_start(text, whole.start);
|
||||
if !text[line..whole.start]
|
||||
.chars()
|
||||
.all(|ch| matches!(ch, ' ' | '\t' | '>'))
|
||||
{
|
||||
return Ok(placed(vec![(whole.clone(), String::new())], whole.start));
|
||||
}
|
||||
// Containers wholly on the lines go too.
|
||||
let last = line_end(text, whole.end);
|
||||
let top = (1..=ix)
|
||||
.find(|j| {
|
||||
let held = span(here.path[*j]);
|
||||
line <= held.start && held.end <= last
|
||||
})
|
||||
.expect("the block lies on its lines");
|
||||
let gone = &here.path[top..=ix];
|
||||
let outer = here.containers(top);
|
||||
let mut end = next_line(text, whole.end).unwrap_or(text.len());
|
||||
let blank_above = prev_line(text, line).is_none_or(|prev| !text_on(text, prev, &outer));
|
||||
if blank_above && end < text.len() && !text_on(text, end, &outer) {
|
||||
end = next_line(text, end).unwrap_or(text.len());
|
||||
}
|
||||
// Neighbors that would join keep a blank line between.
|
||||
let joins = here.joins_before(&whole, gone) && here.joins_after(&whole);
|
||||
let with = match joins {
|
||||
true => format!("{}{}", prefix(text, &outer).trim_end(), eol(text)),
|
||||
false => String::new(),
|
||||
};
|
||||
Ok(placed(vec![(line..end, with)], line))
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
//! Lists on, off and retyped, as Typora's `toggleIndent`.
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
use markdown::mdast::{Code, List, Node};
|
||||
|
||||
use super::{
|
||||
BlockCommand, Here, change, column, eol, indent, inside, line_end, line_start, next_line,
|
||||
opener, prefix, refusal, unblocked,
|
||||
};
|
||||
use crate::documents::{
|
||||
document::format::{Change, EditRefusal},
|
||||
markdown::span,
|
||||
};
|
||||
|
||||
fn kind(list: &List) -> BlockCommand {
|
||||
match (list.ordered, list.children.first()) {
|
||||
(true, _) => BlockCommand::OrderedList,
|
||||
(false, Some(Node::ListItem(item))) if item.checked.is_some() => BlockCommand::TaskList,
|
||||
_ => BlockCommand::BulletList,
|
||||
}
|
||||
}
|
||||
|
||||
/// The marker an item takes: number `n` or the bullet.
|
||||
fn marker(kind: BlockCommand, n: u32, bullet: char) -> String {
|
||||
match kind {
|
||||
BlockCommand::OrderedList => format!("{n}. "),
|
||||
BlockCommand::TaskList => format!("{bullet} [ ] "),
|
||||
_ => format!("{bullet} "),
|
||||
}
|
||||
}
|
||||
|
||||
/// A marker's width for continuation lines; boxes add none.
|
||||
fn width(kind: BlockCommand, n: u32) -> usize {
|
||||
match kind {
|
||||
BlockCommand::OrderedList => format!("{n}. ").len(),
|
||||
_ => 2,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn run(here: &Here, command: BlockCommand) -> Result<Change, EditRefusal> {
|
||||
let caret = here.selection.clone();
|
||||
let path = &here.path;
|
||||
let item = match here.leaf() {
|
||||
Some(leaf) => path.len().checked_sub(2).filter(|ix| match path[*ix] {
|
||||
Node::ListItem(item) => {
|
||||
!caret.is_empty()
|
||||
|| item
|
||||
.children
|
||||
.first()
|
||||
.is_some_and(|kid| std::ptr::eq(kid, leaf))
|
||||
}
|
||||
_ => false,
|
||||
}),
|
||||
None => Some(path.len() - 1).filter(|ix| matches!(path[*ix], Node::ListItem(_))),
|
||||
};
|
||||
// A selection past the list takes every touched sibling.
|
||||
if let Some(ix) = item
|
||||
&& let Node::List(list) = path[ix - 1]
|
||||
&& (here.path_at(caret.end).iter()).any(|node| std::ptr::eq(*node, path[ix - 1]))
|
||||
{
|
||||
let edits = match kind(list) == command {
|
||||
true => unwrap(here, ix - 1, list)?,
|
||||
false => {
|
||||
let start = if list.ordered {
|
||||
list.start.unwrap_or(1)
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let outer = here.containers(ix - 1);
|
||||
retype(here.text, path[ix - 1], list, &outer, command, start)?
|
||||
}
|
||||
};
|
||||
return Ok(change(edits, caret));
|
||||
}
|
||||
if here.leaf().is_none() && caret.is_empty() {
|
||||
if !here.blank() {
|
||||
return Err(EditRefusal::Outside);
|
||||
}
|
||||
let line = line_start(here.text, caret.start);
|
||||
let at = inside(here.text, line, &here.containers(path.len()))
|
||||
.expect("a blank line holds its prefix");
|
||||
return Ok(change(vec![(at..at, marker(command, 1, '-'))], caret));
|
||||
}
|
||||
let (k, blocks) = here.blocks().ok_or(EditRefusal::Outside)?;
|
||||
Ok(change(wrap(here, k, &blocks, command)?, caret))
|
||||
}
|
||||
|
||||
/// Indent from `at` to column `want`: its end, columns past.
|
||||
fn indented(text: &str, at: usize, want: usize) -> Option<(usize, usize)> {
|
||||
let end = line_end(text, at);
|
||||
let mut cut = at;
|
||||
let mut col = column(text, at);
|
||||
while col < want {
|
||||
let ch = text[cut..end]
|
||||
.chars()
|
||||
.next()
|
||||
.filter(|ch| matches!(ch, ' ' | '\t'))?;
|
||||
col = if ch == '\t' { col / 4 * 4 + 4 } else { col + 1 };
|
||||
cut += 1;
|
||||
}
|
||||
Some((cut, col - want))
|
||||
}
|
||||
|
||||
/// Each non-lazy line of `item` after its first, with indent.
|
||||
fn continued<'a>(
|
||||
text: &'a str,
|
||||
item: &Node,
|
||||
outer: &'a [&Node],
|
||||
) -> impl Iterator<Item = (usize, usize, usize)> + 'a {
|
||||
let whole = span(item);
|
||||
let want = indent(text, item);
|
||||
std::iter::successors(next_line(text, whole.start), move |line| {
|
||||
next_line(text, *line)
|
||||
})
|
||||
.take_while(move |line| *line < whole.end)
|
||||
.filter_map(move |line| {
|
||||
let at = inside(text, line, outer)?;
|
||||
if text[at..line_end(text, line)].trim().is_empty() {
|
||||
return None;
|
||||
}
|
||||
let (cut, past) = indented(text, at, want)?;
|
||||
Some((at, cut, past))
|
||||
})
|
||||
}
|
||||
|
||||
/// Every item's marker off, its lines out one level.
|
||||
fn unwrap(here: &Here, ix: usize, list: &List) -> Result<Vec<(Range<usize>, String)>, EditRefusal> {
|
||||
let text = here.text;
|
||||
let outer = here.containers(ix);
|
||||
let blank = format!("{}{}", prefix(text, &outer).trim_end(), eol(text));
|
||||
let whole = span(here.path[ix]);
|
||||
let mut edits = Vec::new();
|
||||
for (n, item) in list.children.iter().enumerate() {
|
||||
let start = span(item).start;
|
||||
let line = line_start(text, start);
|
||||
let joins = match n {
|
||||
0 => here.joins_before(&whole, &[here.path[ix]]),
|
||||
_ => prev_text(text, line, &outer),
|
||||
};
|
||||
if joins {
|
||||
edits.push((line..line, blank.clone()));
|
||||
}
|
||||
let first = item.children().and_then(|kids| kids.first()).map(span);
|
||||
let boxed = matches!(item, Node::ListItem(item) if item.checked.is_some());
|
||||
match &first {
|
||||
Some(kid) if !text[start..kid.start].contains(['\n', '\r']) => {
|
||||
edits.push((start..kid.start, String::new()));
|
||||
}
|
||||
Some(kid) => {
|
||||
let next = next_line(text, start).expect("an item's text lies below");
|
||||
edits.push((line..next, String::new()));
|
||||
if boxed {
|
||||
edits.push((unbox(text, kid.start), String::new()));
|
||||
}
|
||||
}
|
||||
None => edits.push((start..line_end(text, start), String::new())),
|
||||
}
|
||||
if boxed && let Some(kid) = first {
|
||||
edits.extend(escaped(text, kid.start)?);
|
||||
}
|
||||
for (at, cut, past) in continued(text, item, &outer) {
|
||||
edits.push((at..cut, " ".repeat(past)));
|
||||
}
|
||||
}
|
||||
if here.joins_after(&whole) {
|
||||
let next = next_line(text, whole.end).expect("a joining line follows");
|
||||
edits.push((next..next, blank.clone()));
|
||||
}
|
||||
Ok(edits)
|
||||
}
|
||||
|
||||
/// The task box before the text at `kid`.
|
||||
fn unbox(text: &str, kid: usize) -> Range<usize> {
|
||||
let line = line_start(text, kid);
|
||||
line + text[line..kid].rfind('[').expect("a task's box")..kid
|
||||
}
|
||||
|
||||
/// A backslash where text at `at`, unboxed, opens a block.
|
||||
fn escaped(text: &str, at: usize) -> Result<Option<(Range<usize>, String)>, EditRefusal> {
|
||||
let mark = unblocked(&text[at..line_end(text, at)])?;
|
||||
Ok(mark.map(|mark| (at + mark..at + mark, "\\".to_string())))
|
||||
}
|
||||
|
||||
/// Whether `node`, holding `code`, is fenced in the source.
|
||||
fn fenced(text: &str, node: &Node, code: &Code) -> bool {
|
||||
code.lang.is_some()
|
||||
|| code.value.is_empty()
|
||||
|| text[span(node)].lines().count() > code.value.lines().count()
|
||||
}
|
||||
|
||||
/// Whether the line before `line` holds text inside `outer`.
|
||||
fn prev_text(text: &str, line: usize, outer: &[&Node]) -> bool {
|
||||
super::prev_line(text, line).is_some_and(|prev| {
|
||||
inside(text, prev, outer).is_none_or(|at| !text[at..line_end(text, prev)].trim().is_empty())
|
||||
})
|
||||
}
|
||||
|
||||
/// Each marker as `kind`'s from `start`; lines follow.
|
||||
fn retype(
|
||||
text: &str,
|
||||
node: &Node,
|
||||
list: &List,
|
||||
outer: &[&Node],
|
||||
kind: BlockCommand,
|
||||
start: u32,
|
||||
) -> Result<Vec<(Range<usize>, String)>, EditRefusal> {
|
||||
let bullet = match list.ordered {
|
||||
true => '-',
|
||||
false => text[span(node).start..].chars().next().unwrap_or('-'),
|
||||
};
|
||||
let mut edits = Vec::new();
|
||||
let task = kind == BlockCommand::TaskList;
|
||||
for (n, item) in list.children.iter().enumerate() {
|
||||
let number = start + n as u32;
|
||||
let at = span(item).start;
|
||||
let (mark, spaces) = super::marker(text, item);
|
||||
let first = item.children().and_then(|kids| kids.first());
|
||||
let boxed = matches!(item, Node::ListItem(item) if item.checked.is_some());
|
||||
let body = at + mark + spaces;
|
||||
let blank = body == line_end(text, at);
|
||||
if boxed {
|
||||
// A box stays when the kind keeps boxes.
|
||||
let kind = if task { BlockCommand::BulletList } else { kind };
|
||||
let end = if blank { opener(text, item) } else { body };
|
||||
edits.push((at..end, marker(kind, number, bullet)));
|
||||
if !task {
|
||||
let kid = span(first.expect("a boxed item has text")).start;
|
||||
edits.push((unbox(text, kid), String::new()));
|
||||
edits.extend(escaped(text, kid)?);
|
||||
}
|
||||
} else if task && let Some(kid) = first.filter(|kid| !matches!(kid, Node::Paragraph(_))) {
|
||||
// GFM boxes only a paragraph.
|
||||
return Err(refusal(kid));
|
||||
} else if blank {
|
||||
edits.push((at..opener(text, item), marker(kind, number, bullet)));
|
||||
} else {
|
||||
// Indented code is written again from its value.
|
||||
let pad = match first {
|
||||
Some(kid @ Node::Code(code)) if !fenced(text, kid, code) => {
|
||||
let line = code.value.lines().next().unwrap_or_default();
|
||||
4 + line.len() - line.trim_start_matches(' ').len()
|
||||
}
|
||||
_ => 0,
|
||||
};
|
||||
edits.push((at..body, marker(kind, number, bullet) + &" ".repeat(pad)));
|
||||
}
|
||||
let want = column(text, at) + width(kind, number);
|
||||
for (from, cut, past) in continued(text, item, outer) {
|
||||
let pad = want.saturating_sub(column(text, from)) + past;
|
||||
edits.push((from..cut, " ".repeat(pad)));
|
||||
}
|
||||
}
|
||||
Ok(edits)
|
||||
}
|
||||
|
||||
/// Each paragraph line, and each other block, made an item.
|
||||
fn wrap(
|
||||
here: &Here,
|
||||
k: usize,
|
||||
blocks: &[&Node],
|
||||
command: BlockCommand,
|
||||
) -> Result<Vec<(Range<usize>, String)>, EditRefusal> {
|
||||
let text = here.text;
|
||||
let outer = here.containers(k + 1);
|
||||
let lead = prefix(text, &outer);
|
||||
let mut edits = Vec::new();
|
||||
let mut n = 1;
|
||||
for block in blocks {
|
||||
if let Node::List(list) = block {
|
||||
if kind(list) != command {
|
||||
edits.extend(retype(text, block, list, &outer, command, n)?);
|
||||
}
|
||||
n += list.children.len() as u32;
|
||||
continue;
|
||||
}
|
||||
if command == BlockCommand::TaskList && !matches!(block, Node::Paragraph(_)) {
|
||||
return Err(refusal(block));
|
||||
}
|
||||
let whole = span(block);
|
||||
let mut line = line_start(text, whole.start);
|
||||
while line <= whole.end {
|
||||
let first = line == line_start(text, whole.start);
|
||||
let (at, lazy) = match first {
|
||||
true => (Some(whole.start), ""),
|
||||
false => match inside(text, line, &outer) {
|
||||
Some(at) => (Some(at), ""),
|
||||
None => (None, lead.as_str()),
|
||||
},
|
||||
};
|
||||
let at = at.unwrap_or(line);
|
||||
let content = at + text[at..].len() - text[at..].trim_start_matches([' ', '\t']).len();
|
||||
if !text[at..line_end(text, line)].trim().is_empty() {
|
||||
let mark = match first || matches!(block, Node::Paragraph(_)) {
|
||||
true => {
|
||||
n += 1;
|
||||
marker(command, n - 1, '-')
|
||||
}
|
||||
false => " ".repeat(width(command, n - 1)),
|
||||
};
|
||||
let at = if matches!(block, Node::Paragraph(_)) {
|
||||
content
|
||||
} else {
|
||||
at
|
||||
};
|
||||
edits.push((at..at, format!("{lazy}{mark}")));
|
||||
}
|
||||
match next_line(text, line) {
|
||||
Some(next) => line = next,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(edits)
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
//! Quotes on and off, as Typora's `toggleIndent`.
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use super::{
|
||||
Here, change, check, eol, inside, line_end, line_start, next_line, prefix, split::content,
|
||||
task_head,
|
||||
};
|
||||
use crate::documents::{
|
||||
alert,
|
||||
document::format::{Change, EditRefusal},
|
||||
markdown::{file_tree, span},
|
||||
};
|
||||
|
||||
pub(super) fn run(here: &Here) -> Result<Change, EditRefusal> {
|
||||
let caret = here.selection.clone();
|
||||
let ix = match here.leaf() {
|
||||
Some(_) => here.path.len() - 2,
|
||||
None => here.path.len() - 1,
|
||||
};
|
||||
if let Node::Blockquote(_) = here.path[ix]
|
||||
&& whole(here, ix)
|
||||
{
|
||||
return unwrap(here, ix).map(|edits| change(edits, caret));
|
||||
}
|
||||
if here.leaf().is_none() && here.selection.is_empty() {
|
||||
if !here.blank() {
|
||||
return Err(EditRefusal::Outside);
|
||||
}
|
||||
let line = line_start(here.text, caret.start);
|
||||
let at = inside(here.text, line, &here.containers(here.path.len()))
|
||||
.expect("a blank line holds its prefix");
|
||||
return Ok(change(vec![(at..at, "> ".to_string())], caret));
|
||||
}
|
||||
let (k, blocks) = here.blocks().ok_or(EditRefusal::Outside)?;
|
||||
// GFM reads a task's first line as text.
|
||||
if task_head(here.path[k], blocks[0]) {
|
||||
return Err(EditRefusal::Outside);
|
||||
}
|
||||
Ok(change(wrap(here, k, &blocks), caret))
|
||||
}
|
||||
|
||||
/// Whether the selection takes in the whole quote at `ix`.
|
||||
fn whole(here: &Here, ix: usize) -> bool {
|
||||
if here.selection.is_empty() {
|
||||
return true;
|
||||
}
|
||||
let quote = here.path[ix];
|
||||
let end = here.path_at(here.selection.end);
|
||||
let kids = quote.children().map(Vec::as_slice).unwrap_or_default();
|
||||
let first = here.path.get(ix + 1).copied();
|
||||
let last = end.get(ix + 1).copied();
|
||||
end.get(ix).is_some_and(|node| std::ptr::eq(*node, quote))
|
||||
&& first.is_some_and(|first| kids.first().is_some_and(|kid| std::ptr::eq(kid, first)))
|
||||
&& last.is_some_and(|last| kids.last().is_some_and(|kid| std::ptr::eq(kid, last)))
|
||||
}
|
||||
|
||||
/// One `>` off each line of the quote at `ix`.
|
||||
fn unwrap(here: &Here, ix: usize) -> Result<Vec<(Range<usize>, String)>, EditRefusal> {
|
||||
let text = here.text;
|
||||
let quote = here.path[ix];
|
||||
let whole = span(quote);
|
||||
let outer = here.containers(ix);
|
||||
let blank = format!("{}{}", prefix(text, &outer).trim_end(), eol(text));
|
||||
let mut edits = Vec::new();
|
||||
let mut line = line_start(text, whole.start);
|
||||
if here.joins_before(&whole, &[quote]) {
|
||||
edits.push((line..line, blank.clone()));
|
||||
}
|
||||
if let Node::Blockquote(inner) = quote
|
||||
&& alert(inner).is_some()
|
||||
{
|
||||
if !marker_only(&text[whole.start..line_end(text, line)]) {
|
||||
return Err(check::misread("an alert marker shares its line"));
|
||||
}
|
||||
// A body block keeps its indent; text sheds it.
|
||||
let continues = inner
|
||||
.children
|
||||
.first()
|
||||
.is_some_and(|head| span(head).end > line_end(text, line));
|
||||
// The marker goes with blank lines; an outer opener stays.
|
||||
let (mut cut, mut rest) = (whole.end, whole.end);
|
||||
let mut next = next_line(text, line);
|
||||
while let Some(at) = next.filter(|at| *at < whole.end) {
|
||||
let lead = match mark(text, at, &outer) {
|
||||
Some(mark) => mark.end,
|
||||
None => content(text, at, &outer),
|
||||
};
|
||||
let body =
|
||||
lead + text[lead..].len() - text[lead..].trim_start_matches([' ', '\t']).len();
|
||||
if body < line_end(text, at) {
|
||||
cut = if continues { body } else { lead };
|
||||
rest = next_line(text, at).unwrap_or(text.len());
|
||||
break;
|
||||
}
|
||||
next = next_line(text, at);
|
||||
}
|
||||
edits.push((whole.start..cut, String::new()));
|
||||
line = rest;
|
||||
}
|
||||
while line < whole.end {
|
||||
if let Some(mark) = mark(text, line, &outer) {
|
||||
edits.push((mark, String::new()));
|
||||
}
|
||||
match next_line(text, line) {
|
||||
Some(next) => line = next,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
if here.joins_after(&whole) {
|
||||
let next = next_line(text, whole.end).expect("a joining line follows");
|
||||
edits.push((next..next, blank.clone()));
|
||||
}
|
||||
Ok(edits)
|
||||
}
|
||||
|
||||
/// Whether `line`, a quote's first, holds only an alert marker.
|
||||
fn marker_only(line: &str) -> bool {
|
||||
let root = file_tree(&format!("{line}\n> x"));
|
||||
let Some(Node::Blockquote(quote)) = root.children().and_then(|kids| kids.first()) else {
|
||||
return false;
|
||||
};
|
||||
matches!(alert(quote), Some((_, body)) if matches!(&body[..],
|
||||
[Node::Paragraph(head)] if matches!(&head.children[..],
|
||||
[Node::Text(text)] if text.value == "x")))
|
||||
}
|
||||
|
||||
/// `line`'s `>` and one space after, inside `outer`.
|
||||
fn mark(text: &str, line: usize, outer: &[&Node]) -> Option<Range<usize>> {
|
||||
let at = inside(text, line, outer)?;
|
||||
let end = line_end(text, line);
|
||||
let lead = text[at..end].len() - text[at..end].trim_start_matches(' ').len();
|
||||
let mark = at + lead;
|
||||
(lead <= 3 && text[mark..end].starts_with('>')).then(|| {
|
||||
let space = usize::from(text[mark + 1..end].starts_with([' ', '\t']));
|
||||
mark..mark + 1 + space
|
||||
})
|
||||
}
|
||||
|
||||
/// `> ` before each line of `blocks`, held by `path[k]`.
|
||||
fn wrap(here: &Here, k: usize, blocks: &[&Node]) -> Vec<(Range<usize>, String)> {
|
||||
let text = here.text;
|
||||
let outer = here.containers(k + 1);
|
||||
let first = span(blocks[0]).start;
|
||||
let last = span(blocks[blocks.len() - 1]).end;
|
||||
let mut edits = Vec::new();
|
||||
let mut line = line_start(text, first);
|
||||
while line <= last {
|
||||
let at = match line == line_start(text, first) {
|
||||
true => Some(first),
|
||||
false => inside(text, line, &outer),
|
||||
};
|
||||
// A lazy line stays in its paragraph.
|
||||
if let Some(at) = at {
|
||||
let blank = text[at..line_end(text, line)].trim().is_empty();
|
||||
edits.push((at..at, if blank { ">" } else { "> " }.to_string()));
|
||||
}
|
||||
match next_line(text, line) {
|
||||
Some(next) => line = next,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
edits
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
//! A paragraph or heading split, as Typora's `splitAt`.
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use super::{
|
||||
Here, check, closing, inside, line_end, line_start, next_line, placed, prev_line, shifted,
|
||||
task_head, unblocked,
|
||||
};
|
||||
use crate::documents::{
|
||||
document::format::{Change, EditRefusal, marks},
|
||||
markdown::span,
|
||||
};
|
||||
|
||||
/// Where the new block's text goes, ending the edit.
|
||||
pub(super) struct Room<'a> {
|
||||
pub(super) nl: &'static str,
|
||||
pub(super) lead: String,
|
||||
pub(super) blank: String,
|
||||
pub(super) containers: Vec<&'a Node>,
|
||||
}
|
||||
|
||||
impl Room<'_> {
|
||||
/// A blank line, then a new line's prefix.
|
||||
pub(super) fn gap(&self) -> String {
|
||||
format!("{}{}{}{}", self.nl, self.blank, self.nl, self.lead)
|
||||
}
|
||||
}
|
||||
|
||||
/// Where `line`'s text starts inside `containers`; a lazy line's own.
|
||||
pub(super) fn content(text: &str, line: usize, containers: &[&Node]) -> usize {
|
||||
let at = inside(text, line, containers).unwrap_or(line);
|
||||
at + text[at..].len() - text[at..].trim_start_matches([' ', '\t']).len()
|
||||
}
|
||||
|
||||
/// Back from `at` over blanks and prefixes, to `floor`.
|
||||
fn back(text: &str, mut at: usize, floor: usize, containers: &[&Node]) -> usize {
|
||||
loop {
|
||||
let line = line_start(text, at).max(floor);
|
||||
at = line + text[line..at].trim_end_matches([' ', '\t']).len();
|
||||
if line == floor || at > content(text, line, containers) {
|
||||
return at;
|
||||
}
|
||||
at = line_end(text, prev_line(text, line).expect("a line above the floor"));
|
||||
}
|
||||
}
|
||||
|
||||
/// On from `at` over blanks and prefixes, to `ceil`.
|
||||
fn forth(text: &str, mut at: usize, ceil: usize, containers: &[&Node]) -> usize {
|
||||
loop {
|
||||
let end = line_end(text, at).min(ceil);
|
||||
at = end - text[at..end].trim_start_matches([' ', '\t']).len();
|
||||
if at < end {
|
||||
return at;
|
||||
}
|
||||
match next_line(text, at) {
|
||||
Some(next) if next < ceil => at = content(text, next, containers),
|
||||
_ => return ceil,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Inline nodes with no text to cut.
|
||||
fn sealed(node: &Node) -> bool {
|
||||
matches!(
|
||||
node,
|
||||
Node::InlineMath(_) | Node::Html(_) | Node::FootnoteReference(_)
|
||||
)
|
||||
}
|
||||
|
||||
/// `at` off inline edges; marks to close, then reopen.
|
||||
fn seam(here: &Here, mut at: usize) -> Result<(usize, String, String), EditRefusal> {
|
||||
let text = here.text;
|
||||
let marked: Vec<(&Node, [Range<usize>; 2])> = (here.nodes.iter())
|
||||
.filter_map(|node| Some((*node, marks(node, text)?)))
|
||||
.collect();
|
||||
while let Some([open, _]) = (marked.iter())
|
||||
.map(|(_, holes)| holes)
|
||||
.find(|[open, _]| open.start < at && open.end == at)
|
||||
{
|
||||
at = open.start;
|
||||
}
|
||||
while let Some([_, close]) = (marked.iter())
|
||||
.map(|(_, holes)| holes)
|
||||
.find(|[_, close]| close.start == at && at < close.end)
|
||||
{
|
||||
at = close.end;
|
||||
}
|
||||
let strictly = |range: Range<usize>| range.start < at && at < range.end;
|
||||
let mut cut: Vec<&(&Node, [Range<usize>; 2])> = (marked.iter())
|
||||
.filter(|(node, _)| strictly(span(node)))
|
||||
.collect();
|
||||
let paired =
|
||||
|node: &Node| matches!(node, Node::Strong(_) | Node::Emphasis(_) | Node::Delete(_));
|
||||
let unsafe_cut = (here.nodes.iter()).any(|node| sealed(node) && strictly(span(node)))
|
||||
|| (cut.iter()).any(|(node, [open, close])| {
|
||||
!paired(node) || strictly(open.clone()) || strictly(close.clone())
|
||||
});
|
||||
if unsafe_cut {
|
||||
return Err(EditRefusal::Across);
|
||||
}
|
||||
cut.sort_by_key(|(node, _)| span(node).start);
|
||||
let close = (cut.iter().rev()).map(|(_, [_, close])| &text[close.clone()]);
|
||||
let open = cut.iter().map(|(_, [open, _])| &text[open.clone()]);
|
||||
Ok((at, close.collect(), open.collect()))
|
||||
}
|
||||
|
||||
/// `block` put where the selection splits `node`.
|
||||
pub(super) fn split(
|
||||
here: &Here,
|
||||
node: &Node,
|
||||
room: &Room,
|
||||
block: &str,
|
||||
inner: Option<usize>,
|
||||
) -> Result<Change, EditRefusal> {
|
||||
let text = here.text;
|
||||
let whole = span(node);
|
||||
let kids = node.children().map(Vec::as_slice).unwrap_or_default();
|
||||
let (marks, body) = match node {
|
||||
Node::Heading(heading) => (
|
||||
"#".repeat(usize::from(heading.depth)) + " ",
|
||||
kids.first().map_or(whole.end, |kid| span(kid).start)
|
||||
..kids.last().map_or(whole.end, |kid| span(kid).end),
|
||||
),
|
||||
_ => (String::new(), whole.clone()),
|
||||
};
|
||||
// A caret in heading marks takes the text's edge.
|
||||
let s = here.selection.start.clamp(body.start, body.end);
|
||||
let e = here.selection.end.clamp(body.start, body.end);
|
||||
let (s, close, _) = seam(here, s)?;
|
||||
let (e, _, open) = seam(here, e)?;
|
||||
let containers = &room.containers;
|
||||
let mut from = back(text, s, body.start, containers);
|
||||
let after = forth(text, e, body.end, containers);
|
||||
let before = from > body.start;
|
||||
let follows = after < body.end;
|
||||
// GFM reads a task's first line as text.
|
||||
if !before && task_head(here.path[here.path.len() - 2], node) {
|
||||
return Err(EditRefusal::Outside);
|
||||
}
|
||||
if !before {
|
||||
from = whole.start;
|
||||
}
|
||||
let to = if follows { after } else { whole.end };
|
||||
let gap = room.gap();
|
||||
let mut written = String::new();
|
||||
if before {
|
||||
written.push_str(&close);
|
||||
written.push_str(&gap);
|
||||
}
|
||||
let start = written.len();
|
||||
written.push_str(block);
|
||||
let lead = if follows { marks.len() + open.len() } else { 0 };
|
||||
let caret = match inner {
|
||||
Some(inner) => start + inner,
|
||||
None => {
|
||||
written.push_str(&gap);
|
||||
written.len() + lead
|
||||
}
|
||||
};
|
||||
if follows {
|
||||
if inner.is_some() {
|
||||
written.push_str(&gap);
|
||||
}
|
||||
written.push_str(&marks);
|
||||
written.push_str(&open);
|
||||
}
|
||||
let mut edits = Vec::new();
|
||||
let mut shift = 0;
|
||||
if !before && here.joins_before(&whole, &[]) {
|
||||
let line = line_start(text, whole.start);
|
||||
let blank = format!("{}{}", room.blank, room.nl);
|
||||
shift = blank.len();
|
||||
edits.push((line..line, blank));
|
||||
}
|
||||
edits.push((from..to, written));
|
||||
// A heading's cut end must not close it.
|
||||
let line = line_start(text, from);
|
||||
if before
|
||||
&& close.is_empty()
|
||||
&& matches!(node, Node::Heading(_))
|
||||
&& let Some(run) = closing(&text[line..from])
|
||||
{
|
||||
edits.push((line + run..line + run, "\\".to_string()));
|
||||
shift += 1;
|
||||
}
|
||||
// The rest must not open a block.
|
||||
if follows
|
||||
&& marks.is_empty()
|
||||
&& open.is_empty()
|
||||
&& let Some(mark) = unblocked(&text[after..line_end(text, after)])?
|
||||
{
|
||||
edits.push((after + mark..after + mark, "\\".to_string()));
|
||||
}
|
||||
let change = placed(edits, from + shift + caret);
|
||||
let edits = &change.0;
|
||||
let (written, after_root) = check::reread(text, edits);
|
||||
let now = check::Shown::new(&written, &after_root);
|
||||
let level = match node {
|
||||
Node::Heading(heading) => heading.depth,
|
||||
_ => 0,
|
||||
};
|
||||
let piece = |at: usize| {
|
||||
now.at(shifted(at, edits))
|
||||
.and_then(|(got, chars)| (got == level).then_some(chars))
|
||||
};
|
||||
let head = if before {
|
||||
piece(from - 1)
|
||||
} else {
|
||||
Some(Vec::new())
|
||||
};
|
||||
let tail = if follows { piece(to) } else { Some(Vec::new()) };
|
||||
let want = check::Shown::new(text, here.root).inline(node);
|
||||
let kept = head
|
||||
.zip(tail)
|
||||
.is_some_and(|(head, tail)| match here.selection.is_empty() {
|
||||
true => [head, tail].concat() == want,
|
||||
false => {
|
||||
want.starts_with(&head)
|
||||
&& want.ends_with(&tail)
|
||||
&& head.len() + tail.len() <= want.len()
|
||||
}
|
||||
});
|
||||
match kept {
|
||||
true => Ok(change),
|
||||
false => Err(check::misread("a split would reread otherwise")),
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
//! Inline formats toggle at the selection, as Typora's (R4.6).
|
||||
|
||||
mod apply;
|
||||
mod clear;
|
||||
|
||||
use std::ops::Range;
|
||||
@@ -26,9 +25,9 @@ pub enum InlineFormat {
|
||||
Clear,
|
||||
}
|
||||
|
||||
/// Why a format did not happen.
|
||||
/// Why a format or block command did not happen.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum FormatRefusal {
|
||||
pub enum EditRefusal {
|
||||
/// Preview takes no edits.
|
||||
Preview,
|
||||
Code,
|
||||
@@ -37,14 +36,20 @@ pub enum FormatRefusal {
|
||||
FrontMatter,
|
||||
/// No paragraph, heading or cell at the caret.
|
||||
Outside,
|
||||
/// A table holds the caret.
|
||||
Table,
|
||||
/// Front matter is there already.
|
||||
Present,
|
||||
/// The selection crosses a block or another mark.
|
||||
Across,
|
||||
/// Past the largest read: source alone.
|
||||
TooLong,
|
||||
/// A long format still runs.
|
||||
/// Another edit is still read.
|
||||
Busy,
|
||||
/// The text changed before a long format finished.
|
||||
/// The text changed before the edit was read.
|
||||
Changed,
|
||||
/// The result would read as other text or blocks.
|
||||
Misread,
|
||||
}
|
||||
|
||||
/// The edits and the selection after them.
|
||||
@@ -94,7 +99,7 @@ fn block(node: &Node) -> bool {
|
||||
}
|
||||
|
||||
/// A styled node's two marks, kept text between.
|
||||
fn marks(node: &Node, text: &str) -> Option<[Range<usize>; 2]> {
|
||||
pub(super) fn marks(node: &Node, text: &str) -> Option<[Range<usize>; 2]> {
|
||||
let whole = span(node);
|
||||
let inner = match node {
|
||||
Node::Strong(_) | Node::Emphasis(_) | Node::Delete(_) => content(node),
|
||||
@@ -160,7 +165,7 @@ fn verbatim(
|
||||
nodes: &[&Node],
|
||||
selection: &Range<usize>,
|
||||
format: InlineFormat,
|
||||
) -> Result<(), FormatRefusal> {
|
||||
) -> Result<(), EditRefusal> {
|
||||
let mut blocks: Vec<Range<usize>> = nodes
|
||||
.iter()
|
||||
.filter(|node| block(node))
|
||||
@@ -180,14 +185,14 @@ fn verbatim(
|
||||
// Clear takes a block's inline code and tags too.
|
||||
let held = format != InlineFormat::Clear && touches(false);
|
||||
let why = match node {
|
||||
Node::Code(_) if touches(true) => FormatRefusal::Code,
|
||||
Node::Math(_) if touches(true) => FormatRefusal::Math,
|
||||
Node::Yaml(_) | Node::Toml(_) if touches(true) => FormatRefusal::FrontMatter,
|
||||
Node::InlineCode(_) if format != InlineFormat::Code && held => FormatRefusal::Code,
|
||||
Node::InlineMath(_) if held => FormatRefusal::Math,
|
||||
Node::Code(_) if touches(true) => EditRefusal::Code,
|
||||
Node::Math(_) if touches(true) => EditRefusal::Math,
|
||||
Node::Yaml(_) | Node::Toml(_) if touches(true) => EditRefusal::FrontMatter,
|
||||
Node::InlineCode(_) if format != InlineFormat::Code && held => EditRefusal::Code,
|
||||
Node::InlineMath(_) if held => EditRefusal::Math,
|
||||
Node::Html(_) => match holder(&blocks, &whole).is_some() {
|
||||
true if held => FormatRefusal::Html,
|
||||
false if touches(true) => FormatRefusal::Html,
|
||||
true if held => EditRefusal::Html,
|
||||
false if touches(true) => EditRefusal::Html,
|
||||
_ => continue,
|
||||
},
|
||||
_ => continue,
|
||||
@@ -233,7 +238,7 @@ pub(super) fn toggle(
|
||||
text: &str,
|
||||
selection: Range<usize>,
|
||||
format: InlineFormat,
|
||||
) -> Result<Change, FormatRefusal> {
|
||||
) -> Result<Change, EditRefusal> {
|
||||
toggle_tree(text, &file_tree(text), selection, format)
|
||||
}
|
||||
|
||||
@@ -243,7 +248,7 @@ pub(super) fn toggle_tree(
|
||||
root: &Node,
|
||||
selection: Range<usize>,
|
||||
format: InlineFormat,
|
||||
) -> Result<Change, FormatRefusal> {
|
||||
) -> Result<Change, EditRefusal> {
|
||||
let mut nodes = Vec::new();
|
||||
walk(root, &mut nodes);
|
||||
verbatim(&nodes, &selection, format)?;
|
||||
@@ -287,8 +292,8 @@ pub(super) fn toggle_tree(
|
||||
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,
|
||||
true => EditRefusal::Across,
|
||||
false => EditRefusal::Outside,
|
||||
});
|
||||
}
|
||||
let line = text[..s]
|
||||
@@ -299,7 +304,7 @@ pub(super) fn toggle_tree(
|
||||
+ text[s..].split(['\r', '\n']).next().unwrap_or_default();
|
||||
return match line.trim().is_empty() {
|
||||
true => Ok(pair(s, format)),
|
||||
false => Err(FormatRefusal::Outside),
|
||||
false => Err(EditRefusal::Outside),
|
||||
};
|
||||
};
|
||||
if format == InlineFormat::Underline
|
||||
@@ -377,7 +382,7 @@ fn crossed(
|
||||
s: usize,
|
||||
e: usize,
|
||||
format: InlineFormat,
|
||||
) -> Result<(), FormatRefusal> {
|
||||
) -> Result<(), EditRefusal> {
|
||||
for node in nodes {
|
||||
let whole = span(node);
|
||||
if matches!(node, Node::Text(_)) || whole.start < within.start || whole.end > within.end {
|
||||
@@ -398,7 +403,7 @@ fn crossed(
|
||||
// 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);
|
||||
return Err(EditRefusal::Across);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -412,7 +417,7 @@ fn wrap(
|
||||
mut s: usize,
|
||||
mut e: usize,
|
||||
format: InlineFormat,
|
||||
) -> Result<Change, FormatRefusal> {
|
||||
) -> Result<Change, EditRefusal> {
|
||||
crossed(text, nodes, within, s, e, format)?;
|
||||
if merges(format) {
|
||||
for node in nodes.iter().filter(|node| styled(format, node)) {
|
||||
|
||||
@@ -1,138 +0,0 @@
|
||||
//! 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::{DocumentEvent, LARGEST_READ, MarkdownDocument},
|
||||
modes::joint,
|
||||
};
|
||||
|
||||
/// The text formatted, the open block's offset, the selection.
|
||||
type Snapshot = (String, usize, Range<usize>);
|
||||
|
||||
impl MarkdownDocument {
|
||||
/// `format` at the selection, read apart; refusals by event.
|
||||
pub fn format(
|
||||
&mut self,
|
||||
format: InlineFormat,
|
||||
cx: &mut Context<Self>,
|
||||
) -> 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 self.formatting {
|
||||
log::info!("markdown document: format {format:?} refused: one runs");
|
||||
return Err(FormatRefusal::Busy);
|
||||
}
|
||||
log::info!(
|
||||
"markdown document: format {format:?} reads {} bytes apart",
|
||||
full.len()
|
||||
);
|
||||
let taken = (full.clone(), offset, selection.clone());
|
||||
self.formatting = true;
|
||||
cx.spawn(async move |document, cx| {
|
||||
let change = cx
|
||||
.background_spawn(async move { toggle(&full, selection, format) })
|
||||
.await;
|
||||
document
|
||||
.update(cx, |document, cx| {
|
||||
document.formatting = false;
|
||||
let done = if document.mode == MarkdownMode::Preview {
|
||||
Err(FormatRefusal::Preview)
|
||||
} else if document.snapshot(cx) != taken {
|
||||
Err(FormatRefusal::Changed)
|
||||
} else {
|
||||
change.and_then(|change| {
|
||||
document.apply(format, change, &taken.0, taken.1, cx)
|
||||
})
|
||||
};
|
||||
if let Err(why) = done {
|
||||
log::info!("markdown document: format {format:?} refused: {why:?}");
|
||||
cx.emit(DocumentEvent::FormatRefused(why));
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The text a format reads: the open block's in place.
|
||||
fn snapshot(&self, cx: &Context<Self>) -> 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<Self>,
|
||||
) -> 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(())
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ use std::ops::Range;
|
||||
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use super::{Change, FormatRefusal, after, block, cut, holder, marks, room};
|
||||
use super::{Change, EditRefusal, after, block, cut, holder, marks, room};
|
||||
use crate::documents::{markdown::span, tree::inside};
|
||||
|
||||
/// The backslashes of escapes in a text node's span.
|
||||
@@ -46,14 +46,14 @@ pub(super) fn clear(
|
||||
text: &str,
|
||||
nodes: &[&Node],
|
||||
selection: Range<usize>,
|
||||
) -> Result<Change, FormatRefusal> {
|
||||
) -> Result<Change, EditRefusal> {
|
||||
let mut blocks: Vec<Range<usize>> = (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);
|
||||
return Err(EditRefusal::Outside);
|
||||
}
|
||||
blocks.sort_by_key(|block| block.start);
|
||||
let mut autolinks: Vec<Range<usize>> = (nodes.iter())
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
mod apart;
|
||||
mod attach;
|
||||
mod block;
|
||||
mod edit;
|
||||
mod finding;
|
||||
mod format;
|
||||
@@ -23,7 +25,8 @@ use gpui::{
|
||||
|
||||
pub use crate::documents::tree::DocumentHeading;
|
||||
pub use attach::Attachment;
|
||||
pub use format::{FormatRefusal, InlineFormat};
|
||||
pub use block::BlockCommand;
|
||||
pub use format::{EditRefusal, InlineFormat};
|
||||
pub use look::DocumentLook;
|
||||
use parse::{Parsed, alike, parsed};
|
||||
pub use point::{DocumentTarget, TargetBlock};
|
||||
@@ -59,7 +62,7 @@ pub enum DocumentEvent {
|
||||
/// A secondary press moved the caret onto this.
|
||||
Pointed(DocumentTarget),
|
||||
/// A long format, accepted, did not happen.
|
||||
FormatRefused(FormatRefusal),
|
||||
EditRefused(EditRefusal),
|
||||
}
|
||||
|
||||
/// A top line asked, numbered so late callbacks know theirs.
|
||||
@@ -89,7 +92,7 @@ pub struct MarkdownDocument {
|
||||
/// Text changes counted; a read of an older one lapses.
|
||||
edits: u64,
|
||||
_parsing: Option<Task<()>>,
|
||||
formatting: bool,
|
||||
editing: bool,
|
||||
list: ListState,
|
||||
open: Option<Open>,
|
||||
/// A top line asked for, until the view holds it.
|
||||
@@ -161,7 +164,7 @@ impl MarkdownDocument {
|
||||
parsed: Arc::default(),
|
||||
edits: 0,
|
||||
_parsing: None,
|
||||
formatting: false,
|
||||
editing: false,
|
||||
list: ListState::new(1, ListAlignment::Top, overdraw),
|
||||
open: None,
|
||||
top: None,
|
||||
|
||||
@@ -0,0 +1,374 @@
|
||||
//! Block commands as Typora's Paragraph menu (R4.7).
|
||||
|
||||
use gpui::{Modifiers, TestAppContext};
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use super::{open, opened, press, text};
|
||||
use crate::documents::{
|
||||
BlockCommand, EditRefusal, MarkdownMode, document::block::run, markdown::file_tree,
|
||||
};
|
||||
|
||||
/// `marked` after `command`: `‸` is the caret, `⟨…⟩` the selection.
|
||||
pub(super) fn ran(marked: &str, command: BlockCommand) -> Result<String, EditRefusal> {
|
||||
let caret = marked.find('‸');
|
||||
let text: String = marked.chars().filter(|ch| !"‸⟨⟩".contains(*ch)).collect();
|
||||
let selection = match caret {
|
||||
Some(at) => at..at,
|
||||
None => {
|
||||
let start = marked.find('⟨').expect("a selection");
|
||||
start..marked.find('⟩').expect("its end") - '⟨'.len_utf8()
|
||||
}
|
||||
};
|
||||
let (edits, selected) = run(&text, selection, command)?;
|
||||
let mut out = text.clone();
|
||||
for (range, with) in edits.into_iter().rev() {
|
||||
out.replace_range(range, &with);
|
||||
}
|
||||
Ok(match selected.is_empty() {
|
||||
true => format!("{}‸{}", &out[..selected.start], &out[selected.start..]),
|
||||
false => format!(
|
||||
"{}⟨{}⟩{}",
|
||||
&out[..selected.start],
|
||||
&out[selected.clone()],
|
||||
&out[selected.end..]
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn ok(marked: &str, command: BlockCommand) -> String {
|
||||
ran(marked, command).expect("ran")
|
||||
}
|
||||
|
||||
/// The top-level blocks `text` reads as.
|
||||
pub(super) fn kinds(text: &str) -> Vec<&'static str> {
|
||||
let text: String = text.chars().filter(|ch| !"‸⟨⟩".contains(*ch)).collect();
|
||||
let root = file_tree(&text);
|
||||
let kids = root.children().map(Vec::as_slice).unwrap_or_default();
|
||||
(kids.iter())
|
||||
.map(|kid| match kid {
|
||||
Node::Paragraph(_) => "p",
|
||||
Node::Heading(_) => "h",
|
||||
Node::Blockquote(_) => "quote",
|
||||
Node::List(_) => "list",
|
||||
Node::Code(_) => "code",
|
||||
Node::Math(_) => "math",
|
||||
Node::ThematicBreak(_) => "rule",
|
||||
Node::Yaml(_) => "yaml",
|
||||
Node::Definition(_) => "definition",
|
||||
Node::FootnoteDefinition(_) => "note",
|
||||
_ => "other",
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn headings_set_as_typoras_menu() {
|
||||
use BlockCommand::*;
|
||||
assert_eq!(ok("a‸b", Heading(2)), "## a‸b");
|
||||
assert_eq!(ok("## a‸b", Heading(2)), "## a‸b");
|
||||
assert_eq!(ok("## a‸b", Heading(3)), "### a‸b");
|
||||
assert_eq!(ok("a‸", Promote), "###### a‸");
|
||||
assert_eq!(ok("# a‸", Promote), "# a‸");
|
||||
assert_eq!(ok("## a‸", Promote), "# a‸");
|
||||
assert_eq!(ok("###### a‸", Demote), "a‸");
|
||||
assert_eq!(ok("a‸", Demote), "a‸");
|
||||
assert_eq!(ok("a\n\n‸\n\nb", Heading(2)), "a\n\n## ‸\n\nb");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_paragraph_line_becomes_the_heading_alone() {
|
||||
let out = ok("a\nb‸\nc", BlockCommand::Heading(1));
|
||||
assert_eq!(out, "a\n# b‸\nc");
|
||||
assert_eq!(kinds(&out), ["p", "h", "p"]);
|
||||
assert_eq!(ok("> a\nb‸", BlockCommand::Heading(1)), "> a\n> # b‸");
|
||||
assert_eq!(ok("> a‸\nb", BlockCommand::Heading(1)), "> # a‸\n> b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn setext_headings_relevel_and_go_atx_past_two() {
|
||||
use BlockCommand::*;
|
||||
assert_eq!(ok("a‸\n===", Heading(2)), "a‸\n---");
|
||||
assert_eq!(ok("a‸\n===", Heading(3)), "### a‸");
|
||||
assert_eq!(ok("a\nb‸\n---", Heading(4)), "#### a b‸");
|
||||
assert_eq!(ok("a‸\n===", Paragraph), "a‸");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_heading_made_text_keeps_apart_from_its_neighbors() {
|
||||
use BlockCommand::Paragraph;
|
||||
assert_eq!(ok("a\n# h‸", Paragraph), "a\n\nh‸");
|
||||
assert_eq!(ok("# h‸\nb", Paragraph), "h‸\n\nb");
|
||||
let out = ok("# h‸\n---", Paragraph);
|
||||
assert_eq!(kinds(&out), ["p", "rule"]);
|
||||
assert_eq!(ok("- a\n- # h‸", Paragraph), "- a\n- h‸");
|
||||
let out = ok("> # h‸\nb", Paragraph);
|
||||
assert_eq!(kinds(&out), ["quote", "p"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn heading_commands_refuse_or_add_a_paragraph_as_typora() {
|
||||
use BlockCommand::*;
|
||||
assert_eq!(ran("```\na‸\n```", Heading(1)), Err(EditRefusal::Code));
|
||||
assert_eq!(ran("$$\na‸\n$$", Promote), Err(EditRefusal::Math));
|
||||
assert_eq!(ok("$$\na‸\n$$", Heading(1)), "$$\na\n$$\n\n‸");
|
||||
assert_eq!(
|
||||
ran("---\nt: a‸\n---\n", Heading(1)),
|
||||
Err(EditRefusal::FrontMatter)
|
||||
);
|
||||
assert_eq!(ran("- [ ] a‸", Heading(1)), Err(EditRefusal::Outside));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quotes_wrap_and_unwrap() {
|
||||
use BlockCommand::Quote;
|
||||
assert_eq!(ok("a‸", Quote), "> a‸");
|
||||
assert_eq!(ok("> a‸", Quote), "a‸");
|
||||
assert_eq!(ok("> a\n> b‸", Quote), "a\nb‸");
|
||||
assert_eq!(ok("> a\nb‸", Quote), "a\nb‸");
|
||||
assert_eq!(ok("⟨a\n\nb⟩", Quote), "> ⟨a\n>\n> b⟩");
|
||||
assert_eq!(ok("x\n> a‸", Quote), "x\n\na‸");
|
||||
assert_eq!(ok("> [!NOTE]\n> a‸", Quote), "a‸");
|
||||
assert_eq!(ok("- a‸", Quote), "- > a‸");
|
||||
assert_eq!(ok("> a\n>\n> ⟨b⟩", Quote), "> a\n>\n> > ⟨b⟩");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lists_wrap_each_line() {
|
||||
use BlockCommand::*;
|
||||
assert_eq!(ok("a‸", BulletList), "- a‸");
|
||||
assert_eq!(ok("a‸", OrderedList), "1. a‸");
|
||||
assert_eq!(ok("a‸", TaskList), "- [ ] a‸");
|
||||
assert_eq!(ok("⟨a\nb⟩", OrderedList), "1. ⟨a\n2. b⟩");
|
||||
assert_eq!(ok("> a‸", BulletList), "> - a‸");
|
||||
assert_eq!(ok("- a\n\n b‸", BulletList), "- a\n\n - b‸");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_list_of_the_kind_unwraps_and_another_retypes() {
|
||||
use BlockCommand::*;
|
||||
let out = ok("- a\n- b‸", BulletList);
|
||||
assert_eq!(out, "a\n\nb‸");
|
||||
assert_eq!(kinds(&out), ["p", "p"]);
|
||||
assert_eq!(ok("- a\n- b‸", OrderedList), "1. a\n2. b‸");
|
||||
assert_eq!(ok("- a\n b‸", OrderedList), "1. a\n b‸");
|
||||
assert_eq!(ok("- [ ] a‸", BulletList), "- a‸");
|
||||
assert_eq!(ok("* a‸", TaskList), "* [ ] a‸");
|
||||
assert_eq!(ok("3. a\n4. b‸", BulletList), "- a\n- b‸");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inserts_split_the_paragraph_at_the_caret() {
|
||||
use BlockCommand::*;
|
||||
assert_eq!(ok("ab‸cd", Rule), "ab\n\n---\n\n‸cd");
|
||||
assert_eq!(ok("ab‸", CodeBlock), "ab\n\n```\n‸\n```");
|
||||
assert_eq!(ok("a ⟨b⟩", CodeBlock), "a\n\n```\n‸b\n```");
|
||||
assert_eq!(ok("‸ab", Toc), "[toc]‸\n\nab");
|
||||
assert_eq!(ok("# ab‸cd", Rule), "# ab\n\n---\n\n# ‸cd");
|
||||
assert_eq!(ok("- ab‸", Rule), "- ab\n\n ---\n\n ‸");
|
||||
assert_eq!(ok("```\na‸\n```", Rule), "```\na\n```\n\n---\n\n‸");
|
||||
assert_eq!(ok("a\n\n‸\n\nb", Rule), "a\n\n---\n\n‸\n\nb");
|
||||
assert_eq!(ran("⟨a\n\nb⟩", Rule), Err(EditRefusal::Across));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notes_and_definitions_take_free_names() {
|
||||
use BlockCommand::*;
|
||||
let out = ok("x[^1]\n\n[^1]: y\n\nab‸", Footnote);
|
||||
assert_eq!(out, "x[^1]\n\n[^1]: y\n\nab\n\n[^2]: ‸");
|
||||
assert_eq!(ok("‸", LinkDefinition), "[‸]: ");
|
||||
assert_eq!(ok("⟨see⟩", LinkDefinition), "[see]: ‸");
|
||||
assert_eq!(ok("⟨a/b.md⟩", LinkDefinition), "[‸]: a/b.md");
|
||||
let out = ok("ab‸", MathBlock);
|
||||
assert_eq!(kinds(&out), ["p", "math"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_paragraphs_come_before_and_after() {
|
||||
use BlockCommand::*;
|
||||
assert_eq!(ok("a‸", ParagraphAfter), "a\n\n‸");
|
||||
assert_eq!(ok("# a‸\nb", ParagraphAfter), "# a\n\n‸\n\nb");
|
||||
assert_eq!(ok("a\n\nb‸", ParagraphBefore), "a\n\n‸\n\nb");
|
||||
assert_eq!(ok("a\n# h‸", ParagraphBefore), "a\n\n‸\n\n# h");
|
||||
let out = ok("- a‸", ParagraphBefore);
|
||||
assert_eq!(out, "- ‸\n a");
|
||||
assert_eq!(kinds(&out), ["list"]);
|
||||
let out = ok("x[^1]\n\n[^1]: a‸", ParagraphBefore);
|
||||
assert_eq!(out, "x[^1]\n\n[^1]: ‸\n a");
|
||||
assert_eq!(kinds(&out), ["p", "note"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn front_matter_goes_on_top_once() {
|
||||
let out = ok("a‸", BlockCommand::FrontMatter);
|
||||
assert_eq!(out, "---\n‸\n---\n\na");
|
||||
assert_eq!(kinds(&out), ["yaml", "p"]);
|
||||
assert_eq!(
|
||||
ran("---\nt: 1\n---\n\na‸", BlockCommand::FrontMatter),
|
||||
Err(EditRefusal::Present)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_takes_the_block_or_its_lone_holder() {
|
||||
use BlockCommand::Delete;
|
||||
assert_eq!(ok("a\n\nb‸\n\nc", Delete), "a\n\n‸c");
|
||||
assert_eq!(ok("x\n\n> b‸\n\nc", Delete), "x\n\n‸c");
|
||||
assert_eq!(ok("- a\n- b‸\n- c", Delete), "- a\n‸- c");
|
||||
assert_eq!(ok("- a‸\n\n b", Delete), "- ‸\n\n b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_endings_follow_the_text() {
|
||||
assert_eq!(
|
||||
ok("ab‸\r\n", BlockCommand::ParagraphAfter),
|
||||
"ab\r\n\r\n‸\r\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_source_command_is_one_undo_step(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open("alpha\n", MarkdownMode::Source, cx);
|
||||
document.update(cx, |document, cx| {
|
||||
document
|
||||
.editor
|
||||
.update(cx, |editor, cx| editor.select(std::iter::once(2..2), cx));
|
||||
document.block(BlockCommand::Heading(2), cx).expect("taken");
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert_eq!(text(&document, cx), "## alpha\n");
|
||||
document.update(cx, |document, cx| {
|
||||
document.editor.update(cx, |editor, cx| editor.undo(cx));
|
||||
});
|
||||
assert_eq!(text(&document, cx), "alpha\n");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn an_open_block_takes_a_command_and_stays_open(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open("alpha\n", MarkdownMode::Visual, cx);
|
||||
press(0, Modifiers::none(), cx);
|
||||
document.update(cx, |document, cx| {
|
||||
document.block(BlockCommand::Quote, cx).expect("taken");
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert_eq!(opened(&document, cx), "> alpha", "still open to type in");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_command_past_the_open_block_writes_it_back_first(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open("first\n\nalpha\n", MarkdownMode::Visual, cx);
|
||||
press(1, Modifiers::none(), cx);
|
||||
document.update(cx, |document, cx| {
|
||||
let open = document.open.as_ref().expect("opened");
|
||||
open.input.update(cx, |input, cx| input.insert("x", cx));
|
||||
document
|
||||
.block(BlockCommand::FrontMatter, cx)
|
||||
.expect("taken");
|
||||
});
|
||||
cx.run_until_parked();
|
||||
let written = text(&document, cx);
|
||||
assert!(written.starts_with("---\n\n---\n"), "{written:?}");
|
||||
assert!(
|
||||
written.contains("alpha") && written.contains('x'),
|
||||
"{written:?}"
|
||||
);
|
||||
document.read_with(cx, |document, _| assert!(document.open.is_none()));
|
||||
}
|
||||
|
||||
/// The one list's items, each as its blocks' kinds.
|
||||
fn items(text: &str) -> Vec<Vec<&'static str>> {
|
||||
let text: String = text.chars().filter(|ch| !"‸⟨⟩".contains(*ch)).collect();
|
||||
let root = file_tree(&text);
|
||||
let Some(Node::List(list)) = root.children().and_then(|kids| kids.first()) else {
|
||||
panic!("a list first");
|
||||
};
|
||||
(list.children.iter())
|
||||
.map(|item| {
|
||||
(item
|
||||
.children()
|
||||
.map(Vec::as_slice)
|
||||
.unwrap_or_default()
|
||||
.iter())
|
||||
.map(|kid| match kid {
|
||||
Node::Paragraph(_) => "p",
|
||||
Node::Code(_) => "code",
|
||||
_ => "other",
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_caret_in_heading_marks_splits_at_the_text() {
|
||||
use BlockCommand::Rule;
|
||||
assert_eq!(ok("‸# alpha", Rule), "---\n\n# ‸alpha");
|
||||
let out = ok("# alpha #‸##", Rule);
|
||||
assert_eq!(kinds(&out), ["h", "rule"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_task_list_keeps_checked_boxes() {
|
||||
assert_eq!(
|
||||
ok("- a\n- [x] b‸", BlockCommand::TaskList),
|
||||
"- [ ] a\n- [x] b‸"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_alert_in_an_item_keeps_the_item() {
|
||||
let out = ok("- > [!NOTE]\n > a‸\n- b", BlockCommand::Quote);
|
||||
assert_eq!(out, "- a‸\n- b");
|
||||
assert_eq!(items(&out), [vec!["p"], vec!["p"]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_split_closes_and_reopens_inline_marks() {
|
||||
use BlockCommand::Rule;
|
||||
let out = ok("**ab‸cd**", Rule);
|
||||
assert_eq!(out, "**ab**\n\n---\n\n**‸cd**");
|
||||
assert_eq!(kinds(&out), ["p", "rule", "p"]);
|
||||
assert_eq!(ran("[a‸b](u)", Rule), Err(EditRefusal::Across));
|
||||
assert_eq!(ran("`a‸b`", Rule), Err(EditRefusal::Across));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_retyped_item_keeps_its_code_indent() {
|
||||
let out = ok("- code‸", BlockCommand::OrderedList);
|
||||
assert_eq!(out, "1. code‸");
|
||||
assert_eq!(items(&out), [vec!["code"]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_keeps_neighbors_apart() {
|
||||
let out = ok("p\n# h‸\nq", BlockCommand::Delete);
|
||||
assert_eq!(out, "p\n‸\nq");
|
||||
assert_eq!(kinds(&out), ["p", "p"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn heading_marks_keep_their_text() {
|
||||
use BlockCommand::*;
|
||||
let out = ok("alpha #‸", Heading(1));
|
||||
assert_eq!(out, "# alpha \\#‸");
|
||||
assert_eq!(kinds(&out), ["h"]);
|
||||
let out = ok("# - alpha‸", Paragraph);
|
||||
assert_eq!(out, "\\- alpha‸");
|
||||
assert_eq!(kinds(&out), ["p"]);
|
||||
assert_eq!(kinds(&ok("# 1. a‸", Paragraph)), ["p"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tasks_first_text_takes_no_block() {
|
||||
use BlockCommand::*;
|
||||
assert_eq!(ran("- [ ] a‸", Quote), Err(EditRefusal::Outside));
|
||||
assert_eq!(ran("- [ ] ‸ab", Rule), Err(EditRefusal::Outside));
|
||||
assert_eq!(ran("- [ ] ‸ab", CodeBlock), Err(EditRefusal::Outside));
|
||||
assert_eq!(ran("- [ ] a‸", ParagraphBefore), Err(EditRefusal::Outside));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_selection_past_a_list_takes_its_siblings() {
|
||||
let out = ok("- ⟨a\n\nb⟩", BlockCommand::OrderedList);
|
||||
assert_eq!(out, "1. ⟨a\n\n2. b⟩");
|
||||
assert_eq!(kinds(&out), ["list"]);
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
//! Block commands whose results reread as written.
|
||||
|
||||
use markdown::mdast::Node;
|
||||
|
||||
use super::block::{kinds, ok, ran};
|
||||
use crate::documents::{BlockCommand, EditRefusal, markdown::file_tree};
|
||||
|
||||
fn plain(marked: &str) -> String {
|
||||
marked.chars().filter(|ch| !"‸⟨⟩".contains(*ch)).collect()
|
||||
}
|
||||
|
||||
/// Every node of `text`, in document order.
|
||||
fn nodes(text: &str, keep: &mut dyn FnMut(&Node)) {
|
||||
fn walk(node: &Node, keep: &mut dyn FnMut(&Node)) {
|
||||
keep(node);
|
||||
for kid in node.children().map(Vec::as_slice).unwrap_or_default() {
|
||||
walk(kid, keep);
|
||||
}
|
||||
}
|
||||
walk(&file_tree(text), keep);
|
||||
}
|
||||
|
||||
fn checks(text: &str) -> Vec<Option<bool>> {
|
||||
let mut out = Vec::new();
|
||||
nodes(text, &mut |node| {
|
||||
if let Node::ListItem(item) = node {
|
||||
out.push(item.checked);
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
fn codes(text: &str) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
nodes(text, &mut |node| {
|
||||
if let Node::Code(code) = node {
|
||||
out.push(code.value.clone());
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
fn texts(text: &str) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
nodes(text, &mut |node| {
|
||||
if let Node::Text(text) = node {
|
||||
out.push(text.value.clone());
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_split_that_would_reread_otherwise_refuses() {
|
||||
assert_eq!(
|
||||
ran("~~ab‸~cd~~", BlockCommand::Rule),
|
||||
Err(EditRefusal::Misread)
|
||||
);
|
||||
assert_eq!(
|
||||
ran("__ab‸_cd__", BlockCommand::Rule),
|
||||
Err(EditRefusal::Misread)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_split_rest_that_would_open_a_block_is_escaped() {
|
||||
let out = plain(&ok("a ‸- b", BlockCommand::Rule));
|
||||
assert_eq!(out, "a\n\n---\n\n\\- b");
|
||||
assert_eq!(kinds(&out), ["p", "rule", "p"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_hashes_stay_text_wherever_a_heading_line_ends() {
|
||||
let out = plain(&ok("alpha #‸\n===", BlockCommand::Heading(3)));
|
||||
assert_eq!(out, "### alpha \\#");
|
||||
assert_eq!(texts(&out), ["alpha #"]);
|
||||
let out = plain(&ok("# a #‸ b", BlockCommand::Rule));
|
||||
assert_eq!(kinds(&out), ["h", "rule", "h"]);
|
||||
assert_eq!(texts(&out), ["a #", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_paragraph_reads_the_heading_body_without_closing_hashes() {
|
||||
let out = plain(&ok("# ---‸ ###", BlockCommand::Paragraph));
|
||||
assert_eq!(out, "\\---");
|
||||
assert_eq!(kinds(&out), ["p"]);
|
||||
let out = plain(&ok("# [x]: /u‸ ###", BlockCommand::Paragraph));
|
||||
assert_eq!(kinds(&out), ["p"]);
|
||||
assert_eq!(texts(&out).concat(), "[x]: /u");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_task_box_below_its_marker_stays_checked() {
|
||||
let out = plain(&ok("1.‸\n [x] a", BlockCommand::TaskList));
|
||||
assert_eq!(checks(&out), [Some(true)]);
|
||||
assert_eq!(texts(&out), ["a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retyping_keeps_code_columns_past_tabs() {
|
||||
let source = "-\t\t‸code";
|
||||
let out = plain(&ok(source, BlockCommand::OrderedList));
|
||||
assert_eq!(codes(&out), codes(&plain(source)));
|
||||
assert_eq!(codes(&out).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_task_cannot_box_code() {
|
||||
assert_eq!(
|
||||
ran("- ‸a", BlockCommand::TaskList),
|
||||
Err(EditRefusal::Code)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_alert_unwraps_into_its_holder() {
|
||||
assert_eq!(
|
||||
plain(&ok("- > [!NOTE]‸\n a", BlockCommand::Quote)),
|
||||
"- a"
|
||||
);
|
||||
assert_eq!(
|
||||
plain(&ok("> > [!NOTE]‸\n> a", BlockCommand::Quote)),
|
||||
"> a"
|
||||
);
|
||||
let out = plain(&ok("- > [!NOTE]‸\n >\n >\n > a", BlockCommand::Quote));
|
||||
assert_eq!(out, "- a");
|
||||
assert_eq!(kinds(&out), ["list"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_alert_marker_sharing_its_line_refuses() {
|
||||
assert_eq!(
|
||||
ran("> [!NOTE] a‸", BlockCommand::Quote),
|
||||
Err(EditRefusal::Misread)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_keeps_neighbors_apart_past_gone_openers() {
|
||||
let out = plain(&ok("p\n> # ‸h\nq", BlockCommand::Delete));
|
||||
assert_eq!(out, "p\n\nq");
|
||||
assert_eq!(kinds(&out), ["p", "p"]);
|
||||
}
|
||||
|
||||
fn inline_codes(text: &str) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
nodes(text, &mut |node| {
|
||||
if let Node::InlineCode(code) = node {
|
||||
out.push(code.value.clone());
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_heading_join_keeps_white_space_in_code() {
|
||||
let source = "a `x\n y`‸\n===";
|
||||
match ran(source, BlockCommand::Heading(3)) {
|
||||
Err(EditRefusal::Misread) => {}
|
||||
Ok(out) => assert_eq!(inline_codes(&plain(&out)), inline_codes(&plain(source))),
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_split_that_draws_otherwise_refuses() {
|
||||
for (source, command) in [
|
||||
("<u>ab‸cd</u>", BlockCommand::Rule),
|
||||
("<u>a‸\nb</u>", BlockCommand::Heading(1)),
|
||||
("x:smile: ‸:smile:", BlockCommand::Rule),
|
||||
] {
|
||||
assert_eq!(ran(source, command), Err(EditRefusal::Misread), "{source}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_alert_body_block_keeps_its_indent() {
|
||||
let out = plain(&ok("> [!NOTE]\n>\n> co‸de", BlockCommand::Quote));
|
||||
assert_eq!(
|
||||
(kinds(&out), codes(&out)),
|
||||
(vec!["code"], vec!["code".to_string()])
|
||||
);
|
||||
let out = plain(&ok(
|
||||
"> [!NOTE]\n>\n> ```\n> co‸de\n> ```",
|
||||
BlockCommand::Quote,
|
||||
));
|
||||
assert_eq!(
|
||||
(kinds(&out), codes(&out)),
|
||||
(vec!["code"], vec!["code".to_string()])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retyping_reads_a_fence_from_its_source() {
|
||||
let out = plain(&ok("- ```lang‸", BlockCommand::OrderedList));
|
||||
assert_eq!(codes(&out), vec!["```lang".to_string()]);
|
||||
let out = plain(&ok("- \u{a0}code‸", BlockCommand::OrderedList));
|
||||
assert_eq!(codes(&out), vec!["\u{a0}code".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_removed_box_leaves_text() {
|
||||
assert_eq!(
|
||||
plain(&ok("- [x] ---‸", BlockCommand::BulletList)),
|
||||
"- \\---"
|
||||
);
|
||||
assert_eq!(
|
||||
plain(&ok("- [x] # h‸", BlockCommand::BulletList)),
|
||||
"- \\# h"
|
||||
);
|
||||
assert_eq!(plain(&ok("-\n [x] a‸", BlockCommand::TaskList)), "a");
|
||||
assert_eq!(plain(&ok("- [x] # h‸", BlockCommand::TaskList)), "\\# h");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_heading_becomes_an_empty_paragraph() {
|
||||
assert!(ran("# ‸", BlockCommand::Paragraph).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_escaped_alert_marker_on_its_own_line_unwraps() {
|
||||
assert_eq!(plain(&ok("> \\[!NOTE]\n> a‸", BlockCommand::Quote)), "a");
|
||||
}
|
||||
@@ -8,7 +8,7 @@ use web_time::Instant;
|
||||
|
||||
use super::{open, opened, press, text};
|
||||
use crate::documents::{
|
||||
DocumentEvent, FormatRefusal, InlineFormat, LARGEST_READ, MarkdownDocument, MarkdownMode,
|
||||
DocumentEvent, EditRefusal, InlineFormat, LARGEST_READ, MarkdownDocument, MarkdownMode,
|
||||
document::{
|
||||
format::{toggle, toggle_tree},
|
||||
visual,
|
||||
@@ -17,7 +17,7 @@ use crate::documents::{
|
||||
};
|
||||
|
||||
/// `marked` toggled: `‸` is the caret, `⟨…⟩` the selection.
|
||||
fn toggled(marked: &str, format: InlineFormat) -> Result<String, FormatRefusal> {
|
||||
fn toggled(marked: &str, format: InlineFormat) -> Result<String, EditRefusal> {
|
||||
let caret = marked.find('‸');
|
||||
let text: String = marked.chars().filter(|ch| !"‸⟨⟩".contains(*ch)).collect();
|
||||
let selection = match caret {
|
||||
@@ -109,7 +109,7 @@ fn clear_takes_every_mark_off_the_touched_blocks() {
|
||||
|
||||
#[test]
|
||||
fn code_math_html_and_front_matter_refuse() {
|
||||
use FormatRefusal as Why;
|
||||
use EditRefusal as Why;
|
||||
use InlineFormat::*;
|
||||
assert_eq!(toggled("```\nco‸de\n```", Strong), Err(Why::Code));
|
||||
assert_eq!(toggled("x `co‸de` y", Strong), Err(Why::Code));
|
||||
@@ -126,12 +126,9 @@ fn code_math_html_and_front_matter_refuse() {
|
||||
#[test]
|
||||
fn a_selection_across_a_link_or_into_its_address_refuses() {
|
||||
use InlineFormat::*;
|
||||
assert_eq!(toggled("[a ⟨b](u) c⟩", Strong), Err(FormatRefusal::Across));
|
||||
assert_eq!(
|
||||
toggled("[a](ht‸tp://x)", Strong),
|
||||
Err(FormatRefusal::Across)
|
||||
);
|
||||
assert_eq!(toggled("a\n\n> ‸", Strong), Err(FormatRefusal::Outside));
|
||||
assert_eq!(toggled("[a ⟨b](u) c⟩", Strong), Err(EditRefusal::Across));
|
||||
assert_eq!(toggled("[a](ht‸tp://x)", Strong), Err(EditRefusal::Across));
|
||||
assert_eq!(toggled("a\n\n> ‸", Strong), Err(EditRefusal::Outside));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
@@ -185,14 +182,14 @@ fn the_source_takes_a_format_as_one_step(cx: &mut TestAppContext) {
|
||||
fn preview_refuses_a_format(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open("alpha\n", MarkdownMode::Preview, cx);
|
||||
let refused = document.update(cx, |document, cx| document.format(InlineFormat::Strong, cx));
|
||||
assert_eq!(refused, Err(FormatRefusal::Preview));
|
||||
assert_eq!(refused, Err(EditRefusal::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);
|
||||
let across = Err(EditRefusal::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);
|
||||
@@ -283,12 +280,12 @@ fn a_format_lapses_when_the_text_moves(cx: &mut TestAppContext) {
|
||||
fn refusals(
|
||||
document: &Entity<MarkdownDocument>,
|
||||
cx: &mut VisualTestContext,
|
||||
) -> Rc<RefCell<Vec<FormatRefusal>>> {
|
||||
) -> Rc<RefCell<Vec<EditRefusal>>> {
|
||||
let heard = Rc::new(RefCell::new(Vec::new()));
|
||||
let kept = heard.clone();
|
||||
cx.update(|_, cx| {
|
||||
cx.subscribe(document, move |_, event: &DocumentEvent, _| {
|
||||
if let DocumentEvent::FormatRefused(why) = event {
|
||||
if let DocumentEvent::EditRefused(why) = event {
|
||||
kept.borrow_mut().push(*why);
|
||||
}
|
||||
})
|
||||
@@ -308,13 +305,13 @@ fn a_lapse_comes_by_event(cx: &mut TestAppContext) {
|
||||
.update(cx, |editor, cx| editor.edit([(0..0, "y".to_string())], cx));
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert_eq!(*heard.borrow(), [FormatRefusal::Changed]);
|
||||
assert_eq!(*heard.borrow(), [EditRefusal::Changed]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unwrap_refuses_an_end_in_nested_marks() {
|
||||
use InlineFormat::*;
|
||||
let across = Err(FormatRefusal::Across);
|
||||
let across = Err(EditRefusal::Across);
|
||||
assert_eq!(toggled("**[a](ht‸tps://x)**", Strong), across);
|
||||
assert_eq!(toggled("**~~a~‸~**", Strong), across);
|
||||
assert_eq!(toggled("<u>[a](ht‸tps://x)</u>", Underline), across);
|
||||
@@ -359,7 +356,7 @@ fn a_second_format_refuses_while_one_runs(cx: &mut TestAppContext) {
|
||||
document.format(InlineFormat::Strong, cx).expect("taken");
|
||||
document.format(InlineFormat::Emphasis, cx)
|
||||
});
|
||||
assert_eq!(second, Err(FormatRefusal::Busy));
|
||||
assert_eq!(second, Err(EditRefusal::Busy));
|
||||
cx.run_until_parked();
|
||||
document.update(cx, |document, cx| {
|
||||
document.format(InlineFormat::Emphasis, cx).expect("taken")
|
||||
@@ -386,7 +383,7 @@ fn a_refusal_comes_by_event(cx: &mut TestAppContext) {
|
||||
});
|
||||
assert_eq!(taken, Ok(()));
|
||||
cx.run_until_parked();
|
||||
assert_eq!(*heard.borrow(), [FormatRefusal::Code]);
|
||||
assert_eq!(*heard.borrow(), [EditRefusal::Code]);
|
||||
assert_eq!(text(&document, cx), source);
|
||||
}
|
||||
|
||||
@@ -400,14 +397,14 @@ fn a_format_drops_once_preview_shows(cx: &mut TestAppContext) {
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert!(text(&document, cx).starts_with("xx"));
|
||||
assert_eq!(*heard.borrow(), [FormatRefusal::Preview]);
|
||||
assert_eq!(*heard.borrow(), [EditRefusal::Preview]);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_text_past_the_largest_read_refuses(cx: &mut TestAppContext) {
|
||||
let (document, cx) = word(cx, LARGEST_READ + 1);
|
||||
let refused = document.update(cx, |document, cx| document.format(InlineFormat::Strong, cx));
|
||||
assert_eq!(refused, Err(FormatRefusal::TooLong));
|
||||
assert_eq!(refused, Err(EditRefusal::TooLong));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
|
||||
@@ -366,6 +366,8 @@ fn a_press_after_a_write_back_waits_for_the_long_read(cx: &mut TestAppContext) {
|
||||
}
|
||||
|
||||
mod attach;
|
||||
mod block;
|
||||
mod block_edges;
|
||||
mod format;
|
||||
mod history;
|
||||
mod links;
|
||||
|
||||
@@ -36,8 +36,9 @@ pub use counts::{ReadingTime, WordCount};
|
||||
pub use diagram::{Diagram, diagram_svg};
|
||||
pub(crate) use diagram::{flatten, options as svg_options};
|
||||
pub use document::{
|
||||
Attachment, DocumentEvent, DocumentHeading, DocumentLook, DocumentTarget, FormatRefusal,
|
||||
InlineFormat, LARGEST_READ, MarkdownDocument, TableEdit, TableRefusal, TargetBlock,
|
||||
Attachment, BlockCommand, DocumentEvent, DocumentHeading, DocumentLook, DocumentTarget,
|
||||
EditRefusal, InlineFormat, LARGEST_READ, MarkdownDocument, TableEdit, TableRefusal,
|
||||
TargetBlock,
|
||||
};
|
||||
pub use epub::{Chapter, EpubReader};
|
||||
pub use format::{Format, format};
|
||||
|
||||
Reference in new issue
Block a user