From a498df52cf7c09931109d2f1c45e8eab811ca265 Mon Sep 17 00:00:00 2001 From: Maxim Mossienko Date: Mon, 29 Sep 2008 22:02:21 +0400 Subject: [PATCH] completing character classes --- RegExpSupport/RegExpSupport.iml | 1 + RegExpSupport/src/META-INF/plugin.xml | 3 + .../regexp/RegExpCompletionContributor.java | 80 +++++++++++++++++++ .../regexp/RegExpDocumentionProvider.java | 55 +++++++++++++ .../intellij/lang/regexp/RegExpLanguage.java | 24 +----- .../test/test/RegExpCompletionTest.java | 57 +++++++++++++ .../completion/BackSlashVariants.regexp | 1 + .../testData/completion/PropertyAlpha.regexp | 1 + .../completion/PropertyAlphaExpected.regexp | 1 + .../completion/PropertyVariants.regexp | 1 + 10 files changed, 201 insertions(+), 23 deletions(-) create mode 100644 RegExpSupport/src/org/intellij/lang/regexp/RegExpCompletionContributor.java create mode 100644 RegExpSupport/src/org/intellij/lang/regexp/RegExpDocumentionProvider.java create mode 100644 RegExpSupport/test/test/RegExpCompletionTest.java create mode 100644 RegExpSupport/testData/completion/BackSlashVariants.regexp create mode 100644 RegExpSupport/testData/completion/PropertyAlpha.regexp create mode 100644 RegExpSupport/testData/completion/PropertyAlphaExpected.regexp create mode 100644 RegExpSupport/testData/completion/PropertyVariants.regexp diff --git a/RegExpSupport/RegExpSupport.iml b/RegExpSupport/RegExpSupport.iml index 1c538b6691c3..670e32acd0aa 100644 --- a/RegExpSupport/RegExpSupport.iml +++ b/RegExpSupport/RegExpSupport.iml @@ -13,6 +13,7 @@ + diff --git a/RegExpSupport/src/META-INF/plugin.xml b/RegExpSupport/src/META-INF/plugin.xml index a3cef7ba9e1b..5203c1acb1c3 100644 --- a/RegExpSupport/src/META-INF/plugin.xml +++ b/RegExpSupport/src/META-INF/plugin.xml @@ -19,5 +19,8 @@ + + + diff --git a/RegExpSupport/src/org/intellij/lang/regexp/RegExpCompletionContributor.java b/RegExpSupport/src/org/intellij/lang/regexp/RegExpCompletionContributor.java new file mode 100644 index 000000000000..c7a9927ed7f7 --- /dev/null +++ b/RegExpSupport/src/org/intellij/lang/regexp/RegExpCompletionContributor.java @@ -0,0 +1,80 @@ +package org.intellij.lang.regexp; + +import com.intellij.codeInsight.TailType; +import com.intellij.codeInsight.completion.*; +import com.intellij.codeInsight.lookup.LookupElementFactory; +import com.intellij.codeInsight.lookup.MutableLookupElement; +import com.intellij.patterns.ElementPattern; +import static com.intellij.patterns.PlatformPatterns.psiElement; +import com.intellij.psi.PsiElement; +import com.intellij.util.ProcessingContext; +import org.intellij.lang.regexp.psi.impl.RegExpPropertyImpl; +import org.jetbrains.annotations.NotNull; + +/** + * Created by IntelliJ IDEA. + * User: vnikolaenko + * Date: 22.09.2008 + * Time: 12:14:14 + * To change this template use File | Settings | File Templates. + */ +public class RegExpCompletionContributor extends CompletionContributor { + public RegExpCompletionContributor() { + final ElementPattern backSlashPattern = psiElement().withText("\\\\"); + extend(CompletionType.BASIC, psiElement().afterLeaf(backSlashPattern), new CharacterClassesNameCompletionProvider()); + + final ElementPattern propertyPattern + = psiElement().withText("p").afterLeaf(backSlashPattern); + extend(CompletionType.BASIC, psiElement().afterLeaf(propertyPattern), new PropertyCompletionProvider()); + + final ElementPattern propertyNamePattern + = psiElement().afterLeaf(psiElement().withText("{").afterLeaf(propertyPattern)); + extend(CompletionType.BASIC, propertyNamePattern, new PropertyNameCompletionProvider()); + } + + private static MutableLookupElement addLookupElement(final CompletionResultSet result, + final String name) { + MutableLookupElement element = LookupElementFactory.getInstance().createLookupElement(name); + result.addElement(element); + return element; + } + + private static class PropertyNameCompletionProvider extends CompletionProvider { + + public void addCompletions(@NotNull final CompletionParameters parameters, + final ProcessingContext context, + @NotNull final CompletionResultSet result) { + for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) { + addLookupElement(result, stringArray[0]).setTailType(TailType.createSimpleTailType('}')); + } + } + } + + private static class PropertyCompletionProvider extends CompletionProvider { + + public void addCompletions(@NotNull final CompletionParameters parameters, + final ProcessingContext context, + @NotNull final CompletionResultSet result) { + for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) { + addLookupElement(result, "{" + stringArray[0] + "}"); + } + } + } + private static class CharacterClassesNameCompletionProvider extends CompletionProvider { + + public void addCompletions(@NotNull final CompletionParameters parameters, + final ProcessingContext context, + @NotNull final CompletionResultSet result) { + String[] completions = { + "d", "D", "s", "S", "w", "W", "b", "B", "A", "G", "Z", "z", "Q", "E", + "t", "n", "r", "f", "a", "e" + }; + for (String s : completions) { + addLookupElement(result, s); + } + for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) { + addLookupElement(result, "p{" + stringArray[0] + "}"); + } + } + } +} diff --git a/RegExpSupport/src/org/intellij/lang/regexp/RegExpDocumentionProvider.java b/RegExpSupport/src/org/intellij/lang/regexp/RegExpDocumentionProvider.java new file mode 100644 index 000000000000..6fe4d1fce720 --- /dev/null +++ b/RegExpSupport/src/org/intellij/lang/regexp/RegExpDocumentionProvider.java @@ -0,0 +1,55 @@ +package org.intellij.lang.regexp; + +import com.intellij.lang.documentation.QuickDocumentationProvider; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiManager; +import org.intellij.lang.regexp.psi.impl.RegExpPropertyImpl; +import org.intellij.lang.regexp.psi.impl.RegExpElementImpl; +import org.intellij.lang.regexp.psi.RegExpGroup; +import org.jetbrains.annotations.Nullable; + +/** + * Created by IntelliJ IDEA. + * User: vnikolaenko + * Date: 17.09.2008 + * Time: 19:24:29 + * To change this template use File | Settings | File Templates. + */ +public class RegExpDocumentionProvider extends QuickDocumentationProvider { + @Nullable + public String getUrlFor(PsiElement element, PsiElement originalElement) { + return null; + } + + @Nullable + public String generateDoc(PsiElement element, PsiElement originalElement) { + if (element instanceof RegExpPropertyImpl) { + String elementName = ((RegExpPropertyImpl)element).getCategoryNode().getText(); + for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) { + if (stringArray[0].equals(elementName)) { + return "Property block stands for " + stringArray[1]; + } + } + } + return null; + } + + @Nullable + public PsiElement getDocumentationElementForLookupItem(PsiManager psiManager, Object object, PsiElement element){ + return null; + } + + @Nullable + public PsiElement getDocumentationElementForLink(PsiManager psiManager, String link, PsiElement context) { + return null; + } + + @Nullable + public String getQuickNavigateInfo(PsiElement element) { + if (element instanceof RegExpGroup) { + return "Capturing Group: " + ((RegExpElementImpl)element).getUnescapedText(); + } else { + return null; + } + } +} diff --git a/RegExpSupport/src/org/intellij/lang/regexp/RegExpLanguage.java b/RegExpSupport/src/org/intellij/lang/regexp/RegExpLanguage.java index c62146bb36d4..b61e75ee7b52 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/RegExpLanguage.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/RegExpLanguage.java @@ -15,23 +15,12 @@ */ package org.intellij.lang.regexp; -import com.intellij.lang.BracePair; -import com.intellij.lang.Language; -import com.intellij.lang.LanguageAnnotators; -import com.intellij.lang.LanguageBraceMatching; -import com.intellij.lang.LanguageDocumentation; -import com.intellij.lang.LanguageParserDefinitions; -import com.intellij.lang.LanguageSurrounders; -import com.intellij.lang.PairedBraceMatcher; -import com.intellij.lang.documentation.QuickDocumentationProvider; +import com.intellij.lang.*; import com.intellij.openapi.fileTypes.SingleLazyInstanceSyntaxHighlighterFactory; import com.intellij.openapi.fileTypes.SyntaxHighlighter; import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory; -import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.tree.IElementType; -import org.intellij.lang.regexp.psi.RegExpGroup; -import org.intellij.lang.regexp.psi.impl.RegExpElementImpl; import org.intellij.lang.regexp.surroundWith.SimpleSurroundDescriptor; import org.intellij.lang.regexp.validation.RegExpAnnotator; import org.jetbrains.annotations.NotNull; @@ -54,17 +43,6 @@ public class RegExpLanguage extends Language { return new RegExpHighlighter(null, parserDefinition); } }); - - LanguageDocumentation.INSTANCE.addExplicitExtension(this, new QuickDocumentationProvider() { - @Nullable - public String getQuickNavigateInfo(PsiElement element) { - if (element instanceof RegExpGroup) { - return "Capturing Group: " + ((RegExpElementImpl)element).getUnescapedText(); - } else { - return null; - } - } - }); } @NotNull diff --git a/RegExpSupport/test/test/RegExpCompletionTest.java b/RegExpSupport/test/test/RegExpCompletionTest.java new file mode 100644 index 000000000000..6cf24eeb6e04 --- /dev/null +++ b/RegExpSupport/test/test/RegExpCompletionTest.java @@ -0,0 +1,57 @@ +package test; + +import com.intellij.testFramework.fixtures.CodeInsightFixtureTestCase; +import org.intellij.lang.regexp.psi.impl.RegExpPropertyImpl; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Created by IntelliJ IDEA. + * User: vnikolaenko + * Date: 25.09.2008 + * Time: 15:10:10 + */ +public class RegExpCompletionTest extends CodeInsightFixtureTestCase { + + // util methods + private String getInputDataFileName(String testName) { + return Character.toUpperCase(testName.charAt(0)) + testName.substring(1) + ".regexp"; + } + + private String getExpectedResultFileName(String testName) { + return Character.toUpperCase(testName.charAt(0)) + testName.substring(1) + "Expected" + ".regexp"; + } + + public void testBackSlashVariants() throws Throwable { + java.util.List nameList = new ArrayList(Arrays.asList("d", "D", "s", "S", "w", "W", "b", "B", "A", "G", "Z", "z", "Q", "E", + "t", "n", "r", "f", "a", "e")); + for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) { + nameList.add("p{" + stringArray[0] + "}"); + } + myFixture.testCompletionVariants(getInputDataFileName(getTestName(true)), nameList.toArray(new String[nameList.size()])); + } + + public void testPropertyVariants() throws Throwable { + java.util.List nameList = new ArrayList(); + for (String[] stringArray : RegExpPropertyImpl.PROPERTY_NAMES) { + nameList.add("{" + stringArray[0] + "}"); + } + myFixture.testCompletionVariants(getInputDataFileName(getTestName(true)), nameList.toArray(new String[nameList.size()])); + } + + public void testPropertyAlpha() throws Throwable { + doTest(); + } + + public void doTest() throws Throwable { + String inputDataFileName = getInputDataFileName(getTestName(true)); + String expectedResultFileName = getExpectedResultFileName(getTestName(true)); + myFixture.testCompletion(inputDataFileName, expectedResultFileName); + } + + @Override + protected String getBasePath() { + return "/svnPlugins/RegExpSupport/testData/completion"; + } +} diff --git a/RegExpSupport/testData/completion/BackSlashVariants.regexp b/RegExpSupport/testData/completion/BackSlashVariants.regexp new file mode 100644 index 000000000000..144e4789b632 --- /dev/null +++ b/RegExpSupport/testData/completion/BackSlashVariants.regexp @@ -0,0 +1 @@ +[0-9]\\ diff --git a/RegExpSupport/testData/completion/PropertyAlpha.regexp b/RegExpSupport/testData/completion/PropertyAlpha.regexp new file mode 100644 index 000000000000..96509136743d --- /dev/null +++ b/RegExpSupport/testData/completion/PropertyAlpha.regexp @@ -0,0 +1 @@ +\\p{Alp} diff --git a/RegExpSupport/testData/completion/PropertyAlphaExpected.regexp b/RegExpSupport/testData/completion/PropertyAlphaExpected.regexp new file mode 100644 index 000000000000..03696e617f4c --- /dev/null +++ b/RegExpSupport/testData/completion/PropertyAlphaExpected.regexp @@ -0,0 +1 @@ +\\p{Alpha} diff --git a/RegExpSupport/testData/completion/PropertyVariants.regexp b/RegExpSupport/testData/completion/PropertyVariants.regexp new file mode 100644 index 000000000000..ee205bcc806d --- /dev/null +++ b/RegExpSupport/testData/completion/PropertyVariants.regexp @@ -0,0 +1 @@ +[0-9].*\\p