Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions Plugins/BridgeJS/Sources/BridgeJSLink/CodeFragmentPrinter.swift

This file was deleted.

3 changes: 3 additions & 0 deletions Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions Plugins/BridgeJS/Sources/BridgeJSLink/JSIntrinsicRegistry.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
47 changes: 47 additions & 0 deletions Plugins/BridgeJS/Sources/BridgeJSUtilities/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: StringProtocol>(_ 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
}
}