regexp: add test for named character completion
authorBas Leijdekkers <basleijdekkers@gmail.com>
Tue, 30 Aug 2016 15:54:33 +0000 (17:54 +0200)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Tue, 30 Aug 2016 15:56:00 +0000 (17:56 +0200)
RegExpSupport/src/org/intellij/lang/regexp/RegExpCompletionContributor.java
RegExpSupport/src/org/intellij/lang/regexp/UnicodeCharacterNames.java
RegExpSupport/test/org/intellij/lang/regexp/RegExpCompletionTest.java

index dc81e12c3030b7d569163a5bb0d89fc07de77233..97425853729825bebe61aa7051c7d0d972cd07e9 100644 (file)
@@ -97,6 +97,12 @@ public final class RegExpCompletionContributor extends CompletionContributor {
       final ElementPattern<PsiElement> propertyNamePattern
               = psiElement().afterLeaf(psiElement().withText("{").afterLeaf(propertyPattern));
       extend(CompletionType.BASIC, propertyNamePattern, new PropertyNameCompletionProvider());
+
+      final PsiElementPattern.Capture<PsiElement> namedCharacterPattern = psiElement().withText("N");
+      extend(CompletionType.BASIC, psiElement().afterLeaf(namedCharacterPattern),
+             new NamedCharacterCompletionProvider(true));
+      extend(CompletionType.BASIC, psiElement().afterLeaf(psiElement(RegExpTT.LBRACE).afterLeaf(namedCharacterPattern)),
+             new NamedCharacterCompletionProvider(false));
     }
   }
 
index 57a465111d169593c5a5ec3e5f0b34006322a769..a84105ec40df0d2f2236c9506cb8e0122fde88b9 100644 (file)
@@ -33,14 +33,16 @@ public class UnicodeCharacterNames {
     try {
       final Class<?> aClass = Class.forName("java.lang.CharacterName");
       final Method initNamePool = ReflectionUtil.getDeclaredMethod(aClass, "initNamePool");
-      final int[][] lookup2d = ReflectionUtil.getField(aClass, null, int[][].class, "lookup");
-      if (initNamePool != null && lookup2d != null) { // jdk 8
-        byte[] namePool = (byte[])initNamePool.invoke(null);
+      if (initNamePool != null) { // jdk 8
+        byte[] namePool = (byte[])initNamePool.invoke(null); // initializes "lookup" field
+        final int[][] lookup2d = ReflectionUtil.getStaticFieldValue(aClass, int[][].class, "lookup");
+        if (lookup2d == null) {
+          return;
+        }
         for (int[] indexes : lookup2d) {
           if (indexes != null) {
             for (int index : indexes) {
               if (index != 0) {
-                ;
                 final String name = new String(namePool, index >>> 8, index & 0xff, AsciiUtil.ASCII_CHARSET);
                 consumer.accept(name);
               }
index 3ba88ab02baae4ec78638ebaf44c6b436dd7a882..abdee64fbc053f4f82356e644e0889f5f7d3a004 100644 (file)
  */
 package org.intellij.lang.regexp;
 
+import com.intellij.codeInsight.lookup.LookupElement;
 import com.intellij.openapi.application.PathManager;
 import com.intellij.testFramework.fixtures.CodeInsightFixtureTestCase;
 import com.intellij.util.ArrayUtil;
+import com.intellij.util.containers.ContainerUtil;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -57,6 +59,17 @@ public class RegExpCompletionTest extends CodeInsightFixtureTestCase {
       myFixture.checkResult("[[:^alpha:]<caret>");
     }
 
+    public void testNamedCharacter() {
+      myFixture.configureByText(RegExpFileType.INSTANCE, "\\\\N{SMILE<caret>}");
+      final LookupElement[] elements = myFixture.completeBasic();
+      final List<String> strings = ContainerUtil.map(elements, LookupElement::getLookupString);
+      assertEquals(Arrays.asList("SMILE", "SMILING FACE WITH SMILING EYES", "SMILING FACE WITH HEART-SHAPED EYES",
+                                 "SMILING CAT FACE WITH HEART-SHAPED EYES", "SMILING FACE WITH OPEN MOUTH AND SMILING EYES",
+                                 "SMILING FACE WITH OPEN MOUTH AND TIGHTLY-CLOSED EYES", "CAT FACE WITH WRY SMILE",
+                                 "GRINNING CAT FACE WITH SMILING EYES", "GRINNING FACE WITH SMILING EYES",
+                                 "KISSING FACE WITH SMILING EYES"), strings);
+    }
+
     public void testBackSlashVariants() throws Throwable {
         List<String> nameList =
           new ArrayList<>(Arrays.asList("d", "D", "s", "S", "w", "W", "b", "B", "A", "G", "Z", "z", "Q", "E",