Skip to content

Commit

Permalink
Skip comments and remove additional \n added in list mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
kekavc24 committed Jul 1, 2024
1 parent e659cb9 commit 53f9637
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/src/list_mutations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ SourceEdit updateInList(
final listIndentation = getListIndentation(yaml, list);
final indentation = listIndentation + getIndentation(yamlEdit);
final lineEnding = getLineEnding(yaml);
valueString =
yamlEncodeBlock(wrapAsYamlNode(newValue), indentation, lineEnding);

final encoded = yamlEncodeBlock(
wrapAsYamlNode(newValue),
indentation,
lineEnding,
);
valueString = encoded;

/// We prefer the compact nested notation for collections.
///
/// By virtue of [yamlEncodeBlockString], collections automatically
/// By virtue of [yamlEncodeBlock], collections automatically
/// have the necessary line endings.
if ((newValue is List && (newValue as List).isNotEmpty) ||
(newValue is Map && (newValue as Map).isNotEmpty)) {
valueString = valueString.substring(indentation);
} else if (currValue.collectionStyle == CollectionStyle.BLOCK) {
valueString += lineEnding;
}

var end = getContentSensitiveEnd(currValue);
Expand All @@ -50,6 +53,19 @@ SourceEdit updateInList(
valueString = ' $valueString';
}

// Aggressively skip all comments
final (offsetOfLastComment, _) =
skipAndExtractCommentsInBlock(yaml, end, null, lineEnding);
end = offsetOfLastComment;

valueString =
normalizeEncodedBlock(yaml, lineEnding, end, newValue, valueString);

/// [skipAndExtractCommentsInBlock] is greedy and eats up any whitespace
/// it encounters in search of comments. Compensate indent lost in the
/// current edit
if (index != list.length - 1) valueString += ' ' * listIndentation;

return SourceEdit(offset, end - offset, valueString);
} else {
valueString = yamlEncodeFlow(newValue);
Expand Down Expand Up @@ -146,7 +162,7 @@ SourceEdit _appendToBlockList(
valueString = valueString.substring(newIndentation);
}

return (listIndentation, '- $valueString$lineEnding');
return (listIndentation, '- $valueString');
}

/// Formats [item] into a new node for flow lists.
Expand Down

0 comments on commit 53f9637

Please sign in to comment.