diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetCodeStyleSettings.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetCodeStyleSettings.java index b5a2d2d40c6..a5ddd3afe90 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetCodeStyleSettings.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetCodeStyleSettings.java @@ -34,6 +34,11 @@ public class JetCodeStyleSettings extends CustomCodeStyleSettings { public boolean INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = true; public boolean ALIGN_IN_COLUMNS_CASE_BRANCH = false; + public boolean SPACE_AROUND_FUNCTION_TYPE_ARROW = true; + + public boolean SPACE_AROUND_WHEN_ARROW = true; + public boolean SPACE_BEFORE_LAMBDA_ARROW = true; + public static JetCodeStyleSettings getInstance(Project project) { return CodeStyleSettingsManager.getSettings(project).getCustomSettings(JetCodeStyleSettings.class); } diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java index 6db9356dbb8..aa740e1bbb0 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java @@ -36,11 +36,11 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder { public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(JetLanguage.INSTANCE); JetBlock block = new JetBlock( - containingFile.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings, - createSpacingBuilder(settings)); + containingFile.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings, + createSpacingBuilder(settings)); return FormattingModelProvider.createFormattingModelForPsiFile( - element.getContainingFile(), block, settings); + element.getContainingFile(), block, settings); } private static SpacingBuilder createSpacingBuilder(CodeStyleSettings settings) { @@ -106,7 +106,15 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder { .afterInside(COLON, TYPE_PARAMETER).spaceIf(jetSettings.SPACE_AFTER_EXTEND_COLON) .between(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL_EXPRESSION).spaces(1) - .aroundInside(ARROW, WHEN_ENTRY).spaces(1) + .beforeInside(ARROW, FUNCTION_LITERAL).spaceIf(jetSettings.SPACE_BEFORE_LAMBDA_ARROW) + + //when + .aroundInside(ARROW, WHEN_ENTRY).spaceIf(jetSettings.SPACE_AROUND_WHEN_ARROW) + .beforeInside(LBRACE, WHEN).spacing(1, 1, 0, true, 0) //omit blank lines before '{' in 'when' statement + + .aroundInside(ARROW, FUNCTION_TYPE).spaceIf(jetSettings.SPACE_AROUND_FUNCTION_TYPE_ARROW) + + .betweenInside(REFERENCE_EXPRESSION, FUNCTION_LITERAL_EXPRESSION, CALL_EXPRESSION).spaces(1) ; } diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java index 596a6d530cb..22d9ad23575 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java @@ -57,17 +57,25 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti "}"; default: return - "open class Some {\n" + - " private val f = {(a: Int)->a*2}\n" + - " fun foo() : Int {\n" + - " val test : Int = 12\n" + - " return test\n" + - " }\n" + - " private fun foo2():Int where T : List {\n" + - " return 0\n" + - " }\n" + - "}\n\n" + - "class AnotherClass: Some"; + "open class Some {\n"+ + " private val f: (Int)->Int = { (a: Int) -> a * 2 }\n"+ + " fun foo(): Int {\n"+ + " val test: Int = 12\n"+ + " for (i in 10..42) {\n"+ + " println (when {\n"+ + " i < test -> -1\n"+ + " i > test -> 1\n"+ + " else -> 0\n"+ + " })\n"+ + " }\n"+ + " return test\n"+ + " }\n"+ + " private fun foo2(): Int where T : List {\n"+ + " return 0\n"+ + " }\n"+ + "}\n"+ + "class AnotherClass : Some()\n"; + } } @@ -115,6 +123,18 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti "Insert whitespaces in simple one line methods", CodeStyleSettingsCustomizable.SPACES_OTHER); + consumer.showCustomOption(JetCodeStyleSettings.class, "SPACE_AROUND_FUNCTION_TYPE_ARROW", + "Surround arrow in function types with spaces", + CodeStyleSettingsCustomizable.SPACES_OTHER); + + consumer.showCustomOption(JetCodeStyleSettings.class, "SPACE_AROUND_WHEN_ARROW", + "Surround arrow in \"when\" clause with spaces", + CodeStyleSettingsCustomizable.SPACES_OTHER); + + consumer.showCustomOption(JetCodeStyleSettings.class, "SPACE_BEFORE_LAMBDA_ARROW", + "Before lambda arrow", + CodeStyleSettingsCustomizable.SPACES_OTHER); + break; case WRAPPING_AND_BRACES_SETTINGS: consumer.showStandardOptions( diff --git a/idea/testData/formatter/FunctionalType.kt b/idea/testData/formatter/FunctionalType.kt new file mode 100644 index 00000000000..184133106fd --- /dev/null +++ b/idea/testData/formatter/FunctionalType.kt @@ -0,0 +1,4 @@ +fun test(some: ((Int)->Int) ->Int) { +} + +// SET_TRUE: SPACE_AROUND_FUNCTION_TYPE_ARROW \ No newline at end of file diff --git a/idea/testData/formatter/FunctionalType_after.kt b/idea/testData/formatter/FunctionalType_after.kt new file mode 100644 index 00000000000..8b45a18efd8 --- /dev/null +++ b/idea/testData/formatter/FunctionalType_after.kt @@ -0,0 +1,4 @@ +fun test(some: ((Int) -> Int) -> Int) { +} + +// SET_TRUE: SPACE_AROUND_FUNCTION_TYPE_ARROW \ No newline at end of file diff --git a/idea/testData/formatter/FunctionalType_after_inv.kt b/idea/testData/formatter/FunctionalType_after_inv.kt new file mode 100644 index 00000000000..8bbf78cd2bf --- /dev/null +++ b/idea/testData/formatter/FunctionalType_after_inv.kt @@ -0,0 +1,4 @@ +fun test(some: ((Int)->Int)->Int) { +} + +// SET_TRUE: SPACE_AROUND_FUNCTION_TYPE_ARROW \ No newline at end of file diff --git a/idea/testData/formatter/LambdaArrow.kt b/idea/testData/formatter/LambdaArrow.kt new file mode 100644 index 00000000000..ccaae3ddf93 --- /dev/null +++ b/idea/testData/formatter/LambdaArrow.kt @@ -0,0 +1,3 @@ +val f: (Int, Int, Int) -> Int = { x, y, z -> x + y + z } + +// SET_FALSE: SPACE_BEFORE_LAMBDA_ARROW \ No newline at end of file diff --git a/idea/testData/formatter/LambdaArrow_after.kt b/idea/testData/formatter/LambdaArrow_after.kt new file mode 100644 index 00000000000..60fd61f3099 --- /dev/null +++ b/idea/testData/formatter/LambdaArrow_after.kt @@ -0,0 +1,3 @@ +val f: (Int, Int, Int) -> Int = { x, y, z-> x + y + z } + +// SET_FALSE: SPACE_BEFORE_LAMBDA_ARROW \ No newline at end of file diff --git a/idea/testData/formatter/LambdaArrow_after_inv.kt b/idea/testData/formatter/LambdaArrow_after_inv.kt new file mode 100644 index 00000000000..c9a3caca990 --- /dev/null +++ b/idea/testData/formatter/LambdaArrow_after_inv.kt @@ -0,0 +1,3 @@ +val f: (Int, Int, Int) -> Int = { x, y, z -> x + y + z } + +// SET_FALSE: SPACE_BEFORE_LAMBDA_ARROW \ No newline at end of file diff --git a/idea/testData/formatter/ReferenceExpressionFunctionLiteral.kt b/idea/testData/formatter/ReferenceExpressionFunctionLiteral.kt new file mode 100644 index 00000000000..226ba86f1e4 --- /dev/null +++ b/idea/testData/formatter/ReferenceExpressionFunctionLiteral.kt @@ -0,0 +1,3 @@ +fun f() { + array(1, 2, 3).map{ v -> v } +} \ No newline at end of file diff --git a/idea/testData/formatter/ReferenceExpressionFunctionLiteral_after.kt b/idea/testData/formatter/ReferenceExpressionFunctionLiteral_after.kt new file mode 100644 index 00000000000..6cc8094939c --- /dev/null +++ b/idea/testData/formatter/ReferenceExpressionFunctionLiteral_after.kt @@ -0,0 +1,3 @@ +fun f() { + array(1, 2, 3).map { v -> v } +} \ No newline at end of file diff --git a/idea/testData/formatter/WhenArrow.kt b/idea/testData/formatter/WhenArrow.kt new file mode 100644 index 00000000000..2db856359b1 --- /dev/null +++ b/idea/testData/formatter/WhenArrow.kt @@ -0,0 +1,8 @@ +fun f(x: Any): Int { + return when (x) { + is Int ->1 + else->0 + } +} + +// SET_FALSE: SPACE_AROUND_WHEN_ARROW \ No newline at end of file diff --git a/idea/testData/formatter/WhenArrow_after.kt b/idea/testData/formatter/WhenArrow_after.kt new file mode 100644 index 00000000000..405fc9833be --- /dev/null +++ b/idea/testData/formatter/WhenArrow_after.kt @@ -0,0 +1,8 @@ +fun f(x: Any): Int { + return when (x) { + is Int->1 + else->0 + } +} + +// SET_FALSE: SPACE_AROUND_WHEN_ARROW \ No newline at end of file diff --git a/idea/testData/formatter/WhenArrow_after_inv.kt b/idea/testData/formatter/WhenArrow_after_inv.kt new file mode 100644 index 00000000000..1a19bc413e7 --- /dev/null +++ b/idea/testData/formatter/WhenArrow_after_inv.kt @@ -0,0 +1,8 @@ +fun f(x: Any): Int { + return when (x) { + is Int -> 1 + else -> 0 + } +} + +// SET_FALSE: SPACE_AROUND_WHEN_ARROW \ No newline at end of file diff --git a/idea/testData/formatter/WhenLinesBeforeLbrace.kt b/idea/testData/formatter/WhenLinesBeforeLbrace.kt new file mode 100644 index 00000000000..f5da9262b2f --- /dev/null +++ b/idea/testData/formatter/WhenLinesBeforeLbrace.kt @@ -0,0 +1,17 @@ +fun f(x: Any): Int { + return when (x) + + + + + + + + + + + { + is Int->1 + else->0 + } +} \ No newline at end of file diff --git a/idea/testData/formatter/WhenLinesBeforeLbrace_after.kt b/idea/testData/formatter/WhenLinesBeforeLbrace_after.kt new file mode 100644 index 00000000000..1d9ad0a982d --- /dev/null +++ b/idea/testData/formatter/WhenLinesBeforeLbrace_after.kt @@ -0,0 +1,7 @@ +fun f(x: Any): Int { + return when (x) + { + is Int -> 1 + else -> 0 + } +} \ 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 dba2cdc827e..2cee0d234eb 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java @@ -131,6 +131,26 @@ public class JetFormatterTest extends AbstractJetFormatterTest { doTest(); } + public void testWhenArrow() throws Exception { + doTestWithInvert(); + } + + public void testWhenLinesBeforeLbrace() throws Exception { + doTest(); + } + + public void testFunctionalType() throws Exception { + doTestWithInvert(); + } + + public void testLambdaArrow() throws Exception { + doTestWithInvert(); + } + + public void testReferenceExpressionFunctionLiteral() throws Exception { + doTest(); + } + @Override public void doTest() throws Exception { String originalFileText = AbstractJetFormatterTest.loadFile(getTestName(false) + ".kt");