Skip to content

Commit

Permalink
Allow comments to be skipped greedily or lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
kekavc24 committed Jul 5, 2024
1 parent 9101d79 commit 3d99caf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/src/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,13 @@ class YamlEditor {
final start = _contents.span.start.offset;
var end = getContentSensitiveEnd(_contents);
final lineEnding = getLineEnding(_yaml);
end = skipAndExtractCommentsInBlock(_yaml, end, null, lineEnding).$1;
end = skipAndExtractCommentsInBlock(
_yaml,
end,
null,
lineEnding: lineEnding,
greedy: true,
).$1;
var encoded = yamlEncodeBlock(valueNode, 0, lineEnding);
encoded = normalizeEncodedBlock(
_yaml,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/map_mutations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ SourceEdit _replaceInBlockMap(

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

valueAsString = normalizeEncodedBlock(
Expand Down
16 changes: 12 additions & 4 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ String getLineEnding(String yaml) {
(int endOffset, List<String> comments) skipAndExtractCommentsInBlock(
String yaml,
int currentEndOffset,
int? nextStartOffset, [
int? nextStartOffset, {
String lineEnding = '\n',
]) {
bool greedy = false,
}) {
/// If [nextStartOffset] is null, this may be the last element in a collection
/// and thus we have to check and extract comments manually.
///
Expand Down Expand Up @@ -336,6 +337,13 @@ String getLineEnding(String yaml) {
return (firstLineBreak, nextIndex);
}

/// Returns the [currentOffset] if [greedy] is true. Otherwise, attempts
/// returning the [firstLineBreakOffset] if not null if [greedy] is false.
int earlyBreakOffset(int currentOffset, int? firstLineBreakOffset) {
if (greedy) return currentOffset;
return firstLineBreakOffset ?? currentOffset;
}

var currentOffset = currentEndOffset;

while (true) {
Expand All @@ -353,7 +361,7 @@ String getLineEnding(String yaml) {
/// string. Since we lazily evaluated the string, attempt to return the
/// first [lineEnding] we encountered only if not null.
if (nextIndex == null) {
currentOffset = firstLE ?? yaml.length;
currentOffset = earlyBreakOffset(yaml.length, firstLE);
break;
}

Expand All @@ -373,7 +381,7 @@ String getLineEnding(String yaml) {
/// Since we lazily evaluated the string, attempt to return the
/// first [lineEnding] we encountered only if not null.
if (indexOfCommentStart == -1) {
currentOffset = firstLineBreak ?? currentOffset;
currentOffset = earlyBreakOffset(currentOffset, firstLineBreak);
break;
}

Expand Down

0 comments on commit 3d99caf

Please sign in to comment.