Add a new `leaf` to the end of the line. Unless `preformatted` is True, the `leaf` will receive a new consistent whitespace prefix and metadata applied by :class:`BracketTracker`. Trailing commas are maybe removed, unpacked for loop variables are demoted from being d
(
self, leaf: Leaf, preformatted: bool = False, track_bracket: bool = False
)
| 51 | magic_trailing_comma: Leaf | None = None |
| 52 | |
| 53 | def append( |
| 54 | self, leaf: Leaf, preformatted: bool = False, track_bracket: bool = False |
| 55 | ) -> None: |
| 56 | class="st">"""Add a new `leaf` to the end of the line. |
| 57 | |
| 58 | Unless `preformatted` is True, the `leaf` will receive a new consistent |
| 59 | whitespace prefix and metadata applied by :class:`BracketTracker`. |
| 60 | Trailing commas are maybe removed, unpacked for loop variables are |
| 61 | demoted from being delimiters. |
| 62 | |
| 63 | Inline comments are put aside. |
| 64 | class="st">""" |
| 65 | has_value = ( |
| 66 | leaf.type in BRACKETS |
| 67 | class="cm"># empty fstring and tstring middles must not be truncated |
| 68 | or leaf.type in (token.FSTRING_MIDDLE, token.TSTRING_MIDDLE) |
| 69 | or bool(leaf.value.strip()) |
| 70 | ) |
| 71 | if not has_value: |
| 72 | return |
| 73 | |
| 74 | if leaf.type == token.COLON and self.is_class_paren_empty: |
| 75 | del self.leaves[-2:] |
| 76 | if self.leaves and not preformatted: |
| 77 | class="cm"># Note: at this point leaf.prefix should be empty except for |
| 78 | class="cm"># imports, for which we only preserve newlines. |
| 79 | leaf.prefix += whitespace( |
| 80 | leaf, |
| 81 | complex_subscript=self.is_complex_subscript(leaf), |
| 82 | mode=self.mode, |
| 83 | ) |
| 84 | if self.inside_brackets or not preformatted or track_bracket: |
| 85 | self.bracket_tracker.mark(leaf) |
| 86 | if self.mode.magic_trailing_comma: |
| 87 | if self.has_magic_trailing_comma(leaf): |
| 88 | self.magic_trailing_comma = leaf |
| 89 | elif self.has_magic_trailing_comma(leaf): |
| 90 | self.remove_trailing_comma() |
| 91 | if not self.append_comment(leaf): |
| 92 | self.leaves.append(leaf) |
| 93 | |
| 94 | def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None: |
| 95 | class="st">"""Like :func:`append()` but disallow invalid standalone comment structure. |
no test coverage detected