pub struct Doc {
pub blocks: Vec<Block>,
}Expand description
A markdown document: blocks in document order.
Fields§
§blocks: Vec<Block>Implementations§
Source§impl Doc
impl Doc
Sourcepub fn split(&mut self, ix: usize, at: usize) -> usize
pub fn split(&mut self, ix: usize, at: usize) -> usize
Split block ix at byte offset at, returning the new block’s index.
The tail keeps the block’s kind so Enter in a list makes another item — except for a heading, where the body that follows a title is body text.
Sourcepub fn merge_back(&mut self, at: Cursor) -> Option<Cursor>
pub fn merge_back(&mut self, at: Cursor) -> Option<Cursor>
Backspace at the start of a block.
Notion’s chain, in order: an indented block outdents, an image with
nothing written under it goes, a block wearing syntax around its text
gives the syntax up, and only a plain block at the left margin merges
into the one above it. When that one holds no body there is nothing to
merge into, so the caret steps into a fence or a table, and a rule —
which no caret can enter, and so no other key can remove — goes.
Returns where the caret landed, and None when nothing moved.
A table cell is not a position that can swallow its neighbour, so backspace at the start of one does nothing rather than eating the table.
Sourcepub fn edit_at(&mut self, at: Cursor, edit: impl FnOnce(&mut Text))
pub fn edit_at(&mut self, at: Cursor, edit: impl FnOnce(&mut Text))
Apply an edit to the text at at, then put the block back in order.
The editor should reach text through here rather than mutating a block directly: a heading or a table cell that acquires a newline has no spelling, and nothing else is positioned to notice.
Sourcepub fn subtree(&self, ix: usize) -> Range<usize> ⓘ
pub fn subtree(&self, ix: usize) -> Range<usize> ⓘ
The blocks nested under ix, ix included — what a move, a duplicate
or a drag carries with it.
A flat list makes this a scan for the next block that is not deeper, which is the whole argument for the flat list.
Sourcepub fn move_block(&mut self, ix: usize, delta: isize) -> Option<usize>
pub fn move_block(&mut self, ix: usize, delta: isize) -> Option<usize>
Move a block and its children to sit before or after their neighbour.
delta counts siblings, not rows: moving down past a bullet with
three children clears all four, or a block would land inside the run it
was trying to step over.
Sourcepub fn duplicate(&mut self, ix: usize) -> Option<usize>
pub fn duplicate(&mut self, ix: usize) -> Option<usize>
Copy a block and its children in below themselves.
Sourcepub fn remove_block(&mut self, ix: usize)
pub fn remove_block(&mut self, ix: usize)
Delete a block and its children.
Sourcepub fn set_kind(&mut self, ix: usize, kind: BlockKind)
pub fn set_kind(&mut self, ix: usize, kind: BlockKind)
Turn block ix into kind, carrying its text across and keeping its
indent.
The one operation a typed prefix, the slash menu and the block menu all perform, so none of them reaches into a block’s kind on its own.
Sourcepub fn set_language(&mut self, ix: usize, language: Option<String>)
pub fn set_language(&mut self, ix: usize, language: Option<String>)
The tag on a fenced block — what the label shows, what the highlighter
reads, and what the info string carries. Not Doc::set_kind’s job:
that carries a body across, and a fence has none to give back.
Sourcepub fn fence(&mut self, selection: Selection) -> Cursor
pub fn fence(&mut self, selection: Selection) -> Cursor
Turn what a selection covers into one code block, leaving whatever it did not cover as blocks of its own.
The fence is what markdown has for code over more than one line. An inline span is not: no CommonMark spelling puts a line break inside backticks, so one written that way comes back as a space.
Marks are dropped on the way in, the way Doc::set_kind drops them
when it turns a block into a fence — code is literal to its closing
fence, and nothing in it is markup.
Sourcepub fn unfence(&mut self, selection: Selection) -> Option<Cursor>
pub fn unfence(&mut self, selection: Selection) -> Option<Cursor>
The way back out of a fence: every line becomes a paragraph. None when
the selection is not all code, which is what makes this the other half
of a toggle rather than an operation of its own.
Sourcepub fn spans(&self, selection: Selection) -> Vec<(Cursor, Range<usize>)>
pub fn spans(&self, selection: Selection) -> Vec<(Cursor, Range<usize>)>
Every text a selection touches, with the slice of it covered.
One selection can reach across paragraphs and table cells, and a mark
applies to each of them separately — marks live inside a Text and
have no way to span two.
Sourcepub fn toggle_mark(&mut self, selection: Selection, mark: Mark)
pub fn toggle_mark(&mut self, selection: Selection, mark: Mark)
Add mark over a selection, or take it away if every part of the
selection already carries it.
The decision is made across the whole selection before anything moves: dragging over a bold word and a plain one and pressing cmd-B should bold the rest rather than unbolding the half that was already there.
Sourcepub fn marks(&self, selection: Selection) -> Vec<Mark>
pub fn marks(&self, selection: Selection) -> Vec<Mark>
The marks a selection carries throughout — what a toolbar paints as lit.
Collapsed, it answers with the marks the next character typed here would
join, which is the left-sticky rule Text::insert already
follows: the run ending at the caret, never the one starting there.
Sourcepub fn covered_by(&self, selection: Selection, mark: &Mark) -> bool
pub fn covered_by(&self, selection: Selection, mark: &Mark) -> bool
Whether every part of a selection already carries mark — what decides
between adding it and taking it away, and what a toolbar button reads to
know whether it is lit.
Sourcepub fn slice(&self, selection: Selection) -> Doc
pub fn slice(&self, selection: Selection) -> Doc
The sub-document a selection covers — what a copy puts on the clipboard.
A table is atomic here for the same reason it is in Doc::replace:
half a table has no shape worth keeping, so a selection reaching into
one takes it whole.
Sourcepub fn splice(&mut self, selection: Selection, other: Doc) -> Cursor
pub fn splice(&mut self, selection: Selection, other: Doc) -> Cursor
Replace a selection with a whole document — the paste path.
A lone paragraph goes in as inline text, marks and all: pasting a sentence into a sentence must not make a new block. Anything else arrives as blocks, and the remainder of the caret’s block follows them.
Sourcepub fn replace(&mut self, selection: Selection, text: Text) -> Splice
pub fn replace(&mut self, selection: Selection, text: Text) -> Splice
Replace everything a selection covers with text, and say where the
caret lands.
The one mutation. Typing, backspace, delete, cut and paste are all this call with a different argument, which is why none of them needs to know whether a selection was empty, spanned two paragraphs, or swallowed a table on the way past.
Sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Put the document into the form markdown can hold — the save step.
Drops the whitespace markdown discards anyway (leading and trailing on
every line, blank lines at a block’s edges), flattens the blocks whose
output is one line, and renumbers ordered runs. After this,
parse(serialize(doc)) == doc.
Sourcepub fn normalize_with(&mut self, marks: &Marks)
pub fn normalize_with(&mut self, marks: &Marks)
Doc::normalize with the app’s own marks — see crate::Marks.
Sourcepub fn indent(&mut self, ix: usize) -> bool
pub fn indent(&mut self, ix: usize) -> bool
Tab. A block can go one level deeper than the one above it, and its children come with it.
Sourcepub fn ceiling(&self, ix: usize) -> u8
pub fn ceiling(&self, ix: usize) -> u8
How deep block ix is allowed to sit.
Markdown expresses nesting through list items and nothing else, so a block may only go deeper than the one above it when that one is a marker. Indenting a paragraph under a heading would serialize to four leading spaces, which reads back as an indented code block.
Sourcepub fn repair(&mut self)
pub fn repair(&mut self)
Clamp every indent to what the document can actually express, then make ordered runs consecutive. Cheap, total, and called after anything structural — a local rule is not enough, because outdenting one block can leave the block after it stranded a level too deep.
Public because an editor that changes a block’s kind has to restore the invariant too, and only this knows what it is.
Trait Implementations§
impl Eq for Doc
impl StructuralPartialEq for Doc
Auto Trait Implementations§
impl Freeze for Doc
impl RefUnwindSafe for Doc
impl Send for Doc
impl Sync for Doc
impl Unpin for Doc
impl UnsafeUnpin for Doc
impl UnwindSafe for Doc
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().