]> git.madduck.net Git - etc/vim.git/blob - src/black/lines.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Refactor logic for stub empty lines (#2796)
[etc/vim.git] / src / black / lines.py
1 from dataclasses import dataclass, field
2 import itertools
3 import sys
4 from typing import (
5     Callable,
6     Collection,
7     Dict,
8     Iterator,
9     List,
10     Optional,
11     Sequence,
12     Tuple,
13     TypeVar,
14     cast,
15 )
16
17 from blib2to3.pytree import Node, Leaf
18 from blib2to3.pgen2 import token
19
20 from black.brackets import BracketTracker, DOT_PRIORITY
21 from black.mode import Mode
22 from black.nodes import STANDALONE_COMMENT, TEST_DESCENDANTS
23 from black.nodes import BRACKETS, OPENING_BRACKETS, CLOSING_BRACKETS
24 from black.nodes import syms, whitespace, replace_child, child_towards
25 from black.nodes import is_multiline_string, is_import, is_type_comment, last_two_except
26 from black.nodes import is_one_tuple_between
27
28 # types
29 T = TypeVar("T")
30 Index = int
31 LeafID = int
32
33
34 @dataclass
35 class Line:
36     """Holds leaves and comments. Can be printed with `str(line)`."""
37
38     mode: Mode
39     depth: int = 0
40     leaves: List[Leaf] = field(default_factory=list)
41     # keys ordered like `leaves`
42     comments: Dict[LeafID, List[Leaf]] = field(default_factory=dict)
43     bracket_tracker: BracketTracker = field(default_factory=BracketTracker)
44     inside_brackets: bool = False
45     should_split_rhs: bool = False
46     magic_trailing_comma: Optional[Leaf] = None
47
48     def append(self, leaf: Leaf, preformatted: bool = False) -> None:
49         """Add a new `leaf` to the end of the line.
50
51         Unless `preformatted` is True, the `leaf` will receive a new consistent
52         whitespace prefix and metadata applied by :class:`BracketTracker`.
53         Trailing commas are maybe removed, unpacked for loop variables are
54         demoted from being delimiters.
55
56         Inline comments are put aside.
57         """
58         has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
59         if not has_value:
60             return
61
62         if token.COLON == leaf.type and self.is_class_paren_empty:
63             del self.leaves[-2:]
64         if self.leaves and not preformatted:
65             # Note: at this point leaf.prefix should be empty except for
66             # imports, for which we only preserve newlines.
67             leaf.prefix += whitespace(
68                 leaf, complex_subscript=self.is_complex_subscript(leaf)
69             )
70         if self.inside_brackets or not preformatted:
71             self.bracket_tracker.mark(leaf)
72             if self.mode.magic_trailing_comma:
73                 if self.has_magic_trailing_comma(leaf):
74                     self.magic_trailing_comma = leaf
75             elif self.has_magic_trailing_comma(leaf, ensure_removable=True):
76                 self.remove_trailing_comma()
77         if not self.append_comment(leaf):
78             self.leaves.append(leaf)
79
80     def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
81         """Like :func:`append()` but disallow invalid standalone comment structure.
82
83         Raises ValueError when any `leaf` is appended after a standalone comment
84         or when a standalone comment is not the first leaf on the line.
85         """
86         if self.bracket_tracker.depth == 0:
87             if self.is_comment:
88                 raise ValueError("cannot append to standalone comments")
89
90             if self.leaves and leaf.type == STANDALONE_COMMENT:
91                 raise ValueError(
92                     "cannot append standalone comments to a populated line"
93                 )
94
95         self.append(leaf, preformatted=preformatted)
96
97     @property
98     def is_comment(self) -> bool:
99         """Is this line a standalone comment?"""
100         return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
101
102     @property
103     def is_decorator(self) -> bool:
104         """Is this line a decorator?"""
105         return bool(self) and self.leaves[0].type == token.AT
106
107     @property
108     def is_import(self) -> bool:
109         """Is this an import line?"""
110         return bool(self) and is_import(self.leaves[0])
111
112     @property
113     def is_class(self) -> bool:
114         """Is this line a class definition?"""
115         return (
116             bool(self)
117             and self.leaves[0].type == token.NAME
118             and self.leaves[0].value == "class"
119         )
120
121     @property
122     def is_stub_class(self) -> bool:
123         """Is this line a class definition with a body consisting only of "..."?"""
124         return self.is_class and self.leaves[-3:] == [
125             Leaf(token.DOT, ".") for _ in range(3)
126         ]
127
128     @property
129     def is_def(self) -> bool:
130         """Is this a function definition? (Also returns True for async defs.)"""
131         try:
132             first_leaf = self.leaves[0]
133         except IndexError:
134             return False
135
136         try:
137             second_leaf: Optional[Leaf] = self.leaves[1]
138         except IndexError:
139             second_leaf = None
140         return (first_leaf.type == token.NAME and first_leaf.value == "def") or (
141             first_leaf.type == token.ASYNC
142             and second_leaf is not None
143             and second_leaf.type == token.NAME
144             and second_leaf.value == "def"
145         )
146
147     @property
148     def is_class_paren_empty(self) -> bool:
149         """Is this a class with no base classes but using parentheses?
150
151         Those are unnecessary and should be removed.
152         """
153         return (
154             bool(self)
155             and len(self.leaves) == 4
156             and self.is_class
157             and self.leaves[2].type == token.LPAR
158             and self.leaves[2].value == "("
159             and self.leaves[3].type == token.RPAR
160             and self.leaves[3].value == ")"
161         )
162
163     @property
164     def is_triple_quoted_string(self) -> bool:
165         """Is the line a triple quoted string?"""
166         return (
167             bool(self)
168             and self.leaves[0].type == token.STRING
169             and self.leaves[0].value.startswith(('"""', "'''"))
170         )
171
172     def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
173         """If so, needs to be split before emitting."""
174         for leaf in self.leaves:
175             if leaf.type == STANDALONE_COMMENT and leaf.bracket_depth <= depth_limit:
176                 return True
177
178         return False
179
180     def contains_uncollapsable_type_comments(self) -> bool:
181         ignored_ids = set()
182         try:
183             last_leaf = self.leaves[-1]
184             ignored_ids.add(id(last_leaf))
185             if last_leaf.type == token.COMMA or (
186                 last_leaf.type == token.RPAR and not last_leaf.value
187             ):
188                 # When trailing commas or optional parens are inserted by Black for
189                 # consistency, comments after the previous last element are not moved
190                 # (they don't have to, rendering will still be correct).  So we ignore
191                 # trailing commas and invisible.
192                 last_leaf = self.leaves[-2]
193                 ignored_ids.add(id(last_leaf))
194         except IndexError:
195             return False
196
197         # A type comment is uncollapsable if it is attached to a leaf
198         # that isn't at the end of the line (since that could cause it
199         # to get associated to a different argument) or if there are
200         # comments before it (since that could cause it to get hidden
201         # behind a comment.
202         comment_seen = False
203         for leaf_id, comments in self.comments.items():
204             for comment in comments:
205                 if is_type_comment(comment):
206                     if comment_seen or (
207                         not is_type_comment(comment, " ignore")
208                         and leaf_id not in ignored_ids
209                     ):
210                         return True
211
212                 comment_seen = True
213
214         return False
215
216     def contains_unsplittable_type_ignore(self) -> bool:
217         if not self.leaves:
218             return False
219
220         # If a 'type: ignore' is attached to the end of a line, we
221         # can't split the line, because we can't know which of the
222         # subexpressions the ignore was meant to apply to.
223         #
224         # We only want this to apply to actual physical lines from the
225         # original source, though: we don't want the presence of a
226         # 'type: ignore' at the end of a multiline expression to
227         # justify pushing it all onto one line. Thus we
228         # (unfortunately) need to check the actual source lines and
229         # only report an unsplittable 'type: ignore' if this line was
230         # one line in the original code.
231
232         # Grab the first and last line numbers, skipping generated leaves
233         first_line = next((leaf.lineno for leaf in self.leaves if leaf.lineno != 0), 0)
234         last_line = next(
235             (leaf.lineno for leaf in reversed(self.leaves) if leaf.lineno != 0), 0
236         )
237
238         if first_line == last_line:
239             # We look at the last two leaves since a comma or an
240             # invisible paren could have been added at the end of the
241             # line.
242             for node in self.leaves[-2:]:
243                 for comment in self.comments.get(id(node), []):
244                     if is_type_comment(comment, " ignore"):
245                         return True
246
247         return False
248
249     def contains_multiline_strings(self) -> bool:
250         return any(is_multiline_string(leaf) for leaf in self.leaves)
251
252     def has_magic_trailing_comma(
253         self, closing: Leaf, ensure_removable: bool = False
254     ) -> bool:
255         """Return True if we have a magic trailing comma, that is when:
256         - there's a trailing comma here
257         - it's not a one-tuple
258         Additionally, if ensure_removable:
259         - it's not from square bracket indexing
260         """
261         if not (
262             closing.type in CLOSING_BRACKETS
263             and self.leaves
264             and self.leaves[-1].type == token.COMMA
265         ):
266             return False
267
268         if closing.type == token.RBRACE:
269             return True
270
271         if closing.type == token.RSQB:
272             if not ensure_removable:
273                 return True
274             comma = self.leaves[-1]
275             return bool(comma.parent and comma.parent.type == syms.listmaker)
276
277         if self.is_import:
278             return True
279
280         if not is_one_tuple_between(closing.opening_bracket, closing, self.leaves):
281             return True
282
283         return False
284
285     def append_comment(self, comment: Leaf) -> bool:
286         """Add an inline or standalone comment to the line."""
287         if (
288             comment.type == STANDALONE_COMMENT
289             and self.bracket_tracker.any_open_brackets()
290         ):
291             comment.prefix = ""
292             return False
293
294         if comment.type != token.COMMENT:
295             return False
296
297         if not self.leaves:
298             comment.type = STANDALONE_COMMENT
299             comment.prefix = ""
300             return False
301
302         last_leaf = self.leaves[-1]
303         if (
304             last_leaf.type == token.RPAR
305             and not last_leaf.value
306             and last_leaf.parent
307             and len(list(last_leaf.parent.leaves())) <= 3
308             and not is_type_comment(comment)
309         ):
310             # Comments on an optional parens wrapping a single leaf should belong to
311             # the wrapped node except if it's a type comment. Pinning the comment like
312             # this avoids unstable formatting caused by comment migration.
313             if len(self.leaves) < 2:
314                 comment.type = STANDALONE_COMMENT
315                 comment.prefix = ""
316                 return False
317
318             last_leaf = self.leaves[-2]
319         self.comments.setdefault(id(last_leaf), []).append(comment)
320         return True
321
322     def comments_after(self, leaf: Leaf) -> List[Leaf]:
323         """Generate comments that should appear directly after `leaf`."""
324         return self.comments.get(id(leaf), [])
325
326     def remove_trailing_comma(self) -> None:
327         """Remove the trailing comma and moves the comments attached to it."""
328         trailing_comma = self.leaves.pop()
329         trailing_comma_comments = self.comments.pop(id(trailing_comma), [])
330         self.comments.setdefault(id(self.leaves[-1]), []).extend(
331             trailing_comma_comments
332         )
333
334     def is_complex_subscript(self, leaf: Leaf) -> bool:
335         """Return True iff `leaf` is part of a slice with non-trivial exprs."""
336         open_lsqb = self.bracket_tracker.get_open_lsqb()
337         if open_lsqb is None:
338             return False
339
340         subscript_start = open_lsqb.next_sibling
341
342         if isinstance(subscript_start, Node):
343             if subscript_start.type == syms.listmaker:
344                 return False
345
346             if subscript_start.type == syms.subscriptlist:
347                 subscript_start = child_towards(subscript_start, leaf)
348         return subscript_start is not None and any(
349             n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
350         )
351
352     def enumerate_with_length(
353         self, reversed: bool = False
354     ) -> Iterator[Tuple[Index, Leaf, int]]:
355         """Return an enumeration of leaves with their length.
356
357         Stops prematurely on multiline strings and standalone comments.
358         """
359         op = cast(
360             Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
361             enumerate_reversed if reversed else enumerate,
362         )
363         for index, leaf in op(self.leaves):
364             length = len(leaf.prefix) + len(leaf.value)
365             if "\n" in leaf.value:
366                 return  # Multiline strings, we can't continue.
367
368             for comment in self.comments_after(leaf):
369                 length += len(comment.value)
370
371             yield index, leaf, length
372
373     def clone(self) -> "Line":
374         return Line(
375             mode=self.mode,
376             depth=self.depth,
377             inside_brackets=self.inside_brackets,
378             should_split_rhs=self.should_split_rhs,
379             magic_trailing_comma=self.magic_trailing_comma,
380         )
381
382     def __str__(self) -> str:
383         """Render the line."""
384         if not self:
385             return "\n"
386
387         indent = "    " * self.depth
388         leaves = iter(self.leaves)
389         first = next(leaves)
390         res = f"{first.prefix}{indent}{first.value}"
391         for leaf in leaves:
392             res += str(leaf)
393         for comment in itertools.chain.from_iterable(self.comments.values()):
394             res += str(comment)
395
396         return res + "\n"
397
398     def __bool__(self) -> bool:
399         """Return True if the line has leaves or comments."""
400         return bool(self.leaves or self.comments)
401
402
403 @dataclass
404 class EmptyLineTracker:
405     """Provides a stateful method that returns the number of potential extra
406     empty lines needed before and after the currently processed line.
407
408     Note: this tracker works on lines that haven't been split yet.  It assumes
409     the prefix of the first leaf consists of optional newlines.  Those newlines
410     are consumed by `maybe_empty_lines()` and included in the computation.
411     """
412
413     is_pyi: bool = False
414     previous_line: Optional[Line] = None
415     previous_after: int = 0
416     previous_defs: List[int] = field(default_factory=list)
417
418     def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
419         """Return the number of extra empty lines before and after the `current_line`.
420
421         This is for separating `def`, `async def` and `class` with extra empty
422         lines (two on module-level).
423         """
424         before, after = self._maybe_empty_lines(current_line)
425         before = (
426             # Black should not insert empty lines at the beginning
427             # of the file
428             0
429             if self.previous_line is None
430             else before - self.previous_after
431         )
432         self.previous_after = after
433         self.previous_line = current_line
434         return before, after
435
436     def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
437         max_allowed = 1
438         if current_line.depth == 0:
439             max_allowed = 1 if self.is_pyi else 2
440         if current_line.leaves:
441             # Consume the first leaf's extra newlines.
442             first_leaf = current_line.leaves[0]
443             before = first_leaf.prefix.count("\n")
444             before = min(before, max_allowed)
445             first_leaf.prefix = ""
446         else:
447             before = 0
448         depth = current_line.depth
449         while self.previous_defs and self.previous_defs[-1] >= depth:
450             if self.is_pyi:
451                 assert self.previous_line is not None
452                 if depth and not current_line.is_def and self.previous_line.is_def:
453                     # Empty lines between attributes and methods should be preserved.
454                     before = min(1, before)
455                 elif depth:
456                     before = 0
457                 else:
458                     before = 1
459             else:
460                 if depth:
461                     before = 1
462                 elif (
463                     not depth
464                     and self.previous_defs[-1]
465                     and current_line.leaves[-1].type == token.COLON
466                     and (
467                         current_line.leaves[0].value
468                         not in ("with", "try", "for", "while", "if", "match")
469                     )
470                 ):
471                     # We shouldn't add two newlines between an indented function and
472                     # a dependent non-indented clause. This is to avoid issues with
473                     # conditional function definitions that are technically top-level
474                     # and therefore get two trailing newlines, but look weird and
475                     # inconsistent when they're followed by elif, else, etc. This is
476                     # worse because these functions only get *one* preceding newline
477                     # already.
478                     before = 1
479                 else:
480                     before = 2
481             self.previous_defs.pop()
482         if current_line.is_decorator or current_line.is_def or current_line.is_class:
483             return self._maybe_empty_lines_for_class_or_def(current_line, before)
484
485         if (
486             self.previous_line
487             and self.previous_line.is_import
488             and not current_line.is_import
489             and depth == self.previous_line.depth
490         ):
491             return (before or 1), 0
492
493         if (
494             self.previous_line
495             and self.previous_line.is_class
496             and current_line.is_triple_quoted_string
497         ):
498             return before, 1
499
500         return before, 0
501
502     def _maybe_empty_lines_for_class_or_def(
503         self, current_line: Line, before: int
504     ) -> Tuple[int, int]:
505         if not current_line.is_decorator:
506             self.previous_defs.append(current_line.depth)
507         if self.previous_line is None:
508             # Don't insert empty lines before the first line in the file.
509             return 0, 0
510
511         if self.previous_line.is_decorator:
512             if self.is_pyi and current_line.is_stub_class:
513                 # Insert an empty line after a decorated stub class
514                 return 0, 1
515
516             return 0, 0
517
518         if self.previous_line.depth < current_line.depth and (
519             self.previous_line.is_class or self.previous_line.is_def
520         ):
521             return 0, 0
522
523         if (
524             self.previous_line.is_comment
525             and self.previous_line.depth == current_line.depth
526             and before == 0
527         ):
528             return 0, 0
529
530         if self.is_pyi:
531             if self.previous_line.depth > current_line.depth:
532                 newlines = 0 if current_line.depth else 1
533             elif current_line.is_class or self.previous_line.is_class:
534                 if current_line.depth:
535                     newlines = 0
536                 elif current_line.is_stub_class and self.previous_line.is_stub_class:
537                     # No blank line between classes with an empty body
538                     newlines = 0
539                 else:
540                     newlines = 1
541             elif (
542                 current_line.is_def or current_line.is_decorator
543             ) and not self.previous_line.is_def:
544                 if current_line.depth:
545                     # In classes empty lines between attributes and methods should
546                     # be preserved.
547                     newlines = min(1, before)
548                 else:
549                     # Blank line between a block of functions (maybe with preceding
550                     # decorators) and a block of non-functions
551                     newlines = 1
552             else:
553                 newlines = 0
554         else:
555             newlines = 1 if current_line.depth else 2
556         return newlines, 0
557
558
559 def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
560     """Like `reversed(enumerate(sequence))` if that were possible."""
561     index = len(sequence) - 1
562     for element in reversed(sequence):
563         yield (index, element)
564         index -= 1
565
566
567 def append_leaves(
568     new_line: Line, old_line: Line, leaves: List[Leaf], preformatted: bool = False
569 ) -> None:
570     """
571     Append leaves (taken from @old_line) to @new_line, making sure to fix the
572     underlying Node structure where appropriate.
573
574     All of the leaves in @leaves are duplicated. The duplicates are then
575     appended to @new_line and used to replace their originals in the underlying
576     Node structure. Any comments attached to the old leaves are reattached to
577     the new leaves.
578
579     Pre-conditions:
580         set(@leaves) is a subset of set(@old_line.leaves).
581     """
582     for old_leaf in leaves:
583         new_leaf = Leaf(old_leaf.type, old_leaf.value)
584         replace_child(old_leaf, new_leaf)
585         new_line.append(new_leaf, preformatted=preformatted)
586
587         for comment_leaf in old_line.comments_after(old_leaf):
588             new_line.append(comment_leaf, preformatted=True)
589
590
591 def is_line_short_enough(line: Line, *, line_length: int, line_str: str = "") -> bool:
592     """Return True if `line` is no longer than `line_length`.
593
594     Uses the provided `line_str` rendering, if any, otherwise computes a new one.
595     """
596     if not line_str:
597         line_str = line_to_string(line)
598     return (
599         len(line_str) <= line_length
600         and "\n" not in line_str  # multiline strings
601         and not line.contains_standalone_comments()
602     )
603
604
605 def can_be_split(line: Line) -> bool:
606     """Return False if the line cannot be split *for sure*.
607
608     This is not an exhaustive search but a cheap heuristic that we can use to
609     avoid some unfortunate formattings (mostly around wrapping unsplittable code
610     in unnecessary parentheses).
611     """
612     leaves = line.leaves
613     if len(leaves) < 2:
614         return False
615
616     if leaves[0].type == token.STRING and leaves[1].type == token.DOT:
617         call_count = 0
618         dot_count = 0
619         next = leaves[-1]
620         for leaf in leaves[-2::-1]:
621             if leaf.type in OPENING_BRACKETS:
622                 if next.type not in CLOSING_BRACKETS:
623                     return False
624
625                 call_count += 1
626             elif leaf.type == token.DOT:
627                 dot_count += 1
628             elif leaf.type == token.NAME:
629                 if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
630                     return False
631
632             elif leaf.type not in CLOSING_BRACKETS:
633                 return False
634
635             if dot_count > 1 and call_count > 1:
636                 return False
637
638     return True
639
640
641 def can_omit_invisible_parens(
642     line: Line,
643     line_length: int,
644     omit_on_explode: Collection[LeafID] = (),
645 ) -> bool:
646     """Does `line` have a shape safe to reformat without optional parens around it?
647
648     Returns True for only a subset of potentially nice looking formattings but
649     the point is to not return false positives that end up producing lines that
650     are too long.
651     """
652     bt = line.bracket_tracker
653     if not bt.delimiters:
654         # Without delimiters the optional parentheses are useless.
655         return True
656
657     max_priority = bt.max_delimiter_priority()
658     if bt.delimiter_count_with_priority(max_priority) > 1:
659         # With more than one delimiter of a kind the optional parentheses read better.
660         return False
661
662     if max_priority == DOT_PRIORITY:
663         # A single stranded method call doesn't require optional parentheses.
664         return True
665
666     assert len(line.leaves) >= 2, "Stranded delimiter"
667
668     # With a single delimiter, omit if the expression starts or ends with
669     # a bracket.
670     first = line.leaves[0]
671     second = line.leaves[1]
672     if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
673         if _can_omit_opening_paren(line, first=first, line_length=line_length):
674             return True
675
676         # Note: we are not returning False here because a line might have *both*
677         # a leading opening bracket and a trailing closing bracket.  If the
678         # opening bracket doesn't match our rule, maybe the closing will.
679
680     penultimate = line.leaves[-2]
681     last = line.leaves[-1]
682     if line.magic_trailing_comma:
683         try:
684             penultimate, last = last_two_except(line.leaves, omit=omit_on_explode)
685         except LookupError:
686             # Turns out we'd omit everything.  We cannot skip the optional parentheses.
687             return False
688
689     if (
690         last.type == token.RPAR
691         or last.type == token.RBRACE
692         or (
693             # don't use indexing for omitting optional parentheses;
694             # it looks weird
695             last.type == token.RSQB
696             and last.parent
697             and last.parent.type != syms.trailer
698         )
699     ):
700         if penultimate.type in OPENING_BRACKETS:
701             # Empty brackets don't help.
702             return False
703
704         if is_multiline_string(first):
705             # Additional wrapping of a multiline string in this situation is
706             # unnecessary.
707             return True
708
709         if line.magic_trailing_comma and penultimate.type == token.COMMA:
710             # The rightmost non-omitted bracket pair is the one we want to explode on.
711             return True
712
713         if _can_omit_closing_paren(line, last=last, line_length=line_length):
714             return True
715
716     return False
717
718
719 def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
720     """See `can_omit_invisible_parens`."""
721     remainder = False
722     length = 4 * line.depth
723     _index = -1
724     for _index, leaf, leaf_length in line.enumerate_with_length():
725         if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
726             remainder = True
727         if remainder:
728             length += leaf_length
729             if length > line_length:
730                 break
731
732             if leaf.type in OPENING_BRACKETS:
733                 # There are brackets we can further split on.
734                 remainder = False
735
736     else:
737         # checked the entire string and line length wasn't exceeded
738         if len(line.leaves) == _index + 1:
739             return True
740
741     return False
742
743
744 def _can_omit_closing_paren(line: Line, *, last: Leaf, line_length: int) -> bool:
745     """See `can_omit_invisible_parens`."""
746     length = 4 * line.depth
747     seen_other_brackets = False
748     for _index, leaf, leaf_length in line.enumerate_with_length():
749         length += leaf_length
750         if leaf is last.opening_bracket:
751             if seen_other_brackets or length <= line_length:
752                 return True
753
754         elif leaf.type in OPENING_BRACKETS:
755             # There are brackets we can further split on.
756             seen_other_brackets = True
757
758     return False
759
760
761 def line_to_string(line: Line) -> str:
762     """Returns the string representation of @line.
763
764     WARNING: This is known to be computationally expensive.
765     """
766     return str(line).strip("\n")