Skip to main content

Doc

Struct Doc 

Source
pub struct Doc {
    pub blocks: Vec<Block>,
}
Expand description

A markdown document: blocks in document order.

Fields§

§blocks: Vec<Block>

Implementations§

Source§

impl Doc

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn duplicate(&mut self, ix: usize) -> Option<usize>

Copy a block and its children in below themselves.

Source

pub fn remove_block(&mut self, ix: usize)

Delete a block and its children.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn normalize_with(&mut self, marks: &Marks)

Doc::normalize with the app’s own marks — see crate::Marks.

Source

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.

Source

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.

Source

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.

Source

pub fn outdent(&mut self, ix: usize) -> bool

Shift-Tab, children included.

Trait Implementations§

Source§

impl Clone for Doc

Source§

fn clone(&self) -> Doc

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Doc

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Doc

Source§

fn default() -> Doc

Returns the “default value” for a type. Read more
Source§

impl Eq for Doc

Source§

impl PartialEq for Doc

Source§

fn eq(&self, other: &Doc) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more