Skip to content

Commit 602585b

Browse files
authored
Merge pull request swiftlang#232 from google/format-swift5
Update to the Swift 5.0 version of SwiftSyntax.
2 parents 2558d7c + 22d6fad commit 602585b

35 files changed

+682
-570
lines changed

Package.resolved

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
"repositoryURL": "https://github.com/apple/swift-package-manager.git",
77
"state": {
88
"branch": null,
9-
"revision": "6983434787dec4e543e9d398a0a9acf63ccd4da1",
10-
"version": "0.2.1"
9+
"revision": "235aacc514cb81a6881364b0fedcb3dd083228f3",
10+
"version": "0.3.0"
1111
}
1212
},
1313
{
1414
"package": "SwiftSyntax",
1515
"repositoryURL": "https://github.com/apple/swift-syntax",
1616
"state": {
1717
"branch": null,
18-
"revision": "f20c0bcbfb4b07e107c9851350217cb2217f0cb4",
19-
"version": "0.40200.0"
18+
"revision": "43aa4a19b8105a803d8149ad2a86aa53a77efef3",
19+
"version": "0.50000.0"
2020
}
2121
}
2222
]

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let package = Package(
2323
],
2424
dependencies: [
2525
.package(url: "https://github.com/apple/swift-package-manager.git", from: "0.1.0"),
26-
.package(url: "https://github.com/apple/swift-syntax", from: "0.40200.0"),
26+
.package(url: "https://github.com/apple/swift-syntax", from: "0.50000.0"),
2727
],
2828
targets: [
2929
.target(

Sources/SwiftFormat/SwiftLinter.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public final class SwiftLinter {
7171
let pipeline = LintPipeline(context: context)
7272
populate(pipeline)
7373

74-
pipeline.visit(syntax as Syntax)
74+
syntax.walk(pipeline)
7575

7676
// Perform whitespace linting by comparing the input source text with the output of the
7777
// pretty-printer.

Sources/SwiftFormatCore/LintPipeline.swift

+14-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import SwiftSyntax
1414

1515
/// Manages a registry of lint rules to apply to specific kinds of nodes.
1616
public final class LintPipeline: SyntaxVisitor, Pipeline {
17+
private enum Pass {
18+
case linter(SyntaxLintRule)
19+
case formatter(SyntaxFormatRule)
20+
}
21+
1722
private let context: Context
1823

1924
/// Creates a Pipeline with the provided Context and Mode.
@@ -27,7 +32,7 @@ public final class LintPipeline: SyntaxVisitor, Pipeline {
2732
private var fileRules = [FileRule]()
2833

2934
/// A mapping between syntax types and closures that will perform linting operations over them.
30-
private var passMap = [SyntaxType: [(Syntax) -> Void]]()
35+
private var passMap = [SyntaxType: [Pass]]()
3136

3237
/// Adds a file-based rule to be run before format rules.
3338
///
@@ -46,7 +51,7 @@ public final class LintPipeline: SyntaxVisitor, Pipeline {
4651
public func addLinter(_ lintRule: SyntaxLintRule.Type, for syntaxTypes: Syntax.Type...) {
4752
for type in syntaxTypes {
4853
let rule = lintRule.init(context: context)
49-
passMap[SyntaxType(type: type), default: []].append(rule.visit)
54+
passMap[SyntaxType(type: type), default: []].append(.linter(rule))
5055
}
5156
}
5257

@@ -59,17 +64,20 @@ public final class LintPipeline: SyntaxVisitor, Pipeline {
5964
public func addFormatter(_ formatRule: SyntaxFormatRule.Type, for syntaxTypes: Syntax.Type...) {
6065
for type in syntaxTypes {
6166
let rule = formatRule.init(context: context)
62-
passMap[SyntaxType(type: type), default: []].append {
63-
_ = rule.visit($0)
64-
}
67+
passMap[SyntaxType(type: type), default: []].append(.formatter(rule))
6568
}
6669
}
6770

6871
public override func visitPre(_ node: Syntax) {
6972
let syntaxType = SyntaxType(type: type(of: node))
7073
guard let passes = passMap[syntaxType] else { return }
7174
for pass in passes {
72-
pass(node)
75+
switch pass {
76+
case .linter(let visitor):
77+
node.walk(visitor)
78+
case .formatter(let rewriter):
79+
_ = rewriter.visit(node)
80+
}
7381
}
7482
}
7583
}

Sources/SwiftFormatCore/Syntax+Convenience.swift

+47
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,51 @@ extension Syntax {
103103
}
104104
return nil
105105
}
106+
107+
/// Sequence of tokens that are part of this Syntax node.
108+
public var tokens: TokenSequence {
109+
return TokenSequence(self)
110+
}
111+
}
112+
113+
/// Sequence of tokens that are part of the provided Syntax node.
114+
public struct TokenSequence: Sequence {
115+
public struct Iterator: IteratorProtocol {
116+
var nextToken: TokenSyntax?
117+
let endPosition: AbsolutePosition
118+
119+
init(_ token: TokenSyntax?, endPosition: AbsolutePosition) {
120+
self.nextToken = token
121+
self.endPosition = endPosition
122+
}
123+
124+
public mutating func next() -> TokenSyntax? {
125+
guard let token = self.nextToken else { return nil }
126+
self.nextToken = token.nextToken
127+
// Make sure we stop once we reach the end of the containing node.
128+
if let nextTok = self.nextToken, nextTok.position >= self.endPosition {
129+
self.nextToken = nil
130+
}
131+
return token
132+
}
133+
}
134+
135+
let node: Syntax
136+
137+
public init(_ node: Syntax) {
138+
self.node = node
139+
}
140+
141+
public func makeIterator() -> Iterator {
142+
return Iterator(node.firstToken, endPosition: node.endPosition)
143+
}
144+
}
145+
146+
extension AbsolutePosition: Comparable {
147+
public static func <(lhs: AbsolutePosition, rhs: AbsolutePosition) -> Bool {
148+
return lhs.utf8Offset < rhs.utf8Offset
149+
}
150+
public static func ==(lhs: AbsolutePosition, rhs: AbsolutePosition) -> Bool {
151+
return lhs.utf8Offset == rhs.utf8Offset
152+
}
106153
}

0 commit comments

Comments
 (0)