completing character classes
[idea/community.git] / RegExpSupport / src / org / intellij / lang / regexp / RegExpCompletionContributor.java
1 package org.intellij.lang.regexp;
2
3 import com.intellij.codeInsight.TailType;
4 import com.intellij.codeInsight.completion.*;
5 import com.intellij.codeInsight.lookup.LookupElementFactory;
6 import com.intellij.codeInsight.lookup.MutableLookupElement;
7 import com.intellij.patterns.ElementPattern;
8 import static com.intellij.patterns.PlatformPatterns.psiElement;
9 import com.intellij.psi.PsiElement;
10 import com.intellij.util.ProcessingContext;
11 import org.intellij.lang.regexp.psi.impl.RegExpPropertyImpl;
12 import org.jetbrains.annotations.NotNull;
13
14 /**
15  * Created by IntelliJ IDEA.
16  * User: vnikolaenko
17  * Date: 22.09.2008
18  * Time: 12:14:14
19  * To change this template use File | Settings | File Templates.
20  */
21 public class RegExpCompletionContributor extends CompletionContributor {
22     public RegExpCompletionContributor() {
23         final ElementPattern<PsiElement> backSlashPattern = psiElement().withText("\\\\");
24         extend(CompletionType.BASIC, psiElement().afterLeaf(backSlashPattern), new CharacterClassesNameCompletionProvider());
25
26         final ElementPattern<PsiElement> propertyPattern
27                 = psiElement().withText("p").afterLeaf(backSlashPattern);
28         extend(CompletionType.BASIC, psiElement().afterLeaf(propertyPattern), new PropertyCompletionProvider());
29
30         final ElementPattern<PsiElement> propertyNamePattern
31                 = psiElement().afterLeaf(psiElement().withText("{").afterLeaf(propertyPattern));
32         extend(CompletionType.BASIC, propertyNamePattern, new PropertyNameCompletionProvider());
33     }
34
35     private static MutableLookupElement<String> addLookupElement(final CompletionResultSet result,
36                                                                  final String name) {
37         MutableLookupElement<String> element = LookupElementFactory.getInstance().createLookupElement(name);
38         result.addElement(element);
39         return element;
40     }
41
42     private static class PropertyNameCompletionProvider extends CompletionProvider<CompletionParameters> {
43
44         public void addCompletions(@NotNull final CompletionParameters parameters,
45                                    final ProcessingContext context,
46                                    @NotNull final CompletionResultSet result) {
47               for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) {
48                   addLookupElement(result, stringArray[0]).setTailType(TailType.createSimpleTailType('}'));
49               }
50         }
51     }
52
53     private static class PropertyCompletionProvider extends CompletionProvider<CompletionParameters> {
54
55         public void addCompletions(@NotNull final CompletionParameters parameters,
56                                    final ProcessingContext context,
57                                    @NotNull final CompletionResultSet result) {
58             for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) {
59                 addLookupElement(result, "{" + stringArray[0] + "}");
60             }
61         }
62     }
63     private static class CharacterClassesNameCompletionProvider extends CompletionProvider<CompletionParameters> {
64
65         public void addCompletions(@NotNull final CompletionParameters parameters,
66                                    final ProcessingContext context,
67                                    @NotNull final CompletionResultSet result) {
68           String[] completions = {
69                 "d", "D", "s", "S", "w", "W", "b", "B", "A", "G", "Z", "z", "Q", "E",
70                 "t", "n", "r", "f", "a", "e"
71             };
72             for (String s : completions) {
73                 addLookupElement(result, s);
74             }
75             for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) {
76                 addLookupElement(result, "p{" + stringArray[0] + "}");
77             }
78         }
79     }
80 }