Skip to content

Commit

Permalink
Adopt AddPropertyComment in CommentOutSpringPropertyKey
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Nov 27, 2024
1 parent dee8ae2 commit 03bf6f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
String inlineComment = " # " + comment;
String regex = "(?<!" + inlineComment + ")$";
Recipe changeProperties = new org.openrewrite.properties.ChangePropertyValue(propertyKey, inlineComment, regex, true, null);
Recipe changeProperties = new org.openrewrite.properties.AddPropertyComment(propertyKey, comment, true);
Recipe changeYaml = new org.openrewrite.yaml.CommentOutProperty(propertyKey, comment) ;
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void yamlComment() {
}

@Test
void shouldInsertInlineCommentsIntoProperties() {
void multipleYamlComments() {
rewriteRun(
spec -> spec.recipes(
new CommentOutSpringPropertyKey("test.propertyKey1", "my comment 1"),
Expand Down Expand Up @@ -84,6 +84,16 @@ void shouldInsertInlineCommentsIntoProperties() {
"""
)
)
)
);
}

@Test
void multiplePropertiesComments() {
rewriteRun(
spec -> spec.recipes(
new CommentOutSpringPropertyKey("test.propertyKey1", "my comment 1"),
new CommentOutSpringPropertyKey("test.propertyKey2", "my comment 2")
),
//language=properties
properties(
Expand All @@ -93,18 +103,16 @@ void shouldInsertInlineCommentsIntoProperties() {
test.propertyKey3=zzz
""",
"""
test.propertyKey1=xxx # my comment 1
test.propertyKey2=yyy # my comment 2
# my comment 1
# test.propertyKey1=xxx
# my comment 2
# test.propertyKey2=yyy
test.propertyKey3=zzz
""",
spec -> spec.path("application.properties")
.afterRecipe(file ->
// XXX Right now trailing comments are mapped as part of the value, not as separate comments
assertThat(
((Properties.Entry) file.getContent().get(1))
.getValue().getText())
.isEqualTo("yyy # my comment 2")
)
assertThat(((Properties.Comment) file.getContent().get(3)).getMessage())
.isEqualTo(" test.propertyKey2=yyy"))
)
);
}
Expand Down

0 comments on commit 03bf6f1

Please sign in to comment.