Sources/OrgCore/Semantic/SemanticSettings.swift
95 lines · 3551 bytes
1/// App-wide settings that change what a document means without changing how it parses.
2public struct SemanticSettings: Sendable, Equatable {
3 public enum PropertyInheritance: Sendable, Equatable {
4 /// org's default: properties apply only to their own heading.
5 case none
6 case all
7 /// Upper-cased property keys that inherit.
8 case only(Set<String>)
9
10 /// `org-use-property-inheritance` as `config.toml` writes it: empty or `nil` for none,
11 /// `t` for all, or property names separated by spaces.
12 public init(_ text: String) {
13 let names = text.split(whereSeparator: \.isWhitespace).map { $0.uppercased() }
14 if names.isEmpty || names == ["NIL"] {
15 self = .none
16 } else if names == ["T"] {
17 self = .all
18 } else {
19 self = .only(Set(names))
20 }
21 }
22 }
23
24 /// `org-use-tag-inheritance`.
25 public enum TagInheritance: Sendable, Equatable {
26 case all, none
27 /// Only these tags.
28 case only(Set<String>)
29 /// Tags this regular expression matches.
30 case matching(String)
31
32 /// As `config.toml` writes it: `t`, `nil`, tag names separated by spaces, or a regular
33 /// expression in braces, `{^@}`.
34 public init(_ text: String) {
35 let trimmed = text.trimmingCharacters(in: .whitespaces)
36 if trimmed.isEmpty || trimmed == "t" {
37 self = .all
38 } else if trimmed == "nil" {
39 self = .none
40 } else if trimmed.hasPrefix("{"), trimmed.hasSuffix("}"), trimmed.count >= 2 {
41 self = .matching(String(trimmed.dropFirst().dropLast()))
42 } else {
43 self = .only(Set(trimmed.split(whereSeparator: \.isWhitespace).map(String.init)))
44 }
45 }
46 }
47
48 public var tagInheritance: TagInheritance
49 public var tagsExcludedFromInheritance: Set<String>
50 public var propertyInheritance: PropertyInheritance
51
52 /// `org-remove-uninherited-tags` for one tag: whether `tag` passes to children.
53 public func inheritsTag(_ tag: String) -> Bool {
54 switch tagInheritance {
55 case .all: !tagsExcludedFromInheritance.contains(tag)
56 case .none: false
57 case .only(let tags): tags.contains(tag)
58 case .matching(let pattern):
59 tag.range(of: pattern, options: .regularExpression) != nil && !tagsExcludedFromInheritance.contains(tag)
60 }
61 }
62
63 public init(
64 tagInheritance: TagInheritance = .all,
65 tagsExcludedFromInheritance: Set<String> = [],
66 propertyInheritance: PropertyInheritance = .none
67 ) {
68 self.tagInheritance = tagInheritance
69 self.tagsExcludedFromInheritance = tagsExcludedFromInheritance
70 self.propertyInheritance = propertyInheritance
71 }
72
73 public static let `default` = SemanticSettings()
74}
75
76/// Where a resolved value came from.
77public enum ValueSource: Sendable, Equatable {
78 case defaults
79 /// A file-level keyword such as `#+FILETAGS` or `#+PROPERTY`.
80 case file
81 /// A heading, by index into `DocumentModel.headings`.
82 case heading(Int)
83 /// The element itself: a `#+HEADER` line or a block's begin line.
84 case element
85}
86
87public struct Resolved<Value: Sendable & Equatable>: Sendable, Equatable {
88 public let value: Value
89 public let source: ValueSource
90
91 public init(_ value: Value, _ source: ValueSource) {
92 self.value = value
93 self.source = source
94 }
95}