Do not put the closing quotes in a docstring on a separate line (#3430)
Fixes #3320. Followup from #3044.
This commit is contained in:
parent
abd2b2556a
commit
7d062ecd5f
@ -15,6 +15,8 @@
|
|||||||
<!-- Changes that affect Black's preview style -->
|
<!-- Changes that affect Black's preview style -->
|
||||||
|
|
||||||
- Fix a crash in preview style with assert + parenthesized string (#3415)
|
- Fix a crash in preview style with assert + parenthesized string (#3415)
|
||||||
|
- Do not put the closing quotes in a docstring on a separate line, even if the line is
|
||||||
|
too long (#3430)
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
|
@ -389,19 +389,18 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
|
|||||||
# We need to find the length of the last line of the docstring
|
# We need to find the length of the last line of the docstring
|
||||||
# to find if we can add the closing quotes to the line without
|
# to find if we can add the closing quotes to the line without
|
||||||
# exceeding the maximum line length.
|
# exceeding the maximum line length.
|
||||||
# If docstring is one line, then we need to add the length
|
# If docstring is one line, we don't put the closing quotes on a
|
||||||
# of the indent, prefix, and starting quotes. Ending quotes are
|
# separate line because it looks ugly (#3320).
|
||||||
# handled later.
|
|
||||||
lines = docstring.splitlines()
|
lines = docstring.splitlines()
|
||||||
last_line_length = len(lines[-1]) if docstring else 0
|
last_line_length = len(lines[-1]) if docstring else 0
|
||||||
|
|
||||||
if len(lines) == 1:
|
|
||||||
last_line_length += len(indent) + len(prefix) + quote_len
|
|
||||||
|
|
||||||
# If adding closing quotes would cause the last line to exceed
|
# If adding closing quotes would cause the last line to exceed
|
||||||
# the maximum line length then put a line break before the
|
# the maximum line length then put a line break before the
|
||||||
# closing quotes
|
# closing quotes
|
||||||
if last_line_length + quote_len > self.mode.line_length:
|
if (
|
||||||
|
len(lines) > 1
|
||||||
|
and last_line_length + quote_len > self.mode.line_length
|
||||||
|
):
|
||||||
leaf.value = prefix + quote + docstring + "\n" + indent + quote
|
leaf.value = prefix + quote + docstring + "\n" + indent + quote
|
||||||
else:
|
else:
|
||||||
leaf.value = prefix + quote + docstring + quote
|
leaf.value = prefix + quote + docstring + quote
|
||||||
|
@ -54,13 +54,11 @@ def single_quote_docstring_over_line_limit2():
|
|||||||
|
|
||||||
|
|
||||||
def docstring_almost_at_line_limit():
|
def docstring_almost_at_line_limit():
|
||||||
"""long docstring.................................................................
|
"""long docstring................................................................."""
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def docstring_almost_at_line_limit_with_prefix():
|
def docstring_almost_at_line_limit_with_prefix():
|
||||||
f"""long docstring................................................................
|
f"""long docstring................................................................"""
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def mulitline_docstring_almost_at_line_limit():
|
def mulitline_docstring_almost_at_line_limit():
|
||||||
|
Loading…
Reference in New Issue
Block a user