[Java. Code Formatting] IDEA-223507 Use zero indent binded to parent in literals and call methods inside conditional expressions

GitOrigin-RevId: 5c0f6ba1c896e335ef0cccee6eef00b0165aa650
This commit is contained in:
Georgii Ustinov
2025-11-06 10:53:18 +00:00
committed by intellij-monorepo-bot
parent 291f24fc94
commit 92bc9cbbb3
3 changed files with 44 additions and 12 deletions
@@ -337,11 +337,23 @@ 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) {
return Indent.getSpaceIndent(0, true);
}
}
return null;
}
private static @Nullable ASTNode skipParenthesesUp(@NotNull ASTNode node) {
ASTNode currNode = node.getTreeParent();
while (currNode != null && currNode.getElementType() == JavaElementType.PARENTH_EXPRESSION) {
currNode = currNode.getTreeParent();
}
return currNode;
}
private static @Nullable ASTNode skipCommentsAndWhitespacesBackwards(@NotNull ASTNode node) {
ASTNode currNode = node.getTreePrev();
while (currNode != null &&
@@ -826,7 +838,18 @@ 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).build(nodes);
return new ChainMethodCallsBlockBuilder(alignment, blockWrap, indent, mySettings, myJavaSettings, myFormattingMode, shouldUseSpaceIndentInCallChain(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) {
@@ -30,12 +30,19 @@ public class CallChunkBlockBuilder {
public CallChunkBlockBuilder(@NotNull CommonCodeStyleSettings settings, @NotNull JavaCodeStyleSettings javaSettings,
@NotNull FormattingMode formattingMode) {
this(settings, javaSettings, formattingMode, false);
}
public CallChunkBlockBuilder(@NotNull CommonCodeStyleSettings settings, @NotNull JavaCodeStyleSettings javaSettings,
@NotNull FormattingMode formattingMode,
boolean enforceUseSpaceIndent) {
mySettings = settings;
myIndentSettings = settings.getIndentOptions();
myJavaSettings = javaSettings;
myFormattingMode = formattingMode;
myUseRelativeIndents = myIndentSettings != null && myIndentSettings.USE_RELATIVE_INDENTS;
mySmartIndent = Indent.getSmartIndent(Indent.Type.CONTINUATION, myUseRelativeIndents);
mySmartIndent = enforceUseSpaceIndent ? Indent.getSmartIndent(Indent.Type.SPACES, true)
: Indent.getSmartIndent(Indent.Type.CONTINUATION, myUseRelativeIndents);
}
public @NotNull Block create(final @NotNull List<? extends ASTNode> subNodes,
@@ -22,25 +22,26 @@ import java.util.List;
import static com.intellij.psi.formatter.java.JavaFormatterUtil.getWrapType;
class ChainMethodCallsBlockBuilder {
private final CommonCodeStyleSettings mySettings;
private final @NotNull CommonCodeStyleSettings mySettings;
private final CommonCodeStyleSettings.IndentOptions myIndentSettings;
private final JavaCodeStyleSettings myJavaSettings;
private final @NotNull JavaCodeStyleSettings myJavaSettings;
private final Wrap myBlockWrap;
private final Alignment myBlockAlignment;
private final Indent myBlockIndent;
private final FormattingMode myFormattingMode;
private final @NotNull FormattingMode myFormattingMode;
private static final int MANY_METHOD_CALLS_FACTOR = 3;
private final boolean myEnforceSpaceIndent;
ChainMethodCallsBlockBuilder(Alignment alignment,
Wrap wrap,
Indent indent,
CommonCodeStyleSettings settings,
JavaCodeStyleSettings javaSettings,
@NotNull FormattingMode formattingMode)
{
Wrap wrap,
Indent indent,
@NotNull CommonCodeStyleSettings settings,
@NotNull JavaCodeStyleSettings javaSettings,
@NotNull FormattingMode formattingMode,
boolean enforceSpaceIndent) {
myBlockWrap = wrap;
myBlockAlignment = alignment;
myBlockIndent = indent;
@@ -48,6 +49,7 @@ class ChainMethodCallsBlockBuilder {
myIndentSettings = settings.getIndentOptions();
myJavaSettings = javaSettings;
myFormattingMode = formattingMode;
myEnforceSpaceIndent = enforceSpaceIndent;
}
public Block build(List<? extends ASTNode> nodes) {
@@ -68,7 +70,7 @@ class ChainMethodCallsBlockBuilder {
int commonIndentSize = mySettings.KEEP_BUILDER_METHODS_INDENTS ? getCommonIndentSize(methodCall) : -1;
CallChunkBlockBuilder builder = new CallChunkBlockBuilder(mySettings, myJavaSettings, myFormattingMode);
CallChunkBlockBuilder builder = new CallChunkBlockBuilder(mySettings, myJavaSettings, myFormattingMode, myEnforceSpaceIndent);
for (int i = 0; i < methodCall.size(); i++) {
ChainedCallChunk currentCallChunk = methodCall.get(i);
if (isMethodCall(currentCallChunk) && !isBuilderMethod(currentCallChunk, mySettings) || isComment(currentCallChunk)) {