MCPcopy
hub / github.com/psf/black / visit_STANDALONE_COMMENT

Method visit_STANDALONE_COMMENT

src/black/linegen.py:390–431  ·  view source on GitHub ↗
(self, leaf: Leaf)

Source from the content-addressed store, hash-verified

388 yield from self.line()
389
390 def visit_STANDALONE_COMMENT(self, leaf: Leaf) -> Iterator[Line]:
391 any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
392 if not any_open_brackets:
393 yield from self.line()
394 # STANDALONE_COMMENT nodes created by our special handling in
395 # normalize_fmt_off for comment-only blocks have fmt:off as the first
396 # line and fmt:on as the last line (each directive on its own line,
397 # not embedded in other text). These should be appended directly
398 # without calling visit_default, which would process their prefix and
399 # lose indentation. Normal STANDALONE_COMMENT nodes go through
400 # visit_default.
401 value = leaf.value
402 lines = value.splitlines()
403 is_fmt_off_block = (
404 len(lines) >= 2
405 and contains_fmt_directive(lines[0], FMT_OFF)
406 and contains_fmt_directive(lines[-1], FMT_ON)
407 )
408 if is_fmt_off_block:
409 # This is a fmt:off/on block from normalize_fmt_off - we still need
410 # to process any prefix comments (like markdown comments) but append
411 # the fmt block itself directly to preserve its formatting
412
413 # Only process prefix comments if there actually is a prefix with comments
414 if leaf.prefix and any(
415 line.strip().startswith("#")
416 and not contains_fmt_directive(line.strip())
417 for line in leaf.prefix.split("\n")
418 ):
419 for comment in generate_comments(leaf, mode=self.mode):
420 yield from self.line()
421 self.current_line.append(comment)
422 yield from self.line()
423 # Clear the prefix since we've processed it as comments above
424 leaf.prefix = ""
425
426 self.current_line.append(leaf)
427 if not any_open_brackets:
428 yield from self.line()
429 else:
430 # Normal standalone comment - process through visit_default
431 yield from self.visit_default(leaf)
432
433 def visit_factor(self, node: Node) -> Iterator[Line]:
434 """Force parentheses between a unary op and a binary power:

Callers

nothing calls this directly

Calls 6

lineMethod · 0.95
visit_defaultMethod · 0.95
contains_fmt_directiveFunction · 0.90
generate_commentsFunction · 0.90
any_open_bracketsMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected