Skip to content

Commit

Permalink
More comment, more difficult
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Nov 19, 2024
1 parent 34b5f9d commit 1381e9f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,8 @@ private Mapping mergeMapping(Yaml.Mapping m1, Yaml.Mapping m2, P p, Cursor curso
}
}
// workaround: autoFormat cannot handle new inserted values very well
if (!mergedEntries.isEmpty() && it.getValue() instanceof Scalar && MergeYamlVisitor.this.hasLineBreak(mergedEntries.get(0), 2)) {
String prefix = MergeYamlVisitor.this.grabPartLineBreak(mergedEntries.get(0), 1);
String newValue = ((Scalar) it.getValue()).getValue().replaceAll("\\R", prefix + "\n");
return it.withPrefix("\n" + prefix)
.withValue(((Scalar) it.getValue()).withValue(newValue));
if (!mergedEntries.isEmpty() && it.getValue() instanceof Yaml.Scalar && hasLineBreak(mergedEntries.get(0), 2)) {
return it.withPrefix("\n" + grabPartLineBreak(mergedEntries.get(0), 1));
}
return shouldAutoFormat ? MergeYamlVisitor.this.autoFormat(it, p, cursor) : it;
}));
Expand Down
52 changes: 28 additions & 24 deletions rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ void existingEntryBlockWithCommentAtFirstLine() {
)),
yaml(
"""
A: # Some comment
A: # Comment untouched
B:
C:
D:
Expand All @@ -1049,7 +1049,7 @@ void existingEntryBlockWithCommentAtFirstLine() {
1: old text
""",
"""
A: # Some comment
A: # Comment untouched
B:
C:
D:
Expand Down Expand Up @@ -1090,12 +1090,12 @@ void existingEntryBlockWithCommentAtLastLine() {
"""
spring:
application:
name: main # some comment
name: main # Comment moved from root to previous element
""",
"""
spring:
application:
name: main # some comment
name: main # Comment moved from root to previous element
description: a description
"""
)
Expand Down Expand Up @@ -1128,39 +1128,43 @@ void existingEntryBlockWithCommentAllOverThePlace() {
)),
yaml(
"""
A: # Some comment
B: # Some comment 2
C: # Some comment 3
D: # Some comment 4
A: # Comment untouched 1
B: # Comment untouched 2
C: # Comment untouched 3
D: # Comment untouched 4
1: something else
2: old desc # Some comment 5
2: old desc # Comment moved from next block to previous element 1
D2:
1: a
2: b # Some comment 10
# Above comment untouched 1
2: b # Comment untouched 5
3: c
D3: # Some comment 6
1: old description # Some comment 7
D4: # Some comment 8
1: old text # Some comment 9
# Above comment untouched 2
D3: # Comment untouched 6
1: old description # Comment moved from next block to previous element 2
D4: # Comment untouched 7
1: old text # Comment moved from root to previous element
""",
"""
A: # Some comment
B: # Some comment 2
C: # Some comment 3
D: # Some comment 4
A: # Comment untouched 1
B: # Comment untouched 2
C: # Comment untouched 3
D: # Comment untouched 4
1: something else
2: old desc # Some comment 5
2: old desc # Comment moved from next block to previous element 1
3: new desc
D2:
1: a
2: b # Some comment 10
# Above comment untouched 1
2: b # Comment untouched 5
3: c
4: d
D3: # Some comment 6
1: old description # Some comment 7
# Above comment untouched 2
D3: # Comment untouched 6
1: old description # Comment moved from next block to previous element 2
2: new description
D4: # Some comment 8
1: old text # Some comment 9
D4: # Comment untouched 7
1: old text # Comment moved from root to previous element
2: new text
"""
)
Expand Down

0 comments on commit 1381e9f

Please sign in to comment.