From 9c91acac74422eacb9c8898d2ce134280e381d84 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 21 Aug 2012 16:10:45 +0400 Subject: [PATCH] KT-2242 formatting problems: function literal passed outside the parentheses - option for spaces in single line function literal #KT-2242 Fixed --- .../jetbrains/jet/plugin/formatter/JetBlock.java | 14 +++++++++++--- .../jet/plugin/formatter/JetCodeStyleSettings.java | 2 ++ .../JetLanguageCodeStyleSettingsProvider.java | 7 +++++++ .../MultilineFunctionLiteralWithParams.kt | 4 +++- .../MultilineFunctionLiteralWithParams_after.kt | 4 +++- ...MultilineFunctionLiteralWithParams_after_inv.kt | 12 ++++++++++++ .../formatter/SingleLineFunctionLiteral.kt | 4 +++- .../formatter/SingleLineFunctionLiteral_after.kt | 4 +++- .../SingleLineFunctionLiteral_after_inv.kt | 6 ++++++ .../jetbrains/jet/formatter/JetFormatterTest.java | 4 ++-- 10 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 idea/testData/formatter/MultilineFunctionLiteralWithParams_after_inv.kt create mode 100644 idea/testData/formatter/SingleLineFunctionLiteral_after_inv.kt diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java index cfd916d874e..66da68db094 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java @@ -143,8 +143,13 @@ public class JetBlock extends AbstractBlock { IElementType child1Type = ((ASTBlock) child1).getNode().getElementType(); IElementType child2Type = ((ASTBlock) child2).getNode().getElementType(); + JetCodeStyleSettings jetSettings = mySettings.getCustomSettings(JetCodeStyleSettings.class); + int spacesInSimpleMethod = jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD ? 1 : 0; + 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); + return Spacing.createDependentLFSpacing( + spacesInSimpleMethod, spacesInSimpleMethod, this.getTextRange(), + mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); } if (parentType == FUNCTION_LITERAL && child1Type == ARROW && child2Type == BLOCK) { @@ -152,11 +157,14 @@ public class JetBlock extends AbstractBlock { } if (parentType == FUNCTION_LITERAL && child2Type == RBRACE) { - return Spacing.createDependentLFSpacing(1, 1, this.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); + return Spacing.createDependentLFSpacing( + spacesInSimpleMethod, spacesInSimpleMethod, 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 Spacing.createSpacing(spacesInSimpleMethod, spacesInSimpleMethod, 0, + mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); } return null; diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetCodeStyleSettings.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetCodeStyleSettings.java index 1ce4399d9b9..6a80863b6cb 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetCodeStyleSettings.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetCodeStyleSettings.java @@ -31,6 +31,8 @@ public class JetCodeStyleSettings extends CustomCodeStyleSettings { public boolean SPACE_BEFORE_EXTEND_COLON = false; public boolean SPACE_AFTER_EXTEND_COLON = true; + public boolean INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = true; + public static JetCodeStyleSettings getInstance(Project project) { return CodeStyleSettingsManager.getSettings(project).getCustomSettings(JetCodeStyleSettings.class); } diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java index 7ead5318fc8..a98909d73b5 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java @@ -52,10 +52,12 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti " 14\n" + " )\n" + " }\n" + + " private val f = {(a: Int)->a*2}\n" + "}"; default: return "class Some {\n" + + " private val f = {(a: Int)->a*2}\n" + " fun foo() : Int {\n" + " val test : Int = 12\n" + " return test\n" + @@ -104,6 +106,11 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti consumer.showCustomOption(JetCodeStyleSettings.class, "SPACE_BEFORE_EXTEND_COLON", "Space before colon in new type definition", CodeStyleSettingsCustomizable.SPACES_OTHER); + + consumer.showCustomOption(JetCodeStyleSettings.class, "INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD", + "Insert whitespaces in simple one line methods", + CodeStyleSettingsCustomizable.SPACES_OTHER); + break; case WRAPPING_AND_BRACES_SETTINGS: consumer.showStandardOptions( diff --git a/idea/testData/formatter/MultilineFunctionLiteralWithParams.kt b/idea/testData/formatter/MultilineFunctionLiteralWithParams.kt index ee7097a0902..b25c766d443 100644 --- a/idea/testData/formatter/MultilineFunctionLiteralWithParams.kt +++ b/idea/testData/formatter/MultilineFunctionLiteralWithParams.kt @@ -1,4 +1,6 @@ fun test(some: (Int) -> Int) { } -fun foo() = test() {a -> if (true) { a } else { 1 }} \ No newline at end of file +fun foo() = test() {a -> if (true) { a } else { 1 }} + +// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD \ No newline at end of file diff --git a/idea/testData/formatter/MultilineFunctionLiteralWithParams_after.kt b/idea/testData/formatter/MultilineFunctionLiteralWithParams_after.kt index 371b8dd2d9b..ee0fbfe85bc 100644 --- a/idea/testData/formatter/MultilineFunctionLiteralWithParams_after.kt +++ b/idea/testData/formatter/MultilineFunctionLiteralWithParams_after.kt @@ -7,4 +7,6 @@ fun foo() = test() { a -> } else { 1 } -} \ No newline at end of file +} + +// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD \ No newline at end of file diff --git a/idea/testData/formatter/MultilineFunctionLiteralWithParams_after_inv.kt b/idea/testData/formatter/MultilineFunctionLiteralWithParams_after_inv.kt new file mode 100644 index 00000000000..16bb6bb72d6 --- /dev/null +++ b/idea/testData/formatter/MultilineFunctionLiteralWithParams_after_inv.kt @@ -0,0 +1,12 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() = test() {a -> + if (true) { + a + } else { + 1 + } +} + +// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD \ No newline at end of file diff --git a/idea/testData/formatter/SingleLineFunctionLiteral.kt b/idea/testData/formatter/SingleLineFunctionLiteral.kt index 382ad9c2781..07a52f68fa3 100644 --- a/idea/testData/formatter/SingleLineFunctionLiteral.kt +++ b/idea/testData/formatter/SingleLineFunctionLiteral.kt @@ -1,4 +1,6 @@ fun test(some: (Int) -> Int) { } -fun foo() = test() { it } \ No newline at end of file +fun foo() = test() { it } + +// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD \ No newline at end of file diff --git a/idea/testData/formatter/SingleLineFunctionLiteral_after.kt b/idea/testData/formatter/SingleLineFunctionLiteral_after.kt index 382ad9c2781..07a52f68fa3 100644 --- a/idea/testData/formatter/SingleLineFunctionLiteral_after.kt +++ b/idea/testData/formatter/SingleLineFunctionLiteral_after.kt @@ -1,4 +1,6 @@ fun test(some: (Int) -> Int) { } -fun foo() = test() { it } \ No newline at end of file +fun foo() = test() { it } + +// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD \ No newline at end of file diff --git a/idea/testData/formatter/SingleLineFunctionLiteral_after_inv.kt b/idea/testData/formatter/SingleLineFunctionLiteral_after_inv.kt new file mode 100644 index 00000000000..55751353d14 --- /dev/null +++ b/idea/testData/formatter/SingleLineFunctionLiteral_after_inv.kt @@ -0,0 +1,6 @@ +fun test(some: (Int) -> Int) { +} + +fun foo() = test() {it} + +// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD \ 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 987e9654ee3..5da03038273 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java @@ -73,7 +73,7 @@ public class JetFormatterTest extends AbstractJetFormatterTest { } public void testMultilineFunctionLiteralWithParams() throws Exception { - doTest(); + doTestWithInvert(); } public void testParameters() throws Exception { @@ -93,7 +93,7 @@ public class JetFormatterTest extends AbstractJetFormatterTest { } public void testSingleLineFunctionLiteral() throws Exception { - doTest(); + doTestWithInvert(); } public void testSpaceAroundExtendColon() throws Exception {