[copyright] update template so same year is not inserted twice (IDEA-276284)

GitOrigin-RevId: 84e97aa22bf0f01a981d59a2e195f747ec8b00b5
This commit is contained in:
Anna Kozlova
2021-08-18 12:22:48 +02:00
committed by intellij-monorepo-bot
parent 07650a9fbb
commit 853f84791d
3 changed files with 33 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.maddyhome.idea.copyright
import com.intellij.configurationStore.SerializableScheme
@@ -13,7 +13,7 @@ import org.jdom.Element
@JvmField
val DEFAULT_COPYRIGHT_NOTICE: String = EntityUtil.encode(
"Copyright (c) \$originalComment.match(\"Copyright \\(c\\) (\\d+)\", 1, \"-\")\$today.year. Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n" +
"Copyright (c) \$originalComment.match(\"Copyright \\(c\\) (\\d+)\", 1, \"-\", \"\$today.year\")\$today.year. Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n" +
"Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. \n" +
"Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. \n" +
"Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. \n" +

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.maddyhome.idea.copyright.pattern;
import com.intellij.openapi.util.text.StringUtil;
@@ -19,24 +19,31 @@ public class CommentInfo {
}
public String match(String regexp) {
return match(regexp, 1, "");
return match(regexp, 1, "", "");
}
public String match(String regexp, int group, String suffix) {
return match(regexp, group, suffix, "");
}
/**
* @param regexp regular expression to match created date in the existing comment
* @param group group, where date is expected to be found
* @param suffix if created date was detected, allows to append any postfix e.g. ` - ` so the date range looks nice
* @return empty string if created date was not found (it's a new comment or regexp didn't match anything in the comment), or
* created date from the existing comment followed by the <code>suffix</code>
* @param ignoreValue when this value matched in the old comment, then no update is required,
* e.g. to avoid 2020-2020 range; may contain other velocity variables
* @return empty string if created date was not found (it's a new comment or regexp didn't match anything in the comment)
* or it is equal to the {@code ignoreValue},
* otherwise, created date from the existing comment followed by the <code>suffix</code>
*/
public String match(String regexp, int group, String suffix) {
public String match(String regexp, int group, String suffix, String ignoreValue) {
if (myText == null) {
return "";
}
Matcher matcher = Pattern.compile(".*" + regexp + ".*", Pattern.DOTALL | Pattern.MULTILINE).matcher(myText);
if (matcher.matches()) {
String date = matcher.group(group);
if (!StringUtil.isEmpty(date)) {
if (!StringUtil.isEmpty(date) && !date.equals(ignoreValue)) {
return date + suffix;
}
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.copyright
import com.maddyhome.idea.copyright.pattern.DateInfo
@@ -7,7 +7,7 @@ import org.junit.Assert
import org.junit.Test
class CommentInfoTest {
val template = "Copyright (c) \$originalComment.match(\"Copyright \\(c\\) (\\d+)\", 1, \" - \")\$today.year. " +
val template = "Copyright (c) \$originalComment.match(\"Copyright \\(c\\) (\\d+)\", 1, \" - \", \"\$today.year\")\$today.year. " +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n"
@Test
@@ -32,4 +32,20 @@ class CommentInfoTest {
VelocityHelper.evaluate (null, null, null, template,
"Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n"))
}
@Test
fun withCommentOldYears() {
val dateInfo = DateInfo().year
Assert.assertEquals("Copyright (c) 2019 - $dateInfo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n",
VelocityHelper.evaluate (null, null, null, template,
"Copyright (c) 2019 - 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n"))
}
@Test
fun withCommentSameYear() {
val dateInfo = DateInfo().year
Assert.assertEquals("Copyright (c) $dateInfo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n",
VelocityHelper.evaluate (null, null, null, template,
"Copyright (c) $dateInfo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n"))
}
}