From 30ae739859e5b94d474a747c33e2776dce7a8d3b Mon Sep 17 00:00:00 2001 From: braedonbillingsley Date: Mon, 2 Jun 2025 09:49:15 -0700 Subject: [PATCH] Fix regular expression replacement with anchors causing infinite recursion - Prevent infinite loops when using anchors like '^' in replace all operations - Track last found position to detect and skip repeated matches at same location - Resolves issue where '^ ' to '' removed all leading spaces instead of one Fixes #2820 --- .../ui/internal/findandreplace/FindReplaceLogic.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java index 53640118526..89567ec3a0a 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java @@ -359,7 +359,17 @@ private int replaceAll() { executeInForwardMode(() -> { executeWithReplaceAllEnabled(() -> { Point currentSelection = new Point(0, 0); + int lastFoundPosition = -1; + while (findAndSelect(currentSelection.x + currentSelection.y) != -1) { + Point foundMatch = target.getSelection(); + + if (foundMatch.x == lastFoundPosition) { + currentSelection = new Point(foundMatch.x + Math.max(1, foundMatch.y), 0); + continue; + } + + lastFoundPosition = foundMatch.x; currentSelection = replaceSelection(); replacements.add(currentSelection); }