this repo has no description
www.jonmsterling.com/01HC/
1// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers
2//
3// SPDX-License-Identifier: MPL-2.0
4
5import Foundation
6import LanguageServerProtocol
7
8/// This is a “red tree” in the sense of Roslyn. In essence it instruments syntax trees with non-relative location information.
9public final class SyntaxCursor {
10 public let lineMap: LineMap
11 public let node: SyntaxTree.Child
12 public let utf16Offset: Int
13
14 public private(set) lazy var children: [SyntaxCursor] = {
15 var children: [SyntaxCursor] = []
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 public init(lineMap: LineMap, node: SyntaxTree.Child, utf16Offset: Int) {
30 self.lineMap = lineMap
31 self.node = node
32 self.utf16Offset = utf16Offset
33 }
34}
35
36extension SyntaxCursor {
37 public func firstChild<T>(mapping: (SyntaxCursor) -> T?) -> T? {
38 for child in children {
39 if let result = mapping(child) {
40 return result
41 } else {
42 continue
43 }
44 }
45 return nil
46 }
47
48 public func children<T>(mapping: (SyntaxCursor) -> T?) -> [T] {
49 children.compactMap(mapping)
50 }
51}