From 8eb9649e7f793dce1481c3ad0c5c06b528c65f1f Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 9 Feb 2012 15:12:19 +0400 Subject: [PATCH] KT-1158 Formatter: indent fluent api calls --- .../jet/plugin/formatter/JetBlock.java | 20 ++++++++-- idea/testData/formatter/ConsecutiveCalls.kt | 5 +++ .../formatter/ConsecutiveCalls_after.kt | 5 +++ .../ConsecutiveCallsAfterDot.after.kt | 4 ++ .../ConsecutiveCallsAfterDot.kt | 3 ++ .../jet/formatter/JetFormatterTest.java | 4 ++ .../formatter/JetTypingIndentationTest.java | 37 +++++++++++++------ 7 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 idea/testData/formatter/ConsecutiveCalls.kt create mode 100644 idea/testData/formatter/ConsecutiveCalls_after.kt create mode 100644 idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.after.kt create mode 100644 idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.kt diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java index 9bfa22ee50d..d52b7ea66a7 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java @@ -85,11 +85,15 @@ public class JetBlock extends AbstractBlock { Indent childIndent = Indent.getNoneIndent(); Alignment childAlignment = null; + ASTNode childParent = child.getTreeParent(); + if (CODE_BLOCKS.contains(myNode.getElementType())) { childIndent = indentIfNotBrace(child); } - else if (child.getTreeParent() != null && child.getTreeParent().getElementType() == JetNodeTypes.BODY && + else if (childParent != null && + childParent.getElementType() == JetNodeTypes.BODY && child.getElementType() != JetNodeTypes.BLOCK) { + // For a single statement if 'for' childIndent = Indent.getNormalIndent(); } @@ -98,7 +102,7 @@ public class JetBlock extends AbstractBlock { // TODO: Add an option for configuration? childIndent = Indent.getNormalIndent(); } - else if (child.getTreeParent() != null && child.getTreeParent().getElementType() == JetNodeTypes.WHEN_ENTRY) { + else if (childParent != null && childParent.getElementType() == JetNodeTypes.WHEN_ENTRY) { ASTNode prev = getPrevWithoutWhitespace(child); if (prev != null && prev.getText().equals("->")) { childIndent = indentIfNotBrace(child); @@ -107,6 +111,11 @@ public class JetBlock extends AbstractBlock { else if (STATEMENT_PARTS.contains(myNode.getElementType()) && child.getElementType() != JetNodeTypes.BLOCK) { childIndent = Indent.getNormalIndent(); } + else if (childParent != null && childParent.getElementType() == JetNodeTypes.DOT_QUALIFIED_EXPRESSION) { + if (childParent.getFirstChildNode() != this) { + childIndent = Indent.getContinuationWithoutFirstIndent(false); + } + } return new JetBlock(child, childAlignment, childIndent, wrap, mySettings, mySpacingBuilder); } @@ -130,7 +139,7 @@ public class JetBlock extends AbstractBlock { public Spacing getSpacing(Block child1, Block child2) { return mySpacingBuilder.getSpacing(this, child1, child2); } - + @NotNull @Override public ChildAttributes getChildAttributes(int newChildIndex) { @@ -144,8 +153,11 @@ public class JetBlock extends AbstractBlock { return new ChildAttributes(Indent.getNormalIndent(), null); } + else if (type == JetNodeTypes.DOT_QUALIFIED_EXPRESSION) { + return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null); + } - return new ChildAttributes(Indent.getNoneIndent(), null); + return super.getChildAttributes(newChildIndex); } @Override diff --git a/idea/testData/formatter/ConsecutiveCalls.kt b/idea/testData/formatter/ConsecutiveCalls.kt new file mode 100644 index 00000000000..4da5a2a9115 --- /dev/null +++ b/idea/testData/formatter/ConsecutiveCalls.kt @@ -0,0 +1,5 @@ +fun test() { +MyFunc1() +.MyFunc2() +.MyFunc3() +} \ No newline at end of file diff --git a/idea/testData/formatter/ConsecutiveCalls_after.kt b/idea/testData/formatter/ConsecutiveCalls_after.kt new file mode 100644 index 00000000000..323c6f491ec --- /dev/null +++ b/idea/testData/formatter/ConsecutiveCalls_after.kt @@ -0,0 +1,5 @@ +fun test() { + MyFunc1() + .MyFunc2() + .MyFunc3() +} \ No newline at end of file diff --git a/idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.after.kt b/idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.after.kt new file mode 100644 index 00000000000..a1c54856e01 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.after.kt @@ -0,0 +1,4 @@ +fun test() { + some.test(). + +} \ No newline at end of file diff --git a/idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.kt b/idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.kt new file mode 100644 index 00000000000..134d519f127 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.kt @@ -0,0 +1,3 @@ +fun test() { + some.test(). +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java index 5f6ca551d5d..5710cb4afbe 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java @@ -12,6 +12,10 @@ public class JetFormatterTest extends AbstractJetFormatterTest { doTest(); } + public void testConsecutiveCalls() throws Exception { + doTest(); + } + public void testForNoBraces() throws Exception { doTest(); } diff --git a/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java b/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java index 3d70a9da406..cedef07d1c2 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java @@ -9,15 +9,8 @@ import java.io.File; * @author Nikolay Krasko */ public class JetTypingIndentationTest extends LightCodeInsightTestCase { - public void testWhile() { - doFileNewlineTest(); - } - public void testFor() { - doFileNewlineTest(); - } - - public void testIf() { + public void testConsecutiveCallsAfterDot() { doFileNewlineTest(); } @@ -25,9 +18,31 @@ public class JetTypingIndentationTest extends LightCodeInsightTestCase { doFileNewlineTest(); } - // TODO: fix and uncomment - public void testFunctionBlock() { - // doFileNewlineTest(); + // TODO + public void enabletestEmptyParameters() { + doFileNewlineTest(); + } + + public void testFor() { + doFileNewlineTest(); + } + + // TODO + public void enabletestFunctionBlock() { + doFileNewlineTest(); + } + + public void testIf() { + doFileNewlineTest(); + } + + // TODO + public void enabletestNotFirstParameter() { + doFileNewlineTest(); + } + + public void testWhile() { + doFileNewlineTest(); } public void doFileNewlineTest() {