Option to use normal indent in chained calls

#KT-18605 Fixed
This commit is contained in:
Dmitry Jemerov
2017-06-22 14:30:24 +02:00
parent d8bb0b1023
commit 8fe2858c6a
14 changed files with 122 additions and 2 deletions
@@ -42,6 +42,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
public boolean IMPORT_NESTED_CLASSES = false;
public boolean CONTINUATION_INDENT_IN_PARAMETER_LISTS = true;
public boolean CONTINUATION_INDENT_FOR_EXPRESSION_BODIES = true;
public boolean CONTINUATION_INDENT_FOR_CHAINED_CALLS = true;
public KotlinCodeStyleSettings(CodeStyleSettings container) {
super("JetCodeStyleSettings", container);
@@ -94,10 +94,14 @@ abstract class KotlinCommonBlock(
// relative to it when it starts from new line (see Indent javadoc).
val operationBlock = nodeSubBlocks[operationBlockIndex]
val indent = if (settings.kotlinSettings.CONTINUATION_INDENT_FOR_CHAINED_CALLS)
Indent.getContinuationWithoutFirstIndent()
else
Indent.getNormalIndent()
val operationSyntheticBlock = SyntheticKotlinBlock(
(operationBlock as ASTBlock).node,
nodeSubBlocks.subList(operationBlockIndex, nodeSubBlocks.size),
null, operationBlock.getIndent(), null, spacingBuilder) { createSyntheticSpacingNodeBlock(it) }
null, indent, null, spacingBuilder) { createSyntheticSpacingNodeBlock(it) }
nodeSubBlocks = ArrayList<Block>(nodeSubBlocks.subList(0, operationBlockIndex))
nodeSubBlocks.add(operationSyntheticBlock)
@@ -382,7 +386,14 @@ private val INDENT_RULES = arrayOf<NodeIndentStrategy>(
strategy("Chained calls")
.within(KtNodeTypes.DOT_QUALIFIED_EXPRESSION, KtNodeTypes.SAFE_ACCESS_EXPRESSION)
.set(Indent.getContinuationWithoutFirstIndent(false)),
.notForType(KtTokens.DOT, KtTokens.SAFE_ACCESS)
.forElement { it.treeParent.firstChildNode != it }
.set { settings ->
if (settings.kotlinSettings.CONTINUATION_INDENT_FOR_CHAINED_CALLS)
Indent.getContinuationWithoutFirstIndent()
else
Indent.getNormalIndent()
},
strategy("Colon of delegation list")
.within(KtNodeTypes.CLASS, KtNodeTypes.OBJECT_DECLARATION)
@@ -26,11 +26,13 @@ import javax.swing.JCheckBox
class KotlinIndentOptionsEditor : SmartIndentOptionsEditor() {
private val useContinuationIndentInParameterList = JCheckBox("Use continuation indent in parameter lists")
private val useContinuationIndentForExpressionBodies = JCheckBox("Use continuation indent for expression bodies")
private val useContinuationIndentForChainedCalls = JCheckBox("Use continuation indent for chained calls")
override fun addComponents() {
super.addComponents()
add(useContinuationIndentInParameterList)
add(useContinuationIndentForExpressionBodies)
add(useContinuationIndentForChainedCalls)
}
override fun isModified(settings: CodeStyleSettings, options: CommonCodeStyleSettings.IndentOptions): Boolean {
@@ -40,6 +42,8 @@ class KotlinIndentOptionsEditor : SmartIndentOptionsEditor() {
kotlinSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS)
isModified = isModified || IndentOptionsEditor.isFieldModified(useContinuationIndentForExpressionBodies,
kotlinSettings.CONTINUATION_INDENT_FOR_EXPRESSION_BODIES)
isModified = isModified || IndentOptionsEditor.isFieldModified(useContinuationIndentForChainedCalls,
kotlinSettings.CONTINUATION_INDENT_FOR_CHAINED_CALLS)
return isModified
}
@@ -48,6 +52,7 @@ class KotlinIndentOptionsEditor : SmartIndentOptionsEditor() {
val kotlinSettings = settings.getCustomSettings(KotlinCodeStyleSettings::class.java)
kotlinSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS = useContinuationIndentInParameterList.isSelected
kotlinSettings.CONTINUATION_INDENT_FOR_EXPRESSION_BODIES = useContinuationIndentForExpressionBodies.isSelected
kotlinSettings.CONTINUATION_INDENT_FOR_CHAINED_CALLS = useContinuationIndentForChainedCalls.isSelected
}
override fun reset(settings: CodeStyleSettings, options: CommonCodeStyleSettings.IndentOptions) {
@@ -55,11 +60,13 @@ class KotlinIndentOptionsEditor : SmartIndentOptionsEditor() {
val kotlinSettings = settings.getCustomSettings(KotlinCodeStyleSettings::class.java)
useContinuationIndentInParameterList.isSelected = kotlinSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS
useContinuationIndentForExpressionBodies.isSelected = kotlinSettings.CONTINUATION_INDENT_FOR_EXPRESSION_BODIES
useContinuationIndentForChainedCalls.isSelected = kotlinSettings.CONTINUATION_INDENT_FOR_CHAINED_CALLS
}
override fun setEnabled(enabled: Boolean) {
super.setEnabled(enabled)
useContinuationIndentInParameterList.isEnabled = enabled
useContinuationIndentForExpressionBodies.isEnabled = enabled
useContinuationIndentForChainedCalls.isEnabled = enabled
}
}
@@ -125,6 +125,8 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
foo: String,
bar: String
) {
foo
.length
}
fun expressionBodyMethod() =
@@ -0,0 +1,12 @@
class Some {
fun some(): Some? = this
}
public fun bar(): String? =
Some()
?.some()
?.some()
?.some()!!
.toString()
// SET_TRUE: CONTINUATION_INDENT_FOR_CHAINED_CALLS
@@ -9,3 +9,4 @@ public fun bar(): String? =
?.some()!!
.toString()
// SET_TRUE: CONTINUATION_INDENT_FOR_CHAINED_CALLS
+1
View File
@@ -9,3 +9,4 @@ Some()
?.some()!!
.toString()
// SET_TRUE: CONTINUATION_INDENT_FOR_CHAINED_CALLS
@@ -0,0 +1,4 @@
val x = "abc"
.length
// SET_TRUE: CONTINUATION_INDENT_FOR_CHAINED_CALLS
@@ -0,0 +1,4 @@
val x = "abc"
.length
// SET_TRUE: CONTINUATION_INDENT_FOR_CHAINED_CALLS
@@ -0,0 +1,4 @@
val x = "abc"
.length
// SET_TRUE: CONTINUATION_INDENT_FOR_CHAINED_CALLS
@@ -0,0 +1,46 @@
import java.util.ArrayList
fun test() {
val abc = ArrayList<Int>()
.map {
it * 2
}
.filter {
it > 4
}
}
fun test1() {
val abc = ArrayList<Int>()
.map({
it * 2
})
.filter({
it > 4
})
}
fun test2() {
val abc = ArrayList<Int>()
.map {
it * 2
}
}
fun test3() {
val abc = ArrayList<Int>().map {
it * 2
}
}
fun testWithComments() {
val abc = ArrayList<Int>()
// .map {
// it * 2
// }
.filter {
it > 4
}
}
// SET_TRUE: CONTINUATION_INDENT_FOR_CHAINED_CALLS
@@ -43,3 +43,4 @@ fun testWithComments() {
}
}
// SET_TRUE: CONTINUATION_INDENT_FOR_CHAINED_CALLS
@@ -42,3 +42,5 @@ fun testWithComments() {
it > 4
}
}
// SET_TRUE: CONTINUATION_INDENT_FOR_CHAINED_CALLS
@@ -176,6 +176,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
doTest(fileName);
}
@TestMetadata("ContinuationIndentForChainedCalls.after.kt")
public void testContinuationIndentForChainedCalls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ContinuationIndentForChainedCalls.after.kt");
doTest(fileName);
}
@TestMetadata("ContinuationIndentInParameterLists.after.kt")
public void testContinuationIndentInParameterLists() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ContinuationIndentInParameterLists.after.kt");
@@ -1064,6 +1070,18 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
doTestInverted(fileName);
}
@TestMetadata("ConsecutiveSafeCallsIndent.after.inv.kt")
public void testConsecutiveSafeCallsIndent() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ConsecutiveSafeCallsIndent.after.inv.kt");
doTestInverted(fileName);
}
@TestMetadata("ContinuationIndentForChainedCalls.after.inv.kt")
public void testContinuationIndentForChainedCalls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ContinuationIndentForChainedCalls.after.inv.kt");
doTestInverted(fileName);
}
@TestMetadata("ContinuationIndentForExpressionBodies.after.inv.kt")
public void testContinuationIndentForExpressionBodies() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ContinuationIndentForExpressionBodies.after.inv.kt");
@@ -1130,6 +1148,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
doTestInverted(fileName);
}
@TestMetadata("FunctionLiteralsInChainCalls.after.inv.kt")
public void testFunctionLiteralsInChainCalls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FunctionLiteralsInChainCalls.after.inv.kt");
doTestInverted(fileName);
}
@TestMetadata("FunctionalType.after.inv.kt")
public void testFunctionalType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/FunctionalType.after.inv.kt");