forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodePrinter.swift
More file actions
257 lines (221 loc) · 6.87 KB
/
CodePrinter.swift
File metadata and controls
257 lines (221 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
public struct CodePrinter {
var contents: String = ""
var verbose: Bool = false
let log = Logger(label: "printer", logLevel: .info)
var indentationDepth: Int = 0 {
didSet {
indentationText = String(repeating: indentationPart, count: indentationDepth)
}
}
/// String to use for one level of indentationDepth.
public var indentationPart: String = " " {
didSet {
indentationText = String(repeating: indentationPart, count: indentationDepth)
}
}
public var indentationText: String = ""
/// If true, next print() should starts with indentation.
var atNewline = true
public static func toString(_ block: (inout CodePrinter) throws -> ()) rethrows -> String {
var printer = CodePrinter()
try block(&printer)
return printer.finalize()
}
var mode: PrintMode
public enum PrintMode {
case accumulateAll
case flushToFileOnWrite
}
public init(mode: PrintMode = .flushToFileOnWrite) {
self.mode = mode
}
internal mutating func append(_ text: String) {
contents.append(text)
if self.verbose {
Swift.print(text, terminator: "")
}
}
internal mutating func append<S>(contentsOf text: S)
where S: Sequence, S.Element == Character {
contents.append(contentsOf: text)
if self.verbose {
for t in text {
Swift.print(t, terminator: "")
}
}
}
public mutating func printBraceBlock(
_ header: Any,
function: String = #function,
file: String = #fileID,
line: UInt = #line,
body: (inout CodePrinter) throws -> ()
) rethrows {
print("\(header) {")
indent()
try body(&self)
outdent()
print("}", .sloc, function: function, file: file, line: line)
}
public mutating func printParts(
_ parts: String...,
terminator: PrinterTerminator = .newLine,
function: String = #function,
file: String = #fileID,
line: UInt = #line
) {
for part in parts {
guard part.trimmingCharacters(in: .whitespacesAndNewlines).count != 0 else {
continue
}
self.print(part, terminator, function: function, file: file, line: line)
}
}
/// Print a plain newline, e.g. to separate declarations.
public mutating func println(
_ terminator: PrinterTerminator = .newLine,
function: String = #function,
file: String = #fileID,
line: UInt = #line
) {
print("")
}
public mutating func print(
_ text: Any,
_ terminator: PrinterTerminator = .newLine,
function: String = #function,
file: String = #fileID,
line: UInt = #line
) {
let lines = "\(text)".split(separator: "\n", omittingEmptySubsequences: false)
var first = true
for line in lines {
if !first {
append("\n")
append(indentationText)
} else {
if atNewline {
append(indentationText)
}
first = false
}
append(contentsOf: line)
}
if terminator == .sloc {
append(" // \(function) @ \(file):\(line)\n")
atNewline = true
} else {
append(terminator.rawValue)
atNewline = terminator == .newLine || terminator == .commaNewLine
}
}
public mutating func start(_ text: String) {
print(text, .continue)
}
// TODO: remove this in real mode, this just helps visually while working on it
public mutating func printSeparator(_ text: String) {
assert(!text.contains(where: \.isNewline))
print(
"""
// ==== --------------------------------------------------
// \(text)
"""
)
}
public mutating func finalize() -> String {
// assert(indentationDepth == 0, "Finalize CodePrinter with non-zero indentationDepth. Text was: \(contents)") // FIXME: do this
defer { contents = "" }
return contents
}
public mutating func indent(file: String = #fileID, line: UInt = #line, function: String = #function) {
indentationDepth += 1
log.trace("Indent => \(indentationDepth)", file: file, line: line, function: function)
}
public mutating func outdent(file: String = #fileID, line: UInt = #line, function: String = #function) {
indentationDepth -= 1
log.trace("Outdent => \(indentationDepth)", file: file, line: line, function: function)
assert(indentationDepth >= 0, "Outdent beyond zero at [\(file):\(line)](\(function))")
}
public var isEmpty: Bool {
self.contents.isEmpty
}
public mutating func dump(file: String = #fileID, line: UInt = #line) {
Swift.print("// CodePrinter.dump @ \(file):\(line)")
Swift.print(contents)
}
}
public enum PrinterTerminator: String {
case newLine = "\n"
case space = " "
case commaSpace = ", "
case commaNewLine = ",\n"
case `continue` = ""
case sloc = "// <source location>"
public static func parameterSeparator(_ isLast: Bool) -> Self {
if isLast {
.continue
} else {
.commaSpace
}
}
public static func parameterNewlineSeparator(_ isLast: Bool) -> Self {
if isLast {
.newLine
} else {
.commaNewLine
}
}
}
extension CodePrinter {
/// - Returns: the output path of the generated file, if any (i.e. not in accumulate in memory mode)
package mutating func writeContents(
outputDirectory: String,
javaPackagePath: String?,
filename: String
) throws -> URL? {
guard self.mode != .accumulateAll else {
// if we're accumulating everything, we don't want to finalize/flush any contents
// let's mark that this is where a write would have happened though:
print("// ^^^^ Contents of: \(outputDirectory)/\(filename)")
return nil
}
let contents = finalize()
if outputDirectory == "-" {
print(
"// ==== ---------------------------------------------------------------------------------------------------"
)
if let javaPackagePath {
print("// \(javaPackagePath)/\(filename)")
} else {
print("// \(filename)")
}
print(contents)
return nil
}
let targetDirectory = [outputDirectory, javaPackagePath].compactMap { $0 }.joined(separator: PATH_SEPARATOR)
log.trace("Prepare target directory: \(targetDirectory)")
try FileManager.default.createDirectory(
atPath: targetDirectory, withIntermediateDirectories: true)
let outputPath = Foundation.URL(fileURLWithPath: targetDirectory).appendingPathComponent(filename)
try contents.write(
to: outputPath,
atomically: true,
encoding: .utf8
)
return outputPath
}
}