Skip to content

Commit 9e40299

Browse files
committed
Respect indentBlankLines when stripping whitespace inside block comment blank lines
1 parent 83d46d6 commit 9e40299

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

Sources/SwiftFormat/PrettyPrint/Comment.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ struct Comment {
102102
}
103103
}
104104

105-
func print(indent: [Indent]) -> String {
105+
func print(indent: [Indent], shouldIndentBlankLines: Bool = true) -> String {
106106
switch self.kind {
107107
case .line, .docLine:
108108
let separator = "\n" + indent.indentation() + kind.prefix
@@ -121,9 +121,13 @@ struct Comment {
121121
return result
122122
}
123123
if hasLeading, let first = self.text.first, !rest.isEmpty {
124+
let indentation = indent.indentation()
124125
let restStr = rest.map {
126+
guard !$0.isEmpty else {
127+
return shouldIndentBlankLines ? indentation : ""
128+
}
125129
let stripped = $0.dropFirst(leadingIndent.text.count)
126-
return indent.indentation() + stripped
130+
return indentation + stripped
127131
}.joined(separator: separator)
128132
return kind.prefix + first + separator + restStr + "*/"
129133
}

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,12 @@ public class PrettyPrinter {
484484
diagnose(.moveEndOfLineComment, category: .endOfLineComment)
485485
}
486486
}
487-
outputBuffer.write(comment.print(indent: currentIndentation))
487+
outputBuffer.write(
488+
comment.print(
489+
indent: currentIndentation,
490+
shouldIndentBlankLines: configuration.indentBlankLines
491+
)
492+
)
488493

489494
case .verbatim(let verbatim):
490495
outputBuffer.writeVerbatim(verbatim.print(indent: currentIndentation), length)

Tests/SwiftFormatTests/PrettyPrint/IndentBlankLinesTests.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,72 @@ final class IndentBlankLinesTests: PrettyPrintTestCase {
303303
config.indentBlankLines = true
304304
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
305305
}
306+
307+
func testBlockCommentWhenIndentBlankLinesDisabled() {
308+
let input =
309+
"""
310+
struct Foo {
311+
\u{0020}\u{0020}/**\u{0020}\u{0020}
312+
\u{0020}\u{0020}foo bar baz\u{0020}\u{0020}
313+
\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}
314+
\u{0020}\u{0020}quxx\u{0020}\u{0020}
315+
\u{0020}\u{0020}
316+
317+
\u{0020}\u{0020}*/\u{0020}\u{0020}
318+
\u{0020}\u{0020}func foo() {}
319+
}
320+
"""
321+
322+
let expected =
323+
"""
324+
struct Foo {
325+
\u{0020}\u{0020}/**
326+
\u{0020}\u{0020}foo bar baz
327+
328+
\u{0020}\u{0020}quxx
329+
330+
331+
\u{0020}\u{0020}*/
332+
\u{0020}\u{0020}func foo() {}
333+
}
334+
335+
"""
336+
var config = Configuration.forTesting
337+
config.indentBlankLines = false
338+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
339+
}
340+
341+
func testBlockCommentWhenIndentBlankLinesEnabled() {
342+
let input =
343+
"""
344+
struct Foo {
345+
\u{0020}\u{0020}/**\u{0020}\u{0020}
346+
\u{0020}\u{0020}foo bar baz\u{0020}\u{0020}
347+
\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}
348+
\u{0020}\u{0020}quxx\u{0020}\u{0020}
349+
\u{0020}\u{0020}
350+
351+
\u{0020}\u{0020}*/\u{0020}\u{0020}
352+
\u{0020}\u{0020}func foo() {}
353+
}
354+
"""
355+
356+
let expected =
357+
"""
358+
struct Foo {
359+
\u{0020}\u{0020}/**
360+
\u{0020}\u{0020}foo bar baz
361+
\u{0020}\u{0020}
362+
\u{0020}\u{0020}quxx
363+
\u{0020}\u{0020}
364+
\u{0020}\u{0020}
365+
\u{0020}\u{0020}*/
366+
\u{0020}\u{0020}func foo() {}
367+
}
368+
369+
"""
370+
var config = Configuration.forTesting
371+
config.indentBlankLines = true
372+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
373+
}
306374
}

0 commit comments

Comments
 (0)