Weird indentation for new Java object construction
When trying to reformat code, the result can pretty weird:
final var relations = List.of(new NodeAssignment( assignment.getTransportNodeId(), assignment.getDestination(), assignment.getTenantId()));
But when I try to reformat again, I can get what I expected:
final var relations = List.of(new NodeAssignment( assignment.getTransportNodeId(), assignment.getDestination(), assignment.getTenantId()));
This is my Java code style xml:
<component name="ProjectCodeStyleConfiguration"> <code_scheme name="Project" version="173"> <JavaCodeStyleSettings> <option name="ANNOTATION_PARAMETER_WRAP" value="5" /> <option name="ALIGN_MULTILINE_ANNOTATION_PARAMETERS" value="true" /> <option name="NEW_LINE_WHEN_BODY_IS_PRESENTED" value="true" /> <option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="50" /> <option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="2" /> <option name="RECORD_COMPONENTS_WRAP" value="5" /> <option name="NEW_LINE_AFTER_LPAREN_IN_RECORD_HEADER" value="true" /> <option name="RPAREN_ON_NEW_LINE_IN_RECORD_HEADER" value="true" /> <option name="DECONSTRUCTION_LIST_WRAP" value="5" /> </JavaCodeStyleSettings> <ScalaCodeStyleSettings> <option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" /> </ScalaCodeStyleSettings> <codeStyleSettings language="JAVA"> <option name="RIGHT_MARGIN" value="120" /> <option name="LINE_COMMENT_AT_FIRST_COLUMN" value="false" /> <option name="BLOCK_COMMENT_AT_FIRST_COLUMN" value="false" /> <option name="KEEP_FIRST_COLUMN_COMMENT" value="false" /> <option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="0" /> <option name="ALIGN_MULTILINE_CHAINED_METHODS" value="true" /> <option name="ALIGN_MULTILINE_PARAMETERS_IN_CALLS" value="true" /> <option name="ALIGN_MULTILINE_BINARY_OPERATION" value="true" /> <option name="ALIGN_MULTILINE_TERNARY_OPERATION" value="true" /> <option name="ALIGN_MULTILINE_EXTENDS_LIST" value="true" /> <option name="ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION" value="true" /> <option name="CALL_PARAMETERS_WRAP" value="5" /> <option name="CALL_PARAMETERS_LPAREN_ON_NEXT_LINE" value="true" /> <option name="CALL_PARAMETERS_RPAREN_ON_NEXT_LINE" value="true" /> <option name="METHOD_PARAMETERS_WRAP" value="5" /> <option name="METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE" value="true" /> <option name="METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE" value="true" /> <option name="RESOURCE_LIST_WRAP" value="5" /> <option name="RESOURCE_LIST_LPAREN_ON_NEXT_LINE" value="true" /> <option name="RESOURCE_LIST_RPAREN_ON_NEXT_LINE" value="true" /> <option name="METHOD_CALL_CHAIN_WRAP" value="5" /> <option name="WRAP_FIRST_METHOD_IN_CALL_CHAIN" value="true" /> <option name="BINARY_OPERATION_WRAP" value="5" /> <option name="BINARY_OPERATION_SIGN_ON_NEXT_LINE" value="true" /> <option name="TERNARY_OPERATION_WRAP" value="5" /> <option name="TERNARY_OPERATION_SIGNS_ON_NEXT_LINE" value="true" /> <option name="KEEP_SIMPLE_BLOCKS_IN_ONE_LINE" value="true" /> <option name="KEEP_SIMPLE_METHODS_IN_ONE_LINE" value="true" /> <option name="KEEP_SIMPLE_LAMBDAS_IN_ONE_LINE" value="true" /> <option name="KEEP_SIMPLE_CLASSES_IN_ONE_LINE" value="true" /> <option name="FOR_STATEMENT_WRAP" value="5" /> <option name="FOR_STATEMENT_LPAREN_ON_NEXT_LINE" value="true" /> <option name="FOR_STATEMENT_RPAREN_ON_NEXT_LINE" value="true" /> <option name="ARRAY_INITIALIZER_WRAP" value="5" /> <option name="ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE" value="true" /> <option name="ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE" value="true" /> <option name="SWITCH_EXPRESSIONS_WRAP" value="5" /> <option name="ASSERT_STATEMENT_COLON_ON_NEXT_LINE" value="true" /> <option name="IF_BRACE_FORCE" value="3" /> <option name="WHILE_BRACE_FORCE" value="3" /> <option name="FOR_BRACE_FORCE" value="3" /> <option name="ENUM_CONSTANTS_WRAP" value="2" /> <option name="KEEP_BUILDER_METHODS_INDENTS" value="true" /> <option name="SOFT_MARGINS" value="80,100,120" /> <indentOptions> <option name="CONTINUATION_INDENT_SIZE" value="4" /> </indentOptions> <arrangement> <groups> <group> <type>GETTERS_AND_SETTERS</type> <order>KEEP</order> </group> <group> <type>OVERRIDDEN_METHODS</type> <order>KEEP</order> </group> </groups> </arrangement> </codeStyleSettings> </code_scheme></component>
How can I keep the format consistent no matter how many times I reformat the code? Any help is appreciated. Thanks in advance.
请先登录再写评论。
Want to update that it's always resulting in the first (weird) format when setting
<option name="KEEP_LINE_BREAKS" value="false" />When the KEEP_LINE_BREAKS is set to true, it toggles between the two, which I believe is due to a repeated reformat trying to ignore the existing line breaks.
It is possible that the KEEP_LINE_BREAKS toggles when it tries to remove the line breaks the second time.
I tested by pasting the sample code in the setting's preview page. Could you try if the following works for you?
Go to File | Settings | Editor | Code Style | Java, Wrapping and Braces,
Paste the sample code in the preview pane.
Under Method declaration parameters, unselect "Align when multiline".
Hi Abhishek,
Thank you for the reply and sorry for my very delayed follow up…
I think the option in question should be Method call arguments, and yes, unselecting the align when multiline fixed the style shifting from one style to another, which is great.
~~However, I cannot reproduce the weird style in the preview page with Align when multiple selected.~~
nvm, the preview issue has to do with the keep line breaks option.
Thank you for the help!