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

GitOrigin-RevId: ea0e3b45fd12b50c77a5226a04a7baabacf3b93e
This commit is contained in:
Mikhail Pyltsin
2023-02-06 16:07:53 +01:00
committed by intellij-monorepo-bot
parent 4de9fb29a9
commit 6dc90b0e17
7 changed files with 130 additions and 4 deletions

View File

@@ -281,6 +281,13 @@ 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,6 +184,7 @@ 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) : indent;
Indent actualIndent = indent == null ? getDefaultSubtreeIndent(child, settings, javaSettings) : 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);
final Indent indent = getDefaultSubtreeIndent(child, settings, javaSettings);
return newJavaBlock(child, settings, javaSettings, indent, null, AlignmentStrategy.getNullStrategy(), formattingMode);
}
@@ -291,7 +291,9 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
@Nullable
private static Indent getDefaultSubtreeIndent(@NotNull ASTNode child, @NotNull CommonCodeStyleSettings settings) {
private static Indent getDefaultSubtreeIndent(@NotNull ASTNode child,
@NotNull CommonCodeStyleSettings settings,
@NotNull JavaCodeStyleSettings javaSettings) {
CommonCodeStyleSettings.IndentOptions indentOptions= getJavaIndentOptions(settings);
final ASTNode parent = child.getTreeParent();
final IElementType childNodeType = child.getElementType();
@@ -320,6 +322,15 @@ 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) {
if (settings.BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED ||
settings.BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED2) {
return Indent.getNormalIndent();
}
return Indent.getNoneIndent();
}
}
return null;

View File

@@ -49,7 +49,10 @@ public class CodeBlockBlock extends AbstractJavaBlock {
@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) {
(isLambdaCodeBlock() && settings.LAMBDA_BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED) ||
(isSwitchExpressionCodeBlock() &&
settings.BRACE_STYLE == CommonCodeStyleSettings.NEXT_LINE_SHIFTED &&
!javaSettings.DOUBLY_SHIFTED_SWITCH_EXPRESSION_BODY)) {
myChildrenIndent = 0;
}
else {
@@ -97,6 +100,11 @@ public class CodeBlockBlock extends AbstractJavaBlock {
return parent != null && parent.getElementType() == JavaElementType.LAMBDA_EXPRESSION;
}
private boolean isSwitchExpressionCodeBlock() {
ASTNode parent = myNode.getTreeParent();
return parent != null && parent.getElementType() == JavaElementType.SWITCH_EXPRESSION;
}
@Override
protected List<Block> buildChildren() {
final List<Block> result = new ArrayList<>();

View File

@@ -88,6 +88,7 @@
"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

@@ -400,4 +400,100 @@ public class JavaCodeBlockBracesPlacementTest extends AbstractJavaFormatterTest
getSettings().BRACE_STYLE = braceStyle;
doMethodTest(before, after);
}
public void testSwitchExpression() {
String before = """
var x = switch (foo) {
case "bar" ->
{
yield "2000";
}
default -> "n/a";
};""";
String endOfLine = """
var x = switch (foo) {
case "bar" -> {
yield "2000";
}
default -> "n/a";
};""";
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" ->
{
yield "2000";
}
default -> "n/a";
};""";
String nextLineShifted1Shifted = """
var x = switch (foo)
{
case "bar" ->
{
yield "2000";
}
default -> "n/a";
};""";
String nextLineShiftedEach1Shifted = """
var x = switch (foo)
{
case "bar" ->
{
yield "2000";
}
default -> "n/a";
};""";
checkFormatterWithDifferentBraceStyles(before, endOfLine1Shifted, nextLine1Shifted, nextLineShifted1Shifted,
nextLineShiftedEach1Shifted);
}
}

View File

@@ -1289,6 +1289,8 @@ wrapping.annotation.parameters=Annotation parameters
wrapping.record.components=Record components
wrapping.multi.catch.types=Types in multi-catch
wrapping.deconstruction.patterns=Deconstruction patterns
wrapping.switch.expressions=Switch expressions
wrapping.brace.placement.switch.expression.doubly.shifted=Doubly shifted new line
align.types.in.multi.catch=Align types in multi-catch
wrapping.text.blocks=Text blocks
wrong.package.statement=Wrong package statement