diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/CodeFragmentPrinter.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/CodeFragmentPrinter.swift deleted file mode 100644 index 4bad3dd34..000000000 --- a/Plugins/BridgeJS/Sources/BridgeJSLink/CodeFragmentPrinter.swift +++ /dev/null @@ -1,80 +0,0 @@ -/// Registry for JS helper intrinsics used during code generation. -final class JSIntrinsicRegistry { - private var entries: [String: [String]] = [:] - - var isEmpty: Bool { - entries.isEmpty - } - - func register(name: String, build: (CodeFragmentPrinter) throws -> Void) rethrows { - guard entries[name] == nil else { return } - let printer = CodeFragmentPrinter() - try build(printer) - entries[name] = printer.lines - } - - func reset() { - entries.removeAll() - } - - func emitLines() -> [String] { - var emitted: [String] = [] - for key in entries.keys.sorted() { - if let lines = entries[key] { - emitted.append(contentsOf: lines) - emitted.append("") - } - } - if emitted.last == "" { - emitted.removeLast() - } - return emitted - } -} - -/// A printer for code fragments. -final class CodeFragmentPrinter { - private(set) var lines: [String] = [] - private var indentLevel: Int = 0 - - init(header: String = "") { - self.lines.append(contentsOf: header.split(separator: "\n").map { String($0) }) - } - - func nextLine() { - lines.append("") - } - - func write(_ line: S) { - if line.isEmpty { - // Empty lines should not have trailing spaces - lines.append("") - return - } - lines.append(String(repeating: " ", count: indentLevel * 4) + String(line)) - } - - func write(lines: [String]) { - for line in lines { - write(line) - } - } - - func write(contentsOf printer: CodeFragmentPrinter) { - self.write(lines: printer.lines) - } - - func indent() { - indentLevel += 1 - } - - func unindent() { - indentLevel -= 1 - } - - func indent(_ body: () throws -> Void) rethrows { - indentLevel += 1 - try body() - indentLevel -= 1 - } -} diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift index 9402cdd9d..7928f480d 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift @@ -1,6 +1,9 @@ #if canImport(BridgeJSSkeleton) import BridgeJSSkeleton #endif +#if canImport(BridgeJSUtilities) +import BridgeJSUtilities +#endif /// A scope for variables for JS glue code final class JSGlueVariableScope { diff --git a/Plugins/BridgeJS/Sources/BridgeJSLink/JSIntrinsicRegistry.swift b/Plugins/BridgeJS/Sources/BridgeJSLink/JSIntrinsicRegistry.swift new file mode 100644 index 000000000..c3e77d83f --- /dev/null +++ b/Plugins/BridgeJS/Sources/BridgeJSLink/JSIntrinsicRegistry.swift @@ -0,0 +1,37 @@ +#if canImport(BridgeJSUtilities) +import BridgeJSUtilities +#endif + +/// Registry for JS helper intrinsics used during code generation. +final class JSIntrinsicRegistry { + private var entries: [String: [String]] = [:] + + var isEmpty: Bool { + entries.isEmpty + } + + func register(name: String, build: (CodeFragmentPrinter) throws -> Void) rethrows { + guard entries[name] == nil else { return } + let printer = CodeFragmentPrinter() + try build(printer) + entries[name] = printer.lines + } + + func reset() { + entries.removeAll() + } + + func emitLines() -> [String] { + var emitted: [String] = [] + for key in entries.keys.sorted() { + if let lines = entries[key] { + emitted.append(contentsOf: lines) + emitted.append("") + } + } + if emitted.last == "" { + emitted.removeLast() + } + return emitted + } +} diff --git a/Plugins/BridgeJS/Sources/BridgeJSUtilities/Utilities.swift b/Plugins/BridgeJS/Sources/BridgeJSUtilities/Utilities.swift index 2791aa5be..ed6fa04f0 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSUtilities/Utilities.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSUtilities/Utilities.swift @@ -27,3 +27,50 @@ public enum BridgeJSGeneratedFile { """ } } + +/// A printer for code fragments. +public final class CodeFragmentPrinter { + public private(set) var lines: [String] = [] + private var indentLevel: Int = 0 + + public init(header: String = "") { + self.lines.append(contentsOf: header.split(separator: "\n").map { String($0) }) + } + + public func nextLine() { + lines.append("") + } + + public func write(_ line: S) { + if line.isEmpty { + // Empty lines should not have trailing spaces + lines.append("") + return + } + lines.append(String(repeating: " ", count: indentLevel * 4) + String(line)) + } + + public func write(lines: [String]) { + for line in lines { + write(line) + } + } + + public func write(contentsOf printer: CodeFragmentPrinter) { + self.write(lines: printer.lines) + } + + public func indent() { + indentLevel += 1 + } + + public func unindent() { + indentLevel -= 1 + } + + public func indent(_ body: () throws -> Void) rethrows { + indentLevel += 1 + try body() + indentLevel -= 1 + } +}