Documentation ¶
Overview ¶
Package index provides an index tree structure to represent a document of text-base editor.
Index ¶
- Constants
- Variables
- func ToXML[V Value](node *Node[V]) string
- func Traverse[V Value](tree *Tree[V], callback func(node *Node[V], depth int))
- func TraverseNode[V Value](node *Node[V], callback func(node *Node[V], depth int))
- type Node
- func (n *Node[V]) Append(newNodes ...*Node[V]) error
- func (n *Node[V]) Child(index int) (*Node[V], error)
- func (n *Node[V]) Children(includeRemovedNode ...bool) []*Node[V]
- func (n *Node[V]) FindBranchOffset(node *Node[V]) (int, error)
- func (n *Node[V]) FindOffset(node *Node[V]) (int, error)
- func (n *Node[V]) HasTextChild() bool
- func (n *Node[V]) InsertAfter(newNode, referenceNode *Node[V]) error
- func (n *Node[V]) InsertAfterInternal(newNode, prevNode *Node[V]) error
- func (n *Node[V]) InsertAt(newNode *Node[V], offset int) error
- func (n *Node[V]) InsertBefore(newNode, referenceNode *Node[V]) error
- func (n *Node[V]) IsAncestorOf(node *Node[V]) bool
- func (n *Node[V]) IsText() bool
- func (n *Node[V]) Len() int
- func (n *Node[V]) OffsetOfChild(node *Node[V]) int
- func (n *Node[V]) PaddedLength() int
- func (n *Node[V]) Prepend(children ...*Node[V]) error
- func (n *Node[V]) RemoveChild(child *Node[V]) error
- func (n *Node[V]) SetChildren(children []*Node[V]) error
- func (n *Node[V]) UpdateAncestorsSize()
- type TokenType
- type Tree
- func (t *Tree[V]) FindCommonAncestor(nodeA, nodeB *Node[V]) V
- func (t *Tree[V]) FindLeftmost(node *Node[V]) V
- func (t *Tree[V]) FindPostorderRight(pos *TreePos[V]) (V, error)
- func (t *Tree[V]) FindTreePos(index int, preferTexts ...bool) (*TreePos[V], error)
- func (t *Tree[V]) GetAncestors(node *Node[V]) []*Node[V]
- func (t *Tree[V]) IndexOf(pos *TreePos[V]) (int, error)
- func (t *Tree[V]) LeftSiblingsSize(parent *Node[V], offset int) (int, error)
- func (t *Tree[V]) PathToIndex(path []int) (int, error)
- func (t *Tree[V]) PathToTreePos(path []int) (*TreePos[V], error)
- func (t *Tree[V]) Root() *Node[V]
- func (t *Tree[V]) TokensBetween(from int, to int, callback func(token TreeToken[V], ended bool)) error
- func (t *Tree[V]) TreePosToPath(treePos *TreePos[V]) ([]int, error)
- type TreePos
- type TreeToken
- type Value
Constants ¶
const ( // DefaultTextType is the type of default text node. // TODO(hackerwins): Allow users to define the type of text node. DefaultTextType = "text" )
Variables ¶
var ( // ErrInvalidMethodCallForTextNode is returned when a invalid method is called for text node. ErrInvalidMethodCallForTextNode = errors.New("text node cannot have children") // ErrChildNotFound is returned when a child is not found. ErrChildNotFound = errors.New("child not found") // ErrUnreachablePath is returned when a path is unreachable. ErrUnreachablePath = errors.New("unreachable path") // ErrInvalidTreePos is returned when a TreePos is invalid. ErrInvalidTreePos = errors.New("invalid tree pos") )
Functions ¶
Types ¶
type Node ¶
type Node[V Value] struct { Type string Parent *Node[V] Value V Length int // contains filtered or unexported fields }
Node is a node of Tree.
func (*Node[V]) FindBranchOffset ¶
FindBranchOffset returns offset of the given descendant node in this node. If the given node is not a descendant of this node, it returns -1.
func (*Node[V]) FindOffset ¶ added in v0.4.6
FindOffset returns the offset of the given node in the children.
func (*Node[V]) HasTextChild ¶ added in v0.4.9
HasTextChild returns true if the node's children consist of only text children.
func (*Node[V]) InsertAfter ¶
InsertAfter inserts the given node after the given child.
func (*Node[V]) InsertAfterInternal ¶
InsertAfterInternal inserts the given node after the given child. This method does not update the size of the ancestors.
func (*Node[V]) InsertBefore ¶
InsertBefore inserts the given node before the given child.
func (*Node[V]) IsAncestorOf ¶
IsAncestorOf returns true if the node is an ancestor of the given node.
func (*Node[V]) OffsetOfChild ¶
OffsetOfChild returns offset of children of the given node.
func (*Node[V]) PaddedLength ¶
PaddedLength returns the length of the node with padding.
func (*Node[V]) RemoveChild ¶ added in v0.4.3
RemoveChild removes the given child.
func (*Node[V]) SetChildren ¶
SetChildren sets the children of the given node. This method does not update the size of the ancestors.
func (*Node[V]) UpdateAncestorsSize ¶
func (n *Node[V]) UpdateAncestorsSize()
UpdateAncestorsSize updates the size of ancestors.
type TokenType ¶ added in v0.4.12
type TokenType int
TokenType represents the type of the selected token.
type Tree ¶
type Tree[V Value] struct { // contains filtered or unexported fields }
Tree is a tree implementation to represent a document of text-based editors.
func (*Tree[V]) FindCommonAncestor ¶
FindCommonAncestor finds the lowest common ancestor of the given nodes.
func (*Tree[V]) FindLeftmost ¶
FindLeftmost finds the leftmost node of the given tree.
func (*Tree[V]) FindPostorderRight ¶
FindPostorderRight finds right node of the given tree position with postorder traversal.
func (*Tree[V]) FindTreePos ¶
FindTreePos finds the position of the given index in the tree.
func (*Tree[V]) GetAncestors ¶
GetAncestors returns the ancestors of the given node.
func (*Tree[V]) LeftSiblingsSize ¶ added in v0.4.6
LeftSiblingsSize returns the size of left siblings of the given node
func (*Tree[V]) PathToIndex ¶ added in v0.4.6
PathToIndex converts the given path to index.
func (*Tree[V]) PathToTreePos ¶
PathToTreePos returns treePos from given path