Skip to content

Commit

Permalink
Fix line folding (#87)
Browse files Browse the repository at this point in the history
* 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 <jonasfj@google.com>
Co-authored-by: Jonas Finnemann Jensen <jopsen@gmail.com>
  • Loading branch information
3 people authored Jun 27, 2024
1 parent 08a146e commit ad3292c
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 157 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
list.
([#69](https://github.com/dart-lang/yaml_edit/issues/69))

- Fix error thrown when inserting in nested list using `spliceList` method
([#83](https://github.com/dart-lang/yaml_edit/issues/83))

- Fix error thrown when string has spaces when applying `ScalarStyle.FOLDED`.
([#41](https://github.com/dart-lang/yaml_edit/issues/41)). Resolves
([[#86](https://github.com/dart-lang/yaml_edit/issues/86)]).

## 2.2.1

- Require Dart 3.0
Expand Down
2 changes: 1 addition & 1 deletion lib/src/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class YamlEditor {
final end = getContentSensitiveEnd(_contents);
final lineEnding = getLineEnding(_yaml);
final edit = SourceEdit(
start, end - start, yamlEncodeBlockString(valueNode, 0, lineEnding));
start, end - start, yamlEncodeBlock(valueNode, 0, lineEnding));

return _performEdit(edit, path, valueNode);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/src/list_mutations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ SourceEdit updateInList(
final listIndentation = getListIndentation(yaml, list);
final indentation = listIndentation + getIndentation(yamlEdit);
final lineEnding = getLineEnding(yaml);
valueString = yamlEncodeBlockString(
wrapAsYamlNode(newValue), indentation, lineEnding);
valueString =
yamlEncodeBlock(wrapAsYamlNode(newValue), indentation, lineEnding);

/// We prefer the compact nested notation for collections.
///
Expand All @@ -52,7 +52,7 @@ SourceEdit updateInList(

return SourceEdit(offset, end - offset, valueString);
} else {
valueString = yamlEncodeFlowString(newValue);
valueString = yamlEncodeFlow(newValue);
return SourceEdit(offset, currValue.span.length, valueString);
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ SourceEdit _appendToBlockList(
final newIndentation = listIndentation + getIndentation(yamlEdit);
final lineEnding = getLineEnding(yaml);

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

/// Formats [item] into a new node for flow lists.
String _formatNewFlow(YamlList list, YamlNode item, [bool isLast = false]) {
var valueString = yamlEncodeFlowString(item);
var valueString = yamlEncodeFlow(item);
if (list.isNotEmpty) {
if (isLast) {
valueString = ', $valueString';
Expand Down
14 changes: 7 additions & 7 deletions lib/src/map_mutations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ SourceEdit _addToBlockMap(
final yaml = yamlEdit.toString();
final newIndentation =
getMapIndentation(yaml, map) + getIndentation(yamlEdit);
final keyString = yamlEncodeFlowString(wrapAsYamlNode(key));
final keyString = yamlEncodeFlow(wrapAsYamlNode(key));
final lineEnding = getLineEnding(yaml);

var formattedValue = ' ' * getMapIndentation(yaml, map);
Expand Down Expand Up @@ -83,7 +83,7 @@ SourceEdit _addToBlockMap(
}
}

var valueString = yamlEncodeBlockString(newValue, newIndentation, lineEnding);
var valueString = yamlEncodeBlock(newValue, newIndentation, lineEnding);
if (isCollection(newValue) &&
!isFlowYamlCollectionNode(newValue) &&
!isEmpty(newValue)) {
Expand All @@ -100,8 +100,8 @@ SourceEdit _addToBlockMap(
/// map.
SourceEdit _addToFlowMap(
YamlEditor yamlEdit, YamlMap map, YamlNode keyNode, YamlNode newValue) {
final keyString = yamlEncodeFlowString(keyNode);
final valueString = yamlEncodeFlowString(newValue);
final keyString = yamlEncodeFlow(keyNode);
final valueString = yamlEncodeFlow(newValue);

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

final keyNode = getKeyNode(map, key);
var valueAsString = yamlEncodeBlockString(
wrapAsYamlNode(newValue), newIndentation, lineEnding);
var valueAsString =
yamlEncodeBlock(wrapAsYamlNode(newValue), newIndentation, lineEnding);
if (isCollection(newValue) &&
!isFlowYamlCollectionNode(newValue) &&
!isEmpty(newValue)) {
Expand Down Expand Up @@ -163,7 +163,7 @@ SourceEdit _replaceInBlockMap(
SourceEdit _replaceInFlowMap(
YamlEditor yamlEdit, YamlMap map, Object? key, YamlNode newValue) {
final valueSpan = map.nodes[key]!.span;
final valueString = yamlEncodeFlowString(newValue);
final valueString = yamlEncodeFlow(newValue);

return SourceEdit(valueSpan.start.offset, valueSpan.length, valueString);
}
Expand Down
Loading

0 comments on commit ad3292c

Please sign in to comment.