@@ -475,6 +475,7 @@ typedef BlockNodeOffset = ({int start, int end});
475475SourceEdit removeBlockCollectionEntry (
476476 String yaml, {
477477 required YamlNode blockCollection,
478+ required int collectionIndent,
478479 required bool isFirstEntry,
479480 required bool isSingleEntry,
480481 required bool isLastEntry,
@@ -492,11 +493,31 @@ SourceEdit removeBlockCollectionEntry(
492493 var makeNextNodeCompact = false ;
493494 var (: start, : end) = nodeToRemoveOffset;
494495
495- if (start != 0 && ! isSingleEntry) {
496- /// Try making it compact in case the collection's parent is a:
497- /// - block sequence
498- /// - explicit key
499- /// - explicit key's explicit value.
496+ // Skip empty lines to the last line break
497+ end = indexOfLastLineEnding (
498+ yaml,
499+ offset: yaml.indexOf ('\n ' , end - 1 ),
500+ blockIndent: collectionIndent,
501+ );
502+
503+ end = min (++ end, yaml.length); // Mark it for removal
504+
505+ if (isSingleEntry) {
506+ // Take back the last line ending even if multiple were skipped. This node
507+ // is sandwiched between other elements not in this map.
508+ end = end >= yaml.length - 1 ? end : (end - (lineEnding == '\r\n ' ? 2 : 1 ));
509+ return SourceEdit (start, end - start, isBlockList ? '[]' : '{}' );
510+ } else if (isLastEntry) {
511+ start = yaml.lastIndexOf ('\n ' , start) + 1 ;
512+ end = max (yaml.lastIndexOf ('\n ' , blockCollection.span.end.offset) + 1 , end);
513+ return SourceEdit (start, end - start, '' );
514+ }
515+
516+ if (start != 0 ) {
517+ // Try making it compact in case the collection's parent is a:
518+ // - block sequence
519+ // - explicit key
520+ // - explicit key's explicit value.
500521 if (isFirstEntry) {
501522 final (: compactCharOffset, : lineEndingIndex) = indexOfCompactChar (
502523 yaml,
@@ -510,147 +531,45 @@ SourceEdit removeBlockCollectionEntry(
510531 start = lineEndingIndex + 1 ;
511532 }
512533 } else {
513- /// If not possible, just consume this node's indent. This prevents this
514- /// node from interfering with the next node.
534+ // If not possible, just consume this node's indent. This prevents this
535+ // node from interfering with the next node.
515536 start = yaml.lastIndexOf ('\n ' , start) + 1 ;
516537 }
517538 }
518539
519- var replacement = '' ;
520-
521- // Skip empty lines to the last line break
522- end = indexOfLastLineEnding (yaml, yaml.indexOf ('\n ' , end - 1 ));
523- end = min (++ end, yaml.length); // Mark it for removal
524-
525- if (isSingleEntry) {
526- /// Take back the last line ending even if multiple were skipped. This node
527- /// is sandwiched between other elements not in this map.
528- if (end < yaml.length - 1 ) {
529- end -= lineEnding == '\r\n ' ? 2 : 1 ;
530- }
531-
532- replacement = isBlockList ? '[]' : '{}' ;
533- } else if (! isLastEntry) {
534- final (: nearestLineEnding, : nextNodeColStart) = nextBlockNodeInfo ();
535- final trueEndOffset = end - 1 ;
536-
537- /// Make compact only if we are pointing to same line break from different
538- /// node extremes. This can only be true if we are removing the first
539- /// entry.
540- ///
541- /// ** For block lists **
542- ///
543- /// [*] Before:
544- ///
545- /// - - value
546- /// - next
547- ///
548- /// [*] After:
549- ///
550- /// - - next
551- ///
552- /// ** For block maps **
553- ///
554- /// [*] Before:
555- ///
556- /// - key: value
557- /// next: value
558- ///
559- /// [*] After:
560- ///
561- /// - next: value
562- if (makeNextNodeCompact) {
563- /// Leaving comments here has no effect since we are removing the first
564- /// entry of the collection. The next node will be the first. However, the
565- /// inline comment hugging the outermost node in the current line will be
566- /// consumed.
567- ///
568- /// ** For Block Lists **
569- ///
570- /// [*] Before:
571- ///
572- /// - - value # Comment
573- /// # Next Comment
574- /// - next
575- ///
576- /// [*] After:
577- ///
578- /// - # Next Comment
579- /// - next
580- ///
581- /// ** For Block Maps **
582- ///
583- /// [*] Before:
584- ///
585- /// - key: value # Comment
586- /// # Next Comment
587- /// next: value
588- ///
589- /// [*] After:
590- ///
591- /// - # Next Comment
592- /// next-key: value
593- end = nearestLineEnding == trueEndOffset ? end + nextNodeColStart : end;
594- } else if (nearestLineEnding != trueEndOffset) {
595- /// We shouldn't assume the string will always have a flow node. Edits
596- /// may be made to a YAML string which declared block scalars initially.
597- /// Better safe than sorry.
598- ///
599- /// Truncate any dangling comments which may change the structure of
600- /// existing nodes. We must point to the same offset after every empty
601- /// line has been skipped. Mimic (intelligently) what the parser does
602- /// with lexing.
603- ///
604- /// ** For Block Lists **
605- ///
606- /// [*] Before:
607- ///
608- /// - >+
609- /// folded keep
610- ///
611- /// - target # Comment
612- /// # Next Comment
613- /// # Another Comment
614- /// - next
615- ///
616- /// [*] After:
617- ///
618- /// - >+
619- /// folded keep
620- ///
621- /// - next
622- ///
623- /// ** For Block Maps **
624- ///
625- /// [*] Before:
626- ///
627- /// key: >+
628- /// value
629- ///
630- /// target-key: value # Comment
631- /// # Next Comment
632- /// # Another Comment
633- ///
634- /// next: here
635- ///
636- /// [*] After:
637- ///
638- /// key: >+
639- /// value
640- ///
641- /// next: here
642- ///
643- /// We preserved the string's meaning without much effort.
644- end = nearestLineEnding + 1 ;
645- }
646- } else {
647- end = max (
648- yaml.lastIndexOf ('\n ' , blockCollection.span.end.offset) + 1 ,
649- end,
650- );
651- }
652-
653- return SourceEdit (start, end - start, replacement);
540+ final (: nearestLineEnding, : nextNodeColStart) = nextBlockNodeInfo ();
541+ final trueEndOffset = end - 1 ;
542+
543+ // Make compact only if we are pointing to same line break from different
544+ // node extremes. This can only be true if we are removing the first
545+ // entry.
546+ //
547+ // ** For block lists **
548+ //
549+ // [*] Before:
550+ //
551+ // - - value
552+ // - next
553+ //
554+ // [*] After:
555+ //
556+ // - - next
557+ //
558+ // ** For block maps **
559+ //
560+ // [*] Before:
561+ //
562+ // - key: value
563+ // next: value
564+ //
565+ // [*] After:
566+ //
567+ // - next: value
568+ end = makeNextNodeCompact && nearestLineEnding == trueEndOffset
569+ ? end + nextNodeColStart
570+ : end;
571+
572+ return SourceEdit (start, end - start, '' );
654573}
655574
656575extension YamlNodeExtension on YamlNode {
0 commit comments