diff --git a/src/main/scala/de/heikoseeberger/sbtheader/HeaderPlugin.scala b/src/main/scala/de/heikoseeberger/sbtheader/HeaderPlugin.scala index d747ae2..900a1f2 100644 --- a/src/main/scala/de/heikoseeberger/sbtheader/HeaderPlugin.scala +++ b/src/main/scala/de/heikoseeberger/sbtheader/HeaderPlugin.scala @@ -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 diff --git a/src/test/scala/de/heikoseeberger/sbtheader/CommentStyleSpec.scala b/src/test/scala/de/heikoseeberger/sbtheader/CommentStyleSpec.scala index b03f044..618e7d2 100644 --- a/src/test/scala/de/heikoseeberger/sbtheader/CommentStyleSpec.scala +++ b/src/test/scala/de/heikoseeberger/sbtheader/CommentStyleSpec.scala @@ -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 { @@ -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 { diff --git a/src/test/scala/de/heikoseeberger/sbtheader/HeaderCreatorSpec.scala b/src/test/scala/de/heikoseeberger/sbtheader/HeaderCreatorSpec.scala index 84f3041..e628c5c 100644 --- a/src/test/scala/de/heikoseeberger/sbtheader/HeaderCreatorSpec.scala +++ b/src/test/scala/de/heikoseeberger/sbtheader/HeaderCreatorSpec.scala @@ -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 | */ @@ -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 + |This is the file content + |""".stripMargin + + createHeader( + fileContent = fileContent, + header = "Copyright 2017 MyCorp, Inc ", + commentCreator = CommentStyle.cppStyleLineComment, + headerEmptyLine = false + ) shouldBe None + } + + "preserve the file with hash style without the empty line" in { + val fileContent = + """# Copyright 2017 MyCorp, Inc + |This is the file content + |""".stripMargin + + createHeader( + fileContent = fileContent, + header = "Copyright 2017 MyCorp, Inc ", + commentCreator = CommentStyle.hashLineComment, + headerEmptyLine = false + ) shouldBe None + } } }