Skip to content

Commit 74b1286

Browse files
authored
Merge pull request #1113 from ahoppen/ahoppen/format
Format the swift-syntax repository using swift-format
2 parents 528c7f5 + c7a5eb7 commit 74b1286

File tree

200 files changed

+8007
-5535
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

200 files changed

+8007
-5535
lines changed

.swift-format

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": 1,
3+
"lineLength": 1000,
4+
"indentation": {
5+
"spaces": 2
6+
},
7+
"lineBreakBeforeEachArgument": true,
8+
"indentConditionalCompilationBlocks": false,
9+
"rules": {
10+
"AlwaysUseLowerCamelCase": false,
11+
"AmbiguousTrailingClosureOverload": false,
12+
"DontRepeatTypeInStaticProperties": false,
13+
"NoBlockComments": false,
14+
"Spacing": false,
15+
"UseLetInEveryBoundCaseVariable": false,
16+
"UseSynthesizedInitializer": false
17+
}
18+
}

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ Alternatively you can also build it from the command line using `build-script.py
1919
swift-syntax/build-script.py build --toolchain /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-<recent date>.xctoolchain/usr
2020
```
2121
22+
## Formatting
23+
24+
SwiftSyntax is being formatted using [swift-format](http://github.com/apple/swift-format) to ensure a consistent style.
25+
26+
To format your changes run `format.py` at the root of this repository. If you have a `swift-format` executable ready, you can pass it to `format.py`. If you do not, `format.py` will build its own copy of `swift-format` in /tmp/swift-format.
27+
28+
CI will ensure that all hand-written source code is correctly formatted. Generated source code is not formatted to make it easier to spot changes when re-running code generation.
29+
2230
## Testing
2331
2432
Because of SwiftSyntax’s integration with the Swift compiler project, testing certain parts of the project is a little bit more involved than others.

Sources/IDEUtils/SyntaxClassifier.swift

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ fileprivate extension SyntaxProtocol {
1818
var curData = Syntax(self)
1919
repeat {
2020
guard let parent = curData.parent else { break }
21-
contextualClassif = SyntaxClassification.classify(parentKind: parent.raw.kind,
22-
indexInParent: curData.indexInParent, childKind: raw.kind)
21+
contextualClassif = SyntaxClassification.classify(
22+
parentKind: parent.raw.kind,
23+
indexInParent: curData.indexInParent,
24+
childKind: raw.kind
25+
)
2326
curData = parent
2427
} while contextualClassif == nil
2528
return contextualClassif
@@ -33,19 +36,21 @@ extension TokenSyntax {
3336
let relativeOffset = leadingTriviaLength.utf8Length
3437
let absoluteOffset = position.utf8Offset + relativeOffset
3538
return TokenKindAndText(kind: rawTokenKind, text: tokenView.rawText).classify(
36-
offset: absoluteOffset, contextualClassification: contextualClassification)
39+
offset: absoluteOffset,
40+
contextualClassification: contextualClassification
41+
)
3742
}
3843
}
3944

4045
extension RawTriviaPiece {
4146
func classify(offset: Int) -> SyntaxClassifiedRange {
4247
let range = ByteSourceRange(offset: offset, length: byteLength)
4348
switch self {
44-
case .lineComment: return .init(kind: .lineComment, range: range)
45-
case .blockComment: return .init(kind: .blockComment, range: range)
46-
case .docLineComment: return .init(kind: .docLineComment, range: range)
47-
case .docBlockComment: return .init(kind: .docBlockComment, range: range)
48-
default: return .init(kind: .none, range: range)
49+
case .lineComment: return .init(kind: .lineComment, range: range)
50+
case .blockComment: return .init(kind: .blockComment, range: range)
51+
case .docLineComment: return .init(kind: .docLineComment, range: range)
52+
case .docBlockComment: return .init(kind: .docBlockComment, range: range)
53+
default: return .init(kind: .none, range: range)
4954
}
5055
}
5156
}
@@ -55,7 +60,8 @@ fileprivate struct TokenKindAndText {
5560
let text: SyntaxText
5661

5762
func classify(
58-
offset: Int, contextualClassification: (SyntaxClassification, Bool)?
63+
offset: Int,
64+
contextualClassification: (SyntaxClassification, Bool)?
5965
) -> SyntaxClassifiedRange {
6066
let range = ByteSourceRange(offset: offset, length: text.count)
6167

@@ -73,8 +79,9 @@ fileprivate struct TokenKindAndText {
7379
return .stringLiteral
7480
}
7581
if kind == .identifier,
76-
text.hasPrefix("<#"),
77-
text.hasSuffix("#>") {
82+
text.hasPrefix("<#"),
83+
text.hasSuffix("#>")
84+
{
7885
return .editorPlaceholder
7986
}
8087
return kind.classification
@@ -114,17 +121,21 @@ private struct ClassificationVisitor {
114121
init(node: Syntax, relativeClassificationRange: ByteSourceRange) {
115122
let range = ByteSourceRange(
116123
offset: node.position.utf8Offset + relativeClassificationRange.offset,
117-
length: relativeClassificationRange.length)
124+
length: relativeClassificationRange.length
125+
)
118126
self.targetRange = range
119127
self.classifications = []
120128

121129
// `withExtendedLifetime` to make sure `SyntaxArena` for the node alive
122130
// during the visit.
123131
withExtendedLifetime(node) {
124-
_ = self.visit(Descriptor(
125-
node: node.raw,
126-
byteOffset: node.position.utf8Offset,
127-
contextualClassification: node.contextualClassification))
132+
_ = self.visit(
133+
Descriptor(
134+
node: node.raw,
135+
byteOffset: node.position.utf8Offset,
136+
contextualClassification: node.contextualClassification
137+
)
138+
)
128139
}
129140
}
130141

@@ -135,15 +146,19 @@ private struct ClassificationVisitor {
135146

136147
// Merge consecutive classified ranges of the same kind.
137148
if let last = classifications.last,
138-
last.kind == range.kind,
139-
last.endOffset == range.offset {
149+
last.kind == range.kind,
150+
last.endOffset == range.offset
151+
{
140152
classifications[classifications.count - 1].range = ByteSourceRange(
141-
offset: last.offset, length: last.length + range.length)
153+
offset: last.offset,
154+
length: last.length + range.length
155+
)
142156
return
143157
}
144158

145159
guard range.offset <= targetRange.endOffset,
146-
range.endOffset >= targetRange.offset else {
160+
range.endOffset >= targetRange.offset
161+
else {
147162
return
148163
}
149164
classifications.append(range)
@@ -186,11 +201,17 @@ private struct ClassificationVisitor {
186201
for case (let index, let child?) in children.enumerated() {
187202

188203
let classficiation = SyntaxClassification.classify(
189-
parentKind: descriptor.node.kind, indexInParent: index, childKind: child.kind)
190-
let result = visit(.init(
191-
node: child,
192-
byteOffset: byteOffset,
193-
contextualClassification: classficiation ?? descriptor.contextualClassification))
204+
parentKind: descriptor.node.kind,
205+
indexInParent: index,
206+
childKind: child.kind
207+
)
208+
let result = visit(
209+
.init(
210+
node: child,
211+
byteOffset: byteOffset,
212+
contextualClassification: classficiation ?? descriptor.contextualClassification
213+
)
214+
)
194215
if result == .break {
195216
return .break
196217
}

Sources/SwiftCompilerSupport/ConsistencyCheck.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ extension Syntax {
4141
@_cdecl("swift_parser_consistencyCheck")
4242
@_spi(SwiftCompiler)
4343
public func _parserConsistencyCheck(
44-
bufferPtr: UnsafePointer<UInt8>, bufferLength: Int,
45-
filename: UnsafePointer<UInt8>, flags: CUnsignedInt,
44+
bufferPtr: UnsafePointer<UInt8>,
45+
bufferLength: Int,
46+
filename: UnsafePointer<UInt8>,
47+
flags: CUnsignedInt,
4648
hookCtx: OpaquePointer,
4749
diagnosticHook: @convention(c) (Int, UnsafePointer<Int8>, OpaquePointer) -> Void
4850
) -> CInt {
4951
let buffer = UnsafeBufferPointer<UInt8>(
50-
start: bufferPtr, count: bufferLength)
52+
start: bufferPtr,
53+
count: bufferLength
54+
)
5155
var parser = Parser(buffer)
5256
return withExtendedLifetime(parser) { () -> CInt in
5357
// Parse the source file
@@ -57,7 +61,8 @@ public func _parserConsistencyCheck(
5761
if flags & 0x01 != 0 {
5862
if sourceFile.syntaxTextBytes != [UInt8](buffer) {
5963
print(
60-
"\(String(cString: filename)): error: file failed to round-trip")
64+
"\(String(cString: filename)): error: file failed to round-trip"
65+
)
6166
return 1
6267
}
6368
}
@@ -67,7 +72,8 @@ public func _parserConsistencyCheck(
6772
var anyDiags = false
6873

6974
let diags = ParseDiagnosticsGenerator.diagnostics(
70-
for: sourceFile)
75+
for: sourceFile
76+
)
7177
for diag in diags {
7278
// Skip over diagnostics within #if, because we don't know whether
7379
// we are in an active region or not.

Sources/SwiftDiagnostics/Diagnostic.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,3 @@ public struct Diagnostic: CustomDebugStringConvertible {
7171
return "\(location): \(message)"
7272
}
7373
}
74-

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@
1313
import SwiftSyntax
1414

1515
public struct DiagnosticsFormatter {
16-
16+
1717
/// A wrapper struct for a source line and its diagnostics
1818
private struct AnnotatedSourceLine {
1919
var diagnostics: [Diagnostic]
2020
var sourceString: String
2121
}
22-
22+
2323
/// Number of lines which should be printed before and after the diagnostic message
2424
static let contextSize = 2
25-
25+
2626
/// Print given diagnostics for a given syntax tree on the command line
2727
public static func annotatedSource<SyntaxType: SyntaxProtocol>(tree: SyntaxType, diags: [Diagnostic]) -> String {
2828
let slc = SourceLocationConverter(file: "", tree: tree)
29-
29+
3030
// First, we need to put each line and its diagnostics together
3131
var annotatedSourceLines = [AnnotatedSourceLine]()
32-
32+
3333
for (sourceLineIndex, sourceLine) in slc.sourceLines.enumerated() {
3434
let diagsForLine = diags.filter { diag in
3535
return diag.location(converter: slc).line == (sourceLineIndex + 1)
3636
}
3737
annotatedSourceLines.append(AnnotatedSourceLine(diagnostics: diagsForLine, sourceString: sourceLine))
3838
}
39-
39+
4040
// Only lines with diagnostic messages should be printed, but including some context
4141
let rangesToPrint = annotatedSourceLines.enumerated().compactMap { (lineIndex, sourceLine) -> Range<Int>? in
4242
let lineNumber = lineIndex + 1
@@ -45,50 +45,52 @@ public struct DiagnosticsFormatter {
4545
}
4646
return nil
4747
}
48-
48+
4949
var annotatedSource = ""
50-
50+
5151
/// Keep track if a line missing char should be printed
5252
var hasLineBeenSkipped = false
53-
53+
5454
let maxNumberOfDigits = String(annotatedSourceLines.count).count
55-
55+
5656
for (lineIndex, annotatedLine) in annotatedSourceLines.enumerated() {
5757
let lineNumber = lineIndex + 1
58-
guard rangesToPrint.contains(where: { range in
59-
range.contains(lineNumber)
60-
}) else {
58+
guard
59+
rangesToPrint.contains(where: { range in
60+
range.contains(lineNumber)
61+
})
62+
else {
6163
hasLineBeenSkipped = true
6264
continue
6365
}
64-
66+
6567
// line numbers should be right aligned
6668
let lineNumberString = String(lineNumber)
6769
let leadingSpaces = String(repeating: " ", count: maxNumberOfDigits - lineNumberString.count)
6870
let linePrefix = "\(leadingSpaces)\(lineNumberString)"
69-
71+
7072
// If necessary, print a line that indicates that there was lines skipped in the source code
7173
if hasLineBeenSkipped && !annotatedSource.isEmpty {
7274
let lineMissingInfoLine = String(repeating: " ", count: maxNumberOfDigits) + ""
7375
annotatedSource.append("\(lineMissingInfoLine)\n")
7476
}
7577
hasLineBeenSkipped = false
76-
78+
7779
// print the source line
7880
annotatedSource.append("\(linePrefix)\(annotatedLine.sourceString)")
79-
81+
8082
// If the line did not end with \n (e.g. the last line), append it manually
8183
if annotatedSource.last != "\n" {
8284
annotatedSource.append("\n")
8385
}
84-
86+
8587
let columnsWithDiagnostics = Set(annotatedLine.diagnostics.map { $0.location(converter: slc).column ?? 0 })
8688
let diagsPerColumn = Dictionary(grouping: annotatedLine.diagnostics) { diag in
8789
diag.location(converter: slc).column ?? 0
8890
}.sorted { lhs, rhs in
8991
lhs.key > rhs.key
9092
}
91-
93+
9294
for (column, diags) in diagsPerColumn {
9395
// compute the string that is shown before each message
9496
var preMessage = String(repeating: " ", count: maxNumberOfDigits) + ""
@@ -99,12 +101,12 @@ public struct DiagnosticsFormatter {
99101
preMessage.append(" ")
100102
}
101103
}
102-
104+
103105
for diag in diags.dropLast(1) {
104106
annotatedSource.append("\(preMessage)├─ \(diag.message)\n")
105107
}
106108
annotatedSource.append("\(preMessage)╰─ \(diags.last!.message)\n")
107-
109+
108110
}
109111
}
110112
return annotatedSource

Sources/SwiftDiagnostics/FixIt.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public protocol FixItMessage {
2323
var fixItID: MessageID { get }
2424
}
2525

26-
2726
/// A Fix-It that can be applied to resolve a diagnostic.
2827
public struct FixIt {
2928
public struct Changes: ExpressibleByArrayLiteral {
@@ -63,4 +62,3 @@ public struct FixIt {
6362
self.changes = changes
6463
}
6564
}
66-

Sources/SwiftDiagnostics/Message.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
1413
/// An identifier that identifies a diagnostic message's type.
1514
/// Fundamentally different diagnostics should have a different `diagnosticID`
1615
/// so that clients may filter/prioritise/highlight/... certain diagnostics.

Sources/SwiftDiagnostics/Note.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,3 @@ public struct Note: CustomDebugStringConvertible {
6565
}
6666
}
6767
}
68-

Sources/SwiftOperators/Operator.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public struct Operator {
3838
public let syntax: OperatorDeclSyntax?
3939

4040
public init(
41-
kind: OperatorKind, name: OperatorName,
41+
kind: OperatorKind,
42+
name: OperatorName,
4243
precedenceGroup: PrecedenceGroupName? = nil,
4344
syntax: OperatorDeclSyntax? = nil
4445
) {

0 commit comments

Comments
 (0)