fix: additional newline added to docstring when the previous line len… · psf/black@7edb50f (original) (raw)

File tree

6 files changed

lines changed

6 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@
16 16
17 17 - Move the `hug_parens_with_braces_and_square_brackets` feature to the unstable style
18 18 due to an outstanding crash and proposed formatting tweaks (#4198)
19 +- Checking for newline before adding one on docstring that is almost at the line limit
20 + (#4185)
19 21
20 22 ### Configuration
21 23
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@ Currently, the following features are included in the preview style:
28 28 longer normalized
29 29 - `typed_params_trailing_comma`: consistently add trailing commas to typed function
30 30 parameters
31 +- `docstring_check_for_newline`: checks if there is a newline before the terminating
32 + quotes of a docstring
31 33
32 34 (labels/unstable-features)=
33 35
Original file line number Diff line number Diff line change
@@ -477,15 +477,22 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
477 477 last_line_length = len(lines[-1]) if docstring else 0
478 478
479 479 # If adding closing quotes would cause the last line to exceed
480 -# the maximum line length then put a line break before the
481 -# closing quotes
480 +# the maximum line length, and the closing quote is not
481 +# prefixed by a newline then put a line break before
482 +# the closing quotes
482 483 if (
483 484 len(lines) > 1
484 485 and last_line_length + quote_len > self.mode.line_length
485 486 and len(indent) + quote_len <= self.mode.line_length
486 487 and not has_trailing_backslash
487 488 ):
488 -leaf.value = prefix + quote + docstring + "\n" + indent + quote
489 +if (
490 +Preview.docstring_check_for_newline in self.mode
491 +and leaf.value[-1 - quote_len] == "\n"
492 + ):
493 +leaf.value = prefix + quote + docstring + quote
494 +else:
495 +leaf.value = prefix + quote + docstring + "\n" + indent + quote
489 496 else:
490 497 leaf.value = prefix + quote + docstring + quote
491 498 else:
Original file line number Diff line number Diff line change
@@ -177,6 +177,7 @@ class Preview(Enum):
177 177 wrap_long_dict_values_in_parens = auto()
178 178 multiline_string_handling = auto()
179 179 typed_params_trailing_comma = auto()
180 +docstring_check_for_newline = auto()
180 181
181 182
182 183 UNSTABLE_FEATURES: Set[Preview] = {
Original file line number Diff line number Diff line change
@@ -85,7 +85,8 @@
85 85 "no_normalize_fmt_skip_whitespace",
86 86 "wrap_long_dict_values_in_parens",
87 87 "multiline_string_handling",
88 -"typed_params_trailing_comma"
88 +"typed_params_trailing_comma",
89 +"docstring_check_for_newline"
89 90 ]
90 91 },
91 92 "description": "Enable specific features included in the `--unstable` style. Requires `--preview`. No compatibility guarantees are provided on the behavior or existence of any unstable features."
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 +# flags: --preview
2 +"""
3 +87 characters ............................................................................
4 +"""