From 2e9259446266ae2a336eb7fedb16abe42a2f6a8b Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 21 Aug 2012 12:52:42 +0400 Subject: [PATCH] KT-2242 formatting problems: function literal passed outside the parentheses #KT-2242 Fixed --- .../jet/plugin/formatter/JetBlock.java | 40 ++++++++++++++++--- .../formatter/JetFormattingModelBuilder.java | 2 + .../formatter/CommentInFunctionLiteral.kt | 9 +++++ .../CommentInFunctionLiteral_after.kt | 9 +++++ .../formatter/MultilineFunctionLiteral.kt | 4 ++ .../MultilineFunctionLiteralWithParams.kt | 4 ++ ...ultilineFunctionLiteralWithParams_after.kt | 10 +++++ .../MultilineFunctionLiteral_after.kt | 10 +++++ .../formatter/SingleLineFunctionLiteral.kt | 4 ++ .../SingleLineFunctionLiteral_after.kt | 4 ++ .../formatter/SpaceBeforeFunctionLiteral.kt | 6 +++ .../SpaceBeforeFunctionLiteral_after.kt | 6 +++ .../jet/formatter/JetFormatterTest.java | 25 +++++++++--- 13 files changed, 123 insertions(+), 10 deletions(-) create mode 100644 idea/testData/formatter/CommentInFunctionLiteral.kt create mode 100644 idea/testData/formatter/CommentInFunctionLiteral_after.kt create mode 100644 idea/testData/formatter/MultilineFunctionLiteral.kt create mode 100644 idea/testData/formatter/MultilineFunctionLiteralWithParams.kt create mode 100644 idea/testData/formatter/MultilineFunctionLiteralWithParams_after.kt create mode 100644 idea/testData/formatter/MultilineFunctionLiteral_after.kt create mode 100644 idea/testData/formatter/SingleLineFunctionLiteral.kt create mode 100644 idea/testData/formatter/SingleLineFunctionLiteral_after.kt create mode 100644 idea/testData/formatter/SpaceBeforeFunctionLiteral.kt create mode 100644 idea/testData/formatter/SpaceBeforeFunctionLiteral_after.kt diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java index 8eb4e458289..cfd916d874e 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java @@ -32,6 +32,10 @@ import org.jetbrains.jet.plugin.JetLanguage; import java.util.*; +import static org.jetbrains.jet.JetNodeTypes.BLOCK; +import static org.jetbrains.jet.JetNodeTypes.FUNCTION_LITERAL; +import static org.jetbrains.jet.lexer.JetTokens.*; + /** * @author yole * @see Block for good JavaDoc documentation @@ -46,7 +50,6 @@ public class JetBlock extends AbstractBlock { private static final TokenSet CODE_BLOCKS = TokenSet.create( JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, - JetNodeTypes.FUNCTION_LITERAL_EXPRESSION, JetNodeTypes.FUNCTION_LITERAL); // private static final List @@ -129,7 +132,34 @@ public class JetBlock extends AbstractBlock { @Override public Spacing getSpacing(Block child1, Block child2) { - return mySpacingBuilder.getSpacing(this, child1, child2); + Spacing spacing = mySpacingBuilder.getSpacing(this, child1, child2); + if (spacing != null) { + return spacing; + } + + // TODO: extend SpacingBuilder API - afterInside(RBRACE, FUNCTION_LITERAL).spacing(...), beforeInside(RBRACE, FUNCTION_LITERAL).spacing(...) + + IElementType parentType = this.getNode().getElementType(); + IElementType child1Type = ((ASTBlock) child1).getNode().getElementType(); + IElementType child2Type = ((ASTBlock) child2).getNode().getElementType(); + + if (parentType == FUNCTION_LITERAL && child1Type == LBRACE && child2Type == BLOCK) { + return Spacing.createDependentLFSpacing(1, 1, this.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); + } + + if (parentType == FUNCTION_LITERAL && child1Type == ARROW && child2Type == BLOCK) { + return Spacing.createDependentLFSpacing(1, 1, this.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); + } + + if (parentType == FUNCTION_LITERAL && child2Type == RBRACE) { + return Spacing.createDependentLFSpacing(1, 1, this.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); + } + + if (parentType == FUNCTION_LITERAL && child1Type == LBRACE) { + return Spacing.createSpacing(1, 1, 0, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); + } + + return null; } @NotNull @@ -248,13 +278,13 @@ public class JetBlock extends AbstractBlock { static ASTIndentStrategy[] INDENT_RULES = new ASTIndentStrategy[] { ASTIndentStrategy.forNode("No indent for braces in blocks") - .in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL_EXPRESSION) + .in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL) .forType(JetTokens.RBRACE, JetTokens.LBRACE) .set(Indent.getNoneIndent()), ASTIndentStrategy.forNode("Indent for block content") - .in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL_EXPRESSION) - .notForType(JetTokens.RBRACE, JetTokens.LBRACE) + .in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL) + .notForType(JetTokens.RBRACE, JetTokens.LBRACE, JetNodeTypes.BLOCK) .set(Indent.getNormalIndent()), ASTIndentStrategy.forNode("Indent for property accessors") diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java index e27bfe94a29..7269e1280f5 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java @@ -96,6 +96,8 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder { .afterInside(COLON, CLASS).spaceIf(jetSettings.SPACE_AFTER_EXTEND_COLON) .beforeInside(COLON, TYPE_PARAMETER).spaceIf(jetSettings.SPACE_BEFORE_EXTEND_COLON) .afterInside(COLON, TYPE_PARAMETER).spaceIf(jetSettings.SPACE_AFTER_EXTEND_COLON) + + .between(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL_EXPRESSION).spaces(1) ; } diff --git a/idea/testData/formatter/CommentInFunctionLiteral.kt b/idea/testData/formatter/CommentInFunctionLiteral.kt new file mode 100644 index 00000000000..0e29a6295a7 --- /dev/null +++ b/idea/testData/formatter/CommentInFunctionLiteral.kt @@ -0,0 +1,9 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() { + test() { + // Some comment + it + } +} \ No newline at end of file diff --git a/idea/testData/formatter/CommentInFunctionLiteral_after.kt b/idea/testData/formatter/CommentInFunctionLiteral_after.kt new file mode 100644 index 00000000000..796410a85a9 --- /dev/null +++ b/idea/testData/formatter/CommentInFunctionLiteral_after.kt @@ -0,0 +1,9 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() { + test() { + // Some comment + it + } +} \ No newline at end of file diff --git a/idea/testData/formatter/MultilineFunctionLiteral.kt b/idea/testData/formatter/MultilineFunctionLiteral.kt new file mode 100644 index 00000000000..80e599db7fb --- /dev/null +++ b/idea/testData/formatter/MultilineFunctionLiteral.kt @@ -0,0 +1,4 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() = test() {if (true) { 0 } else { 1 }} \ No newline at end of file diff --git a/idea/testData/formatter/MultilineFunctionLiteralWithParams.kt b/idea/testData/formatter/MultilineFunctionLiteralWithParams.kt new file mode 100644 index 00000000000..ee7097a0902 --- /dev/null +++ b/idea/testData/formatter/MultilineFunctionLiteralWithParams.kt @@ -0,0 +1,4 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() = test() {a -> if (true) { a } else { 1 }} \ No newline at end of file diff --git a/idea/testData/formatter/MultilineFunctionLiteralWithParams_after.kt b/idea/testData/formatter/MultilineFunctionLiteralWithParams_after.kt new file mode 100644 index 00000000000..371b8dd2d9b --- /dev/null +++ b/idea/testData/formatter/MultilineFunctionLiteralWithParams_after.kt @@ -0,0 +1,10 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() = test() { a -> + if (true) { + a + } else { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/formatter/MultilineFunctionLiteral_after.kt b/idea/testData/formatter/MultilineFunctionLiteral_after.kt new file mode 100644 index 00000000000..362ea536eb7 --- /dev/null +++ b/idea/testData/formatter/MultilineFunctionLiteral_after.kt @@ -0,0 +1,10 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() = test() { + if (true) { + 0 + } else { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/formatter/SingleLineFunctionLiteral.kt b/idea/testData/formatter/SingleLineFunctionLiteral.kt new file mode 100644 index 00000000000..382ad9c2781 --- /dev/null +++ b/idea/testData/formatter/SingleLineFunctionLiteral.kt @@ -0,0 +1,4 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() = test() { it } \ No newline at end of file diff --git a/idea/testData/formatter/SingleLineFunctionLiteral_after.kt b/idea/testData/formatter/SingleLineFunctionLiteral_after.kt new file mode 100644 index 00000000000..382ad9c2781 --- /dev/null +++ b/idea/testData/formatter/SingleLineFunctionLiteral_after.kt @@ -0,0 +1,4 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() = test() { it } \ No newline at end of file diff --git a/idea/testData/formatter/SpaceBeforeFunctionLiteral.kt b/idea/testData/formatter/SpaceBeforeFunctionLiteral.kt new file mode 100644 index 00000000000..e36d28799af --- /dev/null +++ b/idea/testData/formatter/SpaceBeforeFunctionLiteral.kt @@ -0,0 +1,6 @@ +fun test(some : (Int) -> Int) { +} + +fun foo() { + test() { it } +} \ No newline at end of file diff --git a/idea/testData/formatter/SpaceBeforeFunctionLiteral_after.kt b/idea/testData/formatter/SpaceBeforeFunctionLiteral_after.kt new file mode 100644 index 00000000000..b92a1c6c945 --- /dev/null +++ b/idea/testData/formatter/SpaceBeforeFunctionLiteral_after.kt @@ -0,0 +1,6 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() { + test() { it } +} \ 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 10874a50d1d..0eb7ce5170f 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java @@ -18,7 +18,6 @@ package org.jetbrains.jet.formatter; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; -import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings; /** * Based on com.intellij.psi.formatter.java.JavaFormatterTest @@ -33,6 +32,10 @@ public class JetFormatterTest extends AbstractJetFormatterTest { doTest(); } + public void testCommentInFunctionLiteral() throws Exception { + doTest(); + } + public void testConsecutiveCalls() throws Exception { doTest(); } @@ -65,6 +68,14 @@ public class JetFormatterTest extends AbstractJetFormatterTest { doTest(); } + public void testMultilineFunctionLiteral() throws Exception { + doTest(); + } + + public void testMultilineFunctionLiteralWithParams() throws Exception { + doTest(); + } + public void testParameters() throws Exception { doTestWithInvert(); } @@ -77,10 +88,18 @@ public class JetFormatterTest extends AbstractJetFormatterTest { doTestWithInvert(); } + public void testSingleLineFunctionLiteral() throws Exception { + doTest(); + } + public void testSpaceAroundExtendColon() throws Exception { doTestWithInvert(); } + public void testSpaceBeforeFunctionLiteral() throws Exception { + doTest(); + } + public void testSpacesAroundOperations() throws Exception { doTestWithInvert(); } @@ -97,10 +116,6 @@ public class JetFormatterTest extends AbstractJetFormatterTest { doTest(); } - public static JetCodeStyleSettings getJetSettings() { - return getSettings().getCustomSettings(JetCodeStyleSettings.class); - } - public static CodeStyleSettings getSettings() { return CodeStyleSettingsManager.getSettings(getProject()); }