diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/ASTIndentStrategy.java b/idea/src/org/jetbrains/jet/plugin/formatter/ASTIndentStrategy.java index 7975bda8cc0..95350dd58e4 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/ASTIndentStrategy.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/ASTIndentStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * Copyright 2010-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java index fc96eed1076..cf34748b948 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * Copyright 2010-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -157,7 +157,7 @@ public class JetBlock extends AbstractBlock { // In try - try BLOCK catch BLOCK finally BLOCK return new ChildAttributes(Indent.getNoneIndent(), null); } - else if (type == DOT_QUALIFIED_EXPRESSION) { + else if (type == DOT_QUALIFIED_EXPRESSION || type == SAFE_ACCESS_EXPRESSION) { return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null); } else if (type == VALUE_PARAMETER_LIST || type == VALUE_ARGUMENT_LIST) { @@ -286,6 +286,10 @@ public class JetBlock extends AbstractBlock { .notForType(BLOCK, FUN_KEYWORD, VAL_KEYWORD, VAR_KEYWORD) .set(Indent.getContinuationWithoutFirstIndent()), + ASTIndentStrategy.forNode("Chained calls") + .in(DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION) + .set(Indent.getContinuationWithoutFirstIndent(true)), + ASTIndentStrategy.forNode("KDoc comment indent") .in(DOC_COMMENT) .forType(KDocTokens.LEADING_ASTERISK, KDocTokens.END) @@ -317,12 +321,6 @@ public class JetBlock extends AbstractBlock { } // TODO: Try to rewrite other rules to declarative style - if (childParent != null && childParent.getElementType() == DOT_QUALIFIED_EXPRESSION) { - if (childParent.getFirstChildNode() != child && childParent.getLastChildNode() != child) { - return Indent.getContinuationWithoutFirstIndent(false); - } - } - if (childParent != null) { IElementType parentType = childParent.getElementType(); diff --git a/idea/testData/formatter/ConsecutiveSafeCallsIndent.after.kt b/idea/testData/formatter/ConsecutiveSafeCallsIndent.after.kt new file mode 100644 index 00000000000..dc510293495 --- /dev/null +++ b/idea/testData/formatter/ConsecutiveSafeCallsIndent.after.kt @@ -0,0 +1,11 @@ +class Some { + fun some(): Some? = this +} + +public fun bar(): String? = + Some() + ?.some() + ?.some() + ?.some()!! + .toString() + diff --git a/idea/testData/formatter/ConsecutiveSafeCallsIndent.kt b/idea/testData/formatter/ConsecutiveSafeCallsIndent.kt new file mode 100644 index 00000000000..da7af9c7cf7 --- /dev/null +++ b/idea/testData/formatter/ConsecutiveSafeCallsIndent.kt @@ -0,0 +1,11 @@ +class Some { + fun some(): Some? = this +} + +public fun bar(): String? = +Some() +?.some() +?.some() +?.some()!! +.toString() + diff --git a/idea/testData/indentationOnNewline/ConsecutiveCallsInSaeCallsMiddle.after.kt b/idea/testData/indentationOnNewline/ConsecutiveCallsInSaeCallsMiddle.after.kt new file mode 100644 index 00000000000..fd6eefb12a0 --- /dev/null +++ b/idea/testData/indentationOnNewline/ConsecutiveCallsInSaeCallsMiddle.after.kt @@ -0,0 +1,8 @@ +class Some { + fun some(): Some? = this +} + +public fun bar(): String? = Some()?.some() + + ?.some() + ?.some() diff --git a/idea/testData/indentationOnNewline/ConsecutiveCallsInSaeCallsMiddle.kt b/idea/testData/indentationOnNewline/ConsecutiveCallsInSaeCallsMiddle.kt new file mode 100644 index 00000000000..e051c403daa --- /dev/null +++ b/idea/testData/indentationOnNewline/ConsecutiveCallsInSaeCallsMiddle.kt @@ -0,0 +1,7 @@ +class Some { + fun some(): Some? = this +} + +public fun bar(): String? = Some()?.some() + ?.some() + ?.some() diff --git a/idea/testData/indentationOnNewline/ConsecutiveCallsInSafeCallsEnd.after.kt b/idea/testData/indentationOnNewline/ConsecutiveCallsInSafeCallsEnd.after.kt new file mode 100644 index 00000000000..4b59b36de92 --- /dev/null +++ b/idea/testData/indentationOnNewline/ConsecutiveCallsInSafeCallsEnd.after.kt @@ -0,0 +1,4 @@ +fun test() { + some.test()?. + +} \ No newline at end of file diff --git a/idea/testData/indentationOnNewline/ConsecutiveCallsInSafeCallsEnd.kt b/idea/testData/indentationOnNewline/ConsecutiveCallsInSafeCallsEnd.kt new file mode 100644 index 00000000000..1f9c55d7c53 --- /dev/null +++ b/idea/testData/indentationOnNewline/ConsecutiveCallsInSafeCallsEnd.kt @@ -0,0 +1,3 @@ +fun test() { + some.test()?. +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java index ac33a704a53..343a14b30ed 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java @@ -74,6 +74,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest { doTest("idea/testData/formatter/ConsecutiveCalls.after.kt"); } + @TestMetadata("ConsecutiveSafeCallsIndent.after.kt") + public void testConsecutiveSafeCallsIndent() throws Exception { + doTest("idea/testData/formatter/ConsecutiveSafeCallsIndent.after.kt"); + } + @TestMetadata("DoWhileLineBreak.after.kt") public void testDoWhileLineBreak() throws Exception { doTest("idea/testData/formatter/DoWhileLineBreak.after.kt"); diff --git a/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTestBaseGenerated.java b/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTestBaseGenerated.java index cf86cda8a20..62d9f0293ac 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTestBaseGenerated.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTestBaseGenerated.java @@ -56,7 +56,9 @@ public class JetTypingIndentationTestBaseGenerated extends AbstractJetTypingInde } public void testAllFilesPresentInDirectSettings() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/indentationOnNewline"), Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), true); + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", + new File("idea/testData/indentationOnNewline"), + Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), true); } @TestMetadata("ConsecutiveCallsAfterDot.after.kt") @@ -64,6 +66,16 @@ public class JetTypingIndentationTestBaseGenerated extends AbstractJetTypingInde doNewlineTest("idea/testData/indentationOnNewline/ConsecutiveCallsAfterDot.after.kt"); } + @TestMetadata("ConsecutiveCallsInSaeCallsMiddle.after.kt") + public void testConsecutiveCallsInSaeCallsMiddle() throws Exception { + doNewlineTest("idea/testData/indentationOnNewline/ConsecutiveCallsInSaeCallsMiddle.after.kt"); + } + + @TestMetadata("ConsecutiveCallsInSafeCallsEnd.after.kt") + public void testConsecutiveCallsInSafeCallsEnd() throws Exception { + doNewlineTest("idea/testData/indentationOnNewline/ConsecutiveCallsInSafeCallsEnd.after.kt"); + } + @TestMetadata("DoInFun.after.kt") public void testDoInFun() throws Exception { doNewlineTest("idea/testData/indentationOnNewline/DoInFun.after.kt"); @@ -154,7 +166,9 @@ public class JetTypingIndentationTestBaseGenerated extends AbstractJetTypingInde @InnerTestClasses({}) public static class InvertedSettings extends AbstractJetTypingIndentationTestBase { public void testAllFilesPresentInInvertedSettings() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/indentationOnNewline"), Pattern.compile("^([^\\.]+)\\.after\\.inv\\.kt.*$"), true); + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", + new File("idea/testData/indentationOnNewline"), + Pattern.compile("^([^\\.]+)\\.after\\.inv\\.kt.*$"), true); } @TestMetadata("SettingAlignMultilineParametersInCalls.after.inv.kt")