Collapse whitespace across a list of (lineno, text) pairs. Returns (collapsed_text, linemap) where linemap[i] is the original line number for character i in collapsed_text. Matches the behaviour of collapse_whitespace() so positions are compatible with coverage records.
(raw_lines: List[Tuple[int, str]])
| 29 | |
| 30 | |
| 31 | def collapse_with_linemap(raw_lines: List[Tuple[int, str]]) -> Tuple[str, List[int]]: |
| 32 | """Collapse whitespace across a list of (lineno, text) pairs. |
| 33 | |
| 34 | Returns (collapsed_text, linemap) where linemap[i] is the original line |
| 35 | number for character i in collapsed_text. Matches the behaviour of |
| 36 | collapse_whitespace() so positions are compatible with coverage records. |
| 37 | """ |
| 38 | result: List[str] = [] |
| 39 | linemap: List[int] = [] |
| 40 | in_ws = False |
| 41 | ws_lineno = 1 |
| 42 | |
| 43 | for lineno, line in raw_lines: |
| 44 | for ch in line: |
| 45 | if whitespace_re.match(ch): |
| 46 | if not in_ws: |
| 47 | in_ws = True |
| 48 | ws_lineno = lineno |
| 49 | else: |
| 50 | if in_ws: |
| 51 | result.append(" ") |
| 52 | linemap.append(ws_lineno) |
| 53 | in_ws = False |
| 54 | result.append(ch) |
| 55 | linemap.append(lineno) |
| 56 | |
| 57 | if in_ws: |
| 58 | result.append(" ") |
| 59 | linemap.append(ws_lineno) |
| 60 | |
| 61 | return "".join(result), linemap |
| 62 | |
| 63 | |
| 64 | def load_bolt(boltdir: str, num: int) -> Tuple[str, List[Tuple[str, List[int]]]]: |