1// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers 2// 3// SPDX-License-Identifier: MPL-2.0 4 5import Foundation 6import LanguageServerProtocol 7 8extension SyntaxTree { 9 public final class Cursor { 10 public let lineMap: LineMap 11 public let node: SyntaxTree.Child 12 public let utf16Offset: Int 13 14 public private(set) lazy var children: [Cursor] = { 15 var children: [Cursor] = [] 16 var utf16Offset = utf16Offset 17 for childNode in node.children { 18 children.append(Self(lineMap: lineMap, node: childNode, utf16Offset: utf16Offset)) 19 utf16Offset += childNode.utf16Length 20 } 21 22 return children 23 }() 24 25 public var utf16Range: Range<Int> { 26 utf16Offset..<utf16Offset + node.utf16Length 27 } 28 29 init(lineMap: LineMap, node: SyntaxTree.Child, utf16Offset: Int) { 30 self.lineMap = lineMap 31 self.node = node 32 self.utf16Offset = utf16Offset 33 } 34 } 35} 36 37extension SyntaxTree.Cursor { 38 public func firstChild<T>(mapping: (SyntaxTree.Cursor) -> T?) -> T? { 39 for child in children { 40 if let result = mapping(child) { 41 return result 42 } else { 43 continue 44 } 45 } 46 return nil 47 } 48 49 public func children<T>(mapping: (SyntaxTree.Cursor) -> T?) -> [T] { 50 children.compactMap(mapping) 51 } 52} 53