Span indexing that's inclusive on both ends.
| 30 | |
| 31 | |
| 32 | class Span(NamedTuple): |
| 33 | """Span indexing that's inclusive on both ends.""" |
| 34 | |
| 35 | start: int |
| 36 | end: int |
| 37 | |
| 38 | @classmethod |
| 39 | def from_re(cls, m: Match[str], group: int | str) -> Self: |
| 40 | re_span = m.span(group) |
| 41 | return cls(re_span[0], re_span[1] - 1) |
| 42 | |
| 43 | @classmethod |
| 44 | def from_token(cls, token: TI, line_len: list[int]) -> Self: |
| 45 | end_offset = -1 |
| 46 | if (token.type in {T.FSTRING_MIDDLE, T.TSTRING_MIDDLE} |
| 47 | and token.string.endswith(("{", "}"))): |
| 48 | # gh-134158: a visible trailing brace comes from a double brace in input |
| 49 | end_offset += 1 |
| 50 | |
| 51 | return cls( |
| 52 | line_len[token.start[0] - 1] + token.start[1], |
| 53 | line_len[token.end[0] - 1] + token.end[1] + end_offset, |
| 54 | ) |
| 55 | |
| 56 | |
| 57 | class ColorSpan(NamedTuple): |
no outgoing calls
no test coverage detected
searching dependent graphs…