Skip to content

Commit caf686c

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

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,64 @@ 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}
314+
\u{0020}\u{0020}quxx\u{0020}\u{0020}
315+
\u{0020}\u{0020}*/\u{0020}\u{0020}
316+
\u{0020}\u{0020}func foo() {}
317+
}
318+
"""
319+
320+
let expected =
321+
"""
322+
struct Foo {
323+
\u{0020}\u{0020}/**
324+
\u{0020}\u{0020}foo bar baz
325+
326+
\u{0020}\u{0020}quxx
327+
\u{0020}\u{0020}*/
328+
\u{0020}\u{0020}func foo() {}
329+
}
330+
331+
"""
332+
var config = Configuration.forTesting
333+
config.indentBlankLines = false
334+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
335+
}
336+
337+
func testBlockCommentWhenIndentBlankLinesEnabled() {
338+
let input =
339+
"""
340+
struct Foo {
341+
\u{0020}\u{0020}/**\u{0020}\u{0020}
342+
\u{0020}\u{0020}foo bar baz\u{0020}\u{0020}
343+
\u{0020}\u{0020}
344+
\u{0020}\u{0020}quxx\u{0020}\u{0020}
345+
\u{0020}\u{0020}*/\u{0020}\u{0020}
346+
\u{0020}\u{0020}func foo() {}
347+
}
348+
"""
349+
350+
let expected =
351+
"""
352+
struct Foo {
353+
\u{0020}\u{0020}/**
354+
\u{0020}\u{0020}foo bar baz
355+
\u{0020}\u{0020}
356+
\u{0020}\u{0020}quxx
357+
\u{0020}\u{0020}*/
358+
\u{0020}\u{0020}func foo() {}
359+
}
360+
361+
"""
362+
var config = Configuration.forTesting
363+
config.indentBlankLines = true
364+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
365+
}
306366
}

0 commit comments

Comments
 (0)