diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java index aee420576e6..8f4c0a21ead 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java @@ -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; })); diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java index 655973286fc..03b3cb0ca1b 100644 --- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java +++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java @@ -1036,7 +1036,7 @@ void existingEntryBlockWithCommentAtFirstLine() { )), yaml( """ - A: # Some comment + A: # Comment untouched B: C: D: @@ -1049,7 +1049,7 @@ void existingEntryBlockWithCommentAtFirstLine() { 1: old text """, """ - A: # Some comment + A: # Comment untouched B: C: D: @@ -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 """ ) @@ -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 """ )