psi

package
v0.0.0-...-7b96089 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EdgeKindChild = EdgeKind("child")
View Source
var ErrAbort = errors.New("abort")
View Source
var ErrNodeNotFound = errors.New("node not found")

Functions

func ResolveEdge

func ResolveEdge[T Node](parent Node, key TypedEdgeKey[T]) (def T)

func Walk

func Walk(node Node, walkFn WalkFunc) error

Walk traverses a PSI Tree in depth-first order.

Types

type BaseGraph

type BaseGraph struct {
	// contains filtered or unexported fields
}

func (*BaseGraph) Add

func (g *BaseGraph) Add(n Node)

func (*BaseGraph) Edges

func (g *BaseGraph) Edges() EdgeIterator

func (*BaseGraph) Init

func (g *BaseGraph) Init(self Graph)

func (*BaseGraph) NextEdgeID

func (g *BaseGraph) NextEdgeID() EdgeID

func (*BaseGraph) NextNodeID

func (g *BaseGraph) NextNodeID() int64

func (*BaseGraph) Nodes

func (g *BaseGraph) Nodes() NodeIterator

func (*BaseGraph) OnNodeInvalidated

func (g *BaseGraph) OnNodeInvalidated(n Node)

func (*BaseGraph) OnNodeUpdated

func (g *BaseGraph) OnNodeUpdated(n Node)

func (*BaseGraph) Remove

func (g *BaseGraph) Remove(n Node)

func (*BaseGraph) Replace

func (g *BaseGraph) Replace(old, new Node)

func (*BaseGraph) SetEdge

func (g *BaseGraph) SetEdge(e Edge)

func (*BaseGraph) UnsetEdge

func (g *BaseGraph) UnsetEdge(self Edge)

type Cursor

type Cursor interface {
	Depth() int

	// Current returns the current node.
	Node() Node
	// SetCurrent sets the current node.
	SetCurrent(node Node)
	SetNext(node Node)

	// WalkChildren walks the children of the current node.
	WalkChildren()
	// SkipChildren skips the children of the current node.
	SkipChildren()

	// WalkEdges walks the edges of the current node.
	WalkEdges()
	// SkipEdges skips the edges of the current node.
	SkipEdges()

	// Replace replaces the current node with the given node, modifying the AST.
	// If this operation happens during the enter phase, the children of the new node will be visited.
	// If this operation happens during the leave phase, the children of the new node will NOT be visited.
	Replace(node Node)

	// InsertBefore inserts the given node before the current node, modifying the AST.
	// This node will NOT be visited in the current walk.
	InsertBefore(node Node)

	// InsertAfter inserts the given node before the current node, modifying the AST.
	// This node might be visited in the current walk.
	InsertAfter(node Node)
}

Cursor is a stateful tree traversal interface.

func NewCursor

func NewCursor() Cursor

type Edge

type Edge interface {
	ID() EdgeID
	Key() EdgeReference
	Kind() EdgeKind
	From() Node
	To() Node

	ReplaceTo(node Node) Edge
	ReplaceFrom(node Node) Edge
	// contains filtered or unexported methods
}

type EdgeBase

type EdgeBase struct {
	// contains filtered or unexported fields
}

func NewEdgeBase

func NewEdgeBase(key EdgeReference, from Node, to Node) *EdgeBase

func (*EdgeBase) From

func (e *EdgeBase) From() Node

func (*EdgeBase) ID

func (e *EdgeBase) ID() EdgeID

func (*EdgeBase) Key

func (e *EdgeBase) Key() EdgeReference

func (*EdgeBase) Kind

func (e *EdgeBase) Kind() EdgeKind

func (*EdgeBase) ReplaceFrom

func (e *EdgeBase) ReplaceFrom(node Node) Edge

func (*EdgeBase) ReplaceTo

func (e *EdgeBase) ReplaceTo(node Node) Edge

func (*EdgeBase) String

func (e *EdgeBase) String() string

func (*EdgeBase) To

func (e *EdgeBase) To() Node

type EdgeID

type EdgeID int64

type EdgeIterator

type EdgeIterator interface {
	Next() bool
	Edge() Edge
}

type EdgeKey

type EdgeKey struct {
	Kind  EdgeKind `json:"Kind"`
	Name  string   `json:"Name"`
	Index int64    `json:"Index"`
}

func (EdgeKey) GetIndex

func (k EdgeKey) GetIndex() int64

func (EdgeKey) GetKey

func (k EdgeKey) GetKey() EdgeKey

func (EdgeKey) GetKind

func (k EdgeKey) GetKind() EdgeKind

func (EdgeKey) GetName

func (k EdgeKey) GetName() string

func (EdgeKey) String

func (k EdgeKey) String() string

type EdgeKind

type EdgeKind string

func (EdgeKind) String

func (k EdgeKind) String() string

type EdgeReference

type EdgeReference interface {
	GetKind() EdgeKind
	GetName() string
	GetIndex() int64
	GetKey() EdgeKey
}

type Freezer

type Freezer struct {
	Cas   map[cid.Cid][]byte  `json:"Cas"`
	Cache map[cid.Cid]RawNode `json:"-"`
	IdMap map[NodeID]cid.Cid  `json:"IdMap"`
}

func (*Freezer) Add

func (f *Freezer) Add(n Node) (entry RawNodeEntry)

func (*Freezer) Get

func (f *Freezer) Get(id cid.Cid) (RawNode, bool)

func (*Freezer) GetByID

func (f *Freezer) GetByID(id NodeID) (RawNode, bool)

type Graph

type Graph interface {
	Add(n Node)
	Remove(n Node)
	Replace(old, new Node)

	NextNodeID() int64
	NextEdgeID() EdgeID

	Nodes() NodeIterator
	Edges() EdgeIterator

	SetEdge(e Edge)
	UnsetEdge(self Edge)

	OnNodeUpdated(n Node)
	OnNodeInvalidated(n Node)
}

type InvalidationListener

type InvalidationListener interface {
	OnInvalidated(n Node)
}

func InvalidationListenerFunc

func InvalidationListenerFunc(f func(n Node)) InvalidationListener

type Language

type Language interface {
	Name() LanguageID
	Extensions() []string

	CreateSourceFile(fileName string, fileHandle repofs.FileHandle) SourceFile

	Parse(fileName string, code string) (SourceFile, error)
	ParseCodeBlock(name string, block mdutils.CodeBlock) (SourceFile, error)
}

type LanguageID

type LanguageID string

type NamedNode

type NamedNode interface {
	Node

	PsiNodeName() string
}

type Node

type Node interface {
	NodeLike

	ID() int64
	UUID() NodeID
	CanonicalPath() Path

	Parent() Node
	PreviousSibling() Node
	NextSibling() Node

	// SetParent sets the parent node of the current node.
	// If the parent node is already set to the given parent, no action is taken.
	// If the current node has a parent, it is first removed from its parent node.
	// Then, the parent node is set to the given parent.
	// If the parent node is not nil, the current node is added as a child to the parent node.
	// If the parent node is nil, the current node is detached from the graph.
	SetParent(parent Node)

	Children() []Node
	ChildrenList() collectionsfx.ObservableList[Node]
	ChildrenIterator() NodeIterator

	Comments() []string

	IsContainer() bool
	IsLeaf() bool

	ResolveChild(component PathElement) Node

	// Edges returns the edges of the current node.
	Edges() EdgeIterator
	// SetEdge sets the edge with the given key to the given node.
	SetEdge(key EdgeReference, to Node)
	// UnsetEdge removes the edge with the given key.
	UnsetEdge(key EdgeReference)
	// GetEdge returns the edge with the given key.
	GetEdge(key EdgeReference) Edge

	// Attributes returns the attributes of the current node.
	Attributes() map[string]interface{}
	// SetAttribute sets the attribute with the given key to the given value.
	SetAttribute(key string, value any)
	// GetAttribute returns the attribute with the given key.
	GetAttribute(key string) (any, bool)
	// RemoveAttribute removes the attribute with the given key.
	RemoveAttribute(key string) (any, bool)

	IsValid() bool
	Invalidate()
	Update(context.Context) error

	AddChildNode(node Node)
	RemoveChildNode(node Node)
	ReplaceChildNode(old Node, node Node)
	InsertChildrenAt(idx int, child Node)
	InsertChildBefore(anchor Node, node Node)
	InsertChildAfter(anchor Node, node Node)

	String() string
	// contains filtered or unexported methods
}

Node represents a PSI element in the graph.

func ResolvePath

func ResolvePath(root Node, path Path) (Node, error)

func Rewrite

func Rewrite(node Node, walkFunc WalkFunc) (Node, error)

Rewrite traverses a PSI Tree in depth-first order and rewrites it.

type NodeBase

type NodeBase struct {
	// contains filtered or unexported fields
}

func (*NodeBase) AddChildNode

func (n *NodeBase) AddChildNode(child Node)

AddChildNode adds a child node to the current node. If the child node is already a child of the current node, no action is taken. The child node is appended to the list of children nodes of the current node. Then, the child node is attached to the same graph as the parent node.

Parameters: - child: The child node to be added.

func (*NodeBase) Attributes

func (n *NodeBase) Attributes() map[string]interface{}

func (*NodeBase) CanonicalPath

func (n *NodeBase) CanonicalPath() (res Path)

func (*NodeBase) Children

func (n *NodeBase) Children() []Node

func (*NodeBase) ChildrenIterator

func (n *NodeBase) ChildrenIterator() NodeIterator

func (*NodeBase) ChildrenList

func (n *NodeBase) ChildrenList() collectionsfx.ObservableList[Node]

func (*NodeBase) Comments

func (n *NodeBase) Comments() []string

func (*NodeBase) Edges

func (n *NodeBase) Edges() EdgeIterator

func (*NodeBase) GetAttribute

func (n *NodeBase) GetAttribute(key string) (value any, ok bool)

func (*NodeBase) GetEdge

func (n *NodeBase) GetEdge(key EdgeReference) Edge

func (*NodeBase) ID

func (n *NodeBase) ID() int64

func (*NodeBase) IndexOfChild

func (n *NodeBase) IndexOfChild(node Node) int

func (*NodeBase) Init

func (n *NodeBase) Init(self Node, uid string)

Init initializes the NodeBase struct with the given self node and uid string. It sets the self node, uuid, and initializes the edges map. If the uuid is an empty string, it generates a new UUID using the github.com/google/uuid package.

Parameters: - self: The self node to be set. - uid: The UUID string to be set.

func (*NodeBase) InsertChildAfter

func (n *NodeBase) InsertChildAfter(anchor, node Node)

func (*NodeBase) InsertChildBefore

func (n *NodeBase) InsertChildBefore(anchor, node Node)

func (*NodeBase) InsertChildrenAt

func (n *NodeBase) InsertChildrenAt(idx int, child Node)

func (*NodeBase) Invalidate

func (n *NodeBase) Invalidate()

func (*NodeBase) InvalidateTree

func (n *NodeBase) InvalidateTree()

func (*NodeBase) IsContainer

func (n *NodeBase) IsContainer() bool

func (*NodeBase) IsLeaf

func (n *NodeBase) IsLeaf() bool

func (*NodeBase) IsValid

func (n *NodeBase) IsValid() bool

func (*NodeBase) NextSibling

func (n *NodeBase) NextSibling() Node

func (*NodeBase) Parent

func (n *NodeBase) Parent() Node

func (*NodeBase) ParentProperty

func (n *NodeBase) ParentProperty() obsfx.ObservableValue[Node]

func (*NodeBase) PreviousSibling

func (n *NodeBase) PreviousSibling() Node

func (*NodeBase) PsiNode

func (n *NodeBase) PsiNode() Node

func (*NodeBase) PsiNodeBase

func (n *NodeBase) PsiNodeBase() *NodeBase

func (*NodeBase) PsiNodeType

func (n *NodeBase) PsiNodeType() NodeType

func (*NodeBase) PsiNodeVersion

func (n *NodeBase) PsiNodeVersion() int64

func (*NodeBase) RemoveAttribute

func (n *NodeBase) RemoveAttribute(key string) (value any, ok bool)

func (*NodeBase) RemoveChildNode

func (n *NodeBase) RemoveChildNode(child Node)

RemoveChildNode removes the child node from the current node. If the child node is not a child of the current node, no action is taken.

Parameters: - child: The child node to be removed.

func (*NodeBase) ReplaceChildNode

func (n *NodeBase) ReplaceChildNode(old, new Node)

ReplaceChildNode replaces an old child node with a new child node in the current node. If the old child node is not a child of the current node, no action is taken. The old child node is first removed from its parent node and detached from the graph. Then, the new child node is set as the replacement at the same index in the list of children nodes of the current node. The new child node is attached to the same graph as the parent node. Finally, any edges in the current node that reference the old child node as the destination node are updated to reference the new child node.

Parameters: - old: The old child node to be replaced. - new: The new child node to replace the old child node.

func (*NodeBase) ResolveChild

func (n *NodeBase) ResolveChild(component PathElement) Node

func (*NodeBase) SetAttribute

func (n *NodeBase) SetAttribute(key string, value any)

func (*NodeBase) SetEdge

func (n *NodeBase) SetEdge(key EdgeReference, to Node)

func (*NodeBase) SetParent

func (n *NodeBase) SetParent(parent Node)

func (*NodeBase) String

func (n *NodeBase) String() string

func (*NodeBase) UUID

func (n *NodeBase) UUID() string

func (*NodeBase) UnsetEdge

func (n *NodeBase) UnsetEdge(key EdgeReference)

func (*NodeBase) Update

func (n *NodeBase) Update(ctx context.Context) error

type NodeClass

type NodeClass string
const (
	NodeClassInvalid  NodeClass = ""
	NodeClassGeneric  NodeClass = "generic"
	NodeClassCode     NodeClass = "code"
	NodeClassDocument NodeClass = "document"
)

type NodeID

type NodeID = string

type NodeIterator

type NodeIterator interface {
	Value() Node
	Node() Node
	Next() bool
}

func AppendNodeIterator

func AppendNodeIterator(iterators ...NodeIterator) NodeIterator

type NodeLike

type NodeLike interface {
	PsiNode() Node
	PsiNodeType() NodeType
	PsiNodeBase() *NodeBase
	PsiNodeVersion() int64
}

type NodeLikeBase

type NodeLikeBase struct {
	NodeBase NodeBase
}

func (*NodeLikeBase) PsiNode

func (n *NodeLikeBase) PsiNode() Node

func (*NodeLikeBase) PsiNodeBase

func (n *NodeLikeBase) PsiNodeBase() *NodeBase

func (*NodeLikeBase) PsiNodeType

func (n *NodeLikeBase) PsiNodeType() NodeType

func (*NodeLikeBase) PsiNodeVersion

func (n *NodeLikeBase) PsiNodeVersion() int64

type NodeType

type NodeType interface {
	Name() string
	Class() NodeClass
	RuntimeType() reflect.Type
}

type NodeTypeOption

type NodeTypeOption func(*nodeType)

func WithNodeClass

func WithNodeClass(class NodeClass) NodeTypeOption

type Path

type Path struct {
	// contains filtered or unexported fields
}

func MustParsePath

func MustParsePath(path string) Path

func ParsePath

func ParsePath(path string) (Path, error)

func PathFromComponents

func PathFromComponents(components ...PathElement) Path

func ResolveChild

func ResolveChild(parent Path, child PathElement) Path

func (Path) Child

func (p Path) Child(name PathElement) (res Path)

func (Path) Components

func (p Path) Components() []PathElement

func (Path) IsEmpty

func (p Path) IsEmpty() bool

func (Path) Join

func (p Path) Join(other Path) (res Path)

func (Path) MarshalJSON

func (p Path) MarshalJSON() ([]byte, error)

func (Path) Parent

func (p Path) Parent() Path

func (Path) Root

func (p Path) Root() Node

func (Path) String

func (p Path) String() (res string)

func (*Path) UnmarshalJSON

func (p *Path) UnmarshalJSON(data []byte) error

func (Path) WithRoot

func (p Path) WithRoot(root Node) Path

type PathElement

type PathElement struct {
	Kind  EdgeKind
	Name  string
	Index int64
}

func ParsePathComponent

func ParsePathComponent(str string) (e PathElement, err error)

func (PathElement) IsEmpty

func (p PathElement) IsEmpty() bool

func (PathElement) String

func (p PathElement) String() string

type PhysAddr

type PhysAddr = cid.Cid

type RawAttribute

type RawAttribute struct {
	Key   string `json:"key"`
	Value any    `json:"value"`
}

type RawEdge

type RawEdge struct {
	Key EdgeKey `json:"key"`
	To  Path    `json:"to"`
}

type RawNode

type RawNode struct {
	ID         int64          `json:"ID"`
	UUID       string         `json:"UUID"`
	Attributes []RawAttribute `json:"Attributes"`
	Edges      []RawEdge      `json:"Edges"`
	Children   []RawNodeEntry `json:"Children"`
}

type RawNodeEntry

type RawNodeEntry struct {
	UUID     string   `json:"UUID,omitempty"`
	PhysAddr PhysAddr `json:"PhysAddr,omitempty"`
	Node     Node     `json:"Node,omitempty"`
}

type RawPointer

type RawPointer struct {
	Depth   int      `json:"Depth"`
	Index   int      `json:"Index"`
	Parent  PhysAddr `json:"Parent"`
	Thought PhysAddr `json:"Thought"`
}

type Scope

type Scope interface {
	Root() Node
}

type SourceFile

type SourceFile interface {
	Node

	Name() string
	Language() Language

	Root() Node
	Error() error

	Load() error
	Replace(code string) error

	OriginalText() string
	ToCode(node Node) (mdutils.CodeBlock, error)

	MergeCompletionResults(ctx context.Context, scope Scope, cursor Cursor, newSource SourceFile, newAst Node) error
}

type TypedEdgeKey

type TypedEdgeKey[T Node] struct {
	Kind  TypedEdgeKind[T] `json:"Kind"`
	Name  string           `json:"Name"`
	Index int64            `json:"Index"`
}

func (TypedEdgeKey[T]) GetIndex

func (k TypedEdgeKey[T]) GetIndex() int64

func (TypedEdgeKey[T]) GetKey

func (k TypedEdgeKey[T]) GetKey() EdgeKey

func (TypedEdgeKey[T]) GetKind

func (k TypedEdgeKey[T]) GetKind() EdgeKind

func (TypedEdgeKey[T]) GetName

func (k TypedEdgeKey[T]) GetName() string

func (TypedEdgeKey[T]) String

func (k TypedEdgeKey[T]) String() string

type TypedEdgeKind

type TypedEdgeKind[T Node] EdgeKind

func (TypedEdgeKind[T]) Singleton

func (f TypedEdgeKind[T]) Singleton() TypedEdgeKey[T]

type TypedNodeType

type TypedNodeType[T Node] interface {
	NodeType
}

func RegisterNodeType

func RegisterNodeType[T Node](name string, options ...NodeTypeOption) TypedNodeType[T]

type WalkFunc

type WalkFunc func(cursor Cursor, entering bool) error

WalkFunc is the type of the function called for each node visited by Walk.

Directories

Path Synopsis
langs

Jump to

Keyboard shortcuts

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