| 258 | |
| 259 | # TODO: consider special casing docstrings such that they are not elided. This |
| 260 | # could be accomplished by checking whether `body.text.decode('utf8')` starts |
| 261 | # with `"""` or `'''`. |
| 262 | elide_line_ranges = [ |
| 263 | (node.start_point[0], node.end_point[0]) |
| 264 | for node, _ in query.captures(tree.root_node) |
| 265 | # Only elide if it's sufficiently long |
| 266 | if node.end_point[0] - node.start_point[0] >= 5 |
| 267 | ] |
| 268 | # Note that tree-sitter line numbers are 0-indexed, but we display 1-indexed. |
| 269 | elide_lines = {line for start, end in elide_line_ranges for line in range(start, end + 1)} |
| 270 | elide_messages = [ |
| 271 | (start, f"... eliding lines {start + 1}-{end + 1} ...") |
| 272 | for start, end in elide_line_ranges |
| 273 | ] |
| 274 | out = [] |
| 275 | for i, line in sorted( |
| 276 | elide_messages |
| 277 | + [ |
| 278 | (i, line) |
| 279 | for i, line in enumerate(file_contents.splitlines()) |
| 280 | if i not in elide_lines |
| 281 | ] |
| 282 | ): |
| 283 | out.append(f"{i + 1:6d} {line}") |
| 284 | return "\n".join(out) |
| 285 | |
| 286 | |
| 287 | class WindowExpander: |
| 288 | def __init__(self, suffix: str = ""): |
| 289 | """Try to expand viewports to include whole functions, classes, etc. rather than |
| 290 | using fixed line windows. |
| 291 | |
| 292 | Args: |
| 293 | suffix: Filename suffix |
| 294 | """ |
| 295 | self.suffix = suffix |
| 296 | if self.suffix: |
| 297 | assert self.suffix.startswith(".") |
| 298 | |
| 299 | def _find_breakpoints( |
| 300 | self, lines: list[str], current_line: int, direction=1, max_added_lines: int = 30 |
| 301 | ) -> int: |
| 302 | """Returns 1-based line number of breakpoint. This line is meant to still be included in the viewport. |
| 303 | |
| 304 | Args: |
| 305 | lines: List of lines of the file |
| 306 | current_line: 1-based line number of the current viewport |
| 307 | direction: 1 for down, -1 for up |
| 308 | max_added_lines: Maximum number of lines to extend |
| 309 | |
| 310 | Returns: |
| 311 | 1-based line number of breakpoint. This line is meant to still be included in the viewport. |
| 312 | """ |
| 313 | assert 1 <= current_line <= len(lines) |
| 314 | assert 0 <= max_added_lines |
| 315 | |
| 316 | # 1. Find line range that we want to search for breakpoints in |
| 317 |
no outgoing calls
no test coverage detected