(edits: Sequence[Mapping[str, Any]])
| 899 | |
| 900 | |
| 901 | def _normalize_edits(edits: Sequence[Mapping[str, Any]]) -> List[TextEdit]: |
| 902 | if not edits: |
| 903 | raise ValueError("at least one edit instruction is required") |
| 904 | normalized: List[TextEdit] = [] |
| 905 | for item in edits: |
| 906 | if not isinstance(item, Mapping): |
| 907 | raise ValueError("each edit entry must be a mapping object") |
| 908 | try: |
| 909 | start_line = int(item["start_line"]) |
| 910 | except (KeyError, TypeError, ValueError) as exc: |
| 911 | raise ValueError("start_line is required for each edit") from exc |
| 912 | end_line_raw = item.get("end_line", start_line) |
| 913 | try: |
| 914 | end_line = int(end_line_raw) |
| 915 | except (TypeError, ValueError) as exc: |
| 916 | raise ValueError("end_line must be an integer") from exc |
| 917 | if start_line < 1: |
| 918 | raise ValueError("start_line must be >= 1") |
| 919 | if end_line < start_line - 1: |
| 920 | raise ValueError("end_line must be >= start_line - 1") |
| 921 | replacement = item.get("replacement", "") |
| 922 | if not isinstance(replacement, str): |
| 923 | raise ValueError("replacement must be a string") |
| 924 | normalized.append( |
| 925 | TextEdit( |
| 926 | start_line=start_line, |
| 927 | end_line=end_line, |
| 928 | replacement_lines=replacement.splitlines(), |
| 929 | ) |
| 930 | ) |
| 931 | |
| 932 | normalized.sort(key=lambda edit: (edit.start_line, edit.end_line)) |
| 933 | _validate_edit_ranges(normalized) |
| 934 | return normalized |
| 935 | |
| 936 | |
| 937 | def _build_single_edit( |
no test coverage detected