more org
This commit is contained in:
+28
-34
@@ -1,3 +1,4 @@
|
||||
import difflib
|
||||
import shutil
|
||||
import os
|
||||
|
||||
@@ -8,8 +9,8 @@ from typing import List, Dict, Optional, Tuple
|
||||
|
||||
@dataclass
|
||||
class DiffHunk:
|
||||
header: str
|
||||
lines: List[str]
|
||||
header: str
|
||||
lines: List[str]
|
||||
old_start: int
|
||||
old_count: int
|
||||
new_start: int
|
||||
@@ -19,18 +20,16 @@ class DiffHunk:
|
||||
class DiffFile:
|
||||
old_path: str
|
||||
new_path: str
|
||||
hunks: List[DiffHunk]
|
||||
hunks: List[DiffHunk]
|
||||
|
||||
def parse_hunk_header(line: str) -> Optional[tuple[int, int, int, int]]:
|
||||
"""
|
||||
[C: tests/test_diff_viewer.py:test_parse_hunk_header]
|
||||
[C: tests/test_diff_viewer.py:test_parse_hunk_header]
|
||||
"""
|
||||
if not line.startswith("@@"):
|
||||
return None
|
||||
if not line.startswith("@@"): return None
|
||||
|
||||
parts = line.split()
|
||||
if len(parts) < 2:
|
||||
return None
|
||||
if len(parts) < 2: return None
|
||||
|
||||
old_part = parts[1][1:]
|
||||
new_part = parts[2][1:]
|
||||
@@ -52,7 +51,7 @@ def parse_diff(diff_text: str) -> List[DiffFile]:
|
||||
if not diff_text or not diff_text.strip():
|
||||
return []
|
||||
|
||||
files: List[DiffFile] = []
|
||||
files: List[DiffFile] = []
|
||||
current_file: Optional[DiffFile] = None
|
||||
current_hunk: Optional[DiffHunk] = None
|
||||
|
||||
@@ -83,21 +82,21 @@ def parse_diff(diff_text: str) -> List[DiffFile]:
|
||||
if hunk_info:
|
||||
old_start, old_count, new_start, new_count = hunk_info
|
||||
current_hunk = DiffHunk(
|
||||
header=line,
|
||||
lines=[],
|
||||
old_start=old_start,
|
||||
old_count=old_count,
|
||||
new_start=new_start,
|
||||
new_count=new_count
|
||||
header = line,
|
||||
lines = [],
|
||||
old_start = old_start,
|
||||
old_count = old_count,
|
||||
new_start = new_start,
|
||||
new_count = new_count
|
||||
)
|
||||
else:
|
||||
current_hunk = DiffHunk(
|
||||
header=line,
|
||||
lines=[],
|
||||
old_start=0,
|
||||
old_count=0,
|
||||
new_start=0,
|
||||
new_count=0
|
||||
header = line,
|
||||
lines = [],
|
||||
old_start = 0,
|
||||
old_count = 0,
|
||||
new_start = 0,
|
||||
new_count = 0
|
||||
)
|
||||
|
||||
elif current_hunk is not None:
|
||||
@@ -115,22 +114,17 @@ def parse_diff(diff_text: str) -> List[DiffFile]:
|
||||
|
||||
def get_line_color(line: str) -> Optional[str]:
|
||||
"""
|
||||
[C: tests/test_diff_viewer.py:test_get_line_color]
|
||||
[C: tests/test_diff_viewer.py:test_get_line_color]
|
||||
"""
|
||||
if line.startswith("+"):
|
||||
return "green"
|
||||
elif line.startswith("-"):
|
||||
return "red"
|
||||
elif line.startswith("@@"):
|
||||
return "cyan"
|
||||
if line.startswith("+"): return "green"
|
||||
elif line.startswith("-"): return "red"
|
||||
elif line.startswith("@@"): return "cyan"
|
||||
return None
|
||||
|
||||
def apply_patch_to_file(patch_text: str, base_dir: str = ".") -> Tuple[bool, str]:
|
||||
"""
|
||||
[C: src/gui_2.py:App._apply_pending_patch, tests/test_diff_viewer.py:test_apply_patch_simple, tests/test_diff_viewer.py:test_apply_patch_with_context]
|
||||
[C: src/gui_2.py:App._apply_pending_patch, tests/test_diff_viewer.py:test_apply_patch_simple, tests/test_diff_viewer.py:test_apply_patch_with_context]
|
||||
"""
|
||||
import difflib
|
||||
|
||||
diff_files = parse_diff(patch_text)
|
||||
if not diff_files:
|
||||
return False, "No valid diff found"
|
||||
@@ -147,7 +141,7 @@ def apply_patch_to_file(patch_text: str, base_dir: str = ".") -> Tuple[bool, str
|
||||
original_lines = f.read().splitlines(keepends=True)
|
||||
|
||||
new_lines = original_lines.copy()
|
||||
offset = 0
|
||||
offset = 0
|
||||
|
||||
for hunk in df.hunks:
|
||||
hunk_old_start = hunk.old_start - 1
|
||||
@@ -158,13 +152,13 @@ def apply_patch_to_file(patch_text: str, base_dir: str = ".") -> Tuple[bool, str
|
||||
|
||||
hunk_new_content: List[str] = []
|
||||
for line in hunk.lines:
|
||||
if line.startswith("+") and not line.startswith("+++"):
|
||||
if line.startswith("+") and not line.startswith("+++"):
|
||||
hunk_new_content.append(line[1:] + "\n")
|
||||
elif line.startswith(" ") or (line and not line.startswith(("-", "+", "@@"))):
|
||||
hunk_new_content.append(line + "\n")
|
||||
|
||||
new_lines = new_lines[:replace_start] + hunk_new_content + new_lines[replace_start + replace_count:]
|
||||
offset += len(hunk_new_content) - replace_count
|
||||
offset += len(hunk_new_content) - replace_count
|
||||
|
||||
with open(file_path, "w", encoding="utf-8", newline="") as f:
|
||||
f.writelines(new_lines)
|
||||
|
||||
Reference in New Issue
Block a user