more on FInd Tests

This commit is contained in:
andrey.zaytsev
2013-05-07 17:30:48 +04:00
parent 937f8c0dd8
commit 72dd246326
5 changed files with 210 additions and 49 deletions

View File

@@ -0,0 +1,128 @@
--- FIND MODEL ---
myStringToFind =
myStringToReplace =
isReplaceState =false
isWholeWordsOnly =false
isInStringLiterals =false
isInComments =false
isFromCursor =true
isForward =true
isGlobal =true
isRegularExpressions =true
isCaseSensitive =false
isMultipleFiles =false
isPromptOnReplace =true
isReplaceAll =false
isOpenNewTab =false
isOpenInNewTabEnabled =false
isOpenNewTabVisible =false
isProjectScope =true
directoryName =null
isWithSubdirectories =true
fileFilter =null
moduleName =null
customScopeName =null
--
a
--
------------
--- FIND MODEL ---
myStringToFind =a
myStringToReplace =
isReplaceState =false
isWholeWordsOnly =false
isInStringLiterals =false
isInComments =false
isFromCursor =true
isForward =true
isGlobal =true
isRegularExpressions =true
isCaseSensitive =false
isMultipleFiles =false
isPromptOnReplace =true
isReplaceAll =false
isOpenNewTab =false
isOpenInNewTabEnabled =false
isOpenNewTabVisible =false
isProjectScope =true
directoryName =null
isWithSubdirectories =true
fileFilter =null
moduleName =null
customScopeName =null
--
[[<a>]]
--
RangeHighlighter: (0,1); layer:5999; tooltip: null : [null,java.awt.Color[r=255,g=255,b=0],0,BOXED,null,java.awt.Color[r=0,g=255,b=0]]
RangeHighlighter: (0,1); layer:5999; tooltip: null : [null,null,0,ROUNDED_BOX,java.awt.Color[r=0,g=0,b=0],null]
------------
--- FIND MODEL ---
myStringToFind =a
myStringToReplace =
isReplaceState =true
isWholeWordsOnly =false
isInStringLiterals =false
isInComments =false
isFromCursor =true
isForward =true
isGlobal =true
isRegularExpressions =true
isCaseSensitive =false
isMultipleFiles =false
isPromptOnReplace =true
isReplaceAll =false
isOpenNewTab =false
isOpenInNewTabEnabled =false
isOpenNewTabVisible =false
isProjectScope =true
directoryName =null
isWithSubdirectories =true
fileFilter =null
moduleName =null
customScopeName =null
--
Replacement Preview: <Empty string>
--
[[<a>]]
--
RangeHighlighter: (0,1); layer:5999; tooltip: null : [null,java.awt.Color[r=255,g=255,b=0],0,BOXED,null,java.awt.Color[r=0,g=255,b=0]]
RangeHighlighter: (0,1); layer:5999; tooltip: null : [null,null,0,ROUNDED_BOX,java.awt.Color[r=0,g=0,b=0],null]
------------
--- FIND MODEL ---
myStringToFind =a
myStringToReplace =
isReplaceState =true
isWholeWordsOnly =false
isInStringLiterals =false
isInComments =false
isFromCursor =true
isForward =true
isGlobal =true
isRegularExpressions =true
isCaseSensitive =false
isMultipleFiles =false
isPromptOnReplace =true
isReplaceAll =false
isOpenNewTab =false
isOpenInNewTabEnabled =false
isOpenNewTabVisible =false
isProjectScope =true
directoryName =null
isWithSubdirectories =true
fileFilter =null
moduleName =null
customScopeName =null
--
--
Not Found
--
------------

View File

@@ -36,12 +36,6 @@ public class FindInEditorTest extends LightCodeInsightTestCase {
super.setUp();
myFindModel = new FindModel();
myFindModel.addObserver(new FindModel.FindModelObserver() {
@Override
public void findModelChanged(FindModel findModel) {
myLivePreviewController.updateInBackground(myFindModel, true);
}
});
myOutputStream = new ByteArrayOutputStream();
LivePreview.ourTestOutput = new PrintStream(myOutputStream);
@@ -50,6 +44,12 @@ public class FindInEditorTest extends LightCodeInsightTestCase {
private void initFind() {
mySearchResults = new SearchResults(getEditor(), getProject());
myLivePreviewController = new LivePreviewController(mySearchResults, null);
myFindModel.addObserver(new FindModel.FindModelObserver() {
@Override
public void findModelChanged(FindModel findModel) {
myLivePreviewController.updateInBackground(myFindModel, true);
}
});
myLivePreviewController.on();
}
@@ -69,6 +69,19 @@ public class FindInEditorTest extends LightCodeInsightTestCase {
checkResults();
}
public void testReplacementWithEmptyString() throws Exception {
configureFromFileText("file.txt", "a");
initFind();
myFindModel.setRegularExpressions(true);
myFindModel.setStringToFind("a");
myFindModel.setStringToReplace("");
myFindModel.setReplaceState(true);
myLivePreviewController.performReplace();
checkResults();
}
private void checkResults() {
String name = getTestName(false);
assertSameLinesWithFile(getTestDataPath() + "/find/findInEditor/" + name + ".gold", myOutputStream.toString());

View File

@@ -21,6 +21,7 @@ import com.intellij.codeInsight.hint.HintManagerImpl;
import com.intellij.codeInsight.hint.HintUtil;
import com.intellij.find.findUsages.PsiElement2UsageTargetAdapter;
import com.intellij.find.impl.FindInProjectUtil;
import com.intellij.find.impl.livePreview.LivePreview;
import com.intellij.find.replaceInProject.ReplaceInProjectManager;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
@@ -827,12 +828,17 @@ public class FindUtil {
editor.getCaretModel().addCaretListener(listener);
}
JComponent component = HintUtil.createInformationLabel(JDOMUtil.escapeText(message, false, false));
final LightweightHint hint = new LightweightHint(component);
HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, position,
HintManager.HIDE_BY_ANY_KEY |
HintManager.HIDE_BY_TEXT_CHANGE |
HintManager.HIDE_BY_SCROLLING,
0, false);
if (ApplicationManager.getApplication().isUnitTestMode()) {
LivePreview.processNotFound();
} else {
final LightweightHint hint = new LightweightHint(component);
HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, position,
HintManager.HIDE_BY_ANY_KEY |
HintManager.HIDE_BY_TEXT_CHANGE |
HintManager.HIDE_BY_SCROLLING,
0, false);
}
}
public static TextRange doReplace(final Project project,

View File

@@ -40,7 +40,6 @@ import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Processor;
@@ -54,7 +53,7 @@ import java.io.*;
import java.util.*;
import java.util.List;
public class LivePreview extends DocumentAdapter implements ReplacementView.Delegate, SearchResults.SearchResultsListener,
public class LivePreview extends DocumentAdapter implements SearchResults.SearchResultsListener,
SelectionListener {
private static final Key<Object> IN_SELECTION_KEY = Key.create("LivePreview.IN_SELECTION_KEY");
@@ -71,6 +70,8 @@ public class LivePreview extends DocumentAdapter implements ReplacementView.Dele
private static final Key<Object> SEARCH_MARKER = Key.create("LivePreview.SEARCH_MARKER");
public static PrintStream ourTestOutput;
private String myReplacementPreviewText;
private static boolean NotFound;
@Override
public void selectionChanged(SelectionEvent e) {
@@ -85,6 +86,10 @@ public class LivePreview extends DocumentAdapter implements ReplacementView.Dele
myInSmartUpdate = true;
}
public static void processNotFound() {
NotFound = true;
}
public interface Delegate {
@Nullable
@@ -131,13 +136,17 @@ public class LivePreview extends DocumentAdapter implements ReplacementView.Dele
}
private void dumpState() {
if (ApplicationManager.getApplication().isUnitTestMode()) {
if (ApplicationManager.getApplication().isUnitTestMode() && ourTestOutput != null) {
dumpEditorMarkupAndSelection(ourTestOutput);
}
}
private void dumpEditorMarkupAndSelection(PrintStream dumpStream) {
dumpStream.println(mySearchResults.getFindModel());
if (myReplacementPreviewText != null) {
dumpStream.println("--");
dumpStream.println("Replacement Preview: " + myReplacementPreviewText);
}
dumpStream.println("--");
Editor editor = mySearchResults.getEditor();
@@ -151,9 +160,10 @@ public class LivePreview extends DocumentAdapter implements ReplacementView.Dele
SelectionModel selectionModel = editor.getSelectionModel();
ranges.add(new Pair<Integer, Character>(selectionModel.getSelectionStart(), '<'));
ranges.add(new Pair<Integer, Character>(selectionModel.getSelectionEnd(), '>'));
if (selectionModel.getSelectionStart() != selectionModel.getSelectionEnd()) {
ranges.add(new Pair<Integer, Character>(selectionModel.getSelectionStart(), '<'));
ranges.add(new Pair<Integer, Character>(selectionModel.getSelectionEnd(), '>'));
}
ranges.add(new Pair<Integer, Character>(-1, '\n'));
ranges.add(new Pair<Integer, Character>(editor.getDocument().getTextLength()+1, '\n'));
ContainerUtil.sort(ranges, new Comparator<Pair<Integer, Character>>() {
@@ -166,7 +176,8 @@ public class LivePreview extends DocumentAdapter implements ReplacementView.Dele
Character c2 = pair2.second;
if (c1 == '<' && c2 == '[') {
return 1;
} else if (c1 == '[' && c2 == '<') {
}
else if (c1 == '[' && c2 == '<') {
return -1;
}
return c1.compareTo(c2);
@@ -183,6 +194,12 @@ public class LivePreview extends DocumentAdapter implements ReplacementView.Dele
}
dumpStream.println("\n--");
if (NotFound) {
dumpStream.println("Not Found");
dumpStream.println("--");
NotFound = false;
}
for (RangeHighlighter highlighter : highlighters) {
dumpStream.println(highlighter + " : " + highlighter.getTextAttributes());
}
@@ -393,25 +410,38 @@ public class LivePreview extends DocumentAdapter implements ReplacementView.Dele
final FindModel findModel = mySearchResults.getFindModel();
if (findModel.isRegularExpressions() && findModel.isReplaceState()) {
ReplacementView replacementView = new ReplacementView(replacementPreviewText, cursor);
replacementView.setDelegate(this);
BalloonBuilder balloonBuilder = JBPopupFactory.getInstance().createBalloonBuilder(replacementView);
balloonBuilder.setFadeoutTime(0);
balloonBuilder.setFillColor(IdeTooltipManager.GRAPHITE_COLOR);
balloonBuilder.setAnimationCycle(0);
balloonBuilder.setHideOnClickOutside(false);
balloonBuilder.setHideOnKeyOutside(false);
balloonBuilder.setHideOnAction(false);
balloonBuilder.setCloseButtonEnabled(true);
myReplacementBalloon = balloonBuilder.createBalloon();
myReplacementBalloon.show(new ReplacementBalloonPositionTracker(editor), Balloon.Position.above);
showBalloon(cursor, editor, replacementPreviewText);
}
}
}
private void showBalloon(FindResult cursor, Editor editor, String replacementPreviewText) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
myReplacementPreviewText = replacementPreviewText;
return;
}
ReplacementView replacementView = new ReplacementView(replacementPreviewText, cursor);
BalloonBuilder balloonBuilder = JBPopupFactory.getInstance().createBalloonBuilder(replacementView);
balloonBuilder.setFadeoutTime(0);
balloonBuilder.setFillColor(IdeTooltipManager.GRAPHITE_COLOR);
balloonBuilder.setAnimationCycle(0);
balloonBuilder.setHideOnClickOutside(false);
balloonBuilder.setHideOnKeyOutside(false);
balloonBuilder.setHideOnAction(false);
balloonBuilder.setCloseButtonEnabled(true);
myReplacementBalloon = balloonBuilder.createBalloon();
myReplacementBalloon.show(new ReplacementBalloonPositionTracker(editor), Balloon.Position.above);
}
private void hideBalloon() {
if (ApplicationManager.getApplication().isUnitTestMode()) {
myReplacementPreviewText = null;
return;
}
if (myReplacementBalloon != null) {
myReplacementBalloon.hide();
myReplacementBalloon = null;

View File

@@ -25,22 +25,6 @@ public class ReplacementView extends JPanel {
private static final String MALFORMED_REPLACEMENT_STRING = "Malformed replacement string";
private String myReplacement;
private FindResult myOccurrence;
private JButton myStatusButton;
public interface Delegate {
}
private Delegate myDelegate;
public Delegate getDelegate() {
return myDelegate;
}
public void setDelegate(Delegate delegate) {
this.myDelegate = delegate;
}
@Override
protected void paintComponent(Graphics graphics) {