mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
[java] DeleteSwitchLabelFix must produce 'default:' instead of 'case default:'
IDEA-309550 GitOrigin-RevId: 3224934e68d9b0f9008fe7305f0209d3487a7f65
This commit is contained in:
committed by
intellij-monorepo-bot
parent
0c06fbfcdb
commit
468b4d470a
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInspection.dataFlow.fix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.SwitchBlockHighlightingModel;
|
||||
@@ -16,6 +16,7 @@ import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ControlFlowUtils;
|
||||
import com.siyeh.ig.psiutils.SwitchUtils;
|
||||
import one.util.streamex.StreamEx;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -77,7 +78,7 @@ public class DeleteSwitchLabelFix extends LocalQuickFixAndIntentionActionOnPsiEl
|
||||
if (label == null) return;
|
||||
PsiSwitchBlock block = label.getEnclosingSwitchBlock();
|
||||
if (block == null) return;
|
||||
deleteLabelElement(labelElement);
|
||||
deleteLabelElement(project, labelElement);
|
||||
if (myAddDefaultIfNecessary) {
|
||||
IntentionAction addDefaultFix = SwitchBlockHighlightingModel.createAddDefaultFixIfNecessary(block);
|
||||
if (addDefaultFix != null) {
|
||||
@@ -86,15 +87,27 @@ public class DeleteSwitchLabelFix extends LocalQuickFixAndIntentionActionOnPsiEl
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteLabelElement(@NotNull PsiCaseLabelElement labelElement) {
|
||||
public static void deleteLabelElement(@NotNull Project project, @NotNull PsiCaseLabelElement labelElement) {
|
||||
PsiSwitchLabelStatementBase label = PsiImplUtil.getSwitchLabel(labelElement);
|
||||
if (label == null) return;
|
||||
PsiCaseLabelElementList labelElementList = label.getCaseLabelElementList();
|
||||
if (labelElementList != null && labelElementList.getElementCount() == 1) {
|
||||
deleteLabel(label);
|
||||
} else {
|
||||
new CommentTracker().deleteAndRestoreComments(labelElement);
|
||||
if (labelElementList != null) {
|
||||
if (labelElementList.getElementCount() == 1) {
|
||||
deleteLabel(label);
|
||||
return;
|
||||
}
|
||||
else if (labelElementList.getElementCount() == 2) {
|
||||
PsiElement defaultElement = SwitchUtils.findDefaultElement(label);
|
||||
if (defaultElement != null && defaultElement != labelElement) {
|
||||
new CommentTracker().deleteAndRestoreComments(labelElementList);
|
||||
PsiElementFactory factory = PsiElementFactory.getInstance(project);
|
||||
PsiKeyword defaultKeyword = factory.createKeyword(PsiKeyword.DEFAULT);
|
||||
label.getFirstChild().replace(defaultKeyword);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
new CommentTracker().deleteAndRestoreComments(labelElement);
|
||||
}
|
||||
|
||||
public static void deleteLabel(PsiSwitchLabelStatementBase label) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Remove switch label 'null'" "true-preview"
|
||||
class X {
|
||||
void test(String s) {
|
||||
switch (s) {
|
||||
case null -> System.out.println("null");
|
||||
default -> System.out.println("null or default");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Remove switch label 'null'" "true-preview"
|
||||
class X {
|
||||
void test(String s) {
|
||||
switch (s) {
|
||||
case null -> System.out.println("null");
|
||||
case null<caret>, default -> System.out.println("null or default");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,6 @@ import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ProblemHighlightType;
|
||||
import com.intellij.codeInspection.dataFlow.fix.DeleteSwitchLabelFix;
|
||||
import com.intellij.codeInspection.options.OptPane;
|
||||
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.controlFlow.*;
|
||||
@@ -37,7 +36,6 @@ import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@@ -45,7 +43,8 @@ import java.util.Set;
|
||||
import static com.intellij.codeInsight.daemon.impl.analysis.SwitchBlockHighlightingModel.PatternsInSwitchBlockHighlightingModel.CompletenessResult.COMPLETE_WITHOUT_TOTAL;
|
||||
import static com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
|
||||
import static com.intellij.codeInspection.ProblemHighlightType.INFORMATION;
|
||||
import static com.intellij.codeInspection.options.OptPane.*;
|
||||
import static com.intellij.codeInspection.options.OptPane.checkbox;
|
||||
import static com.intellij.codeInspection.options.OptPane.pane;
|
||||
|
||||
public class UnnecessaryDefaultInspection extends BaseInspection {
|
||||
|
||||
@@ -81,11 +80,11 @@ public class UnnecessaryDefaultInspection extends BaseInspection {
|
||||
@Override
|
||||
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
final PsiElement element = descriptor.getPsiElement().getParent();
|
||||
if (element instanceof PsiSwitchLabelStatementBase) {
|
||||
DeleteSwitchLabelFix.deleteLabel((PsiSwitchLabelStatementBase)element);
|
||||
if (element instanceof PsiSwitchLabelStatementBase label) {
|
||||
DeleteSwitchLabelFix.deleteLabel(label);
|
||||
}
|
||||
else if (element instanceof PsiDefaultCaseLabelElement) {
|
||||
DeleteSwitchLabelFix.deleteLabelElement((PsiDefaultCaseLabelElement)element);
|
||||
else if (element instanceof PsiDefaultCaseLabelElement defaultElement) {
|
||||
DeleteSwitchLabelFix.deleteLabelElement(project, defaultElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user