| 258 | |
| 259 | |
| 260 | class WindowExpander: |
| 261 | def __init__(self, suffix: str = ""): |
| 262 | """Try to expand viewports to include whole functions, classes, etc. rather than |
| 263 | using fixed line windows. |
| 264 | |
| 265 | Args: |
| 266 | suffix: Filename suffix |
| 267 | """ |
| 268 | self.suffix = suffix |
| 269 | if self.suffix: |
| 270 | assert self.suffix.startswith(".") |
| 271 | |
| 272 | def _find_breakpoints(self, lines: List[str], current_line: int, direction=1, max_added_lines: int = 30) -> int: |
| 273 | """Returns 1-based line number of breakpoint. This line is meant to still be included in the viewport. |
| 274 | |
| 275 | Args: |
| 276 | lines: List of lines of the file |
| 277 | current_line: 1-based line number of the current viewport |
| 278 | direction: 1 for down, -1 for up |
| 279 | max_added_lines: Maximum number of lines to extend |
| 280 | |
| 281 | Returns: |
| 282 | 1-based line number of breakpoint. This line is meant to still be included in the viewport. |
| 283 | """ |
| 284 | assert 1 <= current_line <= len(lines) |
| 285 | assert 0 <= max_added_lines |
| 286 | |
| 287 | # 1. Find line range that we want to search for breakpoints in |
| 288 | |
| 289 | if direction == 1: |
| 290 | # down |
| 291 | if current_line == len(lines): |
| 292 | # already last line, can't extend down |
| 293 | return current_line |
| 294 | iter_lines = range(current_line, 1 + min(current_line + max_added_lines, len(lines))) |
| 295 | elif direction == -1: |
| 296 | # up |
| 297 | if current_line == 1: |
| 298 | # already first line, can't extend up |
| 299 | return current_line |
| 300 | iter_lines = range(current_line, -1 + max(current_line - max_added_lines, 1), -1) |
| 301 | else: |
| 302 | msg = f"Invalid direction {direction}" |
| 303 | raise ValueError(msg) |
| 304 | |
| 305 | # 2. Find the best breakpoint in the line range |
| 306 | |
| 307 | # Every condition gives a score, the best score is the best breakpoint |
| 308 | best_score = 0 |
| 309 | best_breakpoint = current_line |
| 310 | for i_line in iter_lines: |
| 311 | next_line = None |
| 312 | line = lines[i_line - 1] |
| 313 | if i_line + direction in iter_lines: |
| 314 | next_line = lines[i_line + direction - 1] |
| 315 | score = 0 |
| 316 | if line == "": |
| 317 | score = 1 |
no outgoing calls
no test coverage detected