IJPL-235313 [regexp] explain simple class more compactly

GitOrigin-RevId: fe925ef6750285cf31ffcd73164f256d6f84e3ba
This commit is contained in:
Bas Leijdekkers
2026-05-27 19:44:25 +00:00
committed by intellij-monorepo-bot
parent f6dc03c9c7
commit e17a22e7ba
2 changed files with 24 additions and 6 deletions
@@ -561,13 +561,14 @@ class ExplanationVisitor extends RegExpRecursiveElementVisitor {
}
}
else {
String set = elements.length == 1 && elements[0] instanceof RegExpCharRange range ? buildRangeText(range) : "in the set";
if (regExpClass.isNegated()) {
branch(regExpClass, new NameNode("Negated Character Class", "https://www.regular-expressions.info/charclass.html#negated"),
"matches 1 character not in the set");
branch(regExpClass, new NameNode("Negated Character Class", "https://www.regular-expressions.info/charclass.html#negated"),
"matches 1 character not " + set);
}
else {
branch(regExpClass, new NameNode("Character Class", "https://www.regular-expressions.info/charclass.html"),
"matches 1 character in the set");
branch(regExpClass, new NameNode("Character Class", "https://www.regular-expressions.info/charclass.html"),
"matches 1 character " + set);
}
}
super.visitRegExpClass(regExpClass);
@@ -584,10 +585,15 @@ class ExplanationVisitor extends RegExpRecursiveElementVisitor {
@Override
public void visitRegExpCharRange(RegExpCharRange range) {
if (range.getParent() instanceof RegExpClass aClass && aClass.getElements().length == 1) return;
leaf(range, new NameNode("Range", "https://www.regular-expressions.info/charclass.html"),
"matches 1 character " + buildRangeText(range));
}
private static @Nls @NotNull String buildRangeText(RegExpCharRange range) {
RegExpChar from = range.getFrom();
RegExpChar to = range.getTo();
leaf(range, new NameNode("Range", "https://www.regular-expressions.info/charclass.html"),
"matches 1 character from " + charText(from) + " to " + charText(to) + " (" + (to.getValue() - from.getValue() + 1) + " characters)");
return "from " + charText(from) + " to " + charText(to) + " (" + (to.getValue() - from.getValue() + 1) + " characters)";
}
@Override
@@ -82,6 +82,18 @@ public final class ExplainRegExpIntentionTest extends BasePlatformTestCase {
\\) – matches the RIGHT PARENTHESIS character
""");
}
public void testExactlyNTimes() {
doTest("[0-9]{3}-[0-9]{4}",
"""
[0-9]{3}-[0-9]{4} – matches elements in order
[0-9]{3} Quantifier (https://www.regular-expressions.info/repeat.html) – matches exactly 3 times
[0-9] Character Class (https://www.regular-expressions.info/charclass.html) – matches 1 character from DIGIT ZERO to DIGIT NINE (10 characters)
- – matches the HYPHEN-MINUS character
[0-9]{4} Quantifier (https://www.regular-expressions.info/repeat.html) – matches exactly 4 times
[0-9] Character Class (https://www.regular-expressions.info/charclass.html) – matches 1 character from DIGIT ZERO to DIGIT NINE (10 characters)
""");
}
public void testComment() {
doTest("(?x) implausible# inconceivable",