Search in sources :

Example 1 with RegExpBranch

use of org.intellij.lang.regexp.psi.RegExpBranch in project intellij-community by JetBrains.

the class RegExpFactory method createBranchFromText.

@NotNull
public static RegExpBranch createBranchFromText(@NotNull CharSequence text, @NotNull PsiElement context) {
    final RegExpPattern pattern = createPatternFromText(text, context);
    final RegExpBranch branch = PsiTreeUtil.getChildOfType(pattern, RegExpBranch.class);
    assert branch != null;
    return branch;
}
Also used : RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) RegExpBranch(org.intellij.lang.regexp.psi.RegExpBranch) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with RegExpBranch

use of org.intellij.lang.regexp.psi.RegExpBranch in project intellij-community by JetBrains.

the class RegExpUtil method getEnumValues.

@Nullable
public static Set<String> getEnumValues(Project project, @NotNull String regExp) {
    final PsiFileFactory factory = PsiFileFactory.getInstance(project);
    final PsiFile file = factory.createFileFromText("dummy.regexp", RegExpFileType.INSTANCE, regExp);
    final RegExpPattern pattern = (RegExpPattern) file.getFirstChild();
    if (pattern == null) {
        return null;
    }
    final RegExpBranch[] branches = pattern.getBranches();
    final Set<String> values = new HashSet<>();
    for (RegExpBranch branch : branches) {
        if (analyzeBranch(branch)) {
            values.add(branch.getUnescapedText());
        }
    }
    return values;
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) RegExpBranch(org.intellij.lang.regexp.psi.RegExpBranch) PsiFile(com.intellij.psi.PsiFile) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RegExpBranch

use of org.intellij.lang.regexp.psi.RegExpBranch in project intellij-community by JetBrains.

the class ValueRegExpAnnotator method annotate.

public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder holder) {
    if (psiElement instanceof RegExpFile && psiElement.getCopyableUserData(KEY) == Boolean.TRUE) {
        final PsiElement pattern = psiElement.getFirstChild();
        if (!(pattern instanceof RegExpPattern)) {
            return;
        }
        final RegExpBranch[] branches = ((RegExpPattern) pattern).getBranches();
        if (branches.length == 1 && branches[0].getAtoms().length == 0) {
            return;
        }
        for (RegExpBranch branch : branches) {
            final int[] count = new int[1];
            branch.accept(new RegExpRecursiveElementVisitor() {

                @Override
                public void visitRegExpGroup(RegExpGroup group) {
                    if (group.isCapturing()) {
                        count[0]++;
                    }
                    super.visitRegExpGroup(group);
                }
            });
            if (count[0] != 1) {
                holder.createWarningAnnotation(branch, "The pattern should contain exactly one capturing group");
            }
        }
    }
}
Also used : RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) RegExpBranch(org.intellij.lang.regexp.psi.RegExpBranch) RegExpFile(org.intellij.lang.regexp.RegExpFile) RegExpRecursiveElementVisitor(org.intellij.lang.regexp.psi.RegExpRecursiveElementVisitor) RegExpGroup(org.intellij.lang.regexp.psi.RegExpGroup) PsiElement(com.intellij.psi.PsiElement)

Aggregations

RegExpBranch (org.intellij.lang.regexp.psi.RegExpBranch)3 RegExpPattern (org.intellij.lang.regexp.psi.RegExpPattern)3 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiFileFactory (com.intellij.psi.PsiFileFactory)1 HashSet (java.util.HashSet)1 RegExpFile (org.intellij.lang.regexp.RegExpFile)1 RegExpGroup (org.intellij.lang.regexp.psi.RegExpGroup)1 RegExpRecursiveElementVisitor (org.intellij.lang.regexp.psi.RegExpRecursiveElementVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1