Skip to content

Commit

Permalink
Do not add trailing whitespace to new map keys (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored Mar 17, 2022
1 parent bd96cbf commit 46eb45d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions lib/src/map_mutations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@ SourceEdit _addToBlockMap(
final keyString = yamlEncodeFlowString(wrapAsYamlNode(key));
final lineEnding = getLineEnding(yaml);

var valueString = yamlEncodeBlockString(newValue, newIndentation, lineEnding);
if (isCollection(newValue) &&
!isFlowYamlCollectionNode(newValue) &&
!isEmpty(newValue)) {
valueString = '$lineEnding$valueString';
}

var formattedValue = ' ' * getMapIndentation(yaml, map) + '$keyString: ';
var formattedValue = ' ' * getMapIndentation(yaml, map);
var offset = map.span.end.offset;

final insertionIndex = getMapInsertionIndex(map, keyString);
Expand All @@ -90,7 +83,14 @@ SourceEdit _addToBlockMap(
}
}

formattedValue += valueString + lineEnding;
var valueString = yamlEncodeBlockString(newValue, newIndentation, lineEnding);
if (isCollection(newValue) &&
!isFlowYamlCollectionNode(newValue) &&
!isEmpty(newValue)) {
formattedValue += '$keyString:' + lineEnding + valueString + lineEnding;
} else {
formattedValue += '$keyString: ' + valueString + lineEnding;
}

return SourceEdit(offset, 0, formattedValue);
}
Expand Down
4 changes: 2 additions & 2 deletions test/update_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ a:
expect(doc.toString(), equals('''
a:
aa: 1
bb:
bb:
aaa:
dddd: c
bbb:
Expand Down Expand Up @@ -806,7 +806,7 @@ a:
expect(doc.toString(), equals('''
a:
aa: 1
bb:
bb:
- 0
- - 1
- 2
Expand Down

0 comments on commit 46eb45d

Please sign in to comment.