@@ -830,6 +830,25 @@ def split_wrapper(line: Line, features: Collection[Feature] = ()) -> Iterator[Li
830
830
return split_wrapper
831
831
832
832
833
+ def _get_last_non_comment_leaf (line : Line ) -> Optional [int ]:
834
+ for leaf_idx in range (len (line .leaves ) - 1 , 0 , - 1 ):
835
+ if line .leaves [leaf_idx ].type != STANDALONE_COMMENT :
836
+ return leaf_idx
837
+ return None
838
+
839
+
840
+ def _safe_add_trailing_comma (safe : bool , delimiter_priority : int , line : Line ) -> Line :
841
+ if (
842
+ safe
843
+ and delimiter_priority == COMMA_PRIORITY
844
+ and line .leaves [- 1 ].type != token .COMMA
845
+ and line .leaves [- 1 ].type != STANDALONE_COMMENT
846
+ ):
847
+ new_comma = Leaf (token .COMMA , "," )
848
+ line .append (new_comma )
849
+ return line
850
+
851
+
833
852
@dont_increase_indentation
834
853
def delimiter_split (line : Line , features : Collection [Feature ] = ()) -> Iterator [Line ]:
835
854
"""Split according to delimiters of the highest priority.
@@ -871,7 +890,9 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
871
890
)
872
891
current_line .append (leaf )
873
892
874
- for leaf in line .leaves :
893
+ last_non_comment_leaf = _get_last_non_comment_leaf (line )
894
+
895
+ for leaf_idx , leaf in enumerate (line .leaves ):
875
896
yield from append_to_line (leaf )
876
897
877
898
for comment_after in line .comments_after (leaf ):
@@ -888,6 +909,11 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
888
909
trailing_comma_safe and Feature .TRAILING_COMMA_IN_CALL in features
889
910
)
890
911
912
+ if leaf_idx == last_non_comment_leaf and leaf_idx != len (line .leaves ) - 1 :
913
+ current_line = _safe_add_trailing_comma (
914
+ trailing_comma_safe , delimiter_priority , current_line
915
+ )
916
+
891
917
leaf_priority = bt .delimiters .get (id (leaf ))
892
918
if leaf_priority == delimiter_priority :
893
919
yield current_line
@@ -896,14 +922,9 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
896
922
mode = line .mode , depth = line .depth , inside_brackets = line .inside_brackets
897
923
)
898
924
if current_line :
899
- if (
900
- trailing_comma_safe
901
- and delimiter_priority == COMMA_PRIORITY
902
- and current_line .leaves [- 1 ].type != token .COMMA
903
- and current_line .leaves [- 1 ].type != STANDALONE_COMMENT
904
- ):
905
- new_comma = Leaf (token .COMMA , "," )
906
- current_line .append (new_comma )
925
+ current_line = _safe_add_trailing_comma (
926
+ trailing_comma_safe , delimiter_priority , current_line
927
+ )
907
928
yield current_line
908
929
909
930
0 commit comments