handle escape sequences in LowLevelSearchUtil.searchWord() only in UsageSearchContext.IN_STRINGS

This commit is contained in:
Dmitry Jemerov
2013-01-31 14:49:02 +01:00
parent 09c0a67d8b
commit 2467b934a7
4 changed files with 20 additions and 8 deletions
@@ -205,11 +205,11 @@ public class LowLevelSearchUtil {
if (index > startOffset) {
char c = textArray != null ? textArray[index - 1]:text.charAt(index - 1);
if (Character.isJavaIdentifierPart(c) && c != '$') {
if (index < 2 || !isNotEscapedBackslash(text, textArray, startOffset, index-2)) { //escape sequence
if (searcher.isHandleEscapeSequences() && (index < 2 || !isNotEscapedBackslash(text, textArray, startOffset, index-2))) { //escape sequence
continue;
}
}
else if (index > 0 && isNotEscapedBackslash(text, textArray, startOffset, index-1)) {
else if (index > 0 && searcher.isHandleEscapeSequences() && isNotEscapedBackslash(text, textArray, startOffset, index-1)) {
continue;
}
}
@@ -126,14 +126,14 @@ public class PsiSearchHelperImpl implements PsiSearchHelper {
public AsyncFuture<Boolean> processElementsWithWordAsync(@NotNull final TextOccurenceProcessor processor,
@NotNull SearchScope searchScope,
@NotNull final String text,
short searchContext,
final short searchContext,
final boolean caseSensitively) {
if (text.isEmpty()) {
return AsyncFutureFactory.wrapException(new IllegalArgumentException("Cannot search for elements with empty text"));
}
final ProgressIndicator progress = ProgressIndicatorProvider.getGlobalProgressIndicator();
if (searchScope instanceof GlobalSearchScope) {
StringSearcher searcher = new StringSearcher(text, caseSensitively, true);
StringSearcher searcher = new StringSearcher(text, caseSensitively, true, searchContext == UsageSearchContext.IN_STRINGS);
return processElementsWithTextInGlobalScopeAsync(processor,
(GlobalSearchScope)searchScope,
@@ -154,6 +154,7 @@ public class PsiSearchHelperImpl implements PsiSearchHelper {
text,
caseSensitively,
ignoreInjectedPsi,
searchContext == UsageSearchContext.IN_STRINGS,
progress);
}
});
@@ -165,11 +166,12 @@ public class PsiSearchHelperImpl implements PsiSearchHelper {
final String word,
final boolean caseSensitive,
final boolean ignoreInjectedPsi,
final boolean handleEscapeSequences,
final ProgressIndicator progress) {
return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
@Override
public Boolean compute() {
StringSearcher searcher = new StringSearcher(word, caseSensitive, true);
StringSearcher searcher = new StringSearcher(word, caseSensitive, true, handleEscapeSequences);
return LowLevelSearchUtil.processElementsContainingWordInElement(processor, scopeElement, searcher, !ignoreInjectedPsi, progress);
}
@@ -389,7 +391,7 @@ public class PsiSearchHelperImpl implements PsiSearchHelper {
}
});
final StringSearcher searcher = new StringSearcher(qName, true, true);
final StringSearcher searcher = new StringSearcher(qName, true, true, false);
if (progress != null) {
progress.pushState();
@@ -615,7 +617,7 @@ public class PsiSearchHelperImpl implements PsiSearchHelper {
final Map<RequestWithProcessor, StringSearcher> searchers = new HashMap<RequestWithProcessor, StringSearcher>();
final Set<String> allWords = new TreeSet<String>();
for (RequestWithProcessor singleRequest : candidateFiles.values()) {
searchers.put(singleRequest, new StringSearcher(singleRequest.request.word, singleRequest.request.caseSensitive, true));
searchers.put(singleRequest, new StringSearcher(singleRequest.request.word, singleRequest.request.caseSensitive, true, false));
allWords.add(singleRequest.request.word);
}
@@ -43,7 +43,7 @@ public class LowLevelSearchUtilTest extends TestCase {
}
private static int doTest(String pattern, String text) {
StringSearcher searcher = new StringSearcher(pattern, true, true);
StringSearcher searcher = new StringSearcher(pattern, true, true, true);
return LowLevelSearchUtil.searchWord(text, 0, text.length(), searcher, null);
}
}
@@ -32,12 +32,18 @@ public class StringSearcher {
private final boolean myCaseSensitive;
private final boolean myForwardDirection;
private final boolean myJavaIdentifier;
private final boolean myHandleEscapeSequences;
public int getPatternLength() {
return myPatternLength;
}
public StringSearcher(@NotNull String pattern, boolean caseSensitive, boolean forwardDirection) {
this(pattern, caseSensitive, forwardDirection, false);
}
public StringSearcher(@NotNull String pattern, boolean caseSensitive, boolean forwardDirection, boolean handleEscapeSequences) {
myHandleEscapeSequences = handleEscapeSequences;
LOG.assertTrue(!pattern.isEmpty());
myPattern = pattern;
myCaseSensitive = caseSensitive;
@@ -66,6 +72,10 @@ public class StringSearcher {
return myForwardDirection;
}
public boolean isHandleEscapeSequences() {
return myHandleEscapeSequences;
}
public int scan(@NotNull CharSequence text) {
return scan(text,0,text.length());
}