CodeNode class

A block of source code.

Its children are ordinary text nodes, CodeHighlightNodes, and line breaks. Note that a code block authored by appending a multi-line text node keeps the newlines inside that node — Lexical preserves them, so this port does too, and a renderer must handle them rather than assuming every break is a LineBreakNode.

Inheritance
Available extensions

Constructors

CodeNode([String? _language])
Creates a code block for language.

Properties

canBeEmpty bool
Whether this element may exist with no children.
no setterinherited
canIndent bool
Whether this element accepts indentation.
no setterinherited
canInsertTextAfter bool
Whether text may be inserted immediately after this element.
no setterinherited
canInsertTextBefore bool
Whether text may be inserted immediately before this element.
no setterinherited
children Iterable<LexicalNode>
The children in document order, walked lazily through the pointers.
no setterinherited
childrenSize int
Number of children.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
indexWithinParent int
This node's position among its parent's children.
no setterinherited
isAttached bool
Whether a path exists from this node to the root.
no setterinherited
isDirty bool
Whether this node was modified during the current update.
no setterinherited
isEmpty bool
Whether this element has no children.
no setterinherited
isInline bool
Whether this node participates in inline text flow.
no setterinherited
isShadowRoot bool
Whether this element behaves as a root for containment purposes.
no setterinherited
key NodeKey
This node's identity within the editor state.
no setterinherited
language String?
The language identifier, or null when unset.
no setter
nodeState NodeState
Extension data for this node, created on first access.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
type String
The wire-format type string of this node.
no setteroverride

Methods

afterCloneFrom(covariant CodeNode prev) → void
Copies mutable state that the constructor does not carry.
override
append(LexicalNode node) ElementNode
Appends node as the last child of this element.
inherited
appendAll(Iterable<LexicalNode> nodes) ElementNode
Appends several nodes in one operation.
inherited
clear() ElementNode
Removes every child.
inherited
clone() CodeNode
Creates a new instance of this node's class carrying its constructor state.
override
collapseAtStart() bool
Handles backspace at the very start of this element, with nothing before it to delete.
inherited
exportJson() Map<String, Object?>
Serializes this node to its wire-format JSON object.
override
getAllTextNodes() List<LexicalNode>
The descendant text and inline nodes of this element, in order.
inherited
getChildAtIndex(int index) LexicalNode?
The child at index, or null when out of range.
inherited
getCommonAncestor(LexicalNode other) ElementNode?
The closest element that contains both this node and other.
inherited
getDescendantByIndex(int index) LexicalNode?
The leaf at child index, reaching into nested elements.
inherited
getDirection() NodeDirection?
Resolved writing direction, or null when undetermined.
inherited
getFirstChild() LexicalNode?
The first child, or null when empty.
inherited
getFirstDescendant() LexicalNode?
The first leaf under this element, or null when it holds nothing.
inherited
getFormat() ElementFormat
Block alignment.
inherited
getIndent() int
Nesting level.
inherited
getLastChild() LexicalNode?
The last child, or null when empty.
inherited
getLastDescendant() LexicalNode?
The last leaf under this element, or null when it holds nothing.
inherited
getLatest<T extends LexicalNode>() → T
Resolves this node's current version from the active editor state.
inherited
getNextSibling() LexicalNode?
The next sibling, or null when this is the last child.
inherited
getNextSiblings() List<LexicalNode>
All siblings after this node, in document order.
inherited
getNodesBetween(LexicalNode target) List<LexicalNode>
Every node between this one and target, inclusive, in document order.
inherited
getParent() ElementNode?
This node's parent element, or null when detached.
inherited
getParentOrThrow() ElementNode
This node's parent element, or throws when detached.
inherited
getParents() List<ElementNode>
The ancestors of this node, closest first.
inherited
getPreviousSibling() LexicalNode?
The previous sibling, or null when this is the first child.
inherited
getPreviousSiblings() List<LexicalNode>
All siblings before this node, in document order.
inherited
getTextContent() String
The plain-text projection of this node.
inherited
getTextContentSize() int
Length of getTextContent in UTF-16 code units.
inherited
getTextFormat() int
The caret's pending text format for this block.
inherited
getTextStyle() String
The caret's pending text style for this block.
inherited
getTopLevelElement() LexicalNode?
The top-level block containing this node, or null for the root.
inherited
getWritable<T extends LexicalNode>() → T
Returns a mutable version of this node, cloning it if necessary.
inherited
insertAfter<T extends LexicalNode>(T nodeToInsert) → T
Inserts nodeToInsert directly after this node.
inherited
insertBefore<T extends LexicalNode>(T nodeToInsert) → T
Inserts nodeToInsert directly before this node.
inherited
insertChildAtStart(LexicalNode node) ElementNode
Inserts node before every existing child.
inherited
insertNewAfter({required bool isAtEnd}) ElementNode
Creates and inserts the element that follows this one when the user splits it — the block Enter produces.
inherited
isBefore(LexicalNode target) bool
Whether this node precedes target in document order.
inherited
isParentOf(LexicalNode target) bool
Whether this node is an ancestor of target.
inherited
isSameNode(LexicalNode? other) bool
Whether other refers to the same node, ignoring version.
inherited
markDirty() → void
Marks this node dirty so the next commit reconciles it.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patchStyle(StylePatch patch) → void

Available on ElementNode, provided by the ElementNodeStylePatch extension

Applies patch to this element's ElementNode.getTextStyle.
remove({bool preserveEmptyParent = false}) → void
Detaches this node from its parent.
inherited
replace<T extends LexicalNode>(T replaceWith, {bool includeChildren = false}) → T
Replaces this node with replaceWith, optionally moving children over.
inherited
select([int? anchorOffset, int? focusOffset]) RangeSelection

Available on ElementNode, provided by the ElementNodeSelection extension

Selects a range of this element's children, by index.
selectEnd() RangeSelection

Available on ElementNode, provided by the ElementNodeSelection extension

Places a collapsed selection at the end of this element's content.
selectNext() → void

Available on LexicalNode, provided by the LexicalNodeSelection extension

Places a collapsed selection immediately after this node.
selectPrevious() → void

Available on LexicalNode, provided by the LexicalNodeSelection extension

Places a collapsed selection immediately before this node.
selectStart() RangeSelection

Available on ElementNode, provided by the ElementNodeSelection extension

Places a collapsed selection at the start of this element's content.
setDirection(NodeDirection? value) ElementNode
Sets the writing direction.
inherited
setFormat(ElementFormat value) ElementNode
Sets block alignment.
inherited
setIndent(int value) ElementNode
Sets the nesting level.
inherited
setLanguage(String? value) CodeNode
Sets the language identifier.
setTextFormat(int value) ElementNode
Sets the caret's pending text format.
inherited
setTextStyle(String value) ElementNode
Sets the caret's pending text style.
inherited
splice(int start, int deleteCount, List<LexicalNode> nodesToInsert) ElementNode
Replaces deleteCount children starting at start with nodesToInsert.
inherited
toString() String
A string representation of this object.
inherited
updateFromJson(Map<String, Object?> json) → void
Applies a serialized node's fields to this instance.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited