@@ -1138,11 +1138,13 @@ def _group_values(self, value: list[Item]) -> list[_ArrayItemGroup]:
11381138 """Group the values into (indent, value, comma, comment) tuples"""
11391139 groups = []
11401140 this_group = _ArrayItemGroup ()
1141+ start_new_group = False
11411142 for item in value :
11421143 if isinstance (item , Whitespace ):
1143- if "," not in item .s :
1144+ if "," not in item .s or start_new_group :
11441145 groups .append (this_group )
11451146 this_group = _ArrayItemGroup (indent = item )
1147+ start_new_group = False
11461148 else :
11471149 if this_group .value is None :
11481150 # when comma is met and no value is provided, add a dummy Null
@@ -1152,6 +1154,8 @@ def _group_values(self, value: list[Item]) -> list[_ArrayItemGroup]:
11521154 if this_group .value is None :
11531155 this_group .value = Null ()
11541156 this_group .comment = item
1157+ # Comments are the last item in a group.
1158+ start_new_group = True
11551159 elif this_group .value is None :
11561160 this_group .value = item
11571161 else :
@@ -1399,6 +1403,7 @@ def __delitem__(self, key: int | slice):
13991403 if not isinstance (key , slice ):
14001404 raise IndexError ("list index out of range" ) from e
14011405 else :
1406+ group_rm = self ._value [idx ]
14021407 del self ._value [idx ]
14031408 if (
14041409 idx == 0
@@ -1408,6 +1413,44 @@ def __delitem__(self, key: int | slice):
14081413 ):
14091414 # Remove the indentation of the first item if not newline
14101415 self ._value [idx ].indent = None
1416+ comma_in_indent = (
1417+ group_rm .indent is not None and "," in group_rm .indent .s
1418+ )
1419+ comma_in_comma = group_rm .comma is not None and "," in group_rm .comma .s
1420+ if comma_in_indent and comma_in_comma :
1421+ # Removed group had both commas. Add one to the next group.
1422+ group = self ._value [idx ] if len (self ._value ) > idx else None
1423+ if group is not None :
1424+ if group .indent is None :
1425+ group .indent = Whitespace ("," )
1426+ elif "," not in group .indent .s :
1427+ # Insert the comma after the newline
1428+ try :
1429+ newline_index = group .indent .s .index ("\n " )
1430+ group .indent ._s = (
1431+ group .indent .s [: newline_index + 1 ]
1432+ + ","
1433+ + group .indent .s [newline_index + 1 :]
1434+ )
1435+ except ValueError :
1436+ group .indent ._s = "," + group .indent .s
1437+ elif not comma_in_indent and not comma_in_comma :
1438+ # Removed group had no commas. Remove the next comma found.
1439+ for j in range (idx , len (self ._value )):
1440+ group = self ._value [j ]
1441+ if group .indent is not None and "," in group .indent .s :
1442+ group .indent ._s = group .indent .s .replace ("," , "" , 1 )
1443+ break
1444+ if group_rm .indent is not None and "\n " in group_rm .indent .s :
1445+ # Restore the removed group's newline onto the next group
1446+ # if the next group does not have a newline.
1447+ # i.e. the two were on the same line
1448+ group = self ._value [idx ] if len (self ._value ) > idx else None
1449+ if group is not None and (
1450+ group .indent is None or "\n " not in group .indent .s
1451+ ):
1452+ group .indent = group_rm .indent
1453+
14111454 if len (self ._value ) > 0 :
14121455 v = self ._value [- 1 ]
14131456 if not v .is_whitespace ():
0 commit comments