mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[Java. Code Formatting] IDEA-352811 Format comments independently from method call arguments
GitOrigin-RevId: a3b1c3ec048a6a7d9c2498786e97d674a3f88231
This commit is contained in:
committed by
intellij-monorepo-bot
parent
a3cdb74de0
commit
03c33340d5
+20
-9
@@ -555,10 +555,11 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
|
||||
else if (childType == JavaTokenType.LBRACE && nodeType == JavaElementType.ANNOTATION_ARRAY_INITIALIZER) {
|
||||
final Wrap wrap = Wrap.createWrap(getWrapType(mySettings.ARRAY_INITIALIZER_WRAP), false);
|
||||
child = processParenthesisBlock(JavaTokenType.LBRACE, JavaTokenType.RBRACE,
|
||||
result,
|
||||
child,
|
||||
WrappingStrategy.createDoNotWrapCommaStrategy(wrap),
|
||||
mySettings.ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION);
|
||||
result,
|
||||
child,
|
||||
WrappingStrategy.createDoNotWrapCommaStrategy(wrap),
|
||||
mySettings.ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION,
|
||||
JavaTokenType.COMMA);
|
||||
}
|
||||
else if (childType == JavaTokenType.LPARENTH && nodeType == JavaElementType.EXPRESSION_LIST) {
|
||||
final Wrap wrap = Wrap.createWrap(getWrapType(mySettings.CALL_PARAMETERS_WRAP), false);
|
||||
@@ -566,8 +567,9 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
|
||||
wrap.ignoreParentWraps();
|
||||
}
|
||||
child = processParenthesisBlock(result, child,
|
||||
WrappingStrategy.createDoNotWrapCommaStrategy(wrap),
|
||||
mySettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS);
|
||||
WrappingStrategy.createDoNotWrapCommaAndCommentStrategy(wrap),
|
||||
mySettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS,
|
||||
JavaTokenType.COMMA, JavaTokenType.END_OF_LINE_COMMENT, JavaTokenType.C_STYLE_COMMENT);
|
||||
}
|
||||
else if (childType == JavaTokenType.LPARENTH && nodeType == JavaElementType.PARAMETER_LIST) {
|
||||
ASTNode parent = myNode.getTreeParent();
|
||||
@@ -945,12 +947,20 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
|
||||
@NotNull ASTNode child,
|
||||
@NotNull WrappingStrategy wrappingStrategy,
|
||||
final boolean doAlign) {
|
||||
return processParenthesisBlock(result, child, wrappingStrategy, doAlign, JavaTokenType.COMMA);
|
||||
}
|
||||
|
||||
private @NotNull ASTNode processParenthesisBlock(@NotNull List<Block> result,
|
||||
@NotNull ASTNode child,
|
||||
@NotNull WrappingStrategy wrappingStrategy,
|
||||
final boolean doAlign,
|
||||
IElementType @NotNull ... ignoreAlignmentTypes) {
|
||||
myUseChildAttributes = true;
|
||||
|
||||
final IElementType from = JavaTokenType.LPARENTH;
|
||||
final IElementType to = JavaTokenType.RPARENTH;
|
||||
|
||||
return processParenthesisBlock(from, to, result, child, wrappingStrategy, doAlign);
|
||||
return processParenthesisBlock(from, to, result, child, wrappingStrategy, doAlign, ignoreAlignmentTypes);
|
||||
}
|
||||
|
||||
private @NotNull ASTNode processParenthesisBlock(@NotNull IElementType from,
|
||||
@@ -958,7 +968,8 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
|
||||
final @NotNull List<Block> result,
|
||||
@NotNull ASTNode child,
|
||||
final @NotNull WrappingStrategy wrappingStrategy,
|
||||
final boolean doAlign)
|
||||
final boolean doAlign,
|
||||
IElementType @NotNull ... ignoreAlignmentTypes)
|
||||
{
|
||||
Indent externalIndent = Indent.getNoneIndent();
|
||||
Indent internalIndent = Indent.getContinuationWithoutFirstIndent(false);
|
||||
@@ -967,7 +978,7 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
|
||||
internalIndent = Indent.getSmartIndent(Indent.Type.CONTINUATION);
|
||||
}
|
||||
|
||||
AlignmentStrategy alignmentStrategy = AlignmentStrategy.wrap(createAlignment(doAlign, null), JavaTokenType.COMMA);
|
||||
AlignmentStrategy alignmentStrategy = AlignmentStrategy.wrap(createAlignment(doAlign, null), ignoreAlignmentTypes);
|
||||
setChildIndent(internalIndent);
|
||||
setChildAlignment(alignmentStrategy.getAlignment(null));
|
||||
boolean methodParametersBlock = true;
|
||||
|
||||
@@ -19,8 +19,11 @@ import com.intellij.formatting.Wrap;
|
||||
import com.intellij.psi.JavaTokenType;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
|
||||
public abstract class WrappingStrategy {
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class WrappingStrategy {
|
||||
private static final Set<IElementType> COMMA_TOKENS = Set.of(JavaTokenType.COMMA, JavaTokenType.SEMICOLON);
|
||||
private static final Set<IElementType> COMMENT_TOKENS = Set.of(JavaTokenType.END_OF_LINE_COMMENT, JavaTokenType.C_STYLE_COMMENT);
|
||||
public static final WrappingStrategy DO_NOT_WRAP = new WrappingStrategy(null) {
|
||||
@Override
|
||||
protected boolean shouldWrap(final IElementType type) {
|
||||
@@ -32,7 +35,19 @@ public abstract class WrappingStrategy {
|
||||
return new WrappingStrategy(wrap) {
|
||||
@Override
|
||||
protected boolean shouldWrap(final IElementType type) {
|
||||
return type != JavaTokenType.COMMA && type != JavaTokenType.SEMICOLON;
|
||||
return !COMMA_TOKENS.contains(type);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return strategy that doesn't wrap blocks if they are commas or plain comments (excluding javadoc)
|
||||
*/
|
||||
public static WrappingStrategy createDoNotWrapCommaAndCommentStrategy(Wrap wrap) {
|
||||
return new WrappingStrategy(wrap) {
|
||||
@Override
|
||||
protected boolean shouldWrap(final IElementType type) {
|
||||
return !COMMA_TOKENS.contains(type) && !COMMENT_TOKENS.contains(type);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
class A {
|
||||
void veryLongMethodName(String... strings) {
|
||||
veryLongMethodName( /* some comment */
|
||||
"name", /* same line comment */
|
||||
/* another comment */
|
||||
"second name" /* final comment */
|
||||
);
|
||||
|
||||
veryLongMethodName( /* some comment */
|
||||
"name", /* same line comment */
|
||||
/* another comment */
|
||||
"second name" /* final comment */);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
class A {
|
||||
void veryLongMethodName(String... strings) {
|
||||
veryLongMethodName( /* some comment */
|
||||
"name", /* same line comment */
|
||||
/* another comment */
|
||||
"second name" /* final comment */
|
||||
);
|
||||
|
||||
veryLongMethodName( /* some comment */
|
||||
"name", /* same line comment */
|
||||
/* another comment */
|
||||
"second name" /* final comment */);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class A {
|
||||
void veryLongMethodName(String... strings) {
|
||||
veryLongMethodName( // some comment
|
||||
"name", // same line comment
|
||||
// another comment
|
||||
"second name" // final comment
|
||||
);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class A {
|
||||
void veryLongMethodName(String... strings) {
|
||||
veryLongMethodName( // some comment
|
||||
"name", // same line comment
|
||||
// another comment
|
||||
"second name" // final comment
|
||||
);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// 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.java.psi.formatter.java
|
||||
|
||||
class JavaFormatterCommentsTest: JavaFormatterIdempotencyTestCase() {
|
||||
override fun getBasePath(): String {
|
||||
return "psi/formatter/java/comments"
|
||||
}
|
||||
|
||||
fun testEndOfLineCommentWithMethodArgumentsAlignment() {
|
||||
commonSettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true
|
||||
doIdempotentTest()
|
||||
}
|
||||
|
||||
fun testCStyleCommentWithMethodArgumentsAlignment() {
|
||||
commonSettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true
|
||||
doIdempotentTest()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user