WI-60779 highlight as error if DEFINE subpattern contains more than two branches

GitOrigin-RevId: 8af565f6438654c11285be1a24a709e40869fb40
This commit is contained in:
Filippova Maria
2022-08-12 16:05:23 +00:00
committed by intellij-monorepo-bot
parent 2a0ffa17ff
commit 2d28eed303
3 changed files with 20 additions and 2 deletions
@@ -31,6 +31,7 @@ error.dangling.opening.bracket=Unexpected start of quantifier '{'
error.embedded.comments.are.not.supported.in.this.regex.dialect=Embedded comments are not supported in this regex dialect
error.empty.group=Empty group
error.group.reference.is.nested.into.the.named.group.it.refers.to=Group reference is nested into the named group it refers to
error.define.subpattern.contains.more.than.one.branch=DEFINE subpattern contains more than one branch
error.group.reference.not.allowed.inside.lookbehind=Group reference not allowed inside lookbehind
error.group.with.name.0.already.defined=Group with name ''{0}'' already defined
error.illegal.character.range.to.from=Illegal character range (to < from)
@@ -102,6 +102,10 @@ public class RegExpGroupImpl extends RegExpElementImpl implements RegExpGroup {
return node != null && node.findChildByType(RegExpTT.PCRE_CONDITION) != null;
}
public static boolean isPcreDefine(ASTNode node) {
return node != null && node.findChildByType(RegExpTT.PCRE_DEFINE) != null;
}
@Override
public String getGroupName() {
final ASTNode nameNode = getNode().findChildByType(RegExpTT.NAME);
@@ -29,10 +29,10 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.StringEscapesTokenTypes;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import org.intellij.lang.regexp.*;
import org.intellij.lang.regexp.psi.*;
import org.intellij.lang.regexp.psi.impl.RegExpNamedGroupRefImpl;
import org.intellij.lang.regexp.psi.impl.RegExpGroupImpl;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
@@ -232,9 +232,22 @@ public final class RegExpAnnotator extends RegExpElementVisitor implements Annot
@Override
public void visitRegExpGroup(RegExpGroup group) {
if (RegExpGroupImpl.isPcreConditionalGroup(group.getNode())) {
if (RegExpGroupImpl.isPcreDefine(group.getNode())) {
RegExpConditional conditional = ObjectUtils.tryCast(group.getParent(), RegExpConditional.class);
if (conditional != null) {
RegExpBranch[] branches = PsiTreeUtil.getChildrenOfType(conditional, RegExpBranch.class);
if (branches != null && branches.length > 1) {
myHolder.newAnnotation(HighlightSeverity.ERROR,
RegExpBundle.message("error.define.subpattern.contains.more.than.one.branch")).create();
}
}
}
return;
}
final RegExpPattern pattern = group.getPattern();
final RegExpBranch[] branches = pattern.getBranches();
if (!RegExpGroupImpl.isPcreConditionalGroup(group.getNode()) && isEmpty(branches) && group.getNode().getLastChildNode().getElementType() == RegExpTT.GROUP_END) {
if (isEmpty(branches) && group.getNode().getLastChildNode().getElementType() == RegExpTT.GROUP_END) {
// catches "()" as well as "(|)"
myHolder.newAnnotation(HighlightSeverity.WARNING, RegExpBundle.message("error.empty.group")).create();
}