@@ -36,13 +36,11 @@ SourceEdit updateInMap(
3636/// removing the element at [key] when re-parsed.
3737SourceEdit removeInMap (YamlEditor yamlEdit, YamlMap map, Object ? key) {
3838 assert (containsKey (map, key));
39- final keyNode = getKeyNode (map, key);
40- final valueNode = map.nodes[keyNode]! ;
4139
4240 if (map.style == CollectionStyle .FLOW ) {
43- return _removeFromFlowMap (yamlEdit, map, keyNode, valueNode );
41+ return _removeFromFlowMap (yamlEdit, map, key );
4442 } else {
45- return _removeFromBlockMap (yamlEdit, map, keyNode, valueNode );
43+ return _removeFromBlockMap (yamlEdit, map, key );
4644 }
4745}
4846
@@ -169,74 +167,47 @@ SourceEdit _replaceInFlowMap(
169167}
170168
171169/// Performs the string operation on [yamlEdit] to achieve the effect of
172- /// removing the [keyNode ] from the map, bearing in mind that this is a block
170+ /// removing the [key ] from the map, bearing in mind that this is a block
173171/// map.
174- SourceEdit _removeFromBlockMap (
175- YamlEditor yamlEdit, YamlMap map, YamlNode keyNode, YamlNode valueNode) {
176- final keySpan = keyNode.span;
177- var end = getContentSensitiveEnd (valueNode);
172+ SourceEdit _removeFromBlockMap (YamlEditor yamlEdit, YamlMap map, Object ? key) {
173+ final (index: entryIndex, : keyNode, : valueNode) = getYamlMapEntry (map, key);
178174 final yaml = yamlEdit.toString ();
179- final lineEnding = getLineEnding (yaml);
180-
181- if (map.length == 1 ) {
182- final start = map.span.start.offset;
183- final nextNewLine = yaml.indexOf (lineEnding, end);
184- if (nextNewLine != - 1 ) {
185- // Remove everything up to the next newline, this strips comments that
186- // follows on the same line as the value we're removing.
187- // It also ensures we consume colon when [valueNode.value] is `null`
188- // because there is no value (e.g. `key: \n`). Because [valueNode.span] in
189- // such cases point to the colon `:`.
190- end = nextNewLine;
191- } else {
192- // Remove everything until the end of the document, if there is no newline
193- end = yaml.length;
194- }
195- return SourceEdit (start, end - start, '{}' );
196- }
197-
198- var start = keySpan.start.offset;
199-
200- /// Adjust the end to clear the new line after the end too.
201- ///
202- /// We do this because we suspect that our users will want the inline
203- /// comments to disappear too.
204- final nextNewLine = yaml.indexOf (lineEnding, end);
205- if (nextNewLine != - 1 ) {
206- end = nextNewLine + lineEnding.length;
207- } else {
208- // Remove everything until the end of the document, if there is no newline
209- end = yaml.length;
210- }
211-
212- final nextNode = getNextKeyNode (map, keyNode);
213-
214- if (start > 0 ) {
215- final lastHyphen = yaml.lastIndexOf ('-' , start - 1 );
216- final lastNewLine = yaml.lastIndexOf (lineEnding, start - 1 );
217- if (lastHyphen > lastNewLine) {
218- start = lastHyphen + 2 ;
219-
220- /// If there is a `-` before the node, and the end is on the same line
221- /// as the next node, we need to add the necessary offset to the end to
222- /// make sure the next node has the correct indentation.
223- if (nextNode != null &&
224- nextNode.span.start.offset - end <= nextNode.span.start.column) {
225- end += nextNode.span.start.column;
226- }
227- } else if (lastNewLine > lastHyphen) {
228- start = lastNewLine + lineEnding.length;
229- }
230- }
175+ final mapSize = map.length;
176+ final keySpan = keyNode.span;
231177
232- return SourceEdit (start, end - start, '' );
178+ return removeBlockCollectionEntry (
179+ yaml,
180+ blockCollection: map,
181+ isFirstEntry: entryIndex == 0 ,
182+ isSingleEntry: mapSize == 1 ,
183+ isLastEntry: entryIndex >= mapSize - 1 ,
184+ nodeToRemoveOffset: (
185+ start: keySpan.start.offset,
186+ end: valueNode.span.length == 0
187+ ? keySpan.end.offset + 2 // Null value have no span. Skip ":".
188+ : getContentSensitiveEnd (valueNode),
189+ ),
190+ lineEnding: getLineEnding (yaml),
191+
192+ // Only called when the next node is present. Never before.
193+ nextBlockNodeInfo: () {
194+ final nextKeyNode = map.nodes.keys.elementAt (entryIndex + 1 ) as YamlNode ;
195+ final nextKeySpan = nextKeyNode.span.start;
196+
197+ return (
198+ nearestLineEnding: yaml.lastIndexOf ('\n ' , nextKeySpan.offset),
199+ nextNodeColStart: nextKeySpan.column
200+ );
201+ },
202+ );
233203}
234204
235205/// Performs the string operation on [yamlEdit] to achieve the effect of
236- /// removing the [keyNode ] from the map, bearing in mind that this is a flow
206+ /// removing the [key ] from the map, bearing in mind that this is a flow
237207/// map.
238- SourceEdit _removeFromFlowMap (
239- YamlEditor yamlEdit, YamlMap map, YamlNode keyNode, YamlNode valueNode) {
208+ SourceEdit _removeFromFlowMap (YamlEditor yamlEdit, YamlMap map, Object ? key) {
209+ final (index: _, : keyNode, : valueNode) = getYamlMapEntry (map, key);
210+
240211 var start = keyNode.span.start.offset;
241212 var end = valueNode.span.end.offset;
242213 final yaml = yamlEdit.toString ();
0 commit comments