Find the best matching location for a search pattern in file lines. Args: file_lines: The file content as a list of lines search_pattern: The pattern to search for (context + old lines) threshold: Minimum similarity score (0.0 to 1.0) Returns: MatchResu
(
file_lines: List[str], search_pattern: List[str], threshold: float = 0.75
)
| 341 | |
| 342 | |
| 343 | def _find_best_match( |
| 344 | file_lines: List[str], search_pattern: List[str], threshold: float = 0.75 |
| 345 | ) -> MatchResult: |
| 346 | """ |
| 347 | Find the best matching location for a search pattern in file lines. |
| 348 | |
| 349 | Args: |
| 350 | file_lines: The file content as a list of lines |
| 351 | search_pattern: The pattern to search for (context + old lines) |
| 352 | threshold: Minimum similarity score (0.0 to 1.0) |
| 353 | |
| 354 | Returns: |
| 355 | MatchResult with location and score |
| 356 | """ |
| 357 | if not search_pattern or not file_lines: |
| 358 | return MatchResult(found=False) |
| 359 | |
| 360 | pattern_len = len(search_pattern) |
| 361 | best_score = 0.0 |
| 362 | best_start = -1 |
| 363 | |
| 364 | # Sliding window search |
| 365 | for i in range(len(file_lines) - pattern_len + 1): |
| 366 | window = file_lines[i : i + pattern_len] |
| 367 | score = _calculate_similarity(search_pattern, window) |
| 368 | |
| 369 | if score > best_score: |
| 370 | best_score = score |
| 371 | best_start = i |
| 372 | |
| 373 | # Check if we found a good enough match |
| 374 | if best_score >= threshold and best_start >= 0: |
| 375 | return MatchResult( |
| 376 | found=True, |
| 377 | score=best_score, |
| 378 | start_line=best_start, |
| 379 | end_line=best_start + pattern_len, |
| 380 | ) |
| 381 | |
| 382 | return MatchResult(found=False, score=best_score) |
| 383 | |
| 384 | |
| 385 | def _apply_hunk_to_lines( |
no test coverage detected