fix: preserve indentation when applying code edits to Python files#11289
Open
mvanhorn wants to merge 1 commit intocontinuedev:mainfrom
Open
fix: preserve indentation when applying code edits to Python files#11289mvanhorn wants to merge 1 commit intocontinuedev:mainfrom
mvanhorn wants to merge 1 commit intocontinuedev:mainfrom
Conversation
When fallback matching strategies (trimmedMatch, whitespaceIgnoredMatch) find a match with different indentation than the LLM-provided old_string, the replacement new_string now has its indentation adjusted to match the actual indentation level in the file. This fixes incorrect indentation in Python code edits where the LLM provides unindented search/replace strings that match indented code blocks. Fixes continuedev#11282 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
I have read the CLA Document and I hereby sign the CLA |
4 tasks
Contributor
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="core/edit/searchAndReplace/performReplace.ts">
<violation number="1" location="core/edit/searchAndReplace/performReplace.ts:58">
P2: First replacement line can remain unindented when `oldIndent` is non-empty, because fallback indentation adjustment only runs for `oldIndent === ""`.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| // For lines that don't start with the old indent (e.g. first line | ||
| // might have no indent if old_string was trimmed), apply the | ||
| // matched indent directly | ||
| if (index === 0 && oldIndent === "" && matchedIndent !== "") { |
Contributor
There was a problem hiding this comment.
P2: First replacement line can remain unindented when oldIndent is non-empty, because fallback indentation adjustment only runs for oldIndent === "".
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At core/edit/searchAndReplace/performReplace.ts, line 58:
<comment>First replacement line can remain unindented when `oldIndent` is non-empty, because fallback indentation adjustment only runs for `oldIndent === ""`.</comment>
<file context>
@@ -1,6 +1,67 @@
+ // For lines that don't start with the old indent (e.g. first line
+ // might have no indent if old_string was trimmed), apply the
+ // matched indent directly
+ if (index === 0 && oldIndent === "" && matchedIndent !== "") {
+ return matchedIndent + line;
+ }
</file context>
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.
Summary
How it works
adjustReplacementIndentation()detects the leading whitespace of the matched text in the file and the LLM-providedold_stringnew_stringis re-indented: the old indent prefix is stripped and replaced with the matched indentTest plan
replaceAllmode also applies indentation normalizationFixes #11282
🤖 Generated with Claude Code
Summary by cubic
Preserves indentation when applying fuzzy-matched code edits so Python blocks remain valid. Re-indents the replacement text to match the file’s actual indent level.
newStringwhen it differs from theoldString.replaceAll.Written for commit 70b6b17. Summary will update on new commits.