z_mirror/luapls

git clone https://git.raiguard.me/z_mirror/luapls

Caleb HeuerGather named typesd109b04

main
986 B46 linesraw
1package ast
2
3import (
4	"reflect"
5
6	"github.com/raiguard/luapls/lua/token"
7)
8
9type Visitor func(node Node) bool
10
11// WalkSemantic performs a depth-first traversal of the AST nodes, calling the visitor for each node.
12// If the visitor returns false, this node's children are not traversed.
13func WalkSemantic(node Node, visitor Visitor) {
14	if node == nil || reflect.ValueOf(node).IsNil() {
15		return
16	}
17	if !visitor(node) {
18		return
19	}
20
21	for _, child := range node.GetSemanticChildren() {
22		WalkSemantic(child, visitor)
23	}
24}
25
26type NodePath struct {
27	Node    Node
28	Parents []Node
29}
30
31// GetSemanticNode returns the innermost node at the given position, and its parent nodes.
32func GetSemanticNode(base Node, pos token.Pos) NodePath {
33	var node Node
34	parents := []Node{}
35	WalkSemantic(base, func(n Node) bool {
36		if n.Pos() <= pos && pos < n.End() {
37			if node != nil {
38				parents = append(parents, node)
39			}
40			node = n
41			return true
42		}
43		return false
44	})
45	return NodePath{node, parents}
46}