Commit afc5028
DeleteProperty: preserve inline comments when deleting entries (#6797)
* DeleteProperty: preserve inline comments when deleting entries
When an entry was deleted from a mapping, the newline fix-up logic
checked `startsWithNewline(prefix)` to decide whether to prepend a
newline. Inline comments are stored in the prefix of the next element
(e.g. ` # comment\n `), which starts with a space, not `\n`.
This caused the comment to be moved onto its own line.
Changed the check to `containsNewline(prefix)` so that prefixes
which already contain a line break (even if preceded by an inline
comment) are left intact.
* Show limitation of the current approach
* DeleteProperty: preserve inline comments when deleting last entry
When the deleted entry is the last in a mapping, its prefix contains
the inline comment from the previous entry's line. Transfer that
comment to the previous kept entry's scalar value so it is not lost.
* Use `ListUtils.mapLast`
---------
Co-authored-by: Tim te Beek <tim@moderne.io>1 parent 5d457b1 commit afc5028
2 files changed
Lines changed: 53 additions & 5 deletions
File tree
- rewrite-yaml/src
- main/java/org/openrewrite/yaml
- test/java/org/openrewrite/yaml
Lines changed: 35 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
| 132 | + | |
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| |||
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| 168 | + | |
168 | 169 | | |
169 | 170 | | |
170 | 171 | | |
| |||
173 | 174 | | |
174 | 175 | | |
175 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
176 | 181 | | |
177 | 182 | | |
178 | 183 | | |
179 | 184 | | |
180 | 185 | | |
181 | | - | |
| 186 | + | |
182 | 187 | | |
183 | 188 | | |
184 | 189 | | |
185 | 190 | | |
| 191 | + | |
186 | 192 | | |
187 | 193 | | |
188 | 194 | | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
189 | 209 | | |
190 | 210 | | |
191 | 211 | | |
| |||
206 | 226 | | |
207 | 227 | | |
208 | 228 | | |
209 | | - | |
| 229 | + | |
210 | 230 | | |
211 | 231 | | |
212 | 232 | | |
| |||
238 | 258 | | |
239 | 259 | | |
240 | 260 | | |
241 | | - | |
242 | | - | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
243 | 273 | | |
244 | 274 | | |
245 | 275 | | |
| |||
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
544 | 544 | | |
545 | 545 | | |
546 | 546 | | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
547 | 565 | | |
0 commit comments