Implement option for wrapping assignment statements

#KT-21718 Fixed
This commit is contained in:
Dmitry Jemerov
2017-12-08 14:07:21 +01:00
parent 947833cad6
commit ab99bf843a
5 changed files with 53 additions and 5 deletions
@@ -415,10 +415,32 @@ abstract class KotlinCommonBlock(
} }
nodePsi is KtProperty -> nodePsi is KtProperty ->
return wrapAfterAnnotation(if (nodePsi.isLocal) return object : WrappingStrategy {
commonSettings.VARIABLE_ANNOTATION_WRAP override fun getWrap(childElement: ASTNode): Wrap? {
else val wrapSetting = if (nodePsi.isLocal) commonSettings.VARIABLE_ANNOTATION_WRAP else commonSettings.FIELD_ANNOTATION_WRAP
commonSettings.FIELD_ANNOTATION_WRAP) getWrapAfterAnnotation(childElement, wrapSetting)?.let {
return it
}
if (getPrevWithoutWhitespaceAndComments(childElement)?.elementType == KtTokens.EQ) {
return Wrap.createWrap(settings.kotlinCommonSettings.ASSIGNMENT_WRAP, true)
}
return null
}
}
nodePsi is KtBinaryExpression -> {
if (nodePsi.operationToken == KtTokens.EQ) {
return object : WrappingStrategy {
override fun getWrap(childElement: ASTNode): Wrap? {
if (getPrevWithoutWhitespaceAndComments(childElement)?.elementType == KtNodeTypes.OPERATION_REFERENCE) {
return Wrap.createWrap(settings.kotlinCommonSettings.ASSIGNMENT_WRAP, true)
}
return null
}
}
}
return WrappingStrategy.NoWrapping
}
} }
return WrappingStrategy.NoWrapping return WrappingStrategy.NoWrapping
@@ -260,7 +260,8 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
"CALL_PARAMETERS_RPAREN_ON_NEXT_LINE", "CALL_PARAMETERS_RPAREN_ON_NEXT_LINE",
"ENUM_CONSTANTS_WRAP", "ENUM_CONSTANTS_WRAP",
"METHOD_CALL_CHAIN_WRAP", "METHOD_CALL_CHAIN_WRAP",
"WRAP_FIRST_METHOD_IN_CALL_CHAIN" "WRAP_FIRST_METHOD_IN_CALL_CHAIN",
"ASSIGNMENT_WRAP"
) )
consumer.renameStandardOption(CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT, "'when' statements") consumer.renameStandardOption(CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT, "'when' statements")
consumer.renameStandardOption("FIELD_ANNOTATION_WRAP", "Property annotations") consumer.renameStandardOption("FIELD_ANNOTATION_WRAP", "Property annotations")
+11
View File
@@ -0,0 +1,11 @@
val x =
1
fun foo() {
val y =
2
y =
3
}
// SET_INT: ASSIGNMENT_WRAP = 2
+8
View File
@@ -0,0 +1,8 @@
val x = 1
fun foo() {
val y = 2
y = 3
}
// SET_INT: ASSIGNMENT_WRAP = 2
@@ -74,6 +74,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("AssignmentWrap.after.kt")
public void testAssignmentWrap() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/AssignmentWrap.after.kt");
doTest(fileName);
}
@TestMetadata("BinaryExpressionAlignmentSpread.after.kt") @TestMetadata("BinaryExpressionAlignmentSpread.after.kt")
public void testBinaryExpressionAlignmentSpread() throws Exception { public void testBinaryExpressionAlignmentSpread() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/BinaryExpressionAlignmentSpread.after.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/BinaryExpressionAlignmentSpread.after.kt");