Skip to content

Commit

Permalink
Fix bug generating code for comments with embedded newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Apr 17, 2024
1 parent 003d0f6 commit 53a5a7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/src/main/scala/generator/GeneratorUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,15 @@ object GeneratorUtil {
* leading indentation.
*/
def splitIntoLines(comment: String, maxLength: Int = 80): Seq[String] = {
comment.split("\\n").toSeq.flatMap { line =>
splitLineByLength(line, maxLength)
}
}

private[this] def splitLineByLength(comment: String, maxLength: Int = 80): Seq[String] = {
val sb = new scala.collection.mutable.ListBuffer[String]()
var currentWord = new StringBuilder()
comment.split(" ").map(_.trim).foreach { word =>
comment.split("\\s+").map(_.trim).foreach { word =>
if (word.length + currentWord.length >= maxLength) {
if (currentWord.nonEmpty) {
sb.append(currentWord.toString)
Expand Down
8 changes: 8 additions & 0 deletions lib/src/test/scala/generator/GeneratorUtilSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import org.scalatest.matchers.should.Matchers

class GeneratorUtilSpec extends AnyFunSpec with Matchers {

it("formatComment handles newlines") {
GeneratorUtil.formatComment("test\nthis") should be("""
# test
# this
""".trim
)
}

it("formatComment") {
GeneratorUtil.formatComment("test") should be("# test")
GeneratorUtil.formatComment("test this") should be("# test this")
Expand Down

0 comments on commit 53a5a7c

Please sign in to comment.