Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158import Foundation
public struct TOMLDocument: Sendable { public private(set) var source: String public private(set) var root: TOMLNode private var sections: [TOMLSection] private var editableRanges: [[TOMLPathComponent]: SourceRange]
public init(parsing source: String) throws { var reader = TOMLReader(source) self.root = try reader.parse() self.source = source self.sections = reader.sections self.editableRanges = reader.editableRanges }
public func render() -> String { source }
public func node(at path: [TOMLPathComponent]) -> TOMLNode? { var node = root for component in path { switch (component, node.value) { case (.key(let key), .table(let entries)): guard let child = entries[key] else { return nil } node = child case (.index(let index), .array(let entries)): guard entries.indices.contains(index) else { return nil } node = entries[index] default: return nil } } return node }
public func node(forKey path: [String]) -> TOMLNode? { node(at: path.map { .key($0) }) }
public mutating func setValue(_ value: TOMLValue, forKey path: [String]) throws { try setValue(value, at: path.map { .key($0) }) }
/// Reparse before committing an edit so a failed change never damages the document. public mutating func setValue(_ value: TOMLValue, at path: [TOMLPathComponent]) throws { let replacement = try TOMLWriter.encode(value) if let range = editableRanges[path] { try replace(range.start.offset..<range.end.offset, with: replacement) return } guard node(at: path) == nil, case .key = path.last else { throw editingError("Choose a value or a new table key, not a table header or an array index.") } for length in stride(from: path.count - 1, through: 1, by: -1) { let parentPath = Array(path.prefix(length)) guard let parent = node(at: parentPath), case .table(let entries) = parent.value, let parentRange = editableRanges[parentPath] else { continue } let suffix = path.dropFirst(length) let keys = suffix.compactMap { component -> String? in guard case .key(let key) = component else { return nil } return TOMLWriter.quote(key) } guard keys.count == suffix.count else { break } let insertion = parentRange.end.offset - 1 let separator = entries.isEmpty ? "" : ", " try replace(insertion..<insertion, with: separator + keys.joined(separator: ".") + " = " + replacement) return } guard let section = sections.filter({ section in path.starts(with: section.path) && path.dropFirst(section.path.count).allSatisfy { if case .key = $0 { true } else { false } } }).max(by: { $0.path.count < $1.path.count }) else { throw editingError("Add a value inside an existing table before editing this path.") } let keys = path.dropFirst(section.path.count).compactMap { component -> String? in guard case .key(let key) = component else { return nil } return TOMLWriter.quote(key) } let newline = source.contains("\r\n") ? "\r\n" : "\n" let offset = section.insertionOffset let needsNewline = offset > 0 && Array(source.unicodeScalars)[offset - 1] != "\n" let assignment = (needsNewline ? newline : "") + keys.joined(separator: ".") + " = " + replacement + newline try replace(offset..<offset, with: assignment) }
private mutating func replace(_ offsets: Range<Int>, with replacement: String) throws { var scalars = Array(source.unicodeScalars) scalars.replaceSubrange(offsets, with: replacement.unicodeScalars) self = try TOMLDocument(parsing: String(String.UnicodeScalarView(scalars))) }
private func editingError(_ message: String.LocalizationValue) -> ConfigDiagnostic { ConfigDiagnostic(message: String(localized: message, table: "Parser", bundle: .module)) }}
public enum TOMLWriter { public static func write(_ document: TOMLDocument) -> String { document.source }
public static func encode(_ value: TOMLValue) throws -> String { try encode(value, depth: 0) }
private static func encode(_ value: TOMLValue, depth: Int) throws -> String { guard depth < 128 else { throw ConfigDiagnostic( message: String( localized: "This value is nested too deeply; use fewer than 128 levels.", table: "Parser", bundle: .module) ) } switch value { case .string(let string): return quote(string) case .integer(let integer): return String(integer) case .float(let number): if number.isNaN { return "nan" } if number == .infinity { return "inf" } if number == -.infinity { return "-inf" } return String(number) case .bool(let bool): return bool ? "true" : "false" case .array(let values): return "[" + (try values.map { try encode($0.value, depth: depth + 1) }).joined(separator: ", ") + "]" case .table(let values): let pairs = try values.sorted { $0.key < $1.key }.map { key, node in quote(key) + " = " + (try encode(node.value, depth: depth + 1)) } return "{" + pairs.joined(separator: ", ") + "}" } }
static func quote(_ string: String) -> String { var result = "\"" for scalar in string.unicodeScalars { switch scalar { case "\"": result += "\\\"" case "\\": result += "\\\\" case "\n": result += "\\n" case "\r": result += "\\r" case "\t": result += "\\t" case "\u{08}": result += "\\b" case "\u{0C}": result += "\\f" default: if scalar.value < 0x20 || scalar.value == 0x7F { let hex = String(scalar.value, radix: 16, uppercase: true) result += "\\u" + String(repeating: "0", count: 4 - hex.count) + hex } else { result.unicodeScalars.append(scalar) } } } return result + "\"" }}