@@ -540,5 +540,42 @@ fn non_existent_dependency_err(
540540}
541541
542542fn remove_array_index ( array : & mut toml_edit:: Array , index : usize ) {
543- array. remove ( index) ;
543+ let value = array. remove ( index) ;
544+
545+ // Captures all lines before leading whitespace
546+ let prefix_lines = value
547+ . decor ( )
548+ . prefix ( )
549+ . and_then ( |p| p. as_str ( ) . expect ( "spans removed" ) . rsplit_once ( '\n' ) )
550+ . map ( |( lines, _current) | lines) ;
551+ // Captures all lines after trailing whitespace, before the next comma
552+ let suffix_lines = value
553+ . decor ( )
554+ . suffix ( )
555+ . and_then ( |p| p. as_str ( ) . expect ( "spans removed" ) . split_once ( '\n' ) )
556+ . map ( |( _current, lines) | lines) ;
557+ let mut merged_lines = String :: new ( ) ;
558+ if let Some ( prefix_lines) = prefix_lines {
559+ merged_lines. push_str ( prefix_lines) ;
560+ merged_lines. push ( '\n' ) ;
561+ }
562+ if let Some ( suffix_lines) = suffix_lines {
563+ merged_lines. push_str ( suffix_lines) ;
564+ merged_lines. push ( '\n' ) ;
565+ }
566+
567+ let next_index = index; // Since `index` was removed, that effectively auto-advances us
568+ if let Some ( next) = array. get_mut ( next_index) {
569+ let next_decor = next. decor_mut ( ) ;
570+ let next_prefix = next_decor
571+ . prefix ( )
572+ . map ( |s| s. as_str ( ) . expect ( "spans removed" ) )
573+ . unwrap_or_default ( ) ;
574+ merged_lines. push_str ( next_prefix) ;
575+ next_decor. set_prefix ( merged_lines) ;
576+ } else {
577+ let trailing = array. trailing ( ) . as_str ( ) . expect ( "spans removed" ) ;
578+ merged_lines. push_str ( trailing) ;
579+ array. set_trailing ( merged_lines) ;
580+ }
544581}
0 commit comments