Fix regular expression replacement with anchors causing infinite recursion #3028
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.
Fixes an issue where using anchors (like ^) in "Replace All" operations would cause infinite loops, removing more text than intended.
When performing a regex "Replace All" with patterns containing anchors like ^ (start of line), the current implementation would recurse infinitely on the same line. For example:
Pattern: ^ (start of line + space)
Replace with: `` (empty string)
Expected: Remove one leading space from each line
Actual: Remove ALL leading spaces from each line
Root Cause: The replaceAll() method uses a loop that continues until no matches are found.
Solution:
Added position tracking to detect when the same position is matched repeatedly:
Track the last found position (lastFoundPosition)
If the same position is found consecutively, advance past it manually
This breaks the infinite loop while preserving normal replacement behavior
Code Changes:
File: bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java
Method: replaceAll()
Changes:
Added lastFoundPosition tracking variable
Added position comparison logic to detect repeated matches
Force advance past problematic positions when detected
Fixes #2820