11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import Foundation
14
+ import SwiftDiagnostics
14
15
import SwiftFormatConfiguration
15
16
import SwiftFormatCore
16
17
import SwiftFormatPrettyPrint
17
18
import SwiftFormatRules
19
+ import SwiftOperators
18
20
import SwiftSyntax
19
- import SwiftParser
20
- import SwiftDiagnostics
21
21
22
22
/// Formats Swift source code or syntax trees according to the Swift style guidelines.
23
23
public final class SwiftFormatter {
@@ -46,6 +46,11 @@ public final class SwiftFormatter {
46
46
47
47
/// Formats the Swift code at the given file URL and writes the result to an output stream.
48
48
///
49
+ /// This form of the `format` function automatically folds expressions using the default operator
50
+ /// set defined in Swift. If you need more control over this—for example, to provide the correct
51
+ /// precedence relationships for custom operators—you must parse and fold the syntax tree
52
+ /// manually and then call ``format(syntax:assumingFileURL:to:)``.
53
+ ///
49
54
/// - Parameters:
50
55
/// - url: The URL of the file containing the code to format.
51
56
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
@@ -66,20 +71,23 @@ public final class SwiftFormatter {
66
71
throw SwiftFormatError . isDirectory
67
72
}
68
73
let source = try String ( contentsOf: url, encoding: . utf8)
69
- let sourceFile = try Parser . parse ( source: source)
70
- if let parsingDiagnosticHandler = parsingDiagnosticHandler {
71
- let expectedConverter = SourceLocationConverter ( file: url. path, tree: sourceFile)
72
- let diagnostics = ParseDiagnosticsGenerator . diagnostics ( for: sourceFile)
73
- for diagnostic in diagnostics {
74
- let location = diagnostic. location ( converter: expectedConverter)
75
- parsingDiagnosticHandler ( diagnostic, location)
76
- }
77
- }
78
- try format ( syntax: sourceFile, assumingFileURL: url, source: source, to: & outputStream)
74
+ let sourceFile = try parseAndEmitDiagnostics (
75
+ source: source,
76
+ operatorTable: . standardOperators,
77
+ assumingFileURL: url,
78
+ parsingDiagnosticHandler: parsingDiagnosticHandler)
79
+ try format (
80
+ syntax: sourceFile, operatorTable: . standardOperators, assumingFileURL: url, source: source,
81
+ to: & outputStream)
79
82
}
80
83
81
84
/// Formats the given Swift source code and writes the result to an output stream.
82
85
///
86
+ /// This form of the `format` function automatically folds expressions using the default operator
87
+ /// set defined in Swift. If you need more control over this—for example, to provide the correct
88
+ /// precedence relationships for custom operators—you must parse and fold the syntax tree
89
+ /// manually and then call ``format(syntax:assumingFileURL:to:)``.
90
+ ///
83
91
/// - Parameters:
84
92
/// - source: The Swift source code to be formatted.
85
93
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree,
@@ -96,48 +104,56 @@ public final class SwiftFormatter {
96
104
to outputStream: inout Output ,
97
105
parsingDiagnosticHandler: ( ( Diagnostic , SourceLocation ) -> Void ) ? = nil
98
106
) throws {
99
- let sourceFile = try Parser . parse ( source: source)
100
- if let parsingDiagnosticHandler = parsingDiagnosticHandler {
101
- let expectedConverter = SourceLocationConverter ( file: url? . path ?? " <unknown> " , tree: sourceFile)
102
- let diagnostics = ParseDiagnosticsGenerator . diagnostics ( for: sourceFile)
103
- for diagnostic in diagnostics {
104
- let location = diagnostic. location ( converter: expectedConverter)
105
- parsingDiagnosticHandler ( diagnostic, location)
106
- }
107
- }
108
- try format ( syntax: sourceFile, assumingFileURL: url, source: source, to: & outputStream)
107
+ let sourceFile = try parseAndEmitDiagnostics (
108
+ source: source,
109
+ operatorTable: . standardOperators,
110
+ assumingFileURL: url,
111
+ parsingDiagnosticHandler: parsingDiagnosticHandler)
112
+ try format (
113
+ syntax: sourceFile, operatorTable: . standardOperators, assumingFileURL: url, source: source,
114
+ to: & outputStream)
109
115
}
110
116
111
117
/// Formats the given Swift syntax tree and writes the result to an output stream.
112
118
///
119
+ /// This form of the `format` function does not perform any additional processing on the given
120
+ /// syntax tree. The tree **must** have all expressions folded using an `OperatorTable`, and no
121
+ /// detection of warnings/errors is performed.
122
+ ///
113
123
/// - Note: The formatter may be faster using the source text, if it's available.
114
124
///
115
125
/// - Parameters:
116
126
/// - syntax: The Swift syntax tree to be converted to source code and formatted.
127
+ /// - operatorTable: The table that defines the operators and their precedence relationships.
128
+ /// This must be the same operator table that was used to fold the expressions in the `syntax`
129
+ /// argument.
117
130
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree,
118
131
/// which is associated with any diagnostics emitted during formatting. If this is nil, a
119
132
/// dummy value will be used.
120
133
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
121
134
/// be written.
122
135
/// - Throws: If an unrecoverable error occurs when formatting the code.
123
136
public func format< Output: TextOutputStream > (
124
- syntax: SourceFileSyntax , assumingFileURL url: URL ? , to outputStream: inout Output
137
+ syntax: SourceFileSyntax , operatorTable: OperatorTable , assumingFileURL url: URL ? ,
138
+ to outputStream: inout Output
125
139
) throws {
126
- try format ( syntax: syntax, assumingFileURL: url, source: nil , to: & outputStream)
140
+ try format (
141
+ syntax: syntax, operatorTable: operatorTable, assumingFileURL: url, source: nil ,
142
+ to: & outputStream)
127
143
}
128
144
129
145
private func format< Output: TextOutputStream > (
130
- syntax: SourceFileSyntax , assumingFileURL url : URL ? , source : String ? ,
131
- to outputStream: inout Output
146
+ syntax: SourceFileSyntax , operatorTable : OperatorTable ,
147
+ assumingFileURL url : URL ? , source : String ? , to outputStream: inout Output
132
148
) throws {
133
149
if let position = _firstInvalidSyntaxPosition ( in: Syntax ( syntax) ) {
134
150
throw SwiftFormatError . fileContainsInvalidSyntax ( position: position)
135
151
}
136
152
137
153
let assumedURL = url ?? URL ( fileURLWithPath: " source " )
138
154
let context = Context (
139
- configuration: configuration, findingConsumer : findingConsumer , fileURL : assumedURL ,
140
- sourceFileSyntax: syntax, source: source, ruleNameCache: ruleNameCache)
155
+ configuration: configuration, operatorTable : operatorTable , findingConsumer : findingConsumer ,
156
+ fileURL : assumedURL , sourceFileSyntax: syntax, source: source, ruleNameCache: ruleNameCache)
141
157
let pipeline = FormatPipeline ( context: context)
142
158
let transformedSyntax = pipeline. visit ( Syntax ( syntax) )
143
159
@@ -146,10 +162,8 @@ public final class SwiftFormatter {
146
162
return
147
163
}
148
164
149
- let operatorContext = OperatorContext . makeBuiltinOperatorContext ( )
150
165
let printer = PrettyPrinter (
151
166
context: context,
152
- operatorContext: operatorContext,
153
167
node: transformedSyntax,
154
168
printTokenStream: debugOptions. contains ( . dumpTokenStream) ,
155
169
whitespaceOnly: false )
0 commit comments