Skip to content

Commit

Permalink
Fix C++ and Hash styles with headerEmptyLine := false (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihostage authored Jun 7, 2023
1 parent fe9933d commit 3d99317
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object HeaderPlugin extends AutoPlugin {
raw"""(?s)($start(?!\$middle).*?$end(?:\n|\r|\r\n)+)(.*)""".r

def commentStartingWith(start: String): Regex =
raw"""(?s)((?:$start[^\n\r]*(?:\n|\r|\r\n))*(?:$start[^\n\r]*(?:(?:\n){2,}|(?:\r){2,}|(?:\r\n){2,})+))(.*)""".r
raw"""(?s)((?:$start[^\n\r]*(?:\n|\r|\r\n))*(?:$start[^\n\r]*(?:(?:\n)+|(?:\r)+|(?:\r\n)+)+))(.*)""".r
}

val HeaderFileType = FileType
Expand Down
20 changes: 9 additions & 11 deletions src/test/scala/de/heikoseeberger/sbtheader/CommentStyleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,10 @@ class CommentStyleSpec extends AnyWordSpec with Matchers {
}

"not match a multiline block comment with a single trailing new line" in {
cppStyleLineComment.pattern.unapplySeq(
"""|// comment/1
|// comment/2
|""".stripMargin
) shouldBe None
val header = """|// comment/1
|// comment/2
|""".stripMargin
cppStyleLineComment.pattern.unapplySeq(header) shouldBe Some(List(header, ""))
}

"match a comment with trailing new lines not followed by a body" in {
Expand Down Expand Up @@ -161,12 +160,11 @@ class CommentStyleSpec extends AnyWordSpec with Matchers {
hashLineComment.pattern.unapplySeq("# comment") shouldBe None
}

"not match a multiline block comment with a single trailing new line" in {
hashLineComment.pattern.unapplySeq(
"""|# comment/1
|# comment/2
|""".stripMargin
) shouldBe None
"match a multiline block comment with a single trailing new line" in {
val header = """|# comment/1
|# comment/2
|""".stripMargin
hashLineComment.pattern.unapplySeq(header) shouldBe Some(List(header, ""))
}

"match a comment with trailing new lines not followed by a body" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ final class HeaderCreatorSpec extends AnyWordSpec with Matchers {
}

"given a file without an empty line between the header and the body" should {
"preserve the file without the empty line" in {
"preserve the file with C style without the empty line" in {
val fileContent = """/*
| * Copyright 2017 MyCorp, Inc <https://mycorp.com>
| */
Expand All @@ -235,6 +235,34 @@ final class HeaderCreatorSpec extends AnyWordSpec with Matchers {
headerEmptyLine = false
) shouldBe None
}

"preserve the file with C++ style without the empty line" in {
val fileContent =
"""// Copyright 2017 MyCorp, Inc <https://mycorp.com>
|This is the file content
|""".stripMargin

createHeader(
fileContent = fileContent,
header = "Copyright 2017 MyCorp, Inc <https://mycorp.com>",
commentCreator = CommentStyle.cppStyleLineComment,
headerEmptyLine = false
) shouldBe None
}

"preserve the file with hash style without the empty line" in {
val fileContent =
"""# Copyright 2017 MyCorp, Inc <https://mycorp.com>
|This is the file content
|""".stripMargin

createHeader(
fileContent = fileContent,
header = "Copyright 2017 MyCorp, Inc <https://mycorp.com>",
commentCreator = CommentStyle.hashLineComment,
headerEmptyLine = false
) shouldBe None
}
}
}

Expand Down

0 comments on commit 3d99317

Please sign in to comment.