diff --git a/pkgs/yaml_edit/CHANGELOG.md b/pkgs/yaml_edit/CHANGELOG.md index 7e1466777..f065a41f3 100644 --- a/pkgs/yaml_edit/CHANGELOG.md +++ b/pkgs/yaml_edit/CHANGELOG.md @@ -1,4 +1,5 @@ ## 2.2.5-wip +- Fix a bug where appending to a nested block list before an outdented sibling failed. ([#2481](https://github.com/dart-lang/tools/issues/2481)). - Fix `YamlEditor.appendToList` and map updates when adding to flow collections with a trailing comma. diff --git a/pkgs/yaml_edit/lib/src/list_mutations.dart b/pkgs/yaml_edit/lib/src/list_mutations.dart index 2b181c301..5b3ab99b7 100644 --- a/pkgs/yaml_edit/lib/src/list_mutations.dart +++ b/pkgs/yaml_edit/lib/src/list_mutations.dart @@ -155,8 +155,8 @@ SourceEdit _appendToBlockList( // Adjusts offset to after the trailing newline of the last entry, if it // exists if (list.isNotEmpty) { - final lastValueSpanEnd = list.nodes.last.span.end.offset; - final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd - 1); + final lastValueSpanEnd = getContentSensitiveEnd(list.nodes.last); + final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd); if (nextNewLineIndex == -1) { formattedValue = getLineEnding(yaml) + formattedValue; } else { diff --git a/pkgs/yaml_edit/test/append_test.dart b/pkgs/yaml_edit/test/append_test.dart index 91adcf92a..2d9b1a5e1 100644 --- a/pkgs/yaml_edit/test/append_test.dart +++ b/pkgs/yaml_edit/test/append_test.dart @@ -218,6 +218,35 @@ b: ''')); }); + test('block append nested to list', () { + final yamlEditor = YamlEditor(''' +family: + - title: Parent + children: + - title: kid1 + - title: kid2 + - title: Uncle +'''); + + expect( + () => yamlEditor + .appendToList(['family', 0, 'children'], {'title': 'kid3'}), + returnsNormally); + expectYamlBuilderValue(yamlEditor, { + 'family': [ + { + 'title': 'Parent', + 'children': [ + {'title': 'kid1'}, + {'title': 'kid2'}, + {'title': 'kid3'}, + ], + }, + {'title': 'Uncle'}, + ] + }); + }); + test('block append nested and with comments', () { final yamlEditor = YamlEditor(''' a: