Sources/OrgCore/Syntax/Wrappers.swift

a866e469a3568bf7a2829a8dd2a56317ced4736e
orgstar/Sources/OrgCore/Syntax/Wrappers.swift history · blame · raw

81 lines · 3531 bytes

 1import Foundation
 2
 3/// Drawers and blocks, the elements org folds on their own (`org-fold-hide-drawer-toggle`,
 4/// `org-fold-hide-block-toggle`).
 5public struct Wrapper: Sendable, Equatable {
 6    public enum Kind: Sendable, Equatable { case drawer, block }
 7
 8    public let kind: Kind
 9    /// Start of the opening line (`:PROPERTIES:`, `#+begin_src`).
10    public let start: Int
11    /// Start of the closing line (`:END:`, `#+end_src`).
12    public let closeLine: Int
13    /// What folding hides: every line after the opening one, through the closing line.
14    public let hidden: Range<Int>
15}
16
17public enum Wrappers {
18    static func kind(_ kind: SyntaxKind) -> Wrapper.Kind? {
19        switch kind {
20        case .drawer, .propertyDrawer: .drawer
21        case .block, .dynamicBlock: .block
22        default: nil
23        }
24    }
25
26    /// `text` is the document's, read in place rather than copying the node's text.
27    static func wrapper(_ node: SyntaxNode, in text: NSString) -> Wrapper? {
28        guard let kind = kind(node.kind) else { return nil }
29        let start = node.range.lowerBound
30        // Blank lines after the element belong to it in the tree, not to the fold.
31        var end = node.range.upperBound
32        while end > start, [9, 10, 13, 32].contains(text.character(at: end - 1)) { end -= 1 }
33        let firstNewline = text.range(of: "\n", options: .literal, range: NSRange(start..<end)).location
34        guard firstNewline != NSNotFound else { return nil }
35        var closeStart = end
36        while closeStart > start, text.character(at: closeStart - 1) != 10 { closeStart -= 1 }
37        let hiddenEnd = end < node.range.upperBound ? end + 1 : end
38        return Wrapper(kind: kind, start: start, closeLine: closeStart, hidden: (firstNewline + 1)..<hiddenEnd)
39    }
40
41    /// Elements a drawer or block can sit in.
42    static let containers: Set<SyntaxKind> = [.document, .zerothSection, .section, .drawer, .block, .dynamicBlock, .plainList, .item, .footnoteDefinition]
43
44    /// Every drawer and block, in document order, nested ones included.
45    public static func all(_ tree: OrgTree) -> [Wrapper] {
46        var result: [Wrapper] = []
47        let text = tree.text as NSString
48        func walk(_ node: SyntaxNode) {
49            for child in node.children {
50                if let wrapper = wrapper(child, in: text) { result.append(wrapper) }
51                if containers.contains(child.kind) { walk(child) }
52            }
53        }
54        walk(tree.root)
55        return result
56    }
57
58    /// The drawer or block whose opening line starts at `offset`.
59    public static func starting(at offset: Int, in tree: OrgTree) -> Wrapper? {
60        var node = tree.root
61        while let child = node.child(containing: offset) {
62            if child.range.lowerBound == offset, let wrapper = wrapper(child, in: tree.text as NSString) { return wrapper }
63            node = child
64        }
65        return nil
66    }
67
68    /// The innermost drawer or block whose opening or closing line holds `offset`, the lines
69    /// TAB toggles it from.
70    public static func toggleable(at offset: Int, in tree: OrgTree) -> Wrapper? {
71        var node = tree.root
72        var found: Wrapper?
73        while let child = node.child(containing: offset) {
74            if let wrapper = wrapper(child, in: tree.text as NSString), offset < wrapper.hidden.lowerBound || (wrapper.closeLine..<wrapper.hidden.upperBound).contains(offset) {
75                found = wrapper
76            }
77            node = child
78        }
79        return found
80    }
81}