Skip to content

Comments from YamlEditor.update #2316

@kekavc24

Description

@kekavc24

For YamlEditor.remove, the objective was clear (even from the existing code). If a node is removed, remove comments that match that node.

However, for YamlEditor.update, it seems the update wants to keep the comment (evident from code. See except from list_mutations.dart).

var end = getContentSensitiveEnd(currValue);
if (end <= offset) {
offset++;
end = offset;
valueString = ' $valueString';
}
return SourceEdit(offset, end - offset, valueString);
} else {
valueString = yamlEncodeFlow(newValue);
return SourceEdit(offset, currValue.span.length, valueString);
}
}


My point is, these comments only pose a threat to block scalars. They could be captured and re-applied to the block scalar is a non-intrusive way.

Consider:

const yaml = ... // Some string from the examples below.

final editor = YamlEditor(
  yaml,
)..update([1], wrapAsYamlNode('taste', scalarStyle: ScalarStyle.FOLDED));

Block lists

# Before 
- eye
- knead # with this comment
- dough
# After
- eye
# with this comment
- >- 
  taste
- dough

--- # Or even this for readability
- eye

# with this comment
- >- 
  taste
- dough

Block maps

# Before
bit-flip: "0-to"
1: jumps # with this comment
# After
bit-flip: "0-to"
1:
  # with this comment
  >- 
  taste

We could do:

  1. What I propose. This is similar to some examples in Comment causes issue with ScalarStyle.FOLDED #1935 but more contextual to value being inserted.
  2. Truncate and remove the comment(s) for updates that add:
    1. Any node (block and flow)
    2. Block scalars only.
    3. Block nodes only

Both implementations are simple, improve package:yaml_edit, use the changes in #2240 and won't be a breaking change for most of the existing tests.


cc @jonasfj @sigurdm

Also see a draft of option 2.i where comments are removed from all nodes in block lists when _updateInList is called. #2315

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions