Return an enumeration of leaves with their length. Stops prematurely on multiline strings and standalone comments.
(
self, is_reversed: bool = False
)
| 446 | ) |
| 447 | |
| 448 | def enumerate_with_length( |
| 449 | self, is_reversed: bool = False |
| 450 | ) -> Iterator[tuple[Index, Leaf, int]]: |
| 451 | """Return an enumeration of leaves with their length. |
| 452 | |
| 453 | Stops prematurely on multiline strings and standalone comments. |
| 454 | """ |
| 455 | op = cast( |
| 456 | Callable[[Sequence[Leaf]], Iterator[tuple[Index, Leaf]]], |
| 457 | enumerate_reversed if is_reversed else enumerate, |
| 458 | ) |
| 459 | for index, leaf in op(self.leaves): |
| 460 | length = len(leaf.prefix) + len(leaf.value) |
| 461 | if "\n" in leaf.value: |
| 462 | return # Multiline strings, we can't continue. |
| 463 | |
| 464 | for comment in self.comments_after(leaf): |
| 465 | length += len(comment.value) |
| 466 | |
| 467 | yield index, leaf, length |
| 468 | |
| 469 | def clone(self) -> "Line": |
| 470 | return Line( |
no test coverage detected