Java formatter: support java Records in formatter: IDEA-229706

GitOrigin-RevId: dc7ee367f5deaf916f7690cbd6e8a351852e914d
This commit is contained in:
Roman.Ivanov
2020-01-31 12:35:15 +00:00
committed by intellij-monorepo-bot
parent 580a378c13
commit bb4852b023
7 changed files with 147 additions and 2 deletions
@@ -213,8 +213,27 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
ApplicationBundle.message("wrapping.text.blocks") );
String groupName = ApplicationBundle.message("wrapping.fields.annotation");
consumer.showCustomOption(JavaCodeStyleSettings.class, "DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION",
ApplicationBundle.message("checkbox.do.not.wrap.after.single.annotation"), groupName);
consumer.showCustomOption(JavaCodeStyleSettings.class, "DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION", ApplicationBundle.message("checkbox.do.not.wrap.after.single.annotation"), groupName);
// Record components
String recordComponentsGroup = ApplicationBundle.message("wrapping.record.components");
consumer.showCustomOption(JavaCodeStyleSettings.class,
"RECORD_COMPONENTS_WRAP",
recordComponentsGroup,
null,
CodeStyleSettingsCustomizable.WRAP_OPTIONS, CodeStyleSettingsCustomizable.WRAP_VALUES);
consumer.showCustomOption(JavaCodeStyleSettings.class,
"ALIGN_MULTILINE_RECORDS",
ApplicationBundle.message("wrapping.align.when.multiline"),
recordComponentsGroup);
consumer.showCustomOption(JavaCodeStyleSettings.class,
"NEW_LINE_AFTER_LPAREN_IN_RECORD_HEADER",
ApplicationBundle.message("wrapping.new.line.after.lpar"),
recordComponentsGroup);
consumer.showCustomOption(JavaCodeStyleSettings.class,
"RPAREN_ON_NEW_LINE_IN_RECORD_HEADER",
ApplicationBundle.message("wrapping.rpar.on.new.line"),
recordComponentsGroup);
}
else if (settingsType == SettingsType.BLANK_LINES_SETTINGS) {
consumer.showAllStandardOptions();
@@ -153,6 +153,14 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im
@Property(externalName = "imports_layout")
public PackageEntryTable IMPORT_LAYOUT_TABLE = new PackageEntryTable();
@WrapConstant
public int RECORD_COMPONENTS_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
public boolean ALIGN_MULTILINE_RECORDS = true;
public boolean NEW_LINE_AFTER_LPAREN_IN_RECORD_HEADER = false;
public boolean RPAREN_ON_NEW_LINE_IN_RECORD_HEADER = false;
// region JavaDoc
@Property(externalName = "doc_enable_formatting")
public boolean ENABLE_JAVADOC_FORMATTING = true;
@@ -550,6 +550,11 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
WrappingStrategy wrapStrategy = WrappingStrategy.createDoNotWrapCommaStrategy(wrapToUse);
child = processParenthesisBlock(result, child, wrapStrategy, mySettings.ALIGN_MULTILINE_PARAMETERS);
}
else if (childType == JavaTokenType.LPARENTH && nodeType == JavaElementType.RECORD_HEADER) {
Wrap wrap = Wrap.createWrap(getWrapType(myJavaSettings.RECORD_COMPONENTS_WRAP), false);
WrappingStrategy wrapStrategy = WrappingStrategy.createDoNotWrapCommaStrategy(wrap);
child = processParenthesisBlock(result, child, wrapStrategy, myJavaSettings.ALIGN_MULTILINE_RECORDS);
}
else if (childType == JavaTokenType.LPARENTH && nodeType == JavaElementType.RESOURCE_LIST) {
Wrap wrap = Wrap.createWrap(getWrapType(mySettings.RESOURCE_LIST_WRAP), false);
child = processParenthesisBlock(result, child,
@@ -1220,6 +1220,18 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor {
}
}
@Override
public void visitRecordHeader(PsiRecordHeader recordHeader) {
if (myType1 != JavaTokenType.LPARENTH || myType2 != JavaTokenType.RPARENTH) {
if (myType2 == JavaTokenType.RPARENTH) {
createParenthSpace(myJavaSettings.RPAREN_ON_NEW_LINE_IN_RECORD_HEADER, false);
}
else if (myType1 == JavaTokenType.LPARENTH) {
createParenthSpace(myJavaSettings.NEW_LINE_AFTER_LPAREN_IN_RECORD_HEADER, false);
}
}
}
private void createParenthSpace(boolean onNewLine, boolean space) {
createParenthSpace(onNewLine, space, myParent.getTextRange());
}
@@ -17,6 +17,7 @@
"align_multiline_parameters": true,
"align_multiline_parameters_in_calls": false,
"align_multiline_parenthesized_expression": false,
"align_multiline_records": true,
"align_multiline_resources": true,
"align_multiline_ternary_operation": false,
"align_multiline_text_blocks": false,
@@ -133,6 +134,7 @@
"method_parameters_wrap": "normal",
"modifier_list_wrap": false,
"names_count_to_use_import_on_demand": 3,
"new_line_after_lparen_in_record_header": false,
"packages_to_use_import_on_demand": [
"java.awt.*",
"javax.swing.*"
@@ -143,6 +145,7 @@
"place_assignment_sign_on_next_line": false,
"prefer_longer_names": true,
"prefer_parameters_wrap": false,
"record_components_wrap": "normal",
"repeat_annotations": [
"com.jetbrains.First",
"com.jetbrains.Second"
@@ -154,6 +157,7 @@
"resource_list_new_line_after_left_paren": false,
"resource_list_right_paren_on_new_line": false,
"resource_list_wrap": "off",
"rparen_on_new_line_in_record_header": false,
"smart_tabs": false,
"space_after_closing_angle_bracket_in_type_argument": false,
"space_after_colon": true,
@@ -3877,4 +3877,100 @@ public enum LevelCode {
""".trimIndent()
)
}
fun testRecordHeaderLparenOnNewLine() {
javaSettings.apply {
NEW_LINE_AFTER_LPAREN_IN_RECORD_HEADER = true
}
doTextTest("""
record A(String s
) {}
""".trimIndent(), """
record A(
String s
) {
}
""".trimIndent())
}
fun testRecordHeaderLparenNotOnNewLine() {
javaSettings.apply {
NEW_LINE_AFTER_LPAREN_IN_RECORD_HEADER = false
}
settings.KEEP_LINE_BREAKS = false
doTextTest("""
record A(String s
) {}
""".trimIndent(), """
record A(String s) {
}
""".trimIndent())
}
fun testRecordHeaderRparenOnNewLine() {
javaSettings.apply {
RPAREN_ON_NEW_LINE_IN_RECORD_HEADER = true
}
doTextTest("""
record A(String s,
String a) {}
""".trimIndent(), """
record A(String s,
String a
) {
}
""".trimIndent())
}
fun testRecordHeaderRparenNotOnNewLine() {
javaSettings.apply {
RPAREN_ON_NEW_LINE_IN_RECORD_HEADER = false
}
settings.KEEP_LINE_BREAKS = false
doTextTest("""
record A(
String s,
String a
) {
}
""".trimIndent(), """
record A(String s,
String a) {
}
""".trimIndent())
}
fun testRecordHeaderMultilineAlign() {
javaSettings.apply {
NEW_LINE_AFTER_LPAREN_IN_RECORD_HEADER = false
ALIGN_MULTILINE_RECORDS = true
}
doTextTest("""
record A(String s,
String a
) {}
""".trimIndent(), """
record A(String s,
String a
) {
}
""".trimIndent())
}
fun testRecordHeaderNotMultilineAlign() {
javaSettings.apply {
NEW_LINE_AFTER_LPAREN_IN_RECORD_HEADER = false
ALIGN_MULTILINE_RECORDS = false
}
doTextTest("""
record A(String s,
String a
) {}
""".trimIndent(), """
record A(String s,
String a
) {
}
""".trimIndent())
}
}
@@ -205,6 +205,7 @@ wrapping.force.braces=Force braces
wrapping.method.parameters=Method declaration parameters
wrapping.method.arguments=Method call arguments
wrapping.record.components=Record components
wrapping.chained.method.calls=Chained method calls
wrapping.chained.method.call.first.on.new.line=Wrap first call
wrapping.for.statement='for()' statement