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:
What I propose. This is similar to some examples in Comment causes issue with ScalarStyle.FOLDED #1935 but more contextual to value being inserted.
Truncate and remove the comment(s) for updates that add:
Any node (block and flow)
Block scalars only.
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
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).tools/pkgs/yaml_edit/lib/src/list_mutations.dart
Lines 46 to 58 in da8a25c
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:
Block lists
Block maps
We could do:
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.iwhere comments are removed from all nodes in block lists when_updateInListis called. #2315