z_mirror/luapls
git clone https://git.raiguard.me/z_mirror/luapls
d109b04
main
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 ) { 14if node == nil || reflect .ValueOf (node ).IsNil () { 15return 16 } 17if ! visitor (node ) { 18return 19 } 20 21for _ ,child := range node .GetSemanticChildren () { 22WalkSemantic (child ,visitor ) 23 } 24} 25 26type NodePath struct { 27Node Node 28Parents []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 { 33var node Node 34parents := []Node {} 35WalkSemantic (base ,func (n Node )bool { 36if n .Pos ()<= pos && pos < n .End () { 37if node != nil { 38parents = append (parents ,node ) 39 } 40node = n 41return true 42 } 43return false 44 }) 45return NodePath {node ,parents } 46}