Sources/OrgCore/Semantic/SemanticSettings.swift
60 lines · 2093 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 public var tagInheritance: Bool
25 public var tagsExcludedFromInheritance: Set<String>
26 public var propertyInheritance: PropertyInheritance
27
28 public init(
29 tagInheritance: Bool = true,
30 tagsExcludedFromInheritance: Set<String> = [],
31 propertyInheritance: PropertyInheritance = .none
32 ) {
33 self.tagInheritance = tagInheritance
34 self.tagsExcludedFromInheritance = tagsExcludedFromInheritance
35 self.propertyInheritance = propertyInheritance
36 }
37
38 public static let `default` = SemanticSettings()
39}
40
41/// Where a resolved value came from.
42public enum ValueSource: Sendable, Equatable {
43 case defaults
44 /// A file-level keyword such as `#+FILETAGS` or `#+PROPERTY`.
45 case file
46 /// A heading, by index into `DocumentModel.headings`.
47 case heading(Int)
48 /// The element itself: a `#+HEADER` line or a block's begin line.
49 case element
50}
51
52public struct Resolved<Value: Sendable & Equatable>: Sendable, Equatable {
53 public let value: Value
54 public let source: ValueSource
55
56 public init(_ value: Value, _ source: ValueSource) {
57 self.value = value
58 self.source = source
59 }
60}