Search in sources :

Example 1 with RegExpGroup

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

the class RegExpBackrefImpl method resolve.

public RegExpGroup resolve() {
    final int index = getIndex();
    final PsiElementProcessor.FindFilteredElement<RegExpElement> processor = new PsiElementProcessor.FindFilteredElement<>(new PsiElementFilter() {

        int groupCount;

        public boolean isAccepted(PsiElement element) {
            if (element instanceof RegExpGroup) {
                if (((RegExpGroup) element).isCapturing() && ++groupCount == index) {
                    return true;
                }
            }
            return element == RegExpBackrefImpl.this;
        }
    });
    PsiTreeUtil.processElements(getContainingFile(), processor);
    if (processor.getFoundElement() instanceof RegExpGroup) {
        return (RegExpGroup) processor.getFoundElement();
    }
    return null;
}
Also used : RegExpElement(org.intellij.lang.regexp.psi.RegExpElement) RegExpGroup(org.intellij.lang.regexp.psi.RegExpGroup) PsiElement(com.intellij.psi.PsiElement) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiElementFilter(com.intellij.psi.util.PsiElementFilter)

Example 2 with RegExpGroup

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

the class RegExpNamedGroupRefImpl method resolve.

@Nullable
public RegExpGroup resolve() {
    final PsiElementProcessor.FindFilteredElement<RegExpGroup> processor = new PsiElementProcessor.FindFilteredElement<>(new PsiElementFilter() {

        public boolean isAccepted(PsiElement element) {
            if (!(element instanceof RegExpGroup)) {
                return false;
            }
            final RegExpGroup group = (RegExpGroup) element;
            return group.isAnyNamedGroup() && Comparing.equal(getGroupName(), group.getGroupName());
        }
    });
    PsiTreeUtil.processElements(getContainingFile(), processor);
    return processor.getFoundElement();
}
Also used : RegExpGroup(org.intellij.lang.regexp.psi.RegExpGroup) PsiElement(com.intellij.psi.PsiElement) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiElementFilter(com.intellij.psi.util.PsiElementFilter) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RegExpGroup

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

the class RegExpNamedGroupRefImpl method getReference.

@Override
public PsiReference getReference() {
    return new PsiReference() {

        public PsiElement getElement() {
            return RegExpNamedGroupRefImpl.this;
        }

        @Override
        public TextRange getRangeInElement() {
            final ASTNode groupNode = getNode().findChildByType(GROUP_REF_TOKENS);
            assert groupNode != null;
            return new TextRange(groupNode.getTextLength(), getTextLength() - 1);
        }

        public PsiElement resolve() {
            return RegExpNamedGroupRefImpl.this.resolve();
        }

        @NotNull
        public String getCanonicalText() {
            return getRangeInElement().substring(getText());
        }

        public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
            throw new UnsupportedOperationException();
        }

        public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
            throw new UnsupportedOperationException();
        }

        public boolean isReferenceTo(PsiElement element) {
            return resolve() == element;
        }

        @Override
        @NotNull
        public Object[] getVariants() {
            final PsiElementProcessor.CollectFilteredElements<RegExpGroup> processor = new PsiElementProcessor.CollectFilteredElements<>(new PsiElementFilter() {

                @Override
                public boolean isAccepted(PsiElement element) {
                    if (!(element instanceof RegExpGroup)) {
                        return false;
                    }
                    final RegExpGroup regExpGroup = (RegExpGroup) element;
                    return regExpGroup.isAnyNamedGroup();
                }
            });
            PsiTreeUtil.processElements(getContainingFile(), processor);
            return processor.toArray();
        }

        public boolean isSoft() {
            return false;
        }
    };
}
Also used : PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) RegExpGroup(org.intellij.lang.regexp.psi.RegExpGroup) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiElementFilter(com.intellij.psi.util.PsiElementFilter) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement)

Example 4 with RegExpGroup

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

PsiElement (com.intellij.psi.PsiElement)4 RegExpGroup (org.intellij.lang.regexp.psi.RegExpGroup)4 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)3 PsiElementFilter (com.intellij.psi.util.PsiElementFilter)3 ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiReference (com.intellij.psi.PsiReference)1 RegExpFile (org.intellij.lang.regexp.RegExpFile)1 RegExpBranch (org.intellij.lang.regexp.psi.RegExpBranch)1 RegExpElement (org.intellij.lang.regexp.psi.RegExpElement)1 RegExpPattern (org.intellij.lang.regexp.psi.RegExpPattern)1 RegExpRecursiveElementVisitor (org.intellij.lang.regexp.psi.RegExpRecursiveElementVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1