agent: Fix edit_file corrupting indentation when old_text omits first-line indent - #60613
Merged
Conversation
MartinYe1234
marked this pull request as ready for review
July 8, 2026 18:15
anantdgoel
approved these changes
Jul 10, 2026
This was referenced Jul 31, 2026
jolutz
pushed a commit
to jolutz/zed
that referenced
this pull request
Aug 8, 2026
…-line indent (zed-industries#60613) The `edit_file` tool re-indents `new_text` by computing a single indent delta from the first line of `old_text` versus the matched buffer line, then applying that delta to every line of the replacement. When a model omits the leading indentation on only the first line of `old_text`/`new_text` (a common pattern when copying from mid-line context), the delta computed from the first line was wrongly applied to the remaining, already-correctly-indented lines, doubling their indentation. This PR: - Tracks the `(query_row, buffer_row)` line pairs aligned by the streaming fuzzy matcher so the indent of each `old_text` line can be compared against the buffer line it actually matched. - Computes a separate indent delta for the lines after the first: when those lines agree on a consistent delta, it's used for the rest of the replacement; otherwise the previous uniform behavior is preserved. - Keeps `query_lines`/line pairs in sync when `finish()` extends a match with a trailing incomplete line. - Adds an end-to-end regression test reproducing the issue, plus unit tests for the new re-indentation logic. Closes zed-industries#60302 Release Notes: - Fixed the agent's `edit_file` tool corrupting indentation when a replacement omitted leading whitespace on only its first line.
playdohface
pushed a commit
to playdohface/zed
that referenced
this pull request
Aug 29, 2026
…-line indent (zed-industries#60613) The `edit_file` tool re-indents `new_text` by computing a single indent delta from the first line of `old_text` versus the matched buffer line, then applying that delta to every line of the replacement. When a model omits the leading indentation on only the first line of `old_text`/`new_text` (a common pattern when copying from mid-line context), the delta computed from the first line was wrongly applied to the remaining, already-correctly-indented lines, doubling their indentation. This PR: - Tracks the `(query_row, buffer_row)` line pairs aligned by the streaming fuzzy matcher so the indent of each `old_text` line can be compared against the buffer line it actually matched. - Computes a separate indent delta for the lines after the first: when those lines agree on a consistent delta, it's used for the rest of the replacement; otherwise the previous uniform behavior is preserved. - Keeps `query_lines`/line pairs in sync when `finish()` extends a match with a trailing incomplete line. - Adds an end-to-end regression test reproducing the issue, plus unit tests for the new re-indentation logic. Closes zed-industries#60302 Release Notes: - Fixed the agent's `edit_file` tool corrupting indentation when a replacement omitted leading whitespace on only its first line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
edit_filetool re-indentsnew_textby computing a single indent delta from the first line ofold_textversus the matched buffer line, then applying that delta to every line of the replacement. When a model omits the leading indentation on only the first line ofold_text/new_text(a common pattern when copying from mid-line context), the delta computed from the first line was wrongly applied to the remaining, already-correctly-indented lines, doubling their indentation.This PR:
(query_row, buffer_row)line pairs aligned by the streaming fuzzy matcher so the indent of eachold_textline can be compared against the buffer line it actually matched.query_lines/line pairs in sync whenfinish()extends a match with a trailing incomplete line.Closes #60302
Release Notes:
edit_filetool corrupting indentation when a replacement omitted leading whitespace on only its first line.