Sources/OrgCore/Syntax/SyntaxNode.swift

53903567a65c44ccdf4ba4ff6ffa6d04721efea1
orgstar/Sources/OrgCore/Syntax/SyntaxNode.swift

59 lines · 1830 bytes

 1/// A view of a green node at an absolute offset, with a parent link. Created on demand.
 2public final class SyntaxNode: Sendable {
 3    public let green: GreenNode
 4    public let offset: Int
 5    public let parent: SyntaxNode?
 6
 7    init(green: GreenNode, offset: Int, parent: SyntaxNode?) {
 8        self.green = green
 9        self.offset = offset
10        self.parent = parent
11    }
12
13    public var kind: SyntaxKind { green.kind }
14    public var range: Range<Int> { offset..<(offset + green.length) }
15    public var text: String { green.text }
16
17    public var children: [SyntaxNode] {
18        var result: [SyntaxNode] = []
19        var at = offset
20        for child in green.children {
21            if case .node(let node) = child {
22                result.append(SyntaxNode(green: node, offset: at, parent: self))
23            }
24            at += child.length
25        }
26        return result
27    }
28
29    public var tokens: [SyntaxToken] {
30        var result: [SyntaxToken] = []
31        var at = offset
32        for child in green.children {
33            if case .token(let token) = child {
34                result.append(SyntaxToken(kind: token.kind, text: token.text, range: at..<(at + token.length)))
35            }
36            at += child.length
37        }
38        return result
39    }
40
41    /// This node and every node below it, in document order.
42    public func descendants() -> [SyntaxNode] {
43        [self] + children.flatMap { $0.descendants() }
44    }
45}
46
47public struct SyntaxToken: Sendable, Equatable {
48    public let kind: SyntaxKind
49    public let text: String
50    public let range: Range<Int>
51}
52
53public struct OrgTree: Sendable {
54    public let green: GreenNode
55    public let settings: OrgSettings
56
57    public var root: SyntaxNode { SyntaxNode(green: green, offset: 0, parent: nil) }
58    public var text: String { green.text }
59}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form