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;
}
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;
}
Aggregations