Skip to content

Commit 970d81e

Browse files
committed
Use ListUtils.mapLast
1 parent eb84f59 commit 970d81e

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,17 @@ public Yaml.Mapping visitMapping(Yaml.Mapping mapping, ExecutionContext ctx) {
193193
}
194194

195195
// Preserve inline comment from deleted trailing entries on the last kept entry
196-
if (trailingInlineComment != null && !entries.isEmpty()) {
197-
int lastIndex = entries.size() - 1;
198-
Yaml.Mapping.Entry lastKept = entries.get(lastIndex);
199-
if (lastKept.getValue() instanceof Yaml.Scalar &&
200-
((Yaml.Scalar) lastKept.getValue()).getStyle() == Yaml.Scalar.Style.PLAIN) {
201-
Yaml.Scalar scalar = (Yaml.Scalar) lastKept.getValue();
202-
entries.set(lastIndex, lastKept.withValue(
203-
scalar.withValue(scalar.getValue() + trailingInlineComment)));
204-
}
196+
if (trailingInlineComment != null) {
197+
String comment = trailingInlineComment;
198+
entries = ListUtils.mapLast(entries, lastKept -> {
199+
if (lastKept.getValue() instanceof Yaml.Scalar &&
200+
((Yaml.Scalar) lastKept.getValue()).getStyle() == Yaml.Scalar.Style.PLAIN) {
201+
Yaml.Scalar scalar = (Yaml.Scalar) lastKept.getValue();
202+
return lastKept.withValue(
203+
scalar.withValue(scalar.getValue() + comment));
204+
}
205+
return lastKept;
206+
});
205207
}
206208

207209
if (changed) {

0 commit comments

Comments
 (0)