From 1d6afa5823b6d2c7451ee75cfac38201f8c312dc Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 11 Dec 2025 17:49:09 +0100 Subject: [PATCH] IJPL-223228 [regexp]: new "Flip alternation branch" intention GitOrigin-RevId: 4eeaae35f52716e3b8fe1f7cadfc5262b2e5f217 --- RegExpSupport/resources/intellij.regexp.xml | 7 +++ .../after.regexp.template | 1 + .../before.regexp.template | 1 + .../FlipAlternationIntention/description.html | 5 ++ .../messages/RegExpBundle.properties | 2 + .../intention/FlipAlternationIntention.java | 51 +++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/after.regexp.template create mode 100644 RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/before.regexp.template create mode 100644 RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/description.html create mode 100644 RegExpSupport/src/org/intellij/lang/regexp/intention/FlipAlternationIntention.java diff --git a/RegExpSupport/resources/intellij.regexp.xml b/RegExpSupport/resources/intellij.regexp.xml index 1b2554939e2e..4b67c31d4cd6 100644 --- a/RegExpSupport/resources/intellij.regexp.xml +++ b/RegExpSupport/resources/intellij.regexp.xml @@ -35,10 +35,17 @@ + RegExp org.intellij.lang.regexp.intention.CheckRegExpIntentionAction messages.RegExpBundle inspection.group.name.regexp + + RegExp + org.intellij.lang.regexp.intention.FlipAlternationIntention + messages.RegExpBundle + inspection.group.name.regexp + diff --git a/RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/after.regexp.template b/RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/after.regexp.template new file mode 100644 index 000000000000..de603311fd0d --- /dev/null +++ b/RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/after.regexp.template @@ -0,0 +1 @@ +(one|two|three) \ No newline at end of file diff --git a/RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/before.regexp.template b/RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/before.regexp.template new file mode 100644 index 000000000000..7138320f63ea --- /dev/null +++ b/RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/before.regexp.template @@ -0,0 +1 @@ +(two|one|three) \ No newline at end of file diff --git a/RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/description.html b/RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/description.html new file mode 100644 index 000000000000..8dd559bdd96f --- /dev/null +++ b/RegExpSupport/resources/intentionDescriptions/FlipAlternationIntention/description.html @@ -0,0 +1,5 @@ + + +Swaps the branches of an alternation. + + \ No newline at end of file diff --git a/RegExpSupport/resources/messages/RegExpBundle.properties b/RegExpSupport/resources/messages/RegExpBundle.properties index 5028e422468c..ff072e44cf9a 100644 --- a/RegExpSupport/resources/messages/RegExpBundle.properties +++ b/RegExpSupport/resources/messages/RegExpBundle.properties @@ -130,6 +130,8 @@ RegExp inspections highlight code snippets matching the specified regular expres Add Custom RegExp Inspection\u2026 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: diff --git a/RegExpSupport/src/org/intellij/lang/regexp/intention/FlipAlternationIntention.java b/RegExpSupport/src/org/intellij/lang/regexp/intention/FlipAlternationIntention.java new file mode 100644 index 000000000000..24b112de641b --- /dev/null +++ b/RegExpSupport/src/org/intellij/lang/regexp/intention/FlipAlternationIntention.java @@ -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 { + + 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); + } +} \ No newline at end of file