Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix C++ and Hash styles with headerEmptyLine := false #314

Merged
merged 1 commit into from
Jun 7, 2023
Merged
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
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