Skip to content

Commit 0f11210

Browse files
authored
Merge pull request swiftlang#172 from dabelknap/debug-stream
Add a debug option to print out the pretty-printing token stream
2 parents 76338cb + c2ff1f5 commit 0f11210

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

Sources/SwiftFormatPrettyPrint/PrettyPrint.swift

+75-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public class PrettyPrinter {
6868
/// breaks occur in the formatted output.
6969
private var isDebugMode: Bool
7070

71+
/// If true, the token stream is printed to the console for debugging purposes.
72+
private var printTokenStream: Bool
73+
7174
/// The current index (0..<6) of the color to be used for the next color group.
7275
private var currentDebugGroupMarkerColor = 0
7376

@@ -84,12 +87,13 @@ public class PrettyPrinter {
8487
/// - Parameters:
8588
/// - configuration: The configuration used to decide whitespace or breaking behavior.
8689
/// - node: The node to be pretty printed.
87-
public init(configuration: Configuration, node: Syntax, isDebugMode: Bool) {
90+
public init(configuration: Configuration, node: Syntax, isDebugMode: Bool, printTokenStream: Bool) {
8891
self.configuration = configuration
8992
self.tokens = node.makeTokenStream(configuration: configuration)
9093
self.maxLineLength = configuration.lineLength
9194
self.isDebugMode = isDebugMode
9295
self.spaceRemaining = self.maxLineLength
96+
self.printTokenStream = printTokenStream
9397
}
9498

9599
/// Append the input string to the output buffer
@@ -107,6 +111,9 @@ public class PrettyPrinter {
107111
/// - token: The token to be printed.
108112
/// - length: The length of the token (number of columns).
109113
private func printToken(token: Token, length: Int) {
114+
if self.printTokenStream {
115+
printDebugToken(token: token, length: length)
116+
}
110117
assert(length >= 0, "Token lengths must be positive")
111118
switch token {
112119

@@ -415,6 +422,73 @@ public class PrettyPrinter {
415422
write(Ansi.reset)
416423
}
417424

425+
/// Used to track the indentation level for the debug token stream output
426+
var debugIndent: Int = 0
427+
428+
/// Print out the token stream to the console for debugging.
429+
///
430+
/// Indentation is applied to make identification of groups easier.
431+
private func printDebugToken(token: Token, length: Int) {
432+
func printDebugIndent() {
433+
print(String(repeating: " ", count: debugIndent), terminator:"")
434+
}
435+
switch token {
436+
case .syntax(let syntax):
437+
printDebugIndent()
438+
print("[SYNTAX \"\(syntax.text)\" Length: \(length)]")
439+
440+
case .break(let size, let offset):
441+
printDebugIndent()
442+
print("[BREAK Size: \(size) Offset: \(offset) Length: \(length)]")
443+
444+
case .open(let breakstyle, let offset):
445+
printDebugIndent()
446+
switch breakstyle {
447+
case .consistent:
448+
print("[OPEN Consistent Offset: \(offset) Length: \(length)]")
449+
case .inconsistent:
450+
print("[OPEN Inconsistent Offset: \(offset) Length: \(length)]")
451+
}
452+
debugIndent += 2
453+
454+
case .close:
455+
debugIndent -= 2
456+
printDebugIndent()
457+
print("[CLOSE]")
458+
459+
case .newlines(let N, let offset):
460+
printDebugIndent()
461+
print("[NEWLINES N: \(N) Offset: \(offset) Length: \(length)]")
462+
463+
case .space(let size):
464+
printDebugIndent()
465+
print("[SPACE Size: \(size) Length: \(length)]")
466+
467+
case .reset:
468+
print("[RESET]")
469+
470+
case .comment(let comment):
471+
printDebugIndent()
472+
switch comment.kind {
473+
case .line:
474+
print("[COMMENT Line Length: \(length)]")
475+
case .docLine:
476+
print("[COMMENT DocLine Length: \(length)]")
477+
case .block:
478+
print("[COMMENT Block Length: \(length)]")
479+
case .docBlock:
480+
print("[COMMENT DocBlock Length: \(length)]")
481+
}
482+
printDebugIndent()
483+
print(comment.print(indent: debugIndent))
484+
485+
case .verbatim(let verbatim):
486+
printDebugIndent()
487+
print("[VERBATIM Length: \(length)]")
488+
print(verbatim.print(indent: debugIndent))
489+
}
490+
}
491+
418492
/// Writes the given number of spaces to the output.
419493
///
420494
/// If debug mode is enabled, spaces are rendered as gray Unicode MIDDLE DOT characters.

Sources/swift-format/Run.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public func lintMain(path: String) -> Int {
5353
///
5454
/// - Parameter path: The absolute path to the source file to be linted.
5555
/// - Returns: Zero if there were no lint errors, otherwise a non-zero number.
56-
public func formatMain(path: String, isDebugMode: Bool, prettyPrint: Bool) -> Int {
56+
public func formatMain(path: String, isDebugMode: Bool, prettyPrint: Bool, printTokenStream: Bool) -> Int {
5757
let url = URL(fileURLWithPath: path)
5858

5959
let context = Context(
@@ -75,7 +75,8 @@ public func formatMain(path: String, isDebugMode: Bool, prettyPrint: Bool) -> In
7575
let printer = PrettyPrinter(
7676
configuration: context.configuration,
7777
node: formatted,
78-
isDebugMode: isDebugMode
78+
isDebugMode: isDebugMode,
79+
printTokenStream: printTokenStream
7980
)
8081
print(printer.prettyPrint(), terminator: "")
8182
} else {

Sources/swift-format/main.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct CommandLineOptions: Codable {
4242
var mode: Mode = .format
4343
var isDebugMode: Bool = false
4444
var prettyPrint: Bool = false
45+
var printTokenStream: Bool = false
4546
}
4647

4748
func processArguments(commandName: String, _ arguments: [String]) -> CommandLineOptions {
@@ -95,6 +96,14 @@ func processArguments(commandName: String, _ arguments: [String]) -> CommandLine
9596
)) {
9697
$0.prettyPrint = $1
9798
}
99+
binder.bind(
100+
option: parser.add(
101+
option: "--token-stream",
102+
kind: Bool.self,
103+
usage: "Print out the pretty-printer token stream."
104+
)) {
105+
$0.printTokenStream = $1
106+
}
98107

99108
var opts = CommandLineOptions()
100109
do {
@@ -118,7 +127,8 @@ func main(_ arguments: [String]) -> Int32 {
118127
ret |= formatMain(
119128
path: path,
120129
isDebugMode: options.isDebugMode,
121-
prettyPrint: options.prettyPrint
130+
prettyPrint: options.prettyPrint,
131+
printTokenStream: options.printTokenStream
122132
)
123133
}
124134
return Int32(ret)

Tests/SwiftFormatPrettyPrintTests/PrettyPrintTestCase.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public class PrettyPrintTestCase: XCTestCase {
2222
let printer = PrettyPrinter(
2323
configuration: context.configuration,
2424
node: syntax,
25-
isDebugMode: false
25+
isDebugMode: false,
26+
printTokenStream: false
2627
)
2728
let output = printer.prettyPrint()
2829
XCTAssertEqual(expected, output)

0 commit comments

Comments
 (0)