From 190697d19130e8d174373c79417868dbc4ce8fe1 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Wed, 16 Apr 2025 11:18:09 +0200 Subject: [PATCH] java: use the same JavaSmartEnterProcessor on frontend and backend GitOrigin-RevId: 70fff966ed99e9f0494a988e989284b29c2bda37 --- .../com/siyeh/ig/psiutils/SwitchUtils.java | 28 +------------- .../intellij.java.frontback.impl.iml | 2 + .../resource/intellij.java.frontback.impl.xml | 3 ++ .../smartEnter/JavaSmartEnterProcessor.java | 3 +- .../smartEnter/JavadocFixer.java | 0 .../smartEnter/MethodCallFixer.java | 0 .../smartEnter/MissingCommaFixer.java | 0 .../smartEnter/SemicolonFixer.java | 0 .../smartEnter/SwitchLabelColonFixer.java | 20 ++-------- .../com/intellij/javadoc/JavadocHelper.java | 0 .../resources/META-INF/JavaPlugin.xml | 2 - .../src/com/intellij/psi/util/PsiUtil.java | 37 +++++++++++++++++++ java/jsp-openapi/intellij.jsp.iml | 2 +- 13 files changed, 49 insertions(+), 48 deletions(-) rename java/{java-impl => java-frontback-impl}/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java (96%) rename java/{java-impl => java-frontback-impl}/src/com/intellij/codeInsight/editorActions/smartEnter/JavadocFixer.java (100%) rename java/{java-impl => java-frontback-impl}/src/com/intellij/codeInsight/editorActions/smartEnter/MethodCallFixer.java (100%) rename java/{java-impl => java-frontback-impl}/src/com/intellij/codeInsight/editorActions/smartEnter/MissingCommaFixer.java (100%) rename java/{java-impl => java-frontback-impl}/src/com/intellij/codeInsight/editorActions/smartEnter/SemicolonFixer.java (100%) rename java/{java-impl => java-frontback-impl}/src/com/intellij/codeInsight/editorActions/smartEnter/SwitchLabelColonFixer.java (64%) rename java/{java-impl => java-frontback-impl}/src/com/intellij/javadoc/JavadocHelper.java (100%) diff --git a/java/java-analysis-impl/src/com/siyeh/ig/psiutils/SwitchUtils.java b/java/java-analysis-impl/src/com/siyeh/ig/psiutils/SwitchUtils.java index fa91076b2aeb..469de5513c7a 100644 --- a/java/java-analysis-impl/src/com/siyeh/ig/psiutils/SwitchUtils.java +++ b/java/java-analysis-impl/src/com/siyeh/ig/psiutils/SwitchUtils.java @@ -325,33 +325,7 @@ public final class SwitchUtils { */ @Contract(pure = true) public static boolean isRuleFormatSwitch(@NotNull PsiSwitchBlock block) { - if (!PsiUtil.isAvailable(JavaFeature.ENHANCED_SWITCH, block)) { - return false; - } - - final PsiCodeBlock switchBody = block.getBody(); - if (switchBody != null) { - for (var child = switchBody.getFirstChild(); child != null; child = child.getNextSibling()) { - if (child instanceof PsiSwitchLabelStatementBase && !isBeingCompleted((PsiSwitchLabelStatementBase)child)) { - return child instanceof PsiSwitchLabeledRuleStatement; - } - } - } - - return true; - } - - /** - * Checks if the label is being completed and there are no other case label elements in the list of the case label's elements - * @param label the label to analyze - * @return true if the label is currently being completed - */ - @Contract(pure = true) - private static boolean isBeingCompleted(@NotNull PsiSwitchLabelStatementBase label) { - if (!(label.getLastChild() instanceof PsiErrorElement)) return false; - - final PsiCaseLabelElementList list = label.getCaseLabelElementList(); - return list != null && list.getElements().length == 1; + return PsiUtil.isRuleFormatSwitch(block); } public static boolean canBeSwitchSelectorExpression(PsiExpression expression, LanguageLevel languageLevel) { diff --git a/java/java-frontback-impl/intellij.java.frontback.impl.iml b/java/java-frontback-impl/intellij.java.frontback.impl.iml index b2b6a1f062b7..b145b3433bd9 100644 --- a/java/java-frontback-impl/intellij.java.frontback.impl.iml +++ b/java/java-frontback-impl/intellij.java.frontback.impl.iml @@ -21,6 +21,8 @@ + + \ No newline at end of file diff --git a/java/java-frontback-impl/resource/intellij.java.frontback.impl.xml b/java/java-frontback-impl/resource/intellij.java.frontback.impl.xml index b44289606d15..c8c24e804db0 100644 --- a/java/java-frontback-impl/resource/intellij.java.frontback.impl.xml +++ b/java/java-frontback-impl/resource/intellij.java.frontback.impl.xml @@ -46,5 +46,8 @@ + + diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java similarity index 96% rename from java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java rename to java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java index e054465dec4d..2aa8ecc90849 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java +++ b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavaSmartEnterProcessor.java @@ -1,4 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.editorActions.smartEnter; import com.intellij.internal.statistic.eventLog.EventLogGroup; @@ -94,6 +94,7 @@ public final class JavaSmartEnterProcessor extends AbstractBasicJavaSmartEnterPr return AbstractBasicJavaSmartEnterProcessor.isModified(editor); } + // looks like it might be called on both FE and BE sides. be careful with 2x numbers. private static final class FixerUsageCollector extends CounterUsagesCollector { private static final EventLogGroup GROUP = new EventLogGroup("java.smart.enter.fixer", 3); private static final EventId1 USED = GROUP.registerEvent("fixer_used", new StringEventField.ValidatedByAllowedValues( diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavadocFixer.java b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavadocFixer.java similarity index 100% rename from java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavadocFixer.java rename to java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/JavadocFixer.java diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MethodCallFixer.java b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MethodCallFixer.java similarity index 100% rename from java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MethodCallFixer.java rename to java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MethodCallFixer.java diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingCommaFixer.java b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingCommaFixer.java similarity index 100% rename from java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingCommaFixer.java rename to java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/MissingCommaFixer.java diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SemicolonFixer.java b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SemicolonFixer.java similarity index 100% rename from java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SemicolonFixer.java rename to java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SemicolonFixer.java diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SwitchLabelColonFixer.java b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SwitchLabelColonFixer.java similarity index 64% rename from java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SwitchLabelColonFixer.java rename to java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SwitchLabelColonFixer.java index 2561af6eb8da..3a2a565e6ec1 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SwitchLabelColonFixer.java +++ b/java/java-frontback-impl/src/com/intellij/codeInsight/editorActions/smartEnter/SwitchLabelColonFixer.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.editorActions.smartEnter; import com.intellij.lang.ASTNode; @@ -22,8 +8,8 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiSwitchBlock; import com.intellij.psi.PsiSwitchLabelStatement; import com.intellij.psi.impl.source.BasicJavaAstTreeUtil; +import com.intellij.psi.util.PsiUtil; import com.intellij.util.IncorrectOperationException; -import com.siyeh.ig.psiutils.SwitchUtils; import org.jetbrains.annotations.NotNull; public class SwitchLabelColonFixer implements Fixer { @@ -33,7 +19,7 @@ public class SwitchLabelColonFixer implements Fixer { if (psiElement instanceof PsiSwitchLabelStatement statement) { PsiSwitchBlock block = statement.getEnclosingSwitchBlock(); if (block == null) return; - String token = SwitchUtils.isRuleFormatSwitch(block) ? "->" : ":"; + String token = PsiUtil.isRuleFormatSwitch(block) ? "->" : ":"; if (!psiElement.getText().endsWith(token)) { PsiCaseLabelElementList labelElementList = statement.getCaseLabelElementList(); if ((labelElementList != null && labelElementList.getElementCount() != 0) || statement.isDefaultCase()) { diff --git a/java/java-impl/src/com/intellij/javadoc/JavadocHelper.java b/java/java-frontback-impl/src/com/intellij/javadoc/JavadocHelper.java similarity index 100% rename from java/java-impl/src/com/intellij/javadoc/JavadocHelper.java rename to java/java-frontback-impl/src/com/intellij/javadoc/JavadocHelper.java diff --git a/java/java-impl/resources/META-INF/JavaPlugin.xml b/java/java-impl/resources/META-INF/JavaPlugin.xml index a951954f9138..5001170c14a8 100644 --- a/java/java-impl/resources/META-INF/JavaPlugin.xml +++ b/java/java-impl/resources/META-INF/JavaPlugin.xml @@ -1293,8 +1293,6 @@ - ') + * @param block a switch block to test + * @return true if a given switch block has a rule-based format; false if it has a conventional label-based format (like 'case 0:') + * If the switch body has no labels yet and language level permits, the rule-based format is assumed. + */ + @Contract(pure = true) + public static boolean isRuleFormatSwitch(@NotNull PsiSwitchBlock block) { + if (!isAvailable(JavaFeature.ENHANCED_SWITCH, block)) { + return false; + } + + final PsiCodeBlock switchBody = block.getBody(); + if (switchBody != null) { + for (PsiElement child = switchBody.getFirstChild(); child != null; child = child.getNextSibling()) { + if (child instanceof PsiSwitchLabelStatementBase && !isBeingCompleted((PsiSwitchLabelStatementBase)child)) { + return child instanceof PsiSwitchLabeledRuleStatement; + } + } + } + + return true; + } + + /** + * Checks if the label is being completed and there are no other case label elements in the list of the case label's elements + * @param label the label to analyze + * @return true if the label is currently being completed + */ + @Contract(pure = true) + private static boolean isBeingCompleted(@NotNull PsiSwitchLabelStatementBase label) { + if (!(label.getLastChild() instanceof PsiErrorElement)) return false; + + final PsiCaseLabelElementList list = label.getCaseLabelElementList(); + return list != null && list.getElements().length == 1; + } + // /** * @deprecated use {@link #isAvailable(JavaFeature, PsiElement)} instead to check whether a particular feature is available, rather diff --git a/java/jsp-openapi/intellij.jsp.iml b/java/jsp-openapi/intellij.jsp.iml index 815cb95d1f33..d3b8c49ce1b3 100644 --- a/java/jsp-openapi/intellij.jsp.iml +++ b/java/jsp-openapi/intellij.jsp.iml @@ -7,7 +7,7 @@ - +