summaryrefslogtreecommitdiff
path: root/RegExpSupport
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2014-08-20 17:01:23 -0700
committerTor Norbye <tnorbye@google.com>2014-08-20 17:01:23 -0700
commit1aa2e09bdbd413eacb677e9fa4b50630530d0656 (patch)
tree2f4cc6d69645bd460aa253fdecb606d764fbd25d /RegExpSupport
parent02cf98d65c798d368fcec43ed64a001d513bdd4f (diff)
downloadidea-1aa2e09bdbd413eacb677e9fa4b50630530d0656.tar.gz
Snapshot idea/138.1696 from git://git.jetbrains.org/idea/community.git
Change-Id: I50c97b83a815ce635e49a38380ba5b8765e4b16a
Diffstat (limited to 'RegExpSupport')
-rw-r--r--RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.form8
-rw-r--r--RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.java127
-rw-r--r--RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpIntentionAction.java12
3 files changed, 55 insertions, 92 deletions
diff --git a/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.form b/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.form
index b04addde5858..9029a33969fc 100644
--- a/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.form
+++ b/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.form
@@ -17,7 +17,9 @@
<grid id="a052" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints border-constraint="Center"/>
<properties/>
- <border type="none"/>
+ <border type="empty">
+ <size top="5" left="5" bottom="10" right="5"/>
+ </border>
<children>
<grid id="b7b2b" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints border-constraint="North"/>
@@ -66,14 +68,14 @@
<properties/>
<border type="none"/>
<children>
- <component id="5de69" class="com.intellij.ui.components.JBLabel">
+ <component id="5de69" class="com.intellij.ui.components.JBLabel" binding="myMessage">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<alignmentX value="0.5"/>
<componentStyle value="SMALL"/>
- <text value="Red means Sample does not match RegExp"/>
+ <text value="Red means I can't hear you!"/>
</properties>
</component>
<hspacer id="c8c90">
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();
}
}
diff --git a/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpIntentionAction.java b/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpIntentionAction.java
index 6c052461c5b8..6da84caa56ec 100644
--- a/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpIntentionAction.java
+++ b/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpIntentionAction.java
@@ -20,10 +20,8 @@ import com.intellij.lang.Language;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.util.Iconable;
import com.intellij.openapi.util.Pair;
-import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
@@ -41,12 +39,10 @@ public class CheckRegExpIntentionAction extends QuickEditAction implements Icona
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
- final Pair<PsiElement, TextRange> pair = getRangePair(file, editor);
- /*super.isAvailable(project, editor, file) && */
+ Pair<PsiElement, TextRange> pair = getRangePair(file, editor);
if (pair != null && pair.first != null) {
Language language = pair.first.getLanguage();
- Language baseLanguage = language.getBaseLanguage();
- return language == RegExpLanguage.INSTANCE || baseLanguage == RegExpLanguage.INSTANCE;
+ return language.isKindOf(RegExpLanguage.INSTANCE);
}
return false;
}
@@ -57,11 +53,11 @@ public class CheckRegExpIntentionAction extends QuickEditAction implements Icona
}
@Override
- protected JComponent createBalloonComponent(PsiFile file, final Ref<Balloon> ref) {
+ protected JComponent createBalloonComponent(@NotNull PsiFile file) {
final Project project = file.getProject();
final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
if (document != null) {
- return new CheckRegExpForm(Pair.create(file, ref)).getRootPanel();
+ return new CheckRegExpForm(file).getRootPanel();
}
return null;
}