IDEA-114094 Search-replace in files does not happen if comments only is enabled

This commit is contained in:
Maxim.Mossienko
2013-10-24 12:54:52 +02:00
parent 4c5364f22c
commit 626bc985a4
3 changed files with 42 additions and 12 deletions
@@ -552,6 +552,31 @@ public class FindManagerTest extends DaemonAnalyzerTestCase {
runFindForwardAndBackward(findManager, findModel, text);
}
public void testFindInCommentsProperlyWorksWithOffsets() throws Exception{
FindManager findManager = FindManager.getInstance(myProject);
FindModel findModel = new FindModel();
findModel.setStringToFind("done");
findModel.setWholeWordsOnly(false);
findModel.setFromCursor(false);
findModel.setGlobal(true);
findModel.setMultipleFiles(false);
findModel.setProjectScope(true);
String prefix = "/*";
String text = prefix + "done*/";
findModel.setInCommentsOnly(true);
LightVirtualFile file = new LightVirtualFile("A.java", text);
FindResult findResult = findManager.findString(text, prefix.length(), findModel, file);
assertTrue(findResult.isStringFound());
findModel.setRegularExpressions(true);
findResult = findManager.findString(text, prefix.length(), findModel, file);
assertTrue(findResult.isStringFound());
}
public void testFindInUserFileType() throws Exception{
FindManager findManager = FindManager.getInstance(myProject);
@@ -411,9 +411,10 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo
final StringSearcher searcher;
final Matcher matcher;
final Set<Language> relevantLanguages;
final FindModel myModel;
public CommentsLiteralsSearchData(VirtualFile lastFile, Set<Language> relevantLanguages, SyntaxHighlighter highlighter, TokenSet tokensOfInterest,
StringSearcher searcher, Matcher matcher) {
StringSearcher searcher, Matcher matcher, FindModel model) {
this.lastFile = lastFile;
this.highlighter = highlighter;
this.tokensOfInterest = tokensOfInterest;
@@ -421,6 +422,7 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo
this.matcher = matcher;
this.relevantLanguages = relevantLanguages;
highlightingLexer = highlighter.getHighlightingLexer();
myModel = model;
}
}
@@ -439,7 +441,7 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo
}
CommentsLiteralsSearchData data = model.getUserData(ourCommentsLiteralsSearchDataKey);
if (data == null || !Comparing.equal(data.lastFile, file)) {
if (data == null || !Comparing.equal(data.lastFile, file) || !data.myModel.equals(model)) {
SyntaxHighlighter highlighter = getHighlighter(file, lang);
if (highlighter == null) {
@@ -504,8 +506,8 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo
}
Matcher matcher = model.isRegularExpressions() ? compileRegExp(model, ""):null;
StringSearcher searcher = matcher != null ? null: createStringSearcher(model);
data = new CommentsLiteralsSearchData(file, relevantLanguages, highlighter, tokensOfInterest, searcher, matcher);
StringSearcher searcher = matcher != null ? null: new StringSearcher(model.getStringToFind(), model.isCaseSensitive(), true);
data = new CommentsLiteralsSearchData(file, relevantLanguages, highlighter, tokensOfInterest, searcher, matcher, (FindModel)model.clone());
model.putUserData(ourCommentsLiteralsSearchDataKey, data);
}
@@ -545,12 +547,12 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo
FindResultImpl findResult = null;
if (data.searcher != null) {
int i = data.searcher.scan(text, textArray, start, end);
int matchStart = data.searcher.scan(text, textArray, start, end);
if (i != -1 && i >= start) {
final int matchEnd = i + model.getStringToFind().length();
if (start >= offset || !scanningForward)
findResult = new FindResultImpl(i, matchEnd);
if (matchStart != -1 && matchStart >= start) {
final int matchEnd = matchStart + model.getStringToFind().length();
if (matchStart >= offset || !scanningForward)
findResult = new FindResultImpl(matchStart, matchEnd);
else {
start = matchEnd;
continue;
@@ -560,8 +562,9 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo
data.matcher.reset(text.subSequence(start, end));
if (data.matcher.find()) {
final int matchEnd = start + data.matcher.end();
if (start >= offset || !scanningForward) {
findResult = new FindResultImpl(start + data.matcher.start(), matchEnd);
int matchStart = start + data.matcher.start();
if (matchStart >= offset || !scanningForward) {
findResult = new FindResultImpl(matchStart, matchEnd);
}
else {
start = matchEnd;
@@ -42,6 +42,7 @@ import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.usageView.UsageInfo;
@@ -433,7 +434,8 @@ public class ReplaceInProjectManager {
}
FindManager findManager = FindManager.getInstance(myProject);
final CharSequence foundString = document.getCharsSequence().subSequence(textOffset, textEndOffset);
FindResult findResult = findManager.findString(document.getCharsSequence(), textOffset, findModel);
PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
FindResult findResult = findManager.findString(document.getCharsSequence(), textOffset, findModel, file != null ? file.getVirtualFile() : null);
if (!findResult.isStringFound()) {
return false;
}