IJ-CR-102460 [java-formatting] IDEA-262126. align Switch Expressions with other styles

GitOrigin-RevId: 5a6e5b975b045ebbb7f7e8bb459f86081e18c47f
This commit is contained in:
Mikhail Pyltsin
2023-02-16 21:33:24 +01:00
committed by intellij-monorepo-bot
parent 87ad691194
commit 80897d6617
6 changed files with 15 additions and 69 deletions

View File

@@ -281,13 +281,6 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
"RPAREN_ON_NEW_LINE_IN_DECONSTRUCTION_PATTERN",
ApplicationBundle.message("wrapping.rpar.on.new.line"),
deconstructionComponentsGroup);
//switch expression
String switchExpressionComponentsGroup = JavaBundle.message("wrapping.switch.expressions");
consumer.showCustomOption(JavaCodeStyleSettings.class,
"DOUBLY_SHIFTED_SWITCH_EXPRESSION_BODY",
JavaBundle.message("wrapping.brace.placement.switch.expression.doubly.shifted"),
switchExpressionComponentsGroup);
}
else if (settingsType == SettingsType.BLANK_LINES_SETTINGS) {
consumer.showAllStandardOptions();

View File

@@ -184,7 +184,6 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im
*/
public boolean SPACE_BEFORE_DECONSTRUCTION_LIST = false;
public boolean DOUBLY_SHIFTED_SWITCH_EXPRESSION_BODY = true;
@WrapConstant
public int MULTI_CATCH_TYPES_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
public boolean ALIGN_TYPES_IN_MULTI_CATCH = true;

View File

@@ -172,7 +172,7 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
@NotNull AlignmentStrategy alignmentStrategy,
int startOffset,
@NotNull FormattingMode formattingMode) {
Indent actualIndent = indent == null ? getDefaultSubtreeIndent(child, settings, javaSettings) : indent;
Indent actualIndent = indent == null ? getDefaultSubtreeIndent(child, settings) : indent;
IElementType elementType = child.getElementType();
Alignment alignment = alignmentStrategy.getAlignment(elementType);
PsiElement childPsi = child.getPsi();
@@ -238,7 +238,7 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
@NotNull CommonCodeStyleSettings settings,
@NotNull JavaCodeStyleSettings javaSettings,
@NotNull FormattingMode formattingMode) {
final Indent indent = getDefaultSubtreeIndent(child, settings, javaSettings);
final Indent indent = getDefaultSubtreeIndent(child, settings);
return newJavaBlock(child, settings, javaSettings, indent, null, AlignmentStrategy.getNullStrategy(), formattingMode);
}
@@ -291,9 +291,7 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
@Nullable
private static Indent getDefaultSubtreeIndent(@NotNull ASTNode child,
@NotNull CommonCodeStyleSettings settings,
@NotNull JavaCodeStyleSettings javaSettings) {
private static Indent getDefaultSubtreeIndent(@NotNull ASTNode child, @NotNull CommonCodeStyleSettings settings) {
CommonCodeStyleSettings.IndentOptions indentOptions= getJavaIndentOptions(settings);
final ASTNode parent = child.getTreeParent();
final IElementType childNodeType = child.getElementType();
@@ -323,8 +321,7 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
return Indent.getNoneIndent();
}
if (parent.getPsi() instanceof PsiSwitchExpression &&
child instanceof PsiCodeBlock &&
!javaSettings.DOUBLY_SHIFTED_SWITCH_EXPRESSION_BODY) {
child instanceof PsiCodeBlock) {
if (settings.BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED ||
settings.BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED2) {
return Indent.getNormalIndent();

View File

@@ -48,11 +48,7 @@ public class CodeBlockBlock extends AbstractJavaBlock {
JavaCodeStyleSettings javaSettings,
@NotNull FormattingMode formattingMode) {
super(node, wrap, getAlignmentStrategy(alignment, node, settings), indent, settings, javaSettings, formattingMode);
if (isSwitchCodeBlock() && !settings.INDENT_CASE_FROM_SWITCH ||
(isLambdaCodeBlock() && settings.LAMBDA_BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED) ||
(isSwitchExpressionCodeBlock() &&
settings.BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED &&
!javaSettings.DOUBLY_SHIFTED_SWITCH_EXPRESSION_BODY)) {
if (codeBlockChildrenWithoutIndent(settings)) {
myChildrenIndent = 0;
}
else {
@@ -60,6 +56,12 @@ public class CodeBlockBlock extends AbstractJavaBlock {
}
}
private boolean codeBlockChildrenWithoutIndent(CommonCodeStyleSettings settings) {
return (isSwitchCodeBlock() && !settings.INDENT_CASE_FROM_SWITCH) ||
(isLambdaCodeBlock() && settings.LAMBDA_BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED) ||
(isSwitchExpressionCodeBlock() && settings.BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED);
}
/**
* There is a possible case that 'implements' section is incomplete (e.g. ends with comma). We may want to align lbrace
* to the comma then.

View File

@@ -88,7 +88,6 @@
"doc_param_description_on_new_line": false,
"doc_preserve_line_breaks": false,
"doc_use_throws_not_exception_tag": true,
"doubly_shifted_switch_expression_body": true,
"else_on_new_line": false,
"enum_constants_wrap": "off",
"extends_keyword_wrap": "off",

View File

@@ -420,50 +420,6 @@ public class JavaCodeBlockBracesPlacementTest extends AbstractJavaFormatterTest
};""";
String nextLine = """
var x = switch (foo)
{
case "bar" ->
{
yield "2000";
}
default -> "n/a";
};""";
String nextLineShifted = """
var x = switch (foo)
{
case "bar" ->
{
yield "2000";
}
default -> "n/a";
};""";
String nextLineShiftedEach = """
var x = switch (foo)
{
case "bar" ->
{
yield "2000";
}
default -> "n/a";
};""";
checkFormatterWithDifferentBraceStyles(before, endOfLine, nextLine, nextLineShifted, nextLineShiftedEach);
getJavaSettings().DOUBLY_SHIFTED_SWITCH_EXPRESSION_BODY = false;
String endOfLine1Shifted = """
var x = switch (foo) {
case "bar" -> {
yield "2000";
}
default -> "n/a";
};""";
String nextLine1Shifted = """
var x = switch (foo)
{
case "bar" ->
@@ -473,7 +429,7 @@ public class JavaCodeBlockBracesPlacementTest extends AbstractJavaFormatterTest
default -> "n/a";
};""";
String nextLineShifted1Shifted = """
String nextLineShifted = """
var x = switch (foo)
{
case "bar" ->
@@ -483,7 +439,7 @@ public class JavaCodeBlockBracesPlacementTest extends AbstractJavaFormatterTest
default -> "n/a";
};""";
String nextLineShiftedEach1Shifted = """
String nextLineShiftedEach = """
var x = switch (foo)
{
case "bar" ->
@@ -493,7 +449,7 @@ public class JavaCodeBlockBracesPlacementTest extends AbstractJavaFormatterTest
default -> "n/a";
};""";
checkFormatterWithDifferentBraceStyles(before, endOfLine1Shifted, nextLine1Shifted, nextLineShifted1Shifted,
nextLineShiftedEach1Shifted);
checkFormatterWithDifferentBraceStyles(before, endOfLine, nextLine, nextLineShifted,
nextLineShiftedEach);
}
}