Skip to content

Commit 0b605dd

Browse files
kekavc24jonasfj
andauthored
Fix line folding (dart-archive/yaml_edit#87)
* Fix line folding to match specification * Add test case and remove TODO for fix * Remove indent creation from utility function * Update changelog * Return null if strings cannot be encoded in desired `ScalarStyle` > Fix invalid encoding for string with trailing line breaks or white-space * Clean up code > Update `_tryYamlEncodedPlain` to return null if string cannot be encoded > Make `_yamlEncodeFlowScalar` and `yamlEncodeBlockScalar` code concise * Remove unnecessary assertions and add docs > `_yamlEncodeFlowScalar` and `_yamlEncodeBlockScalar` always encode YamlScalars * Minor refactoring * Fix condition check for encoding as literal/folded * Remove keep chomping indicator references * Update lib/src/strings.dart --------- Co-authored-by: Jonas Finnemann Jensen <[email protected]> Co-authored-by: Jonas Finnemann Jensen <[email protected]>
1 parent aafb9aa commit 0b605dd

File tree

6 files changed

+219
-157
lines changed

6 files changed

+219
-157
lines changed

pkgs/yaml_edit/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
list.
66
([#69](https://github.com/dart-lang/yaml_edit/issues/69))
77

8+
- Fix error thrown when inserting in nested list using `spliceList` method
9+
([#83](https://github.com/dart-lang/yaml_edit/issues/83))
10+
11+
- Fix error thrown when string has spaces when applying `ScalarStyle.FOLDED`.
12+
([#41](https://github.com/dart-lang/yaml_edit/issues/41)). Resolves
13+
([[#86](https://github.com/dart-lang/yaml_edit/issues/86)]).
14+
815
## 2.2.1
916

1017
- Require Dart 3.0

pkgs/yaml_edit/lib/src/editor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class YamlEditor {
244244
final end = getContentSensitiveEnd(_contents);
245245
final lineEnding = getLineEnding(_yaml);
246246
final edit = SourceEdit(
247-
start, end - start, yamlEncodeBlockString(valueNode, 0, lineEnding));
247+
start, end - start, yamlEncodeBlock(valueNode, 0, lineEnding));
248248

249249
return _performEdit(edit, path, valueNode);
250250
}

pkgs/yaml_edit/lib/src/list_mutations.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ SourceEdit updateInList(
2929
final listIndentation = getListIndentation(yaml, list);
3030
final indentation = listIndentation + getIndentation(yamlEdit);
3131
final lineEnding = getLineEnding(yaml);
32-
valueString = yamlEncodeBlockString(
33-
wrapAsYamlNode(newValue), indentation, lineEnding);
32+
valueString =
33+
yamlEncodeBlock(wrapAsYamlNode(newValue), indentation, lineEnding);
3434

3535
/// We prefer the compact nested notation for collections.
3636
///
@@ -52,7 +52,7 @@ SourceEdit updateInList(
5252

5353
return SourceEdit(offset, end - offset, valueString);
5454
} else {
55-
valueString = yamlEncodeFlowString(newValue);
55+
valueString = yamlEncodeFlow(newValue);
5656
return SourceEdit(offset, currValue.span.length, valueString);
5757
}
5858
}
@@ -141,7 +141,7 @@ SourceEdit _appendToBlockList(
141141
final newIndentation = listIndentation + getIndentation(yamlEdit);
142142
final lineEnding = getLineEnding(yaml);
143143

144-
var valueString = yamlEncodeBlockString(item, newIndentation, lineEnding);
144+
var valueString = yamlEncodeBlock(item, newIndentation, lineEnding);
145145
if (isCollection(item) && !isFlowYamlCollectionNode(item) && !isEmpty(item)) {
146146
valueString = valueString.substring(newIndentation);
147147
}
@@ -151,7 +151,7 @@ SourceEdit _appendToBlockList(
151151

152152
/// Formats [item] into a new node for flow lists.
153153
String _formatNewFlow(YamlList list, YamlNode item, [bool isLast = false]) {
154-
var valueString = yamlEncodeFlowString(item);
154+
var valueString = yamlEncodeFlow(item);
155155
if (list.isNotEmpty) {
156156
if (isLast) {
157157
valueString = ', $valueString';

pkgs/yaml_edit/lib/src/map_mutations.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ SourceEdit _addToBlockMap(
5454
final yaml = yamlEdit.toString();
5555
final newIndentation =
5656
getMapIndentation(yaml, map) + getIndentation(yamlEdit);
57-
final keyString = yamlEncodeFlowString(wrapAsYamlNode(key));
57+
final keyString = yamlEncodeFlow(wrapAsYamlNode(key));
5858
final lineEnding = getLineEnding(yaml);
5959

6060
var formattedValue = ' ' * getMapIndentation(yaml, map);
@@ -83,7 +83,7 @@ SourceEdit _addToBlockMap(
8383
}
8484
}
8585

86-
var valueString = yamlEncodeBlockString(newValue, newIndentation, lineEnding);
86+
var valueString = yamlEncodeBlock(newValue, newIndentation, lineEnding);
8787
if (isCollection(newValue) &&
8888
!isFlowYamlCollectionNode(newValue) &&
8989
!isEmpty(newValue)) {
@@ -100,8 +100,8 @@ SourceEdit _addToBlockMap(
100100
/// map.
101101
SourceEdit _addToFlowMap(
102102
YamlEditor yamlEdit, YamlMap map, YamlNode keyNode, YamlNode newValue) {
103-
final keyString = yamlEncodeFlowString(keyNode);
104-
final valueString = yamlEncodeFlowString(newValue);
103+
final keyString = yamlEncodeFlow(keyNode);
104+
final valueString = yamlEncodeFlow(newValue);
105105

106106
// The -1 accounts for the closing bracket.
107107
if (map.isEmpty) {
@@ -131,8 +131,8 @@ SourceEdit _replaceInBlockMap(
131131
getMapIndentation(yaml, map) + getIndentation(yamlEdit);
132132

133133
final keyNode = getKeyNode(map, key);
134-
var valueAsString = yamlEncodeBlockString(
135-
wrapAsYamlNode(newValue), newIndentation, lineEnding);
134+
var valueAsString =
135+
yamlEncodeBlock(wrapAsYamlNode(newValue), newIndentation, lineEnding);
136136
if (isCollection(newValue) &&
137137
!isFlowYamlCollectionNode(newValue) &&
138138
!isEmpty(newValue)) {
@@ -163,7 +163,7 @@ SourceEdit _replaceInBlockMap(
163163
SourceEdit _replaceInFlowMap(
164164
YamlEditor yamlEdit, YamlMap map, Object? key, YamlNode newValue) {
165165
final valueSpan = map.nodes[key]!.span;
166-
final valueString = yamlEncodeFlowString(newValue);
166+
final valueString = yamlEncodeFlow(newValue);
167167

168168
return SourceEdit(valueSpan.start.offset, valueSpan.length, valueString);
169169
}

0 commit comments

Comments
 (0)