From 304c80d1c7083ae8a8fda888d1510de0a22422e1 Mon Sep 17 00:00:00 2001 From: Liam Nichols Date: Tue, 26 Sep 2023 22:57:11 +0200 Subject: [PATCH] Generate multiline comments properly (Closes #11) --- Sources/StringGenerator/StringGenerator.swift | 17 ++++++++--------- .../StringGeneratorTests.swift | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Sources/StringGenerator/StringGenerator.swift b/Sources/StringGenerator/StringGenerator.swift index 9acc56d..4949946 100644 --- a/Sources/StringGenerator/StringGenerator.swift +++ b/Sources/StringGenerator/StringGenerator.swift @@ -252,17 +252,16 @@ extension Resource { } var leadingTrivia: Trivia { - let commentTrivia: [TriviaPiece] = if let comment { - comment - .components(separatedBy: .newlines) - .map { TriviaPiece.docLineComment("/// \($0)") } - } else { - [] + var trivia: Trivia = .newlines(2) + + if let commentLines = comment?.components(separatedBy: .newlines), !commentLines.isEmpty { + for line in commentLines { + trivia = trivia.appending(Trivia.docLineComment("/// \(line)")) + trivia = trivia.appending(.newline) + } } - return .newlines(2) - .merging(Trivia(pieces: commentTrivia)) - .merging(.newline) + return trivia } func statements(table: String, bundle: TokenSyntax) -> CodeBlockItemListSyntax { diff --git a/Tests/LocalizationTests/StringGeneratorTests.swift b/Tests/LocalizationTests/StringGeneratorTests.swift index 34b6ed1..1af5684 100644 --- a/Tests/LocalizationTests/StringGeneratorTests.swift +++ b/Tests/LocalizationTests/StringGeneratorTests.swift @@ -17,6 +17,20 @@ final class StringGeneratorTests: XCTestCase { XCTAssertNotNil(output) } + func testGenerateMultilineComment() { + let output = generate([ + Resource( + key: "foo", + comment: "A comment\n\nA comment that covers many lines.", + identifier: "foo", + arguments: [], + defaultValue: [.string("FOO")] + ) + ]) + + XCTAssertNotNil(output) + } + private func generate( _ resources: [Resource], tableName: String = "Localizable",