[Java. Code Formatting] IDEA-223507 Write tests for formatting conditional expressions

GitOrigin-RevId: b5ec80926db80796a899ccded49c38acb45c6dac
This commit is contained in:
Georgii Ustinov
2025-11-06 10:53:18 +00:00
committed by intellij-monorepo-bot
parent 92bc9cbbb3
commit 9e3542c650
36 changed files with 547 additions and 16 deletions
@@ -337,8 +337,8 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
}
return Indent.getNoneIndent();
}
final ASTNode grandParent = skipParenthesesUp(parent.getTreeParent());
if (grandParent != null && grandParent.getElementType() == JavaElementType.CONDITIONAL_EXPRESSION) {
if (JavaFormatterConditionalExpressionUtil.isInsideConditionalExpression(parent)) {
return Indent.getSpaceIndent(0, true);
}
}
@@ -838,19 +838,10 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
return
new LegacyChainedMethodCallsBlockBuilder(alignment, blockWrap, indent, mySettings, myJavaSettings, myFormattingMode).build(nodes);
}
return new ChainMethodCallsBlockBuilder(alignment, blockWrap, indent, mySettings, myJavaSettings, myFormattingMode, shouldUseSpaceIndentInCallChain(node)).build(nodes);
return new ChainMethodCallsBlockBuilder(alignment, blockWrap, indent, mySettings, myJavaSettings,
myFormattingMode, JavaFormatterConditionalExpressionUtil.isInsideConditionalExpression(node)).build(nodes);
}
private static boolean shouldUseSpaceIndentInCallChain(@NotNull ASTNode node) {
ASTNode parent = skipParenthesesUp(node);
if (parent == null) return false;
while (parent != null && parent.getElementType() == JavaElementType.REFERENCE_EXPRESSION) {
parent = parent.getTreeParent();
if (parent == null || parent.getElementType() != JavaElementType.METHOD_CALL_EXPRESSION) return false;
parent = skipParenthesesUp(parent);
}
return parent != null && parent.getElementType() == JavaElementType.CONDITIONAL_EXPRESSION;
}
private boolean shouldAlignChild(final @NotNull ASTNode child) {
int role = getChildRole(child);
@@ -0,0 +1,26 @@
// 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.psi.formatter.java
import com.intellij.lang.ASTNode
import com.intellij.psi.impl.source.BasicJavaAstTreeUtil
import com.intellij.psi.impl.source.tree.ChildRole
import com.intellij.psi.impl.source.tree.CompositeElement
import com.intellij.psi.impl.source.tree.JavaElementType
import com.intellij.psi.tree.ParentAwareTokenSet
internal object JavaFormatterConditionalExpressionUtil {
private val STOP_TOKENS = ParentAwareTokenSet.create(JavaElementType.METHOD, JavaElementType.LOCAL_VARIABLE, JavaElementType.METHOD)
/**
* Checks if the given AST node is inside a conditional expression then or else branch.
*/
@JvmStatic
fun isInsideConditionalExpression(node : ASTNode) : Boolean {
val child = BasicJavaAstTreeUtil.getAncestorWithParentOfType(node, JavaElementType.CONDITIONAL_EXPRESSION, STOP_TOKENS)
if (child == null) return false
val parent = child.treeParent
if (parent !is CompositeElement) return false
val childRole = parent.getChildRole(child)
return childRole != ChildRole.CONDITION
}
}