- for comment in generate_comments(node):
- if self.current_line.bracket_tracker.any_open_brackets():
- # any comment within brackets is subject to splitting
- self.current_line.append(comment)
- elif comment.type == token.COMMENT:
- # regular trailing comment
- self.current_line.append(comment)
- yield from self.line()
-
- else:
- # regular standalone comment, to be processed later (see
- # docstring in `generate_comments()`
- self.standalone_comments.append(comment)
- normalize_prefix(node)
- if node.type not in WHITESPACE:
- for comment in self.standalone_comments:
- yield from self.line()
-
- self.current_line.append(comment)
- yield from self.line()
-
- self.standalone_comments = []
- self.current_line.append(node)
+ any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
+ try:
+ for comment in generate_comments(node):
+ if any_open_brackets:
+ # any comment within brackets is subject to splitting
+ self.current_line.append(comment)
+ elif comment.type == token.COMMENT:
+ # regular trailing comment
+ self.current_line.append(comment)
+ yield from self.line()
+
+ else:
+ # regular standalone comment
+ yield from self.line()
+
+ self.current_line.append(comment)
+ yield from self.line()
+
+ except FormatOff as f_off:
+ f_off.trim_prefix(node)
+ yield from self.line(type=UnformattedLines)
+ yield from self.visit(node)
+
+ except FormatOn as f_on:
+ # This only happens here if somebody says "fmt: on" multiple
+ # times in a row.
+ f_on.trim_prefix(node)
+ yield from self.visit_default(node)
+
+ else:
+ normalize_prefix(node, inside_brackets=any_open_brackets)
+ if node.type not in WHITESPACE:
+ self.current_line.append(node)