Skip to content

Commit 3b9b51c

Browse files
Fix UnfoldProperties recipe for 4 space indent (#6447)
* Fix UnfoldProperties recipe for 4 space indent * Do not change the default fallback indentation to four --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent a237de3 commit 3b9b51c

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeDuplicateSectionsVisitor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ public Yaml.Document visitDocument(Yaml.Document document, P p) {
3737
Yaml.Document d = super.visitDocument(document, p);
3838
String newRootComments = getCursor().getMessage("NEW_ROOT_COMMENTS", "");
3939
if (!newRootComments.isEmpty()) {
40-
d = d.withPrefix(d.getPrefix() + newRootComments);
40+
// Clean up extra leading newlines when adding root comments
41+
String cleanComments = newRootComments;
42+
if (cleanComments.startsWith("\n") && d.getPrefix().isEmpty()) {
43+
cleanComments = cleanComments.substring(1);
44+
}
45+
d = d.withPrefix(d.getPrefix() + cleanComments);
4146
}
4247
return d;
4348
}

rewrite-yaml/src/main/java/org/openrewrite/yaml/UnfoldProperties.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import lombok.Value;
2020
import org.jspecify.annotations.Nullable;
2121
import org.openrewrite.*;
22+
import org.openrewrite.yaml.search.FindIndentYamlVisitor;
2223
import org.openrewrite.yaml.tree.Yaml;
2324

2425
import java.util.ArrayList;
@@ -71,8 +72,11 @@ public String getDescription() {
7172
public TreeVisitor<?, ExecutionContext> getVisitor() {
7273
List<JsonPathMatcher> exclusionMatchers = exclusions.stream().map(JsonPathMatcher::new).collect(toList());
7374
return new YamlIsoVisitor<ExecutionContext>() {
75+
private final FindIndentYamlVisitor<ExecutionContext> findIndent = new FindIndentYamlVisitor<>();
76+
7477
@Override
7578
public Yaml.Document visitDocument(Yaml.Document document, ExecutionContext ctx) {
79+
findIndent.visit(document, ctx);
7680
Yaml.Document doc = super.visitDocument(document, ctx);
7781
doAfterVisit(new MergeDuplicateSectionsVisitor<>(doc));
7882
return doc;
@@ -106,7 +110,10 @@ public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry e, ExecutionConte
106110
if (!hasLineBreak(entry.getPrefix()) && hasLineBreak(newEntry.getPrefix())) {
107111
newEntry = newEntry.withPrefix(substringOfAfterFirstLineBreak(entry.getPrefix()));
108112
} else if (identLevel == 0 && hasLineBreak(newEntry.getPrefix())) {
109-
identLevel = 2; // autFormat indents the entire block by 2 spaces though it is a root level entry -> shift by 2 later
113+
// Use the detected indentation from the document, defaulting to 2 if none detected
114+
int mostCommonIndent = findIndent.getMostCommonIndent();
115+
// autoFormat indents the entire block by detected spaces though it is a root level entry -> shift by detected amount later
116+
identLevel = 0 < mostCommonIndent ? mostCommonIndent : 2;
110117
}
111118
doAfterVisit(new ShiftFormatLeftVisitor<>(newEntry, identLevel));
112119
}

rewrite-yaml/src/test/java/org/openrewrite/yaml/UnfoldPropertiesTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,58 @@ void applyToWithExclusion() {
472472
)
473473
);
474474
}
475+
476+
@Test
477+
void unfoldSpringPropertiesTwoSpace() {
478+
rewriteRun(
479+
yaml(
480+
"""
481+
spring:
482+
datasource:
483+
url: jdbc:postgresql://localhost/test
484+
485+
spring.servlet.encoding.charset: UTF-8
486+
spring.servlet.encoding.enabled: true
487+
spring.servlet.encoding.force: true
488+
""",
489+
"""
490+
spring:
491+
datasource:
492+
url: jdbc:postgresql://localhost/test
493+
servlet:
494+
encoding:
495+
charset: UTF-8
496+
enabled: true
497+
force: true
498+
"""
499+
)
500+
);
501+
}
502+
503+
@Test
504+
void unfoldSpringPropertiesFourSpace() {
505+
rewriteRun(
506+
yaml(
507+
"""
508+
spring:
509+
datasource:
510+
url: jdbc:postgresql://localhost/test
511+
512+
spring.servlet.encoding.charset: UTF-8
513+
spring.servlet.encoding.enabled: true
514+
spring.servlet.encoding.force: true
515+
""",
516+
"""
517+
spring:
518+
datasource:
519+
url: jdbc:postgresql://localhost/test
520+
servlet:
521+
encoding:
522+
charset: UTF-8
523+
enabled: true
524+
force: true
525+
"""
526+
)
527+
);
528+
}
475529
}

0 commit comments

Comments
 (0)