这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@MartinKalema
Copy link
Contributor

@MartinKalema MartinKalema commented Jul 22, 2025

Summary

This PR fixes critical issues with the text editor tool's diff generation and string replacement functionality, particularly for large files. The changes significantly improve the user experience by showing only relevant context in diffs and enabling robust multi-line string replacements.

Problem

Users reported that "the edit tool doesn't apply diffs correctly for larger files" (#12). Investigation revealed two major issues:

  1. Excessive diff output: For a single line change in a 336-line file, the tool displayed all 336 lines in the confirmation dialog, making it impossible to review changes effectively
  2. Failed multi-line replacements: Multi-line string replacements failed due to formatting differences (quotes, whitespace, indentation)

Before

  • Single line change showed entire file: @@ -1,336 +1,336 @@
  • Incorrect change count: "272 additions and 272 removals" for a 1-line change
  • Multi-line replacements failed with "String not found in file"

After

  • Single line change shows only context: @@ -62,7 +62,7 @@
  • Accurate change count: "1 addition and 1 removal"
  • Multi-line replacements work with intelligent fuzzy matching

Changes

1. Rewrote generateDiff method with proper unified diff algorithm

  • Implements two-pass algorithm: first finds changes, then generates contextual hunks
  • Shows only 3 lines of context before/after changes (configurable via CONTEXT_LINES)
  • Merges nearby changes into single hunks for better readability
  • Correctly calculates line numbers and change counts

2. Added fuzzy matching for multi-line string replacements

  • New findFuzzyMatch method intelligently matches code blocks (especially functions)
  • Uses structural matching based on brace counting and token analysis
  • Handles differences in quotes, whitespace, and formatting
  • Returns the actual content from the file for accurate replacement

3. Additional improvements

  • Added replaceAll parameter to replace all occurrences (not just first)
  • Added replaceLines method for line-based editing when string matching isn't suitable
  • Improved user feedback with occurrence counts
  • Better error messages guiding users to appropriate tools

Testing

Tested with:

  • Large files (300+ lines) with single line changes
  • Multi-line function replacements with formatting differences
  • Multiple scattered changes throughout a file
  • Adjacent changes that should merge into single hunks

Breaking Changes

None. The changes are backward compatible and improve existing functionality.

Screenshots

Before

Screenshot 2025-07-22 at 15 31 39

After

Screenshot 2025-07-22 at 15 50 48

Checklist

  • Self-review completed
  • No new warnings introduced
  • Documentation updated if needed

Related Issues

Fixes #12 - Edit tool doesn't apply diffs correctly for larger files

…rge files

- rewrote generateDiff() to show only relevant context (3 lines before/after changes)
- fixed incorrect change counts (was showing 272 additions/removals for 1-line change)
- added fuzzy matching for multi-line string replacements to handle formatting differences
- added replaceAll parameter to replace all occurrences of a string
- added replaceLines() method for line-based editing when string matching isn't suitable
- improved hunk generation to merge nearby changes for better readability

this significantly improves the user experience when editing large files by showing
only the relevant changes instead of the entire file content in confirmation dialogs.
@homanp
Copy link
Contributor

homanp commented Jul 22, 2025

@MartinKalema this is great, will test it out and merge, thank you!

@homanp homanp merged commit e662dd3 into superagent-ai:main Jul 22, 2025
2 checks passed
@homanp
Copy link
Contributor

homanp commented Jul 22, 2025

@MartinKalema looks amazing, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: file edit tool does a crappy job at showing diffs.

2 participants