Search in sources :

Example 1 with RegExpElement

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

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

the class SimpleSurroundDescriptor method getElementsToSurround.

@NotNull
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
    // adjust start/end
    PsiElement element1 = file.findElementAt(startOffset);
    PsiElement element2 = file.findElementAt(endOffset - 1);
    if (element1 instanceof PsiWhiteSpace) {
        startOffset = element1.getTextRange().getEndOffset();
    }
    if (element2 instanceof PsiWhiteSpace) {
        endOffset = element2.getTextRange().getStartOffset();
    }
    RegExpElement pattern = findElementAtStrict(file, startOffset, endOffset, RegExpPattern.class);
    if (pattern != null)
        return new RegExpElement[] { pattern };
    RegExpElement branch = findElementAtStrict(file, startOffset, endOffset, RegExpBranch.class);
    if (branch != null)
        return new RegExpElement[] { branch };
    List<PsiElement> atoms = new ArrayList<>();
    RegExpAtom atom = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, RegExpAtom.class);
    for (; atom != null; atom = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, RegExpAtom.class)) {
        atoms.add(atom);
        startOffset = atom.getTextRange().getEndOffset();
        // handle embedded whitespace
        if ((element1 = file.findElementAt(startOffset)) instanceof PsiWhiteSpace) {
            startOffset = element1.getTextRange().getEndOffset();
            atoms.add(element1);
        }
    }
    if (startOffset == endOffset && atoms.size() > 0) {
        PsiElement[] elements = PsiUtilCore.toPsiElementArray(atoms);
        if ((atoms.size() == 1 || PsiTreeUtil.findCommonParent(elements) == elements[0].getParent())) {
            return elements;
        }
    }
    return PsiElement.EMPTY_ARRAY;
}
Also used : RegExpElement(org.intellij.lang.regexp.psi.RegExpElement) RegExpAtom(org.intellij.lang.regexp.psi.RegExpAtom) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 RegExpElement (org.intellij.lang.regexp.psi.RegExpElement)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)1 PsiElementFilter (com.intellij.psi.util.PsiElementFilter)1 ArrayList (java.util.ArrayList)1 RegExpAtom (org.intellij.lang.regexp.psi.RegExpAtom)1 RegExpGroup (org.intellij.lang.regexp.psi.RegExpGroup)1 NotNull (org.jetbrains.annotations.NotNull)1