Skip to content

Commit d4f7e52

Browse files
Handle dashes on its own lines better (#7174)
1 parent d63c831 commit d4f7e52

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

rewrite-yaml/src/main/java/org/openrewrite/yaml/format/IndentsVisitor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ public IndentsVisitor(IndentsStyle style, @Nullable Tree stopAfter) {
8282

8383
getCursor().getParentOrThrow().putMessage("sequenceEntryIndent", indent);
8484
// the +1 is for the '-' character
85-
getCursor().getParentOrThrow().putMessage("lastIndent",
86-
indent + firstIndent(((Yaml.Sequence.Entry) y).getBlock()).length() + 1);
85+
String fi = firstIndent(((Yaml.Sequence.Entry) y).getBlock());
86+
int contentIndent;
87+
if (StringUtils.hasLineBreak(fi)) {
88+
// When the dash is on its own line, content is indented by indentSize from the dash
89+
contentIndent = indent + style.getIndentSize();
90+
} else {
91+
contentIndent = indent + fi.length() + 1;
92+
}
93+
getCursor().getParentOrThrow().putMessage("lastIndent", contentIndent);
8794
} else if (y instanceof Yaml.Mapping.Entry) {
8895
y = y.withPrefix(indentTo(y.getPrefix(), indent + style.getIndentSize()));
8996
getCursor().putMessage("lastIndent", indent + style.getIndentSize());

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,45 @@ void doesNotBreakOnKeysWhichIncludeRegexSpecialCharacters() {
766766
);
767767
}
768768

769+
@Issue("https://github.com/openrewrite/rewrite/issues/4802")
770+
@Test
771+
void changePropertyKeyWithSequenceContainingDashOnOwnLine() {
772+
rewriteRun(
773+
spec -> spec.recipe(new ChangePropertyKey("app.rhino", "rhino", null, null, null)),
774+
yaml(
775+
"""
776+
app:
777+
rhino:
778+
config:
779+
props:
780+
- prop: 'name1'
781+
config[0]:
782+
prop1: 1
783+
prop2: 2
784+
-
785+
prop: 'name2'
786+
config[0]:
787+
prop1: 3
788+
prop2: 4
789+
""",
790+
"""
791+
rhino:
792+
config:
793+
props:
794+
- prop: 'name1'
795+
config[0]:
796+
prop1: 1
797+
prop2: 2
798+
-
799+
prop: 'name2'
800+
config[0]:
801+
prop1: 3
802+
prop2: 4
803+
"""
804+
)
805+
);
806+
}
807+
769808
@Issue("https://github.com/openrewrite/rewrite/issues/2881")
770809
@Test
771810
void embedIndentedPropertyIntoExisting() {

0 commit comments

Comments
 (0)