mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
RegExp checker cosmetic
This commit is contained in:
@@ -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">
|
||||
|
||||
@@ -32,13 +32,14 @@ import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
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.psi.PsiLanguageInjectionHost;
|
||||
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
|
||||
import com.intellij.ui.BalloonImpl;
|
||||
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;
|
||||
@@ -62,6 +63,7 @@ public class CheckRegExpForm {
|
||||
|
||||
private EditorTextField myRegExp;
|
||||
private JPanel myRootPanel;
|
||||
private JBLabel myMessage;
|
||||
private Ref<Balloon> myRef;
|
||||
private Project myProject;
|
||||
|
||||
@@ -77,6 +79,7 @@ public class CheckRegExpForm {
|
||||
Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
|
||||
|
||||
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(
|
||||
@@ -158,35 +161,23 @@ public class CheckRegExpForm {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
PsiFile file = myParams.first;
|
||||
PsiLanguageInjectionHost host = InjectedLanguageUtil.findInjectionHost(file);
|
||||
int flags = 0;
|
||||
if (host != null) {
|
||||
for (RegExpModifierProvider provider : RegExpModifierProvider.EP.getExtensions()) {
|
||||
flags = provider.getFlags(host, file);
|
||||
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)));
|
||||
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");
|
||||
BalloonImpl balloon = (BalloonImpl)myRef.get();
|
||||
if (balloon != null && balloon.isDisposed()) {
|
||||
balloon.revalidate();
|
||||
|
||||
+2
-4
@@ -41,12 +41,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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user