Remove no longer used diff helpers

This commit is contained in:
Amar Sood (tekacs) 2025-04-12 11:01:54 -04:00
parent a4bcf4f938
commit 7676425d1f

View file

@ -1534,184 +1534,3 @@ Just reply with fixed versions of the {blocks} above that failed to match.
# ------------------- Diff Generation Helpers -------------------
def _generate_diff_snippet(self, original_content, start_index, replaced_len, replacement_text):
"""Generate a git-style diff snippet for a simple text replacement."""
try:
lines = original_content.splitlines()
char_count = 0
start_line_idx = -1
start_char_idx_in_line = -1
# Find the line and character index where the change starts
for i, line in enumerate(lines):
line_len_with_newline = len(line) + 1 # Account for newline character
if char_count + line_len_with_newline > start_index:
start_line_idx = i
start_char_idx_in_line = start_index - char_count
break
char_count += line_len_with_newline
if start_line_idx == -1: return "[Diff generation error: start index out of bounds]"
# Determine the end line and character index
end_index = start_index + replaced_len
char_count = 0
end_line_idx = -1
end_char_idx_in_line = -1
for i, line in enumerate(lines):
line_len_with_newline = len(line) + 1
if char_count + line_len_with_newline > end_index:
end_line_idx = i
# End char index is relative to the start of *its* line
end_char_idx_in_line = end_index - char_count
break
char_count += line_len_with_newline
# If end_index is exactly at the end of the content
if end_line_idx == -1 and end_index == len(original_content):
end_line_idx = len(lines) - 1
end_char_idx_in_line = len(lines[end_line_idx])
if end_line_idx == -1: return "[Diff generation error: end index out of bounds]"
# Get context lines
context = 3
diff_start_line = max(0, start_line_idx - context)
diff_end_line = min(len(lines) - 1, end_line_idx + context)
diff_lines = [f"@@ line ~{start_line_idx + 1} @@"]
for i in range(diff_start_line, diff_end_line + 1):
if i >= start_line_idx and i <= end_line_idx:
# Line is part of the original replaced block
diff_lines.append(f"- {lines[i]}")
else:
# Context line
diff_lines.append(f" {lines[i]}")
# Construct the new lines based on the replacement
prefix = lines[start_line_idx][:start_char_idx_in_line]
suffix = lines[end_line_idx][end_char_idx_in_line:]
# Combine prefix, replacement, and suffix, then split into lines
combined_new_content = prefix + replacement_text + suffix
new_content_lines = combined_new_content.splitlines()
# Add new lines to diff
for new_line in new_content_lines:
diff_lines.append(f"+ {new_line}")
return "\n".join(diff_lines)
except Exception as e:
return f"[Diff generation error: {e}]"
def _generate_diff_chunks(self, original_content, find_text, replace_text):
"""Generate multiple git-style diff snippets for ReplaceAll."""
try:
lines = original_content.splitlines()
new_lines_content = original_content.replace(find_text, replace_text)
new_lines = new_lines_content.splitlines()
# Use difflib for a more robust diff
import difflib
diff = list(difflib.unified_diff(lines, new_lines, lineterm='', n=3)) # n=3 lines of context
if len(diff) <= 2: # Only header lines, no changes found by diff
return "No significant changes detected by diff."
# Process the diff output into readable chunks
# Skip header lines (---, +++)
processed_diff = "\n".join(diff[2:])
# Limit the output size if it's too large
max_diff_len = 2000 # Limit diff snippet size
if len(processed_diff) > max_diff_len:
processed_diff = processed_diff[:max_diff_len] + "\n... (diff truncated)"
return processed_diff if processed_diff else "No changes detected."
except Exception as e:
return f"[Diff generation error: {e}]"
def _generate_diff_snippet_insert(self, original_content, insertion_line_idx, content_lines_to_insert):
"""Generate a git-style diff snippet for an insertion."""
try:
lines = original_content.splitlines()
context = 3
# Determine context range
start_context = max(0, insertion_line_idx - context)
end_context = min(len(lines), insertion_line_idx + context) # End index is exclusive for slicing
diff_lines = [f"@@ line ~{insertion_line_idx + 1} @@"] # Indicate insertion point
# Add lines before insertion point
for i in range(start_context, insertion_line_idx):
diff_lines.append(f" {lines[i]}")
# Add inserted lines
for line in content_lines_to_insert:
diff_lines.append(f"+ {line}")
# Add lines after insertion point
for i in range(insertion_line_idx, end_context):
diff_lines.append(f" {lines[i]}")
return "\n".join(diff_lines)
except Exception as e:
return f"[Diff generation error: {e}]"
def _generate_diff_snippet_delete(self, original_content, start_line, end_line):
"""Generate a git-style diff snippet for a deletion."""
try:
lines = original_content.splitlines()
context = 3
# Determine context range
diff_start_line = max(0, start_line - context)
diff_end_line = min(len(lines) - 1, end_line + context)
diff_lines = [f"@@ line {start_line + 1},{end_line + 1} @@"] # Indicate deletion range
for i in range(diff_start_line, diff_end_line + 1):
if i >= start_line and i <= end_line:
# Line was deleted
diff_lines.append(f"- {lines[i]}")
else:
# Context line
diff_lines.append(f" {lines[i]}")
return "\n".join(diff_lines)
except Exception as e:
return f"[Diff generation error: {e}]"
def _generate_diff_snippet_indent(self, original_content, new_content, start_line, end_line):
"""Generate a git-style diff snippet for indentation changes."""
try:
original_lines = original_content.splitlines()
new_lines = new_content.splitlines()
context = 3
# Determine context range
diff_start_line = max(0, start_line - context)
diff_end_line = min(len(original_lines) - 1, end_line + context)
diff_lines_output = [f"@@ lines ~{start_line + 1}-{end_line + 1} @@"] # Indicate affected range
for i in range(diff_start_line, diff_end_line + 1):
# Ensure index is valid for both lists (should be, as only indentation changes)
if i < len(original_lines) and i < len(new_lines):
if i >= start_line and i <= end_line:
# Line is within the indented/unindented block
if original_lines[i] != new_lines[i]: # Show only if changed
diff_lines_output.append(f"- {original_lines[i]}")
diff_lines_output.append(f"+ {new_lines[i]}")
else: # If somehow unchanged, show as context
diff_lines_output.append(f" {original_lines[i]}")
else:
# Context line
diff_lines_output.append(f" {original_lines[i]}")
return "\n".join(diff_lines_output)
except Exception as e:
return f"[Diff generation error: {e}]"