java: use the same JavaSmartEnterProcessor on frontend and backend

GitOrigin-RevId: 70fff966ed99e9f0494a988e989284b29c2bda37
This commit is contained in:
Dmitry Batkovich
2025-04-17 13:10:08 +00:00
committed by intellij-monorepo-bot
parent 6af2de0a6a
commit 190697d191
13 changed files with 49 additions and 48 deletions
@@ -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) {
@@ -21,6 +21,8 @@
<orderEntry type="module" module-name="intellij.platform.util.jdom" />
<orderEntry type="module" module-name="intellij.platform.syntax.psi" />
<orderEntry type="module" module-name="intellij.java.psi.impl" />
<orderEntry type="module" module-name="intellij.jsp" />
<orderEntry type="module" module-name="intellij.platform.statistics" />
<orderEntry type="module" module-name="intellij.platform.threadDumpParser" />
</component>
</module>
@@ -46,5 +46,8 @@
<syntaxHighlighter id="java.class" key="CLASS" factoryClass="com.intellij.lang.java.JavaSyntaxHighlighterFactory"/>
<lang.syntaxHighlighterFactory language="JAVA" implementationClass="com.intellij.lang.java.JavaSyntaxHighlighterFactory"/>
<lang.smartEnterProcessor language="JAVA"
implementationClass="com.intellij.codeInsight.editorActions.smartEnter.JavaSmartEnterProcessor"/>
</extensions>
</idea-plugin>
@@ -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<String> USED = GROUP.registerEvent("fixer_used", new StringEventField.ValidatedByAllowedValues(
@@ -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()) {
@@ -1293,8 +1293,6 @@
<codeCompletionConfigurable instance="com.intellij.application.options.JavaCompletionConfigurable" order="first"/>
<iconProvider implementation="com.intellij.psi.impl.JavaDirectoryIconProvider" id="javaDirectory"/>
<codeStyle.ReferenceAdjuster language="JAVA" implementationClass="com.intellij.psi.impl.source.codeStyle.JavaReferenceAdjuster"/>
<lang.smartEnterProcessor language="JAVA"
implementationClass="com.intellij.codeInsight.editorActions.smartEnter.JavaSmartEnterProcessor"/>
<enterHandlerDelegate
id="JavaEnterInInjectedTextBlockHandler"
order="before EnterBetweenBracesHandler, before JavaEnterInTextBlockHandler, first"
@@ -1596,6 +1596,43 @@ public final class PsiUtil extends PsiUtilCore {
return docComment != null && docComment.isMarkdownComment();
}
/**
* Returns true if a given switch block has a rule-based format (like 'case 0 ->')
* @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;
}
//<editor-fold desc="Deprecated stuff">
/**
* @deprecated use {@link #isAvailable(JavaFeature, PsiElement)} instead to check whether a particular feature is available, rather
+1 -1
View File
@@ -7,7 +7,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="intellij.java" />
<orderEntry type="module" module-name="intellij.java.psi" />
<orderEntry type="module" module-name="intellij.platform.util" />
<orderEntry type="module" module-name="intellij.jsp.base" exported="" />
</component>