mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-382503 [java-formatter] Remove java.formatter.chained.calls.pre212.compatibility
GitOrigin-RevId: 99cf272d7071700fff5f216c7584a75c78976db0
This commit is contained in:
committed by
intellij-monorepo-bot
parent
4c6eef049c
commit
9c8a982242
@@ -75,10 +75,6 @@
|
||||
<statementUpDownMover implementation="com.intellij.codeInsight.editorActions.moveUpDown.JavaCatchBlockMover" id="catchBlock"
|
||||
order="before statement"/>
|
||||
|
||||
<registryKey key="java.formatter.chained.calls.pre212.compatibility"
|
||||
defaultValue="false"
|
||||
description="Format chained calls as in versions prior to 2021.2"/>
|
||||
|
||||
<completion.frontendFriendlyInsertHandler handler="com.intellij.codeInsight.completion.method.DiamondInsertHandler"/>
|
||||
<completion.frontendFriendlyInsertHandler handler="com.intellij.codeInsight.completion.method.RefStartInsertHandler"/>
|
||||
<completion.frontendFriendlyInsertHandler handler="com.intellij.codeInsight.completion.method.FrontendFriendlyParenthesesInsertHandler"/>
|
||||
|
||||
@@ -6,7 +6,6 @@ import com.intellij.formatting.alignment.AlignmentStrategy;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
@@ -835,10 +834,6 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
|
||||
if (nodes.isEmpty()) {
|
||||
return new LeafBlock(node, blockWrap, alignment, indent);
|
||||
}
|
||||
if (Registry.is(LegacyChainedMethodCallsBlockBuilder.COMPATIBILITY_KEY)) {
|
||||
return
|
||||
new LegacyChainedMethodCallsBlockBuilder(alignment, blockWrap, indent, mySettings, myJavaSettings, myFormattingMode).build(nodes);
|
||||
}
|
||||
return new ChainMethodCallsBlockBuilder(alignment, blockWrap, indent, mySettings, myJavaSettings,
|
||||
myFormattingMode, JavaFormatterConditionalExpressionUtil.isInsideConditionalExpression(node)).build(nodes);
|
||||
}
|
||||
|
||||
-3
@@ -11,7 +11,6 @@ import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
@@ -2049,8 +2048,6 @@ public final class JavaSpacePropertyProcessor extends JavaElementVisitor {
|
||||
private boolean isAllowedToMoveSemicolonInLongCallChain() {
|
||||
if (!myJavaSettings.WRAP_SEMICOLON_AFTER_CALL_CHAIN) return false;
|
||||
|
||||
if (Registry.is(LegacyChainedMethodCallsBlockBuilder.COMPATIBILITY_KEY)) return false;
|
||||
|
||||
if(!(myChild1 instanceof PsiMethodCallExpression) || myType1 != JavaElementType.METHOD_CALL_EXPRESSION) return false;
|
||||
|
||||
if (isInsidePsiForStatement()) return false;
|
||||
|
||||
-62
@@ -1,62 +0,0 @@
|
||||
// Copyright 2000-2024 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.formatting.*;
|
||||
import com.intellij.formatting.alignment.AlignmentStrategy;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.JavaTokenType;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.psi.formatter.java.AbstractJavaBlock.newJavaBlock;
|
||||
|
||||
/**
|
||||
* The previous version of {@link CallChunkBlockBuilder} for compatibility with releases prior to 2021.2,
|
||||
* see <a href="https://youtrack.jetbrains.com/issue/IDEA-274778">IDEA-274778</a>.
|
||||
*/
|
||||
public class LegacyCallChunkBlockBuilder {
|
||||
|
||||
private final CommonCodeStyleSettings mySettings;
|
||||
private final CommonCodeStyleSettings.IndentOptions myIndentSettings;
|
||||
private final JavaCodeStyleSettings myJavaSettings;
|
||||
private final FormattingMode myFormattingMode;
|
||||
|
||||
public LegacyCallChunkBlockBuilder(@NotNull CommonCodeStyleSettings settings, @NotNull JavaCodeStyleSettings javaSettings,
|
||||
@NotNull FormattingMode formattingMode) {
|
||||
mySettings = settings;
|
||||
myIndentSettings = settings.getIndentOptions();
|
||||
myJavaSettings = javaSettings;
|
||||
myFormattingMode = formattingMode;
|
||||
}
|
||||
|
||||
public @NotNull Block create(final @NotNull List<? extends ASTNode> subNodes, final Wrap wrap, final @Nullable Alignment alignment) {
|
||||
final ArrayList<Block> subBlocks = new ArrayList<>();
|
||||
final ASTNode firstNode = subNodes.get(0);
|
||||
if (firstNode.getElementType() == JavaTokenType.DOT) {
|
||||
AlignmentStrategy strategy = AlignmentStrategy.getNullStrategy();
|
||||
Block block = newJavaBlock(firstNode, mySettings, myJavaSettings, Indent.getNoneIndent(), null, strategy, myFormattingMode);
|
||||
subBlocks.add(block);
|
||||
subNodes.remove(0);
|
||||
if (!subNodes.isEmpty()) {
|
||||
subBlocks.add(create(subNodes, wrap, null));
|
||||
}
|
||||
return new SyntheticCodeBlock(subBlocks, alignment, mySettings, myJavaSettings, Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS), wrap);
|
||||
}
|
||||
return new SyntheticCodeBlock(createJavaBlocks(subNodes), alignment, mySettings, myJavaSettings, Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS), null);
|
||||
}
|
||||
|
||||
private @NotNull List<Block> createJavaBlocks(final @NotNull List<? extends ASTNode> subNodes) {
|
||||
final ArrayList<Block> result = new ArrayList<>();
|
||||
for (ASTNode node : subNodes) {
|
||||
Indent indent = Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS);
|
||||
result.add(newJavaBlock(node, mySettings, myJavaSettings, indent, null, AlignmentStrategy.getNullStrategy(), myFormattingMode));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
-155
@@ -1,155 +0,0 @@
|
||||
// Copyright 2000-2024 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.formatting.*;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.JavaTokenType;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import com.intellij.psi.impl.source.tree.JavaElementType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.psi.formatter.java.JavaFormatterUtil.getWrapType;
|
||||
|
||||
/**
|
||||
* The previous version of {@link ChainMethodCallsBlockBuilder} for compatibility with releases prior to 2021.2,
|
||||
* see <a href="https://youtrack.jetbrains.com/issue/IDEA-274778">IDEA-274778</a>
|
||||
*/
|
||||
public class LegacyChainedMethodCallsBlockBuilder {
|
||||
public static final String COMPATIBILITY_KEY = "java.formatter.chained.calls.pre212.compatibility";
|
||||
|
||||
private final CommonCodeStyleSettings mySettings;
|
||||
private final CommonCodeStyleSettings.IndentOptions myIndentSettings;
|
||||
private final JavaCodeStyleSettings myJavaSettings;
|
||||
|
||||
private final Wrap myBlockWrap;
|
||||
private final Alignment myBlockAlignment;
|
||||
private final Indent myBlockIndent;
|
||||
|
||||
private final FormattingMode myFormattingMode;
|
||||
|
||||
LegacyChainedMethodCallsBlockBuilder(Alignment alignment,
|
||||
Wrap wrap,
|
||||
Indent indent,
|
||||
CommonCodeStyleSettings settings,
|
||||
JavaCodeStyleSettings javaSettings,
|
||||
@NotNull FormattingMode formattingMode) {
|
||||
myBlockWrap = wrap;
|
||||
myBlockAlignment = alignment;
|
||||
myBlockIndent = indent;
|
||||
mySettings = settings;
|
||||
myIndentSettings = settings.getIndentOptions();
|
||||
myJavaSettings = javaSettings;
|
||||
myFormattingMode = formattingMode;
|
||||
}
|
||||
|
||||
public Block build(List<? extends ASTNode> nodes) {
|
||||
List<Block> blocks = buildBlocksFrom(nodes);
|
||||
|
||||
Indent indent = myBlockIndent != null ? myBlockIndent : Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS);
|
||||
return new SyntheticCodeBlock(blocks, myBlockAlignment, mySettings, myJavaSettings, indent, myBlockWrap);
|
||||
}
|
||||
|
||||
private List<Block> buildBlocksFrom(List<? extends ASTNode> nodes) {
|
||||
List<ChainedCallChunk> methodCall = splitMethodCallOnChunksByDots(nodes);
|
||||
|
||||
Wrap wrap = null;
|
||||
Alignment chainedCallsAlignment = null;
|
||||
|
||||
List<Block> blocks = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < methodCall.size(); i++) {
|
||||
ChainedCallChunk currentCallChunk = methodCall.get(i);
|
||||
if (isMethodCall(currentCallChunk) || isComment(currentCallChunk)) {
|
||||
if (wrap == null) {
|
||||
wrap = createCallChunkWrap(i, methodCall);
|
||||
}
|
||||
if (chainedCallsAlignment == null) {
|
||||
chainedCallsAlignment = createCallChunkAlignment(i, methodCall);
|
||||
}
|
||||
}
|
||||
else {
|
||||
wrap = null;
|
||||
chainedCallsAlignment = null;
|
||||
}
|
||||
|
||||
LegacyCallChunkBlockBuilder builder = new LegacyCallChunkBlockBuilder(mySettings, myJavaSettings, myFormattingMode);
|
||||
blocks.add(builder.create(currentCallChunk.nodes, wrap, chainedCallsAlignment));
|
||||
}
|
||||
|
||||
return blocks;
|
||||
}
|
||||
|
||||
private static boolean isComment(ChainedCallChunk chunk) {
|
||||
List<ASTNode> nodes = chunk.nodes;
|
||||
if (nodes.size() == 1) {
|
||||
return nodes.get(0).getPsi() instanceof PsiComment;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Wrap createCallChunkWrap(int chunkIndex, @NotNull List<? extends ChainedCallChunk> methodCall) {
|
||||
if (mySettings.WRAP_FIRST_METHOD_IN_CALL_CHAIN) {
|
||||
ChainedCallChunk next = chunkIndex + 1 < methodCall.size() ? methodCall.get(chunkIndex + 1) : null;
|
||||
if (next != null && isMethodCall(next)) {
|
||||
return Wrap.createWrap(getWrapType(mySettings.METHOD_CALL_CHAIN_WRAP), true);
|
||||
}
|
||||
}
|
||||
|
||||
return Wrap.createWrap(getWrapType(mySettings.METHOD_CALL_CHAIN_WRAP), false);
|
||||
}
|
||||
|
||||
private boolean shouldAlignMethod(ChainedCallChunk currentMethodChunk, List<ChainedCallChunk> methodCall) {
|
||||
return mySettings.ALIGN_MULTILINE_CHAINED_METHODS
|
||||
&& !currentMethodChunk.isEmpty()
|
||||
&& !chunkIsFirstInChainMethodCall(currentMethodChunk, methodCall);
|
||||
}
|
||||
|
||||
private static boolean chunkIsFirstInChainMethodCall(@NotNull ChainedCallChunk callChunk, @NotNull List<ChainedCallChunk> methodCall) {
|
||||
return !methodCall.isEmpty() && callChunk == methodCall.get(0);
|
||||
}
|
||||
|
||||
private static @NotNull List<ChainedCallChunk> splitMethodCallOnChunksByDots(@NotNull List<? extends ASTNode> nodes) {
|
||||
List<ChainedCallChunk> result = new ArrayList<>();
|
||||
|
||||
List<ASTNode> current = new ArrayList<>();
|
||||
for (ASTNode node : nodes) {
|
||||
if (node.getElementType() == JavaTokenType.DOT || node.getPsi() instanceof PsiComment) {
|
||||
if (!current.isEmpty()) {
|
||||
result.add(new ChainedCallChunk(current));
|
||||
}
|
||||
current = new ArrayList<>();
|
||||
}
|
||||
current.add(node);
|
||||
}
|
||||
|
||||
if (!current.isEmpty()) {
|
||||
result.add(new ChainedCallChunk(current));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Alignment createCallChunkAlignment(int chunkIndex, @NotNull List<ChainedCallChunk> methodCall) {
|
||||
ChainedCallChunk current = methodCall.get(chunkIndex);
|
||||
return shouldAlignMethod(current, methodCall)
|
||||
? AbstractJavaBlock.createAlignment(mySettings.ALIGN_MULTILINE_CHAINED_METHODS, null)
|
||||
: null;
|
||||
}
|
||||
|
||||
private static boolean isMethodCall(@NotNull ChainedCallChunk callChunk) {
|
||||
List<ASTNode> nodes = callChunk.nodes;
|
||||
return nodes.size() >= 3 && nodes.get(2).getElementType() == JavaElementType.EXPRESSION_LIST;
|
||||
}
|
||||
|
||||
private record ChainedCallChunk(@NotNull List<ASTNode> nodes) {
|
||||
boolean isEmpty() {
|
||||
return nodes.isEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-38
@@ -16,10 +16,7 @@
|
||||
package com.intellij.java.psi.formatter.java;
|
||||
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
import com.intellij.openapi.util.registry.RegistryValue;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.formatter.java.LegacyChainedMethodCallsBlockBuilder;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
|
||||
/**
|
||||
@@ -839,39 +836,4 @@ public class JavaFormatterIndentationTest extends AbstractJavaFormatterTest {
|
||||
}"""
|
||||
);
|
||||
}
|
||||
|
||||
public void testIdea274778() {
|
||||
CommonCodeStyleSettings.IndentOptions indentOptions = getSettings().getIndentOptions();
|
||||
indentOptions.INDENT_SIZE = 3;
|
||||
indentOptions.CONTINUATION_INDENT_SIZE = 3;
|
||||
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
|
||||
RegistryValue pre212compat = Registry.get(LegacyChainedMethodCallsBlockBuilder.COMPATIBILITY_KEY);
|
||||
try {
|
||||
pre212compat.setValue(true);
|
||||
doTextTest(
|
||||
"""
|
||||
class Foo {
|
||||
void foo() {
|
||||
LOG.error(DetailsMessage.of(
|
||||
"TITLE",
|
||||
"LONG MESSAGE TEXT...")
|
||||
.with("value", value));
|
||||
}
|
||||
}""",
|
||||
|
||||
"""
|
||||
class Foo {
|
||||
void foo() {
|
||||
LOG.error(DetailsMessage.of(
|
||||
"TITLE",
|
||||
"LONG MESSAGE TEXT...")
|
||||
.with("value", value));
|
||||
}
|
||||
}"""
|
||||
);
|
||||
}
|
||||
finally {
|
||||
pre212compat.setValue(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user