RegExp: remove unnecessary escaping when replacing alternation with character class

This commit is contained in:
Bas Leijdekkers
2017-02-09 14:39:46 +01:00
parent d578783f8f
commit 2f473ff2ee
2 changed files with 43 additions and 1 deletions
@@ -22,6 +22,8 @@ import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.tree.IElementType;
import org.intellij.lang.regexp.RegExpTT;
import org.intellij.lang.regexp.psi.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -58,6 +60,7 @@ public class SingleCharAlternationInspection extends LocalInspectionTool {
}
}
final String text = buildReplacementText(pattern);
//noinspection DialogTitleCapitalization
myHolder.registerProblem(pattern, "Single character alternation in RegExp", new SingleCharAlternationFix(text));
}
@@ -100,7 +103,42 @@ public class SingleCharAlternationInspection extends LocalInspectionTool {
static String buildReplacementText(RegExpPattern pattern) {
final StringBuilder text = new StringBuilder("[");
for (RegExpBranch branch : pattern.getBranches()) {
text.append(branch.getText());
for (PsiElement child : branch.getChildren()) {
final RegExpChar ch = (RegExpChar)child;
final IElementType type = ch.getNode().getFirstChildNode().getElementType();
if (type == RegExpTT.REDUNDANT_ESCAPE) {
text.append((char)ch.getValue());
}
else if (type == RegExpTT.ESC_CHARACTER) {
final int value = ch.getValue();
switch (value) {
case '.':
case '$':
case '?':
case '*':
case '+':
case '|':
case '{':
case '(':
case ')':
text.append((char)value);
break;
case '^':
if (text.length() == 1) {
text.append(ch.getText());
}
else {
text.append((char)value);
}
break;
default:
text.append(ch.getText());
}
}
else {
text.append(ch.getText());
}
}
}
text.append("]");
return text.toString();
@@ -35,6 +35,10 @@ public class SingleCharAlternationInspectionTest extends RegExpInspectionTestCas
quickfixTest("<warning descr=\"Single character alternation in RegExp\">x|y|z</warning>", "[xyz]", "Replace with '[xyz]'");
}
public void testRemoveEscaping() {
quickfixTest("<warning descr=\"Single character alternation in RegExp\">\\^|<weak_warning descr=\"Redundant character escape\">\\å</weak_warning>|\\{|\\\\|\\[</warning>", "[\\^å{\\\\\\[]", "Replace with '[\\^å{\\\\\\[]'");
}
@NotNull
@Override
protected LocalInspectionTool getInspection() {