Mark `leaf` with bracket-related metadata. Keep track of delimiters. All leaves receive an int `bracket_depth` field that stores how deep within brackets a given leaf is. 0 means there are no enclosing brackets that started on this line. If a leaf is itself a closin
(self, leaf: Leaf)
| 69 | invisible: list[Leaf] = field(default_factory=list) |
| 70 | |
| 71 | def mark(self, leaf: Leaf) -> None: |
| 72 | """Mark `leaf` with bracket-related metadata. Keep track of delimiters. |
| 73 | |
| 74 | All leaves receive an int `bracket_depth` field that stores how deep |
| 75 | within brackets a given leaf is. 0 means there are no enclosing brackets |
| 76 | that started on this line. |
| 77 | |
| 78 | If a leaf is itself a closing bracket and there is a matching opening |
| 79 | bracket earlier, it receives an `opening_bracket` field with which it forms a |
| 80 | pair. This is a one-directional link to avoid reference cycles. Closing |
| 81 | bracket without opening happens on lines continued from previous |
| 82 | breaks, e.g. `) -> "ReturnType":` as part of a funcdef where we place |
| 83 | the return type annotation on its own line of the previous closing RPAR. |
| 84 | |
| 85 | If a leaf is a delimiter (a token on which Black can split the line if |
| 86 | needed) and it's on depth 0, its `id()` is stored in the tracker's |
| 87 | `delimiters` field. |
| 88 | """ |
| 89 | if leaf.type == token.COMMENT: |
| 90 | return |
| 91 | |
| 92 | if ( |
| 93 | self.depth == 0 |
| 94 | and leaf.type in CLOSING_BRACKETS |
| 95 | and (self.depth, leaf.type) not in self.bracket_match |
| 96 | ): |
| 97 | return |
| 98 | |
| 99 | self.maybe_decrement_after_for_loop_variable(leaf) |
| 100 | self.maybe_decrement_after_lambda_arguments(leaf) |
| 101 | if leaf.type in CLOSING_BRACKETS: |
| 102 | self.depth -= 1 |
| 103 | try: |
| 104 | opening_bracket = self.bracket_match.pop((self.depth, leaf.type)) |
| 105 | except KeyError as e: |
| 106 | raise BracketMatchError( |
| 107 | "Unable to match a closing bracket to the following opening" |
| 108 | f" bracket: {leaf}" |
| 109 | ) from e |
| 110 | leaf.opening_bracket = opening_bracket |
| 111 | if not leaf.value: |
| 112 | self.invisible.append(leaf) |
| 113 | leaf.bracket_depth = self.depth |
| 114 | if self.depth == 0: |
| 115 | delim = is_split_before_delimiter(leaf, self.previous) |
| 116 | if delim and self.previous is not None: |
| 117 | self.delimiters[id(self.previous)] = delim |
| 118 | else: |
| 119 | delim = is_split_after_delimiter(leaf) |
| 120 | if delim: |
| 121 | self.delimiters[id(leaf)] = delim |
| 122 | if leaf.type in OPENING_BRACKETS: |
| 123 | self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf |
| 124 | self.depth += 1 |
| 125 | if not leaf.value: |
| 126 | self.invisible.append(leaf) |
| 127 | self.previous = leaf |
| 128 | self.maybe_increment_lambda_arguments(leaf) |
no test coverage detected