Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKalyan Prasad Tatavarthi2018-07-30 09:45:56 +0000
committerKalyan Prasad Tatavarthi2018-07-30 09:48:18 +0000
commitc8c61ceaae40fb7480be6c7f29082aea579b47b5 (patch)
tree8c613595c7605f0ae1c081e7b0f23aa75141f09b
parente18255b8f790682a2a9e9778cfcbf52de4f9c859 (diff)
downloadeclipse.platform.text-I20180730-2240.tar.gz
eclipse.platform.text-I20180730-2240.tar.xz
eclipse.platform.text-I20180730-2240.zip
Bug 109481 - [find/replace] replace doesn't work when using a regex withI20180730-2240I20180730-2000I20180730-0800
a lookahead or boundary matchers :- Reverting Changes as a result of Bug 537377 Change-Id: I975d326e67f650bc2dddac6948775b3bb3fbdf5a Signed-off-by: Kalyan Prasad Tatavarthi <kalyan_prasad@in.ibm.com>
-rw-r--r--org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java57
-rw-r--r--org.eclipse.search/search/org/eclipse/search/internal/ui/text/ReplaceRefactoring.java22
-rw-r--r--org.eclipse.text.tests/src/org/eclipse/text/tests/FindReplaceDocumentAdapterTest.java26
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/FindReplaceDocumentAdapter.java10
4 files changed, 16 insertions, 99 deletions
diff --git a/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java
index 675013fbb19..4496d61fb60 100644
--- a/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java
+++ b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2017 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -9,16 +9,13 @@
* IBM Corporation - initial API and implementation
* Christian Walther (Indel AG) - Bug 399094, 402009: Add whole word option to file search
* Terry Parker <tparker@google.com> (Google Inc.) - Bug 441016 - Speed up text search by parallelizing it using JobGroups
- * Florian Ingerl <imelflorianingerl@gmail.com> - Bug 109481 - [find/replace] replace doesn't work when using a regex with a lookahead or boundary matchers
*******************************************************************************/
package org.eclipse.search.tests.filesearch;
import static org.junit.Assert.assertEquals;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-import java.util.Scanner;
import java.util.regex.Pattern;
import org.junit.After;
@@ -27,8 +24,6 @@ import org.junit.ClassRule;
import org.junit.Test;
import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
@@ -43,15 +38,10 @@ import org.eclipse.search.core.text.TextSearchRequestor;
import org.eclipse.search.core.text.TextSearchScope;
import org.eclipse.search.internal.core.text.PatternConstructor;
import org.eclipse.search.internal.ui.SearchPlugin;
-import org.eclipse.search.internal.ui.text.FileSearchQuery;
-import org.eclipse.search.internal.ui.text.FileSearchResult;
-import org.eclipse.search.internal.ui.text.ReplaceRefactoring;
import org.eclipse.search.tests.ResourceHelper;
import org.eclipse.search.tests.SearchTestPlugin;
import org.eclipse.search.ui.text.FileTextSearchScope;
-import org.eclipse.ltk.core.refactoring.Change;
-
public class FileSearchTests {
private static class TestResult {
@@ -533,50 +523,5 @@ public class FileSearchTests {
assertEquals("Number of results in file", expectedCount, k);
}
- @Test
- public void testReplaceWithLookarounds() throws CoreException, IOException {
- IFolder folder= ResourceHelper.createFolder(fProject.getFolder("folder1"));
- IFile file1= ResourceHelper.createFile(folder, "file1.txt", "1<2<3<4");
- IFile file2= ResourceHelper.createFile(folder, "file2.txt", "4<5<6<7");
-
- FileSearchResult searchResult= performSearch(new String[] { "*.txt" }, "(?<=(\\d)\\<)\\d(?=\\<(\\d))");
- performReplace(searchResult, "$0=($1+$2)/2");
-
- assertFileContent(file1, "1<2=(1+3)/2<3=(2+4)/2<4");
- assertFileContent(file2, "4<5=(4+6)/2<6=(5+7)/2<7");
- }
-
- @Test
- public void testReplaceRetainCase() throws CoreException, IOException {
- IFolder folder= ResourceHelper.createFolder(fProject.getFolder("folder1"));
- IFile file1= ResourceHelper.createFile(folder, "file1.txt", "FOO");
-
- FileSearchResult searchResult= performSearch(new String[] { "*.txt" }, "FOO");
- performReplace(searchResult, "xyz\\Cbar\\Cfar");
-
- assertFileContent(file1, "xyzBARFAR");
- }
-
- private FileSearchResult performSearch(String[] fileNamePatterns, String pattern) {
- FileTextSearchScope scope= FileTextSearchScope.newSearchScope(new IResource[] { fProject }, fileNamePatterns, false);
- FileSearchQuery query= new FileSearchQuery(pattern, true, true, scope);
- query.run(null);
- return (FileSearchResult) query.getSearchResult();
- }
- private void performReplace(FileSearchResult searchResult, String replacementText) throws OperationCanceledException, CoreException {
- ReplaceRefactoring refactoring= new ReplaceRefactoring(searchResult, null);
- refactoring.setReplaceString(replacementText);
- refactoring.checkInitialConditions(null);
- refactoring.checkFinalConditions(null);
- Change change= refactoring.createChange(null);
- change.perform(new NullProgressMonitor());
- }
-
- private void assertFileContent(IFile file, String expected) throws CoreException {
- try (Scanner scanner= new Scanner(file.getContents())) {
- scanner.useDelimiter("\\A");
- assertEquals(expected, scanner.next());
- }
- }
}
diff --git a/org.eclipse.search/search/org/eclipse/search/internal/ui/text/ReplaceRefactoring.java b/org.eclipse.search/search/org/eclipse/search/internal/ui/text/ReplaceRefactoring.java
index 496fb8a45ba..6ae6d9da17a 100644
--- a/org.eclipse.search/search/org/eclipse/search/internal/ui/text/ReplaceRefactoring.java
+++ b/org.eclipse.search/search/org/eclipse/search/internal/ui/text/ReplaceRefactoring.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2017 IBM Corporation and others.
+ * Copyright (c) 2007, 2015 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,7 +7,6 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
- * Florian Ingerl <imelflorianingerl@gmail.com> - Bug 109481 - [find/replace] replace doesn't work when using a regex with a lookahead or boundary matchers
*******************************************************************************/
package org.eclipse.search.internal.ui.text;
@@ -438,9 +437,7 @@ public class ReplaceRefactoring extends Refactoring {
continue;
}
- String replacementString= PatternConstructor.interpretReplaceEscapes(fReplaceString, originalText,
- lineDelimiter);
- replacementString= computeReplacementString(pattern, document, offset, replacementString);
+ String replacementString= computeReplacementString(pattern, originalText, fReplaceString, lineDelimiter);
if (replacementString == null) {
resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
continue;
@@ -470,18 +467,21 @@ public class ReplaceRefactoring extends Refactoring {
return PatternConstructor.createPattern(query.getSearchString(), true, true, query.isCaseSensitive(), false);
}
- private String computeReplacementString(Pattern pattern, IDocument document, int offset, String replacementText)
- throws PatternSyntaxException {
+ private String computeReplacementString(Pattern pattern, String originalText, String replacementText, String lineDelimiter) throws PatternSyntaxException {
if (pattern != null) {
try {
- Matcher matcher= pattern.matcher(document.get());
- if (matcher.find(offset)) {
- StringBuffer sb= new StringBuffer();
+ replacementText= PatternConstructor.interpretReplaceEscapes(replacementText, originalText, lineDelimiter);
+
+ Matcher matcher= pattern.matcher(originalText);
+ StringBuffer sb = new StringBuffer();
+ matcher.reset();
+ if (matcher.find()) {
matcher.appendReplacement(sb, replacementText);
- return sb.substring(offset);
} else {
return null;
}
+ matcher.appendTail(sb);
+ return sb.toString();
} catch (IndexOutOfBoundsException ex) {
throw new PatternSyntaxException(ex.getLocalizedMessage(), replacementText, -1);
}
diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/FindReplaceDocumentAdapterTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/FindReplaceDocumentAdapterTest.java
index a51c2b55c4c..01e6ad5e1e4 100644
--- a/org.eclipse.text.tests/src/org/eclipse/text/tests/FindReplaceDocumentAdapterTest.java
+++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/FindReplaceDocumentAdapterTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2017 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -9,7 +9,6 @@
* IBM Corporation - initial API and implementation
* Cagatay Calli <ccalli@gmail.com> - [find/replace] retain caps when replacing - https://bugs.eclipse.org/bugs/show_bug.cgi?id=28949
* Cagatay Calli <ccalli@gmail.com> - [find/replace] define & fix behavior of retain caps with other escapes and text before \C - https://bugs.eclipse.org/bugs/show_bug.cgi?id=217061
- * Florian Ingerl <imelflorianingerl@gmail.com> - [find/replace] replace doesn't work when using a regex with a lookahead or boundary matchers - https://bugs.eclipse.org/bugs/show_bug.cgi?id=109481
*******************************************************************************/
package org.eclipse.text.tests;
@@ -501,27 +500,4 @@ public class FindReplaceDocumentAdapterTest {
}
fail();
}
-
- @Test
- public void testRegexReplaceWithLookarounds() throws Exception {
- fDocument.set("1<2<3<4");
- replaceAllRegex("(?<=(\\d))\\<(\\d)(?=\\<(\\d))", "<($1+$3)/2=$2", true);
- assertEquals("1<(1+3)/2=2<(2+4)/2=3<4", fDocument.get());
-
- fDocument.set("1<2<3<4");
- replaceAllRegex("(?<=(\\d)\\<)(\\d)\\<(?=(\\d))", "$2=($1+$3)/2<", false);
- assertEquals("1<2=(1+3)/2<3=(2+4)/2<4", fDocument.get());
- }
-
- private void replaceAllRegex(String findString, String replaceString, boolean forwardSearch) throws BadLocationException {
- FindReplaceDocumentAdapter findReplaceDocumentAdapter= new FindReplaceDocumentAdapter(fDocument);
-
- int index= forwardSearch ? 0 : fDocument.getLength();
- while(index!=-1) {
- if(findReplaceDocumentAdapter.find(index, findString, forwardSearch, true, false, true)==null)
- break;
- IRegion region= findReplaceDocumentAdapter.replace(replaceString, true);
- index= forwardSearch ? region.getOffset() + region.getLength() : region.getOffset() - 1;
- }
- }
}
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/FindReplaceDocumentAdapter.java b/org.eclipse.text/src/org/eclipse/jface/text/FindReplaceDocumentAdapter.java
index 7fc5049fee1..d80a2f9cc16 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/FindReplaceDocumentAdapter.java
+++ b/org.eclipse.text/src/org/eclipse/jface/text/FindReplaceDocumentAdapter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2017 IBM Corporation and others.
+ * Copyright (c) 2000, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -9,7 +9,6 @@
* IBM Corporation - initial API and implementation
* Cagatay Calli <ccalli@gmail.com> - [find/replace] retain caps when replacing - https://bugs.eclipse.org/bugs/show_bug.cgi?id=28949
* Cagatay Calli <ccalli@gmail.com> - [find/replace] define & fix behavior of retain caps with other escapes and text before \C - https://bugs.eclipse.org/bugs/show_bug.cgi?id=217061
- * Florian Ingerl <imelflorianingerl@gmail.com> - [find/replace] replace doesn't work when using a regex with a lookahead or boundary matchers - https://bugs.eclipse.org/bugs/show_bug.cgi?id=109481
*******************************************************************************/
package org.eclipse.jface.text;
@@ -195,11 +194,8 @@ public class FindReplaceDocumentAdapter implements CharSequence {
String prevMatch= fFindReplaceMatcher.group();
try {
replaceText= interpretReplaceEscapes(replaceText, prevMatch);
- Matcher replaceTextMatcher= pattern.matcher(this);
- replaceTextMatcher.find(fFindReplaceMatcher.start());
- StringBuffer sb= new StringBuffer();
- replaceTextMatcher.appendReplacement(sb, replaceText);
- replaceText= sb.substring(fFindReplaceMatcher.start());
+ Matcher replaceTextMatcher= pattern.matcher(prevMatch);
+ replaceText= replaceTextMatcher.replaceFirst(replaceText);
} catch (IndexOutOfBoundsException ex) {
throw new PatternSyntaxException(ex.getLocalizedMessage(), replaceText, -1);
}

Back to the top