Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve spacing when merging flow style YAML #4981

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, P p) {
return e;
}
}

if (" ".equals(e.getPrefix())) {
return e;
}
return e.withPrefix("\n");
}
return e;
Expand Down
103 changes: 102 additions & 1 deletion rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,108 @@ void addFoldedStyleMinusBlock() {
script: >-
#!/bin/bash
echo "hello"
""")
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/4958")
@Test
void preserveSpacingWhenMergingFlowStyle() {
rewriteRun(spec -> spec
.recipe(new MergeYaml(//language=jsonpath
"$.jobs[?(@.name=='test')].plan[?(@.task=='sonar')]",
// language=yaml
"vars: { version: 10.3 }",
false,
null,
null
)),

yaml(// language=yaml
"""
jobs:
- name: test
plan:
- task: sonar
""",
// language=yaml
"""
jobs:
- name: test
plan:
- task: sonar
vars: { version: 10.3 }
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/4958")
@Test
void preserveSpacingWhenMergingFlowStyleNested() {
rewriteRun(spec -> spec
.recipe(new MergeYaml(//language=jsonpath
"$.jobs[?(@.name=='test')].plan[?(@.task=='sonar')]",
// language=yaml
"vars: { mapping: { version: 10.3 } }",
false,
null,
null
)),

yaml(// language=yaml
"""
jobs:
- name: test
plan:
- task: sonar
""",
// language=yaml
"""
jobs:
- name: test
plan:
- task: sonar
vars: { mapping: { version: 10.3 } }
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/4958")
@Test
void preserveSpacingWhenMergingFlowStyleWithNewline() {
rewriteRun(spec -> spec
.recipe(new MergeYaml(//language=jsonpath
"$.jobs[?(@.name=='test')].plan[?(@.task=='sonar')]",
// language=yaml
"""
vars: {
version: 10.3 }
""",
false,
null,
null
)),

yaml(// language=yaml
"""
jobs:
- name: test
plan:
- task: sonar
""",
// language=yaml
"""
jobs:
- name: test
plan:
- task: sonar
vars: {
version: 10.3 }
"""
)
);
}
}
Loading