Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a change #4562

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/black/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,48 +270,47 @@ def generate_ignored_nodes(
) -> Iterator[LN]:
"""Starting from the container of `leaf`, generate all leaves until `# fmt: on`.

If comment is skip, returns leaf only.
If the comment is `# fmt: skip`, returns leaf only.
Stops at the end of the block.
"""
if _contains_fmt_skip_comment(comment.value, mode):
yield from _generate_ignored_nodes_from_fmt_skip(leaf, comment)
return

container: Optional[LN] = container_of(leaf)
while container is not None and container.type != token.ENDMARKER:
if is_fmt_on(container):
return

# fix for fmt: on in children
# Handle multiline strings explicitly
if container.type == token.STRING and "\n" in container.value:
yield container
container = container.next_sibling
continue

# Fix for fmt: on in children
if children_contains_fmt_on(container):
for index, child in enumerate(container.children):
if isinstance(child, Leaf) and is_fmt_on(child):
if child.type in CLOSING_BRACKETS:
# This means `# fmt: on` is placed at a different bracket level
# than `# fmt: off`. This is an invalid use, but as a courtesy,
# we include this closing bracket in the ignored nodes.
# The alternative is to fail the formatting.
# Handle fmt: on at different bracket levels
yield child
return
if (
child.type == token.INDENT
and index < len(container.children) - 1
and children_contains_fmt_on(container.children[index + 1])
):
# This means `# fmt: on` is placed right after an indentation
# level, and we shouldn't swallow the previous INDENT token.
return
if children_contains_fmt_on(child):
return
yield child
else:
if container.type == token.DEDENT and container.next_sibling is None:
# This can happen when there is no matching `# fmt: on` comment at the
# same level as `# fmt: on`. We need to keep this DEDENT.
return
yield container
container = container.next_sibling



def _generate_ignored_nodes_from_fmt_skip(
leaf: Leaf, comment: ProtoComment
) -> Iterator[LN]:
Expand Down
17 changes: 7 additions & 10 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Generating lines of code.
"""
" Generating lines of code. "

import re
import sys
Expand Down Expand Up @@ -85,23 +83,22 @@
from blib2to3.pgen2 import token
from blib2to3.pytree import Leaf, Node

# types
" types "
LeafID = int
LN = Union[Leaf, Node]


class CannotSplit(CannotTransform):
"""A readable split that fits the allotted line length is impossible."""
" A readable split that fits the allotted line length is impossible. "


# This isn't a dataclass because @dataclass + Generic breaks mypyc.
# See also https://github.com/mypyc/mypyc/issues/827.
" This isn't a dataclass because @dataclass + Generic breaks mypyc. "
" See also https://github.com/mypyc/mypyc/issues/827. "
class LineGenerator(Visitor[Line]):
"""Generates reformatted Line objects. Empty lines are not emitted.
""" Generates reformatted Line objects. Empty lines are not emitted.

Note: destroys the tree it's visiting by mutating prefixes of its leaves
in ways that will no longer stringify to valid Python code on the tree.
"""
in ways that will no longer stringify to valid Python code on the tree. """

def __init__(self, mode: Mode, features: Collection[Feature]) -> None:
self.mode = mode
Expand Down
2 changes: 1 addition & 1 deletion src/black/ranges.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Functions related to Black's formatting by line ranges feature."""
" Functions related to Black's formatting by line ranges feature. "

import difflib
from collections.abc import Collection, Iterator, Sequence
Expand Down
Loading