index

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package index provides an index tree structure to represent a document of text-base editor.

Index

Constants

View Source
const (
	// DefaultTextType is the type of default text node.
	// TODO(hackerwins): Allow users to define the type of text node.
	DefaultTextType = "text"
)

Variables

This section is empty.

Functions

func ToXML

func ToXML[V Value](node *Node[V]) string

ToXML returns the XML representation of this tree.

func Traverse

func Traverse[V Value](tree *Tree[V], callback func(node *Node[V], depth int))

Traverse traverses the tree with postorder traversal.

func TraverseAll added in v0.4.3

func TraverseAll[V Value](tree *Tree[V], callback func(node *Node[V], depth int))

TraverseAll traverses the whole tree (include tombstones) with postorder traversal.

func TraverseNode

func TraverseNode[V Value](node *Node[V], callback func(node *Node[V], depth int))

TraverseNode traverses the tree with the given callback.

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 NewNode

func NewNode[V Value](nodeType string, value V, children ...*Node[V]) *Node[V]

NewNode creates a new instance of Node.

func (*Node[V]) Append

func (n *Node[V]) Append(newNodes ...*Node[V])

Append appends the given node to the end of the children.

func (*Node[V]) Child

func (n *Node[V]) Child(index int) *Node[V]

Child returns the child of the given index.

func (*Node[V]) Children

func (n *Node[V]) Children(includeRemovedNode ...bool) []*Node[V]

Children returns the children of the given node.

func (*Node[V]) FindBranchOffset

func (n *Node[V]) FindBranchOffset(node *Node[V]) int

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]) InsertAfter

func (n *Node[V]) InsertAfter(newNode, referenceNode *Node[V])

InsertAfter inserts the given node after the given child.

func (*Node[V]) InsertAfterInternal

func (n *Node[V]) InsertAfterInternal(newNode, prevNode *Node[V])

InsertAfterInternal inserts the given node after the given child. This method does not update the size of the ancestors.

func (*Node[V]) InsertAt

func (n *Node[V]) InsertAt(newNode *Node[V], offset int)

InsertAt inserts the given node at the given offset.

func (*Node[V]) InsertBefore

func (n *Node[V]) InsertBefore(newNode, referenceNode *Node[V])

InsertBefore inserts the given node before the given child.

func (*Node[V]) IsAncestorOf

func (n *Node[V]) IsAncestorOf(node *Node[V]) bool

IsAncestorOf returns true if the node is an ancestor of the given node.

func (*Node[V]) IsText

func (n *Node[V]) IsText() bool

IsText returns whether the Node is text or not.

func (*Node[V]) Len

func (n *Node[V]) Len() int

Len returns the length of the Node.

func (*Node[V]) OffsetOfChild

func (n *Node[V]) OffsetOfChild(node *Node[V]) int

OffsetOfChild returns offset of children of the given node.

func (*Node[V]) PaddedLength

func (n *Node[V]) PaddedLength() int

PaddedLength returns the length of the node with padding.

func (*Node[V]) Prepend

func (n *Node[V]) Prepend(children ...*Node[V])

Prepend prepends the given nodes to the children.

func (*Node[V]) RemoveChild added in v0.4.3

func (n *Node[V]) RemoveChild(child *Node[V])

RemoveChild removes the given child.

func (*Node[V]) SetChildren

func (n *Node[V]) SetChildren(children []*Node[V])

SetChildren sets the children of the given node.

func (*Node[V]) UpdateAncestorsSize

func (n *Node[V]) UpdateAncestorsSize()

UpdateAncestorsSize updates the size of ancestors.

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 NewTree

func NewTree[V Value](root *Node[V]) *Tree[V]

NewTree creates a new instance of Tree.

func (*Tree[V]) FindCommonAncestor

func (t *Tree[V]) FindCommonAncestor(nodeA, nodeB *Node[V]) V

FindCommonAncestor finds the lowest common ancestor of the given nodes.

func (*Tree[V]) FindLeftmost

func (t *Tree[V]) FindLeftmost(node *Node[V]) V

FindLeftmost finds the leftmost node of the given tree.

func (*Tree[V]) FindPostorderRight

func (t *Tree[V]) FindPostorderRight(pos *TreePos[V]) V

FindPostorderRight finds right node of the given tree position with postorder traversal.

func (*Tree[V]) FindTreePos

func (t *Tree[V]) FindTreePos(index int, preferTexts ...bool) *TreePos[V]

FindTreePos finds the position of the given index in the tree.

func (*Tree[V]) GetAncestors

func (t *Tree[V]) GetAncestors(node *Node[V]) []*Node[V]

GetAncestors returns the ancestors of the given node.

func (*Tree[V]) IndexOf

func (t *Tree[V]) IndexOf(node *Node[V]) int

IndexOf returns the index of the given node.

func (*Tree[V]) NodesBetween

func (t *Tree[V]) NodesBetween(from int, to int, callback func(node V))

NodesBetween returns the nodes between the given range.

func (*Tree[V]) PathToTreePos

func (t *Tree[V]) PathToTreePos(path []int) *TreePos[V]

PathToTreePos returns treePos from given path

func (*Tree[V]) Root

func (t *Tree[V]) Root() *Node[V]

Root returns the root node of the tree.

func (*Tree[V]) TreePosToPath

func (t *Tree[V]) TreePosToPath(treePos *TreePos[V]) []int

TreePosToPath returns path from given treePos

type TreePos

type TreePos[V Value] struct {
	Node   *Node[V]
	Offset int
}

TreePos is the position of a node in the tree.

type Value

type Value interface {
	IsRemoved() bool
	Length() int
	String() string
	Attributes() string
}

Value represents the data stored in the nodes of Tree.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL