IJPL-223228 [regexp]: new "Flip alternation branch" intention

GitOrigin-RevId: 4eeaae35f52716e3b8fe1f7cadfc5262b2e5f217
This commit is contained in:
Bas Leijdekkers
2025-12-11 19:15:07 +00:00
committed by intellij-monorepo-bot
parent 54868aa3d7
commit 1d6afa5823
6 changed files with 67 additions and 0 deletions
@@ -35,10 +35,17 @@
<inspectionTreeAdvertiser implementation="org.intellij.lang.regexp.inspection.custom.RegExpAdvertiser" />
<intentionAction>
<language>RegExp</language>
<className>org.intellij.lang.regexp.intention.CheckRegExpIntentionAction</className>
<bundleName>messages.RegExpBundle</bundleName>
<categoryKey>inspection.group.name.regexp</categoryKey>
</intentionAction>
<intentionAction>
<language>RegExp</language>
<className>org.intellij.lang.regexp.intention.FlipAlternationIntention</className>
<bundleName>messages.RegExpBundle</bundleName>
<categoryKey>inspection.group.name.regexp</categoryKey>
</intentionAction>
<inspection.basicVisitor class="org.intellij.lang.regexp.psi.RegExpElementVisitor"/>
@@ -0,0 +1 @@
(two<spot>|</spot>one|three)
@@ -0,0 +1,5 @@
<html>
<body>
Swaps the branches of an alternation.
</body>
</html>
@@ -130,6 +130,8 @@ RegExp inspections highlight code snippets matching the specified regular expres
<a href="action://regexp.profile.action.provider.add.group">Add Custom RegExp Inspection\u2026</a>
intention.family.name.replace=Replace
intention.name.check.regexp=Check RegExp
intention.family.name.flip.alternation=Flip alternation branches
intention.name.flip.alternation=Flip '|'
label.any=Any
label.regexp.patterns=RegExp patterns:
label.regexp=&RegExp:
@@ -0,0 +1,51 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.intellij.lang.regexp.intention;
import com.intellij.modcommand.ActionContext;
import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.Presentation;
import com.intellij.modcommand.PsiUpdateModCommandAction;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import org.intellij.lang.regexp.RegExpBundle;
import org.intellij.lang.regexp.RegExpTT;
import org.intellij.lang.regexp.psi.RegExpPattern;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
final class FlipAlternationIntention extends PsiUpdateModCommandAction<PsiElement> {
FlipAlternationIntention() {
super(PsiElement.class);
}
@Override
public @NotNull String getFamilyName() {
return RegExpBundle.message("intention.family.name.flip.alternation");
}
@Override
protected @NotNull Presentation getPresentation(@NotNull ActionContext context, @NotNull PsiElement element) {
return Presentation.of(RegExpBundle.message("intention.name.flip.alternation"));
}
@Override
protected boolean isElementApplicable(@NotNull PsiElement element, @NotNull ActionContext context) {
return element instanceof LeafPsiElement leaf
&& leaf.getElementType() == RegExpTT.UNION
&& element.getParent() instanceof RegExpPattern;
}
@Override
protected void invoke(@NotNull ActionContext context, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) {
final PsiElement left = element.getPrevSibling();
final PsiElement right = element.getNextSibling();
if (left == null || right == null) return;
final PsiElement copyLeft = left.copy();
final PsiElement copyRight = right.copy();
left.replace(copyRight);
right.replace(copyLeft);
}
}