summaryrefslogtreecommitdiff
path: root/RegExpSupport/src/org/intellij/lang/regexp/intention/CheckRegExpForm.java
blob: ce1047eebc5c536c59d4bdf416c1a56a75ecd020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.intellij.lang.regexp.intention;

import com.intellij.ide.util.PropertiesComponent;
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.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.ui.EditorTextField;
import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import com.intellij.util.Alarm;
import com.intellij.util.ui.UIUtil;
import org.intellij.lang.regexp.RegExpLanguage;
import org.intellij.lang.regexp.RegExpModifierProvider;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.regex.Pattern;

/**
 * @author Konstantin Bulenkov
 */
public class CheckRegExpForm {
  private static final String LAST_EDITED_REGEXP = "last.edited.regexp";
  private Pair<PsiFile, Ref<Balloon>> myParams;

  private EditorTextField mySampleText; //TODO[kb]: make it multiline

  private EditorTextField myRegExp;
  private JPanel myRootPanel;
  private Ref<Balloon> myRef;
  private Project myProject;


  public CheckRegExpForm(Pair<PsiFile, Ref<Balloon>> params) {
    myParams = params;
  }

  private void createUIComponents() {
    PsiFile file = myParams.first;
    myProject = file.getProject();
    myRef = myParams.second;
    Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);

    myRegExp = new EditorTextField(document, myProject, RegExpLanguage.INSTANCE.getAssociatedFileType());
    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())));
    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()) {
      @Override
      public void addNotify() {
        super.addNotify();
        IdeFocusManager.getGlobalInstance().requestFocus(mySampleText, true);

        new AnAction(){
          @Override
          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());
          }
        });

        final Alarm updater = new Alarm(Alarm.ThreadToUse.SWING_THREAD, myRef.get());
        final DocumentAdapter documentListener = new DocumentAdapter() {
          @Override
          public void documentChanged(DocumentEvent e) {
            updater.cancelAllRequests();
            if (!updater.isDisposed()) {
              updater.addRequest(new Runnable() {
                @Override
                public void run() {
                  updateBalloon();
                }
              }, 200);
            }
          }
        };
        myRegExp.addDocumentListener(documentListener);
        mySampleText.addDocumentListener(documentListener);

        updateBalloon();
        mySampleText.selectAll();
      }
    };
  }


  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;
              }
            }
          }
        }
      }
      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();
    }
  }
}