pub struct Document { /* private fields */ }Expand description
Public, fallible, editable DOM over an internal Ast.
Implementations§
Source§impl Document
impl Document
Sourcepub fn with_mode(mode: ContentMode) -> Self
pub fn with_mode(mode: ContentMode) -> Self
Like Document::new but with an explicit root content mode.
Sourcepub fn from_syntax(node: &SyntaxNode) -> Result<Document, FromSyntaxError>
pub fn from_syntax(node: &SyntaxNode) -> Result<Document, FromSyntaxError>
Build a document from a parsed syntax tree.
Sourcepub fn id(&self) -> DocumentId
pub fn id(&self) -> DocumentId
Process-wide unique id of this document.
Sourcepub fn node(&self, id: NodeId) -> Result<NodeRef<'_>, EditError>
pub fn node(&self, id: NodeId) -> Result<NodeRef<'_>, EditError>
Return a read-only handle for a public id.
Sourcepub fn has_errors(&self) -> bool
pub fn has_errors(&self) -> bool
true when the tree contains one or more Error nodes.
Sourcepub fn errors(&self) -> impl Iterator<Item = NodeRef<'_>> + '_
pub fn errors(&self) -> impl Iterator<Item = NodeRef<'_>> + '_
Iterate every Error node in the tree.
Sourcepub fn is_read_only(&self) -> bool
pub fn is_read_only(&self) -> bool
true when this document is read-only.
Sourcepub fn find<'a>(
&'a self,
start: NodeRef<'a>,
pred: impl Fn(NodeRef<'a>) -> bool + 'a,
) -> Option<NodeRef<'a>>
pub fn find<'a>( &'a self, start: NodeRef<'a>, pred: impl Fn(NodeRef<'a>) -> bool + 'a, ) -> Option<NodeRef<'a>>
Find the first matching node under start, including start.
Sourcepub fn find_all<'a>(
&'a self,
start: NodeRef<'a>,
pred: impl Fn(NodeRef<'a>) -> bool + 'a,
) -> impl Iterator<Item = NodeRef<'a>> + 'a
pub fn find_all<'a>( &'a self, start: NodeRef<'a>, pred: impl Fn(NodeRef<'a>) -> bool + 'a, ) -> impl Iterator<Item = NodeRef<'a>> + 'a
Collect matching nodes under start, including start.
Sourcepub fn find_commands<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = NodeRef<'a>> + 'a
pub fn find_commands<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = NodeRef<'a>> + 'a
Find commands by name.
Sourcepub fn find_environments<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = NodeRef<'a>> + 'a
pub fn find_environments<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = NodeRef<'a>> + 'a
Find environments by name.
Sourcepub fn to_syntax(&self) -> SyntaxNode
pub fn to_syntax(&self) -> SyntaxNode
Convert this document back into a lossless SyntaxNode tree.
Sourcepub fn to_latex(&self) -> Result<String, SerializeError>
pub fn to_latex(&self) -> Result<String, SerializeError>
Serialize to LaTeX using the default canonical style.
Sourcepub fn to_latex_with(
&self,
options: &SerializeOptions,
) -> Result<String, SerializeError>
pub fn to_latex_with( &self, options: &SerializeOptions, ) -> Result<String, SerializeError>
Serialize to LaTeX with explicit style options.
Sourcepub fn create_char(&mut self, c: char) -> Result<NodeId, EditError>
pub fn create_char(&mut self, c: char) -> Result<NodeId, EditError>
Create a detached character node.
Sourcepub fn create_text(&mut self, s: impl Into<String>) -> Result<NodeId, EditError>
pub fn create_text(&mut self, s: impl Into<String>) -> Result<NodeId, EditError>
Create a detached text node.
Sourcepub fn create_active_space(&mut self) -> Result<NodeId, EditError>
pub fn create_active_space(&mut self) -> Result<NodeId, EditError>
Create a detached active ~ space node.
Sourcepub fn create_group(&mut self, mode: ContentMode) -> Result<NodeId, EditError>
pub fn create_group(&mut self, mode: ContentMode) -> Result<NodeId, EditError>
Create a detached group node.
Sourcepub fn create_command(
&mut self,
name: impl Into<String>,
args: Vec<ArgValue>,
) -> Result<NodeId, EditError>
pub fn create_command( &mut self, name: impl Into<String>, args: Vec<ArgValue>, ) -> Result<NodeId, EditError>
Create a detached command node.
Sourcepub fn create_declarative(
&mut self,
name: impl Into<String>,
args: Vec<ArgValue>,
) -> Result<NodeId, EditError>
pub fn create_declarative( &mut self, name: impl Into<String>, args: Vec<ArgValue>, ) -> Result<NodeId, EditError>
Create a detached declarative command node.
Sourcepub fn create_environment(
&mut self,
name: impl Into<String>,
args: Vec<ArgValue>,
body: NodeId,
) -> Result<NodeId, EditError>
pub fn create_environment( &mut self, name: impl Into<String>, args: Vec<ArgValue>, body: NodeId, ) -> Result<NodeId, EditError>
Create a detached environment node.
Sourcepub fn append_child(
&mut self,
parent: NodeId,
child: NodeId,
) -> Result<(), EditError>
pub fn append_child( &mut self, parent: NodeId, child: NodeId, ) -> Result<(), EditError>
Append a detached node to a root/group container.
Sourcepub fn insert_before(
&mut self,
anchor: NodeId,
new: NodeId,
) -> Result<(), EditError>
pub fn insert_before( &mut self, anchor: NodeId, new: NodeId, ) -> Result<(), EditError>
Insert a detached node before an attached group child.
Sourcepub fn insert_after(
&mut self,
anchor: NodeId,
new: NodeId,
) -> Result<(), EditError>
pub fn insert_after( &mut self, anchor: NodeId, new: NodeId, ) -> Result<(), EditError>
Insert a detached node after an attached group child.
Sourcepub fn insert_child(
&mut self,
parent: NodeId,
index: usize,
child: NodeId,
) -> Result<(), EditError>
pub fn insert_child( &mut self, parent: NodeId, index: usize, child: NodeId, ) -> Result<(), EditError>
Insert a detached node at index in a root/group container.
Sourcepub fn extract(&mut self, id: NodeId) -> Result<NodeId, EditError>
pub fn extract(&mut self, id: NodeId) -> Result<NodeId, EditError>
Detach an attached group child and return it as a detached root.
Sourcepub fn remove(&mut self, id: NodeId) -> Result<(), EditError>
pub fn remove(&mut self, id: NodeId) -> Result<(), EditError>
Remove an attached group child and its subtree.
Sourcepub fn replace_with(
&mut self,
target: NodeId,
replacement: NodeId,
) -> Result<(), EditError>
pub fn replace_with( &mut self, target: NodeId, replacement: NodeId, ) -> Result<(), EditError>
Replace target with a detached replacement.
Sourcepub fn clear(&mut self, container: NodeId) -> Result<(), EditError>
pub fn clear(&mut self, container: NodeId) -> Result<(), EditError>
Remove all direct children from a root/group container.
Sourcepub fn set_command_name(
&mut self,
id: NodeId,
name: impl Into<String>,
) -> Result<(), EditError>
pub fn set_command_name( &mut self, id: NodeId, name: impl Into<String>, ) -> Result<(), EditError>
Set the name of a command/infix/declarative/environment node.
Sourcepub fn set_text(
&mut self,
id: NodeId,
s: impl Into<String>,
) -> Result<(), EditError>
pub fn set_text( &mut self, id: NodeId, s: impl Into<String>, ) -> Result<(), EditError>
Set the payload of a text node.
Sourcepub fn set_char(&mut self, id: NodeId, c: char) -> Result<(), EditError>
pub fn set_char(&mut self, id: NodeId, c: char) -> Result<(), EditError>
Set the character of a char node.
Sourcepub fn set_arg(
&mut self,
id: NodeId,
index: usize,
value: ArgValue,
) -> Result<(), EditError>
pub fn set_arg( &mut self, id: NodeId, index: usize, value: ArgValue, ) -> Result<(), EditError>
Replace the value at argument slot index of a command-like node.
Sourcepub fn wrap(
&mut self,
target: NodeId,
wrapper: NodeId,
) -> Result<NodeId, EditError>
pub fn wrap( &mut self, target: NodeId, wrapper: NodeId, ) -> Result<NodeId, EditError>
Wrap a group-child target with a detached root/group wrapper.
Sourcepub fn unwrap(&mut self, group: NodeId) -> Result<Vec<NodeId>, EditError>
pub fn unwrap(&mut self, group: NodeId) -> Result<Vec<NodeId>, EditError>
Remove a group-child group and splice its children into the parent.
Sourcepub fn node_spans(&self) -> Vec<(String, Span)>
pub fn node_spans(&self) -> Vec<(String, Span)>
Export the parse-time span side table as (path, span) pairs.
Paths follow the parser’s tree-path scheme rooted at root:
.child.N for container children, .arg.N.content for content-carrying
argument slots, .left / .right for infix operands, .body for
environment bodies, and .base / .sub / .sup for script slots.
Nodes without a recorded span (e.g. created by edits, or any node of a
document built without parser spans) are omitted. Spans reflect the
original parse and are not updated by document edits.