Skip to content

Commit 290a28c

Browse files
authored
Merge pull request swiftlang#190 from google/format-fix-type-decls
Fix type decls containing only a comment.
2 parents e6d8220 + f06712a commit 290a28c

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,14 @@ private final class TokenStreamCreator: SyntaxVisitor {
171171
before(members.leftBrace, tokens: .break)
172172
}
173173

174-
if !members.members.isEmpty {
175-
after(
176-
members.leftBrace,
177-
tokens: .close, .close, .break(size: 1, offset: 2), .open(.consistent, 0)
178-
)
179-
before(members.rightBrace, tokens: .break(size: 1, offset: -2), .close)
174+
// The body may be free of other syntax nodes, but we still need to insert the breaks if it
175+
// contains a comment (which will be in the leading trivia of the right brace).
176+
let commentPrecedesRightBrace = members.rightBrace.leadingTrivia.numberOfComments > 0
177+
let isBodyCompletelyEmpty = members.members.isEmpty && !commentPrecedesRightBrace
178+
179+
if !isBodyCompletelyEmpty {
180+
after(members.leftBrace, tokens: .close, .close, .break(offset: 2), .open(.consistent, 0))
181+
before(members.rightBrace, tokens: .break(offset: -2), .close)
180182
} else {
181183
// The size-0 break in the empty case allows for a break between the braces in the rare event
182184
// that the declaration would be exactly the column limit + 1.

Tests/SwiftFormatPrettyPrintTests/ClassDeclTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ public class ClassDeclTests: PrettyPrintTestCase {
291291
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 11)
292292
}
293293

294+
public func testEmptyClassWithComment() {
295+
let input = """
296+
class Foo {
297+
// foo
298+
}
299+
"""
300+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)
301+
}
302+
294303
public func testOneMemberClass() {
295304
let input = "class Foo { var bar: Int }"
296305
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)

Tests/SwiftFormatPrettyPrintTests/EnumDeclTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,15 @@ public class EnumDeclTests: PrettyPrintTestCase {
370370
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 10)
371371
}
372372

373+
public func testEmptyEnumWithComment() {
374+
let input = """
375+
enum Foo {
376+
// foo
377+
}
378+
"""
379+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)
380+
}
381+
373382
public func testOneMemberEnum() {
374383
let input = "enum Foo { var bar: Int }"
375384
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)

Tests/SwiftFormatPrettyPrintTests/ExtensionDeclTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ public class ExtensionDeclTests: PrettyPrintTestCase {
246246
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 15)
247247
}
248248

249+
public func testEmptyExtensionWithComment() {
250+
let input = """
251+
extension Foo {
252+
// foo
253+
}
254+
"""
255+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)
256+
}
257+
249258
public func testOneMemberExtension() {
250259
let input = "extension Foo { var bar: Int { return 0 } }"
251260
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)

Tests/SwiftFormatPrettyPrintTests/ProtocolDeclTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ public class ProtocolDeclTests: PrettyPrintTestCase {
239239
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 14)
240240
}
241241

242+
public func testEmptyProtocolWithComment() {
243+
let input = """
244+
protocol Foo {
245+
// foo
246+
}
247+
"""
248+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)
249+
}
250+
242251
public func testOneMemberProtocol() {
243252
let input = "protocol Foo { var bar: Int { get } }"
244253
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)

Tests/SwiftFormatPrettyPrintTests/StructDeclTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ public class StructDeclTests: PrettyPrintTestCase {
291291
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 12)
292292
}
293293

294+
public func testEmptyStructWithComment() {
295+
let input = """
296+
struct Foo {
297+
// foo
298+
}
299+
"""
300+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)
301+
}
302+
294303
public func testOneMemberStruct() {
295304
let input = "struct Foo { var bar: Int }"
296305
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)

0 commit comments

Comments
 (0)