From 403c201a0fb23aa32f72edc1e53baa88ee042562 Mon Sep 17 00:00:00 2001 From: "andrey.zaytsev" Date: Mon, 29 Apr 2013 20:10:21 +0400 Subject: [PATCH] IDEA-105409 Replace All is very slow when used with reg exp option --- .../src/com/intellij/find/FindUtil.java | 3 +- .../intellij/find/impl/FindManagerImpl.java | 34 ++++++++++++------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/find/FindUtil.java b/platform/lang-impl/src/com/intellij/find/FindUtil.java index 156a77e72642..ef997ad047f4 100644 --- a/platform/lang-impl/src/com/intellij/find/FindUtil.java +++ b/platform/lang-impl/src/com/intellij/find/FindUtil.java @@ -539,7 +539,8 @@ public class FindUtil { String foundString = document.getCharsSequence().subSequence(startOffset, endOffset).toString(); String toReplace; try { - toReplace = findManager.getStringToReplace(foundString, model, startOffset, document.getText()); + CharSequence textToMatch = document.getCharsSequence().subSequence(offset, endOffset < document.getTextLength() ? endOffset+1 : endOffset); + toReplace = findManager.getStringToReplace(foundString, model, startOffset - offset, textToMatch); } catch (FindManager.MalformedReplacementStringException e) { if (!ApplicationManager.getApplication().isUnitTestMode()) { diff --git a/platform/lang-impl/src/com/intellij/find/impl/FindManagerImpl.java b/platform/lang-impl/src/com/intellij/find/impl/FindManagerImpl.java index e860ab5d4254..5f616453eb52 100644 --- a/platform/lang-impl/src/com/intellij/find/impl/FindManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/find/impl/FindManagerImpl.java @@ -624,7 +624,7 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo @Override public String getStringToReplace(@NotNull String foundString, @NotNull FindModel model, - int startOffset, @NotNull String documentText) throws MalformedReplacementStringException{ + int startOffset, @NotNull CharSequence documentText) throws MalformedReplacementStringException{ String toReplace = model.getStringToReplace(); if (model.isRegularExpressions()) { return getStringToReplaceByRegexp(model, documentText, startOffset); @@ -635,7 +635,26 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo return toReplace; } - private static String getStringToReplaceByRegexp(@NotNull final FindModel model, @NotNull String text, int startOffset) throws MalformedReplacementStringException{ + private static String getStringToReplaceByRegexp(@NotNull final FindModel model, @NotNull CharSequence text, int startOffset) throws MalformedReplacementStringException { + Matcher matcher = compileRegexAndFindFirst(model, text, startOffset); + return getStringToReplaceByRegexp(model, matcher); + } + + private static String getStringToReplaceByRegexp(@NotNull final FindModel model, Matcher matcher) throws MalformedReplacementStringException{ + StringBuffer replaced = new StringBuffer(); + if (matcher == null) return null; + try { + String toReplace = StringUtil.unescapeStringCharacters(model.getStringToReplace()); + matcher.appendReplacement(replaced, toReplace); + + return replaced.substring(matcher.start()); + } + catch (Exception e) { + throw createMalformedReplacementException(model, e); + } + } + + private static Matcher compileRegexAndFindFirst(FindModel model, CharSequence text, int startOffset) { Matcher matcher = compileRegExp(model, text); if (model.isForward()){ @@ -655,16 +674,7 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo return null; } } - try { - StringBuffer replaced = new StringBuffer(); - String toReplace = StringUtil.unescapeStringCharacters(model.getStringToReplace()); - matcher.appendReplacement(replaced, toReplace); - - return replaced.substring(matcher.start()); - } - catch (Exception e) { - throw createMalformedReplacementException(model, e); - } + return matcher; } private static MalformedReplacementStringException createMalformedReplacementException(FindModel model, Exception e) {