RegExp: avoid invalid group references (EA-101239)

This commit is contained in:
Bas Leijdekkers
2017-05-14 00:31:42 +02:00
parent 6d2d8b7440
commit 29ee694a66
@@ -39,9 +39,6 @@ public class RegExpNamedGroupRefImpl extends RegExpElementImpl implements RegExp
private static final TokenSet RUBY_GROUP_REF_TOKENS =
TokenSet.create(RegExpTT.RUBY_NAMED_GROUP_REF, RegExpTT.RUBY_QUOTED_NAMED_GROUP_REF,
RegExpTT.RUBY_NAMED_GROUP_CALL, RegExpTT.RUBY_QUOTED_NAMED_GROUP_CALL);
private static final TokenSet GROUP_REF_TOKENS =
TokenSet.create(RegExpTT.PYTHON_NAMED_GROUP_REF, RegExpTT.RUBY_NAMED_GROUP_REF, RegExpTT.RUBY_QUOTED_NAMED_GROUP_REF,
RegExpTT.RUBY_NAMED_GROUP_CALL, RegExpTT.RUBY_QUOTED_NAMED_GROUP_CALL);
public RegExpNamedGroupRefImpl(ASTNode node) {
super(node);
@@ -52,10 +49,12 @@ public class RegExpNamedGroupRefImpl extends RegExpElementImpl implements RegExp
visitor.visitRegExpNamedGroupRef(this);
}
@Override
@Nullable
public RegExpGroup resolve() {
final PsiElementProcessor.FindFilteredElement<RegExpGroup> processor = new PsiElementProcessor.FindFilteredElement<>(
new PsiElementFilter() {
@Override
public boolean isAccepted(PsiElement element) {
if (!(element instanceof RegExpGroup)) {
return false;
@@ -69,6 +68,7 @@ public class RegExpNamedGroupRefImpl extends RegExpElementImpl implements RegExp
return processor.getFoundElement();
}
@Override
@Nullable
public String getGroupName() {
final ASTNode nameNode = getNode().findChildByType(RegExpTT.NAME);
@@ -93,35 +93,47 @@ public class RegExpNamedGroupRefImpl extends RegExpElementImpl implements RegExp
@Override
public PsiReference getReference() {
if (getNode().findChildByType(RegExpTT.NAME) == null) {
return null;
}
return new PsiReference() {
@Override
public PsiElement getElement() {
return RegExpNamedGroupRefImpl.this;
}
@Override
public TextRange getRangeInElement() {
final ASTNode groupNode = getNode().findChildByType(GROUP_REF_TOKENS);
final ASTNode groupNode = getNode().getFirstChildNode();
assert groupNode != null;
return new TextRange(groupNode.getTextLength(), getTextLength() - 1);
final ASTNode nameNode = getNode().findChildByType(RegExpTT.NAME);
assert nameNode != null;
final int startOffset = groupNode.getTextLength();
return new TextRange(startOffset, startOffset + nameNode.getTextLength());
}
@Override
public PsiElement resolve() {
return RegExpNamedGroupRefImpl.this.resolve();
}
@Override
@NotNull
public String getCanonicalText() {
return getRangeInElement().substring(getText());
}
@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
throw new UnsupportedOperationException();
}
@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
throw new UnsupportedOperationException();
}
@Override
public boolean isReferenceTo(PsiElement element) {
return resolve() == element;
}
@@ -145,6 +157,7 @@ public class RegExpNamedGroupRefImpl extends RegExpElementImpl implements RegExp
return processor.toArray();
}
@Override
public boolean isSoft() {
return false;
}