summaryrefslogtreecommitdiff
path: root/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.java
diff options
context:
space:
mode:
Diffstat (limited to 'RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.java')
-rw-r--r--RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.java127
1 files changed, 46 insertions, 81 deletions
diff --git a/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.java b/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.java
index ce1047eebc5c..f8269dcd498b 100644
--- a/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.java
+++ b/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.java
@@ -16,33 +16,30 @@
package org.intellij.lang.regexp.intention;
import com.intellij.ide.util.PropertiesComponent;
+import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
-import com.intellij.openapi.actionSystem.CommonShortcuts;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.fileTypes.PlainTextFileType;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.popup.Balloon;
-import com.intellij.openapi.ui.popup.JBPopupAdapter;
-import com.intellij.openapi.ui.popup.LightweightWindowEvent;
-import com.intellij.openapi.util.Pair;
-import com.intellij.openapi.util.Ref;
+import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.psi.PsiDocumentManager;
-import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
-import com.intellij.psi.impl.source.resolve.FileContextUtil;
-import com.intellij.ui.BalloonImpl;
+import com.intellij.psi.PsiLanguageInjectionHost;
+import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
+import com.intellij.ui.components.JBLabel;
import com.intellij.util.Alarm;
import com.intellij.util.ui.UIUtil;
import org.intellij.lang.regexp.RegExpLanguage;
import org.intellij.lang.regexp.RegExpModifierProvider;
+import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
@@ -56,52 +53,40 @@ import java.util.regex.Pattern;
*/
public class CheckRegExpForm {
private static final String LAST_EDITED_REGEXP = "last.edited.regexp";
- private Pair<PsiFile, Ref<Balloon>> myParams;
+ private final PsiFile myRegexpFile;
private EditorTextField mySampleText; //TODO[kb]: make it multiline
private EditorTextField myRegExp;
private JPanel myRootPanel;
- private Ref<Balloon> myRef;
+ private JBLabel myMessage;
private Project myProject;
- public CheckRegExpForm(Pair<PsiFile, Ref<Balloon>> params) {
- myParams = params;
+ public CheckRegExpForm(@NotNull PsiFile regexpFile) {
+ myRegexpFile = regexpFile;
}
private void createUIComponents() {
- PsiFile file = myParams.first;
- myProject = file.getProject();
- myRef = myParams.second;
- Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
+ myProject = myRegexpFile.getProject();
+ Document document = PsiDocumentManager.getInstance(myProject).getDocument(myRegexpFile);
myRegExp = new EditorTextField(document, myProject, RegExpLanguage.INSTANCE.getAssociatedFileType());
+ myRegExp.setPreferredWidth(Math.max(300, myRegExp.getPreferredSize().width));
final String sampleText = PropertiesComponent.getInstance(myProject).getValue(LAST_EDITED_REGEXP, "Sample Text");
mySampleText = new EditorTextField(sampleText, myProject, PlainTextFileType.INSTANCE);
mySampleText.setBorder(
- new CompoundBorder(new EmptyBorder(2, 2, 2, 4), new LineBorder(UIUtil.isUnderDarcula() ? Gray._100 : UIUtil.getBorderColor())));
+ new CompoundBorder(new EmptyBorder(2, 2, 2, 4), new LineBorder(UIUtil.isUnderDarcula() ? Gray._100 : JBColor.border())));
mySampleText.setOneLineMode(false);
- mySampleText.addDocumentListener(new DocumentAdapter() {
- @Override
- public void documentChanged(DocumentEvent e) {
- //noinspection SSBasedInspection
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- myRootPanel.revalidate();
- final Balloon balloon = myRef.get();
- if (balloon != null) {
- balloon.revalidate();
- }
- }
- });
- }
- });
myRootPanel = new JPanel(new BorderLayout()) {
+ Disposable disposable;
+
@Override
public void addNotify() {
super.addNotify();
+ disposable = Disposer.newDisposable();
+
IdeFocusManager.getGlobalInstance().requestFocus(mySampleText, true);
new AnAction(){
@@ -109,26 +94,10 @@ public class CheckRegExpForm {
public void actionPerformed(AnActionEvent e) {
IdeFocusManager.findInstance().requestFocus(myRegExp.getFocusTarget(), true);
}
- }.registerCustomShortcutSet(CustomShortcutSet.fromString("shift TAB"), mySampleText, myRef.get());
- final AnAction escaper = new AnAction() {
- @Override
- public void actionPerformed(AnActionEvent e) {
- myRef.get().hide();
- }
- };
- escaper.registerCustomShortcutSet(CommonShortcuts.ESCAPE, myRegExp.getFocusTarget(), myRef.get());
- escaper.registerCustomShortcutSet(CommonShortcuts.ESCAPE, mySampleText.getFocusTarget(), myRef.get());
-
-
- myRef.get().addListener(new JBPopupAdapter() {
- @Override
- public void onClosed(LightweightWindowEvent event) {
- PropertiesComponent.getInstance(myProject).setValue(LAST_EDITED_REGEXP, mySampleText.getText());
- }
- });
+ }.registerCustomShortcutSet(CustomShortcutSet.fromString("shift TAB"), mySampleText);
- final Alarm updater = new Alarm(Alarm.ThreadToUse.SWING_THREAD, myRef.get());
- final DocumentAdapter documentListener = new DocumentAdapter() {
+ final Alarm updater = new Alarm(Alarm.ThreadToUse.SWING_THREAD, disposable);
+ DocumentAdapter documentListener = new DocumentAdapter() {
@Override
public void documentChanged(DocumentEvent e) {
updater.cancelAllRequests();
@@ -148,48 +117,44 @@ public class CheckRegExpForm {
updateBalloon();
mySampleText.selectAll();
}
+
+ @Override
+ public void removeNotify() {
+ super.removeNotify();
+ Disposer.dispose(disposable);
+ PropertiesComponent.getInstance(myProject).setValue(LAST_EDITED_REGEXP, mySampleText.getText());
+ }
};
}
+ @NotNull
+ public JComponent getPreferredFocusedComponent() {
+ return mySampleText;
+ }
+ @NotNull
public JPanel getRootPanel() {
return myRootPanel;
}
private void updateBalloon() {
boolean correct = false;
- try {
- final PsiFile file = myParams.first;
- //todo: unfortunately there is no way to access host element representing regexp
- int offset = -1;
- try {
- final String name = file.getName();
- offset = Integer.parseInt(name.substring(name.lastIndexOf(':') + 1, name.lastIndexOf(')')));
- } catch (Exception ignore) {}
-
- int flags = 0;
- if (offset != -1) {
- final PsiFile host = FileContextUtil.getContextFile(file);
- if (host != null) {
- final PsiElement regexpInHost = host.findElementAt(offset);
- if (regexpInHost != null) {
- for (RegExpModifierProvider provider : RegExpModifierProvider.EP.getExtensions()) {
- final int modifiers = provider.getFlags(regexpInHost, file);
- if (modifiers > 0) {
- flags = modifiers;
- break;
- }
- }
- }
- }
+ PsiLanguageInjectionHost host = InjectedLanguageUtil.findInjectionHost(myRegexpFile);
+ int flags = 0;
+ if (host != null) {
+ for (RegExpModifierProvider provider : RegExpModifierProvider.EP.getExtensions()) {
+ flags = provider.getFlags(host, myRegexpFile);
+ if (flags > 0) break;
}
+ }
+ try {
correct = Pattern.compile(myRegExp.getText(), flags).matcher(mySampleText.getText()).matches();
} catch (Exception ignore) {}
- mySampleText.setBackground(correct ? new JBColor(new Color(231, 250, 219), new Color(68, 85, 66)) : new JBColor(new Color(255, 177, 160), new Color(110, 43, 40)));
- BalloonImpl balloon = (BalloonImpl)myRef.get();
- if (balloon != null && balloon.isDisposed()) {
- balloon.revalidate();
- }
+ JBColor color1 = new JBColor(new Color(231, 250, 219), new Color(68, 85, 66));
+ JBColor color2 = new JBColor(new Color(255, 177, 160), new Color(110, 43, 40));
+ mySampleText.setBackground(correct ? color1 : color2);
+ myMessage.setText(correct ? "Matches!" : "no match");
+ myRootPanel.revalidate();
}
}