12
12
from black .brackets import (
13
13
COMMA_PRIORITY ,
14
14
DOT_PRIORITY ,
15
+ STRING_PRIORITY ,
15
16
get_leaves_inside_matching_brackets ,
16
17
max_delimiter_priority_in_atom ,
17
18
)
@@ -1143,6 +1144,9 @@ def _safe_add_trailing_comma(safe: bool, delimiter_priority: int, line: Line) ->
1143
1144
return line
1144
1145
1145
1146
1147
+ MIGRATE_COMMENT_DELIMITERS = {STRING_PRIORITY , COMMA_PRIORITY }
1148
+
1149
+
1146
1150
@dont_increase_indentation
1147
1151
def delimiter_split (
1148
1152
line : Line , features : Collection [Feature ], mode : Mode
@@ -1187,12 +1191,22 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
1187
1191
)
1188
1192
current_line .append (leaf )
1189
1193
1194
+ def append_comments (leaf : Leaf ) -> Iterator [Line ]:
1195
+ for comment_after in line .comments_after (leaf ):
1196
+ yield from append_to_line (comment_after )
1197
+
1190
1198
last_non_comment_leaf = _get_last_non_comment_leaf (line )
1191
1199
for leaf_idx , leaf in enumerate (line .leaves ):
1192
1200
yield from append_to_line (leaf )
1193
1201
1194
- for comment_after in line .comments_after (leaf ):
1195
- yield from append_to_line (comment_after )
1202
+ previous_priority = leaf_idx > 0 and bt .delimiters .get (
1203
+ id (line .leaves [leaf_idx - 1 ])
1204
+ )
1205
+ if (
1206
+ previous_priority != delimiter_priority
1207
+ or delimiter_priority in MIGRATE_COMMENT_DELIMITERS
1208
+ ):
1209
+ yield from append_comments (leaf )
1196
1210
1197
1211
lowest_depth = min (lowest_depth , leaf .bracket_depth )
1198
1212
if trailing_comma_safe and leaf .bracket_depth == lowest_depth :
@@ -1205,8 +1219,13 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
1205
1219
1206
1220
leaf_priority = bt .delimiters .get (id (leaf ))
1207
1221
if leaf_priority == delimiter_priority :
1208
- yield current_line
1222
+ if (
1223
+ leaf_idx + 1 < len (line .leaves )
1224
+ and delimiter_priority not in MIGRATE_COMMENT_DELIMITERS
1225
+ ):
1226
+ yield from append_comments (line .leaves [leaf_idx + 1 ])
1209
1227
1228
+ yield current_line
1210
1229
current_line = Line (
1211
1230
mode = line .mode , depth = line .depth , inside_brackets = line .inside_brackets
1212
1231
)
0 commit comments