Skip to content

Commit c327979

Browse files
authored
Merge pull request swiftlang#212 from google/pretty-print-by-default
Enable the pretty-printer pass by default.
2 parents c9c669a + b101050 commit c327979

File tree

3 files changed

+73
-29
lines changed

3 files changed

+73
-29
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// Advanced options that are useful when debugging and developing the formatter, but are otherwise
14+
/// not meant for general use.
15+
struct DebugOptions: OptionSet {
16+
17+
/// Disables the pretty-printer pass entirely, executing only the syntax-transforming rules in the
18+
/// pipeline.
19+
static let disablePrettyPrint = DebugOptions(rawValue: 1 << 0)
20+
21+
/// Dumps a verbose representation of the raw pretty-printer token stream.
22+
static let dumpTokenStream = DebugOptions(rawValue: 1 << 1)
23+
24+
let rawValue: Int
25+
26+
init(rawValue: Int) { self.rawValue = rawValue }
27+
28+
/// Inserts or removes the given element from the option set, based on the value of `enabled`.
29+
mutating func set(_ element: Element, enabled: Bool) {
30+
if enabled { insert(element) } else { remove(element) }
31+
}
32+
}

Sources/swift-format/Run.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import SwiftSyntax
2323
/// - Parameter configuration: The `Configuration` that contains user-specific settings.
2424
/// - Parameter path: The absolute path to the source file to be linted.
2525
/// - Returns: Zero if there were no lint errors, otherwise a non-zero number.
26-
public func lintMain(configuration: Configuration, path: String) -> Int {
26+
func lintMain(configuration: Configuration, path: String) -> Int {
2727
let url = URL(fileURLWithPath: path)
2828
let engine = makeDiagnosticEngine()
2929

@@ -50,11 +50,13 @@ public func lintMain(configuration: Configuration, path: String) -> Int {
5050

5151
/// Runs the formatting pipeline over the provided source file.
5252
///
53-
/// - Parameter configuration: The `Configuration` that contains user-specific settings.
54-
/// - Parameter path: The absolute path to the source file to be linted.
53+
/// - Parameters:
54+
/// - configuration: The `Configuration` that contains user-specific settings.
55+
/// - path: The absolute path to the source file to be formatted.
56+
/// - debugOptions: The set containing any debug options that were supplied on the command line.
5557
/// - Returns: Zero if there were no lint errors, otherwise a non-zero number.
56-
public func formatMain(
57-
configuration: Configuration, path: String, prettyPrint: Bool, printTokenStream: Bool
58+
func formatMain(
59+
configuration: Configuration, path: String, debugOptions: DebugOptions
5860
) -> Int {
5961
let url = URL(fileURLWithPath: path)
6062
let context = Context(configuration: configuration, diagnosticEngine: nil, fileURL: url)
@@ -68,7 +70,7 @@ public func formatMain(
6870
// version of visit(_: SourceFileSyntax), which will not run the pipeline properly.
6971
let formatted = pipeline.visit(file as Syntax)
7072

71-
if prettyPrint {
73+
if !debugOptions.contains(.disablePrettyPrint) {
7274
// We create a different context here because we only want diagnostics from the pretty printer
7375
// phase when formatting.
7476
let prettyPrintContext = Context(
@@ -79,8 +81,7 @@ public func formatMain(
7981
let printer = PrettyPrinter(
8082
context: prettyPrintContext,
8183
node: formatted,
82-
printTokenStream: printTokenStream
83-
)
84+
printTokenStream: debugOptions.contains(.dumpTokenStream))
8485
print(printer.prettyPrint(), terminator: "")
8586
} else {
8687
print(formatted.description, terminator: "")

Sources/swift-format/main.swift

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SwiftFormatCore
1717
import SwiftFormatPrettyPrint
1818
import Utility
1919

20-
enum Mode: String, Codable, ArgumentKind {
20+
fileprivate enum Mode: String, Codable, ArgumentKind {
2121
case format
2222
case lint
2323
case dumpConfiguration = "dump-configuration"
@@ -31,6 +31,14 @@ enum Mode: String, Codable, ArgumentKind {
3131
])
3232
}
3333

34+
/// Indicates whether the mode requires at least one input file passed as a positional argument.
35+
var requiresFiles: Bool {
36+
switch self {
37+
case .format, .lint: return true
38+
case .dumpConfiguration, .version: return false
39+
}
40+
}
41+
3442
init(argument: String) throws {
3543
guard let mode = Mode(rawValue: argument) else {
3644
throw ArgumentParserError.invalidValue(argument: argument, error: .unknown(value: argument))
@@ -39,16 +47,17 @@ enum Mode: String, Codable, ArgumentKind {
3947
}
4048
}
4149

42-
struct CommandLineOptions: Codable {
50+
fileprivate struct CommandLineOptions {
4351
var configurationPath: String? = nil
4452
var paths: [String] = []
4553
var verboseLevel = 0
4654
var mode: Mode = .format
47-
var prettyPrint: Bool = false
48-
var printTokenStream: Bool = false
55+
var debugOptions: DebugOptions = []
4956
}
5057

51-
func processArguments(commandName: String, _ arguments: [String]) -> CommandLineOptions {
58+
fileprivate func processArguments(
59+
commandName: String, _ arguments: [String]
60+
) -> CommandLineOptions {
5261
let parser = ArgumentParser(commandName: commandName,
5362
usage: "[options] <filename or path> ...",
5463
overview: "Format or lint Swift source code.")
@@ -84,34 +93,38 @@ func processArguments(commandName: String, _ arguments: [String]) -> CommandLine
8493
}
8594
binder.bind(
8695
option: parser.add(
87-
option: "--pretty-print",
88-
shortName: "-p",
89-
kind: Bool.self,
90-
usage: "Pretty-print the output and automatically apply line-wrapping."
96+
option: "--configuration",
97+
kind: String.self,
98+
usage: "The path to a JSON file containing the configuration of the linter/formatter."
9199
)) {
92-
$0.prettyPrint = $1
100+
$0.configurationPath = $1
93101
}
102+
103+
// Add advanced debug/developer options. These intentionally have no usage strings, which omits
104+
// them from the `--help` screen to avoid noise for the general user.
94105
binder.bind(
95106
option: parser.add(
96-
option: "--token-stream",
97-
kind: Bool.self,
98-
usage: "Print out the pretty-printer token stream."
107+
option: "--debug-disable-pretty-print",
108+
kind: Bool.self
99109
)) {
100-
$0.printTokenStream = $1
110+
$0.debugOptions.set(.disablePrettyPrint, enabled: $1)
101111
}
102112
binder.bind(
103113
option: parser.add(
104-
option: "--configuration",
105-
kind: String.self,
106-
usage: "The path to a JSON file containing the configuration of the linter/formatter."
114+
option: "--debug-dump-token-stream",
115+
kind: Bool.self
107116
)) {
108-
$0.configurationPath = $1
117+
$0.debugOptions.set(.dumpTokenStream, enabled: $1)
109118
}
110119

111120
var opts = CommandLineOptions()
112121
do {
113122
let args = try parser.parse(arguments)
114123
binder.fill(args, into: &opts)
124+
125+
if opts.mode.requiresFiles && opts.paths.isEmpty {
126+
throw ArgumentParserError.expectedArguments(parser, ["filenames or paths"])
127+
}
115128
} catch {
116129
stderrStream.write("error: \(error)\n\n")
117130
parser.printUsage(on: stderrStream)
@@ -131,9 +144,7 @@ func main(_ arguments: [String]) -> Int32 {
131144
ret |= formatMain(
132145
configuration: configuration,
133146
path: path,
134-
prettyPrint: options.prettyPrint,
135-
printTokenStream: options.printTokenStream
136-
)
147+
debugOptions: options.debugOptions)
137148
}
138149
return Int32(ret)
139150
case .lint:

0 commit comments

Comments
 (0)