Skip to content

Commit

Permalink
Remove keep chomping indicator references
Browse files Browse the repository at this point in the history
  • Loading branch information
kekavc24 committed Jun 19, 2024
1 parent df86c7d commit a9b5f6a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
33 changes: 5 additions & 28 deletions lib/src/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,6 @@ String? _tryYamlEncodeSingleQuoted(String string) {
return '\'$result\'';
}

/// Returns the correct block chomping indicator for [ScalarStyle.FOLDED]
/// and [ScalarStyle.LITERAL].
///
/// See https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
String _getChompingIndicator(String string) {
/// By default, we apply an indent to the string after every new line.
///
/// Apply the `keep (+)` chomping indicator for trailing whitespace to be
/// treated as content.
///
/// [NOTE]: We only check for trailing whitespace rather than `\n `. This is
/// a coin-toss approach. If there is a new line after this, it will be kept.
/// If not, nothing happens.
if (string.endsWith(' ') || string.endsWith('\n')) return '+';

return '-';
}

/// Attempts to encode a [string] as a _YAML folded string_ and apply the
/// appropriate _chomping indicator_.
///
Expand Down Expand Up @@ -155,7 +137,7 @@ String? _tryYamlEncodeFolded(String string, int indentSize, String lineEnding) {
return previous + lineEnding + updated;
});

return '>${_getChompingIndicator(string)}\n'
return '>-\n'
'$indent$trimmed'
'${stripped.replaceAll('\n', lineEnding + indent)}';
}
Expand Down Expand Up @@ -194,8 +176,7 @@ String? _tryYamlEncodeLiteral(

/// Simplest block style.
/// * https://yaml.org/spec/1.2.2/#812-literal-style
return '|${_getChompingIndicator(string)}\n$indent'
'${string.replaceAll('\n', lineEnding + indent)}';
return '|-\n$indent${string.replaceAll('\n', lineEnding + indent)}';
}

///Encodes a flow [YamlScalar] based on the provided [YamlScalar.style].
Expand Down Expand Up @@ -243,9 +224,7 @@ String _yamlEncodeBlockScalar(
}

switch (style) {
/// Prefer 'plain', fallback to "double quoted". Cast into [String] if
/// null as this condition only returns [null] for a [String] that can't
/// be encoded.
/// Prefer 'plain', fallback to "double quoted"
case ScalarStyle.PLAIN:
return _tryYamlEncodePlain(value) ?? _yamlEncodeDoubleQuoted(value);

Expand All @@ -254,14 +233,12 @@ String _yamlEncodeBlockScalar(
return _tryYamlEncodeSingleQuoted(value) ??
_yamlEncodeDoubleQuoted(value);

/// Prefer folded string, try literal as fallback
/// otherwise fallback to "double quoted"
/// Prefer folded string, fallback to "double quoted"
case ScalarStyle.FOLDED:
return _tryYamlEncodeFolded(value, indentation, lineEnding) ??
_yamlEncodeDoubleQuoted(value);

/// Prefer literal string, try folded as fallback
/// otherwise fallback to "double quoted"
/// Prefer literal string, fallback to "double quoted"
case ScalarStyle.LITERAL:
return _tryYamlEncodeLiteral(value, indentation, lineEnding) ??
_yamlEncodeDoubleQuoted(value);
Expand Down
1 change: 1 addition & 0 deletions test/string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final _testStrings = [
"this is a fairly' long string with\nline breaks",
'whitespace\n after line breaks',
'whitespace\n \nbetween line breaks',
'\n line break at the start',
'word',
'foo bar',
'foo\nbar',
Expand Down

0 comments on commit a9b5f6a

Please sign in to comment.