@@ -1083,30 +1083,33 @@ def _getstate(self, protocol: int = 3) -> tuple:
10831083
10841084
10851085class _ArrayItemGroup :
1086- __slots__ = ("comma" , "comment" , "indent" , "value" )
1086+ __slots__ = ("comma" , "comment" , "comment2" , " indent" , "value" )
10871087
10881088 def __init__ (
10891089 self ,
10901090 value : Item | None = None ,
10911091 indent : Whitespace | None = None ,
10921092 comma : Whitespace | None = None ,
10931093 comment : Comment | None = None ,
1094+ comment2 : Comment | None = None ,
10941095 ) -> None :
10951096 self .value = value
10961097 self .indent = indent
10971098 self .comma = comma
10981099 self .comment = comment
1100+ self .comment2 = comment2
10991101
11001102 def __iter__ (self ) -> Iterator [Item ]:
11011103 return filter (
1102- lambda x : x is not None , (self .indent , self .value , self .comma , self .comment )
1104+ lambda x : x is not None ,
1105+ (self .indent , self .value , self .comment , self .comma , self .comment2 ),
11031106 )
11041107
11051108 def __repr__ (self ) -> str :
11061109 return repr (tuple (self ))
11071110
11081111 def is_whitespace (self ) -> bool :
1109- return self .value is None and self .comment is None
1112+ return self .value is None and self .comment is None and self . comment2 is None
11101113
11111114 def __bool__ (self ) -> bool :
11121115 try :
@@ -1135,12 +1138,24 @@ def __init__(
11351138 self ._reindex ()
11361139
11371140 def _group_values (self , value : list [Item ]) -> list [_ArrayItemGroup ]:
1138- """Group the values into (indent, value, comma, comment ) tuples"""
1141+ """Group the values into (indent, value, comment, comma, comment2 ) tuples"""
11391142 groups = []
11401143 this_group = _ArrayItemGroup ()
1141- for item in value :
1144+ for i , item in enumerate ( value ) :
11421145 if isinstance (item , Whitespace ):
11431146 if "," not in item .s :
1147+ # Check the next item to see if it has a comma.
1148+ if (
1149+ len (value ) > i + 1
1150+ and isinstance (value [i + 1 ], Whitespace )
1151+ and "," in value [i + 1 ].s
1152+ ):
1153+ # Prepend this whitespace to that item
1154+ value [i + 1 ] = Whitespace (item .s + value [i + 1 ].s )
1155+ # Set an indent if this group does not have one.
1156+ if not this_group .indent :
1157+ this_group .indent = Whitespace ("\n " )
1158+ continue
11441159 groups .append (this_group )
11451160 this_group = _ArrayItemGroup (indent = item )
11461161 else :
@@ -1151,7 +1166,15 @@ def _group_values(self, value: list[Item]) -> list[_ArrayItemGroup]:
11511166 elif isinstance (item , Comment ):
11521167 if this_group .value is None :
11531168 this_group .value = Null ()
1154- this_group .comment = item
1169+ # It's possible to have two comments per group.
1170+ # Comment one, if present, is always immediately after the value,
1171+ # and Comment two after a Whitespace with comma.
1172+ if isinstance (value [i - 1 ], Whitespace ) and "," in value [i - 1 ].s :
1173+ # If this is Comment two, this is the end of the current group,
1174+ # But let the next item end/start it.
1175+ this_group .comment2 = item
1176+ else :
1177+ this_group .comment = item
11551178 elif this_group .value is None :
11561179 this_group .value = item
11571180 else :
@@ -1209,8 +1232,9 @@ def as_string(self) -> str:
12091232 self .trivia .indent
12101233 + " " * 4
12111234 + v .value .as_string ()
1212- + ("," if not isinstance (v .value , Null ) else "" )
12131235 + (v .comment .as_string () if v .comment is not None else "" )
1236+ + ("," if not isinstance (v .value , Null ) else "" )
1237+ + (v .comment2 .as_string () if v .comment2 is not None else "" )
12141238 + "\n "
12151239 for v in self ._value
12161240 if v .value is not None
@@ -1372,6 +1396,11 @@ def insert(self, pos: int, value: Any) -> None:
13721396 # Copy the comma from the last item if 1) it contains a value and
13731397 # 2) the array is multiline
13741398 comma = last_item .comma
1399+ if last_item .comment and not last_item .comma :
1400+ # Arrays with a single value might not have a comma after the value, but may have a comment.
1401+ # Move comment one to comment two if so.
1402+ last_item .comment2 = last_item .comment
1403+ last_item .comment = None
13751404 if last_item .comma is None and not isinstance (last_item .value , Null ):
13761405 # Add comma to the last item to separate it from the following items.
13771406 last_item .comma = Whitespace ("," )
0 commit comments