Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/SwiftFormat/PrettyPrint/Comment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct Comment {
}
}

func print(indent: [Indent]) -> String {
func print(indent: [Indent], shouldIndentBlankLines: Bool = true) -> String {
switch self.kind {
case .line, .docLine:
let separator = "\n" + indent.indentation() + kind.prefix
Expand All @@ -121,9 +121,13 @@ struct Comment {
return result
}
if hasLeading, let first = self.text.first, !rest.isEmpty {
let indentation = indent.indentation()
let restStr = rest.map {
guard !$0.isEmpty else {
return shouldIndentBlankLines ? indentation : ""
}
let stripped = $0.dropFirst(leadingIndent.text.count)
return indent.indentation() + stripped
return indentation + stripped
}.joined(separator: separator)
return kind.prefix + first + separator + restStr + "*/"
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,12 @@ public class PrettyPrinter {
diagnose(.moveEndOfLineComment, category: .endOfLineComment)
}
}
outputBuffer.write(comment.print(indent: currentIndentation))
outputBuffer.write(
comment.print(
indent: currentIndentation,
shouldIndentBlankLines: configuration.indentBlankLines
)
)

case .verbatim(let verbatim):
outputBuffer.writeVerbatim(verbatim.print(indent: currentIndentation), length)
Expand Down
68 changes: 68 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/IndentBlankLinesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,72 @@ final class IndentBlankLinesTests: PrettyPrintTestCase {
config.indentBlankLines = true
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
}

func testBlockCommentWhenIndentBlankLinesDisabled() {
let input =
"""
struct Foo {
\u{0020}\u{0020}/**\u{0020}\u{0020}
\u{0020}\u{0020}foo bar baz\u{0020}\u{0020}
\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}
\u{0020}\u{0020}quxx\u{0020}\u{0020}
\u{0020}\u{0020}

\u{0020}\u{0020}*/\u{0020}\u{0020}
\u{0020}\u{0020}func foo() {}
}
"""

let expected =
"""
struct Foo {
\u{0020}\u{0020}/**
\u{0020}\u{0020}foo bar baz

\u{0020}\u{0020}quxx


\u{0020}\u{0020}*/
\u{0020}\u{0020}func foo() {}
}

"""
var config = Configuration.forTesting
config.indentBlankLines = false
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
}

func testBlockCommentWhenIndentBlankLinesEnabled() {
let input =
"""
struct Foo {
\u{0020}\u{0020}/**\u{0020}\u{0020}
\u{0020}\u{0020}foo bar baz\u{0020}\u{0020}
\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}
\u{0020}\u{0020}quxx\u{0020}\u{0020}
\u{0020}\u{0020}

\u{0020}\u{0020}*/\u{0020}\u{0020}
\u{0020}\u{0020}func foo() {}
}
"""

let expected =
"""
struct Foo {
\u{0020}\u{0020}/**
\u{0020}\u{0020}foo bar baz
\u{0020}\u{0020}
\u{0020}\u{0020}quxx
\u{0020}\u{0020}
\u{0020}\u{0020}
\u{0020}\u{0020}*/
\u{0020}\u{0020}func foo() {}
}

"""
var config = Configuration.forTesting
config.indentBlankLines = true
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
}
}
Loading