def maybe_should_explode(self, closing: Leaf) -> bool:
"""Return True if this line should explode (always be split), that is when:
- - there's a pre-existing trailing comma here; and
+ - there's a trailing comma here; and
- it's not a one-tuple.
"""
if not (
closing.type in CLOSING_BRACKETS
and self.leaves
and self.leaves[-1].type == token.COMMA
- and not self.leaves[-1].was_checked # pre-existing
):
return False
yield from self.visit_default(node)
def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
- # Check if it's a docstring
- if prev_siblings_are(
- leaf.parent, [None, token.NEWLINE, token.INDENT, syms.simple_stmt]
- ) and is_multiline_string(leaf):
- prefix = " " * self.current_line.depth
- docstring = fix_docstring(leaf.value[3:-3], prefix)
- leaf.value = leaf.value[0:3] + docstring + leaf.value[-3:]
+ if is_docstring(leaf) and "\\\n" not in leaf.value:
+ # We're ignoring docstrings with backslash newline escapes because changing
+ # indentation of those changes the AST representation of the code.
+ prefix = get_string_prefix(leaf.value)
+ lead_len = len(prefix) + 3
+ tail_len = -3
+ indent = " " * 4 * self.current_line.depth
+ docstring = fix_docstring(leaf.value[lead_len:tail_len], indent)
+ if docstring:
+ if leaf.value[lead_len - 1] == docstring[0]:
+ docstring = " " + docstring
+ if leaf.value[tail_len + 1] == docstring[-1]:
+ docstring = docstring + " "
+ leaf.value = leaf.value[0:lead_len] + docstring + leaf.value[tail_len:]
normalize_string_quotes(leaf)
yield from self.visit_default(leaf)
# All splits failed, best effort split with no omits.
# This mostly happens to multiline strings that are by definition
# reported as not fitting a single line, as well as lines that contain
- # pre-existing trailing commas (those have to be exploded).
+ # trailing commas (those have to be exploded).
yield from right_hand_split(
line, line_length=mode.line_length, features=features
)
if leaves[i].type != token.COMMA:
new_comma = Leaf(token.COMMA, ",")
- new_comma.was_checked = True
leaves.insert(i + 1, new_comma)
break
and current_line.leaves[-1].type != STANDALONE_COMMENT
):
new_comma = Leaf(token.COMMA, ",")
- new_comma.was_checked = True
current_line.append(new_comma)
yield current_line
# than one of them (we're excluding the trailing comma and if the delimiter priority
# is still commas, that means there's more).
exclude = set()
- pre_existing_trailing_comma = False
+ trailing_comma = False
try:
last_leaf = line.leaves[-1]
if last_leaf.type == token.COMMA:
- pre_existing_trailing_comma = not last_leaf.was_checked
+ trailing_comma = True
exclude.add(id(last_leaf))
max_priority = line.bracket_tracker.max_delimiter_priority(exclude=exclude)
except (IndexError, ValueError):
return False
return max_priority == COMMA_PRIORITY and (
+ trailing_comma
# always explode imports
- opening_bracket.parent.type in {syms.atom, syms.import_from}
- or pre_existing_trailing_comma
+ or opening_bracket.parent.type in {syms.atom, syms.import_from}
)
line.should_explode
and prev
and prev.type == token.COMMA
- and not prev.was_checked
and not is_one_tuple_between(
leaf.opening_bracket, leaf, line.leaves
)
):
- # Never omit bracket pairs with pre-existing trailing commas.
+ # Never omit bracket pairs with trailing commas.
# We need to explode on those.
break
line.should_explode
and prev
and prev.type == token.COMMA
- and not prev.was_checked
and not is_one_tuple_between(leaf.opening_bracket, leaf, line.leaves)
):
- # Never omit bracket pairs with pre-existing trailing commas.
+ # Never omit bracket pairs with trailing commas.
# We need to explode on those.
break
# unnecessary.
return True
- if (
- line.should_explode
- and penultimate.type == token.COMMA
- and not penultimate.was_checked
- ):
+ if line.should_explode and penultimate.type == token.COMMA:
# The rightmost non-omitted bracket pair is the one we want to explode on.
return True
main()
+def is_docstring(leaf: Leaf) -> bool:
+ if not is_multiline_string(leaf):
+ # For the purposes of docstring re-indentation, we don't need to do anything
+ # with single-line docstrings.
+ return False
+
+ if prev_siblings_are(
+ leaf.parent, [None, token.NEWLINE, token.INDENT, syms.simple_stmt]
+ ):
+ return True
+
+ # Multiline docstring on the same line as the `def`.
+ if prev_siblings_are(leaf.parent, [syms.parameters, token.COLON, syms.simple_stmt]):
+ # `syms.parameters` is only used in funcdefs and async_funcdefs in the Python
+ # grammar. We're safe to return True without further checks.
+ return True
+
+ return False
+
+
def fix_docstring(docstring: str, prefix: str) -> str:
# https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation
if not docstring:
trimmed.append(prefix + stripped_line)
else:
trimmed.append("")
- # Return a single string:
return "\n".join(trimmed)