Skip to content

Commit 1afa185

Browse files
authored
Merge pull request swiftlang#195 from dabelknap/assorted-fixes
Fix '(set)' keyword, trailing array comments, and verbatim indentation
2 parents e085ee9 + 16ac305 commit 1afa185

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
10531053
}
10541054

10551055
override func visit(_ node: DeclModifierSyntax) {
1056-
after(node.name, tokens: .break)
1056+
after(node.lastToken, tokens: .break)
10571057
super.visit(node)
10581058
}
10591059

@@ -1531,7 +1531,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
15311531
appendToken(.break(size: 2, offset: 0))
15321532
appendToken(.comment(commentToken, wasEndOfLine: true))
15331533

1534-
if nextToken != nil, ["}", ")"].contains(nextToken?.withoutTrivia().text), trivia.numberOfComments == 1 {
1534+
if nextToken != nil, ["}", ")", "]"].contains(nextToken?.withoutTrivia().text), trivia.numberOfComments == 1 {
15351535
appendToken(.break(size: maxlinelength, offset: -2))
15361536
} else {
15371537
appendToken(.break(size: maxlinelength))

Sources/SwiftFormatPrettyPrint/Verbatim.swift

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,23 @@ struct Verbatim {
2828
lines.remove(at: 0)
2929
}
3030

31-
guard lines.count > 0 else { return }
31+
guard lines.count > 0, let index = lines.firstIndex(where: { $0 != "" }) else { return }
3232

3333
// Get the number of leading whitespaces of the first line, and subract this from the number of
3434
// leading whitespaces for subsequent lines (if possible). Record the new leading whitespaces
3535
// counts, and trim off whitespace from the ends of the strings.
36-
let count = countLeadingWhitespaces(text: lines[0])
36+
let count = countLeadingWhitespaces(text: lines[index])
3737
leadingWhitespaceCounts = lines.map { max(countLeadingWhitespaces(text: $0) - count, 0) }
3838
lines = lines.map { $0.trimmingCharacters(in: CharacterSet(charactersIn: " ")) }
3939
}
4040

4141
func print(indent: Int) -> String {
4242
var output = ""
4343
for i in 0..<lines.count {
44-
output += String(repeating: " ", count: indent + leadingWhitespaceCounts[i])
45-
output += lines[i]
44+
if lines[i] != "" {
45+
output += String(repeating: " ", count: indent + leadingWhitespaceCounts[i])
46+
output += lines[i]
47+
}
4648
if i < lines.count - 1 {
4749
output += "\n"
4850
}

Tests/SwiftFormatPrettyPrintTests/AccessorTests.swift

+13
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,17 @@ public class AccessorTests: PrettyPrintTestCase {
144144
"""
145145
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)
146146
}
147+
148+
public func testSetModifier() {
149+
let input =
150+
"""
151+
fileprivate(set) var somevar = 0
152+
struct MyStruct {
153+
private(set) var myvar = 0
154+
internal(set) var anothervar = 0
155+
}
156+
"""
157+
158+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)
159+
}
147160
}

Tests/SwiftFormatPrettyPrintTests/CommentTests.swift

+10
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ public class CommentTests: PrettyPrintTestCase {
199199
let b = ["abc": 456, // small comment
200200
"def": 789]
201201
202+
// Trailing comment
203+
let c = [123, 456 // small comment
204+
]
205+
202206
/* Array comment */
203207
let a = [456, /* small comment */
204208
789]
@@ -222,6 +226,12 @@ public class CommentTests: PrettyPrintTestCase {
222226
"def": 789
223227
]
224228
229+
// Trailing comment
230+
let c = [
231+
123,
232+
456 // small comment
233+
]
234+
225235
/* Array comment */
226236
let a = [
227237
456, /* small comment */

Tests/SwiftFormatPrettyPrintTests/UnknownStmtTests.swift

+19
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ public class UnknownStmtTests: PrettyPrintTestCase {
4444
}
4545
#endif
4646
47+
func myfun() {
48+
49+
if #available(OSX 10.12, *) {
50+
51+
let a = 123
52+
} else {
53+
// do stuff
54+
}
55+
}
4756
"""
4857

4958
let expected =
@@ -87,6 +96,16 @@ public class UnknownStmtTests: PrettyPrintTestCase {
8796
}
8897
#endif
8998
99+
func myfun() {
100+
101+
if #available(OSX 10.12, *) {
102+
103+
let a = 123
104+
} else {
105+
// do stuff
106+
}
107+
}
108+
90109
"""
91110

92111
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)

0 commit comments

Comments
 (0)