Search in sources :

Example 6 with RegExpPattern

use of org.intellij.lang.regexp.psi.RegExpPattern 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

RegExpPattern (org.intellij.lang.regexp.psi.RegExpPattern)6 PsiFile (com.intellij.psi.PsiFile)4 PsiFileFactory (com.intellij.psi.PsiFileFactory)4 RegExpBranch (org.intellij.lang.regexp.psi.RegExpBranch)3 ASTNode (com.intellij.lang.ASTNode)2 PsiElement (com.intellij.psi.PsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 HashSet (java.util.HashSet)1 RegExpFile (org.intellij.lang.regexp.RegExpFile)1 RegExpAtom (org.intellij.lang.regexp.psi.RegExpAtom)1 RegExpClosure (org.intellij.lang.regexp.psi.RegExpClosure)1 RegExpGroup (org.intellij.lang.regexp.psi.RegExpGroup)1 RegExpRecursiveElementVisitor (org.intellij.lang.regexp.psi.RegExpRecursiveElementVisitor)1 RegExpElementImpl (org.intellij.lang.regexp.psi.impl.RegExpElementImpl)1