From 8f2c543f7eb247be5a54231b8d3222dffac018a5 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 26 Nov 2014 17:42:55 +0300 Subject: [PATCH] Formatter: Add wrapping support for parameter/argument lists #KT-6032 Fixed --- .../jet/plugin/formatter/JetBlock.java | 44 ++++++++++++-- .../JetLanguageCodeStyleSettingsProvider.java | 13 ++-- .../jet/plugin/formatter/WrappingStrategy.kt | 30 ++++++++++ .../ArgumentListChopAsNeeded.after.kt | 21 +++++++ .../parameterList/ArgumentListChopAsNeeded.kt | 17 ++++++ .../ArgumentListDoNotWrap.after.kt | 17 ++++++ .../parameterList/ArgumentListDoNotWrap.kt | 17 ++++++ .../ArgumentListWrapAlways.after.kt | 21 +++++++ .../parameterList/ArgumentListWrapAlways.kt | 17 ++++++ .../ArgumentListWrapAsNeeded.after.kt | 21 +++++++ .../parameterList/ArgumentListWrapAsNeeded.kt | 17 ++++++ .../ParameterListChopAsNeeded.after.kt | 36 +++++++++++ .../ParameterListChopAsNeeded.kt | 33 +++++++++++ .../ParameterListDoNotWrap.after.kt | 33 +++++++++++ .../parameterList/ParameterListDoNotWrap.kt | 33 +++++++++++ .../ParameterListWrapAlways.after.kt | 37 ++++++++++++ .../parameterList/ParameterListWrapAlways.kt | 33 +++++++++++ .../ParameterListWrapAsNeeded.after.kt | 38 ++++++++++++ .../ParameterListWrapAsNeeded.kt | 33 +++++++++++ .../formatter/AbstractJetFormatterTest.java | 14 ++++- .../formatter/JetFormatterTestGenerated.java | 59 ++++++++++++++++++- .../jet/testing/SettingsConfigurator.java | 49 +++++++++++++-- 22 files changed, 612 insertions(+), 21 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/formatter/WrappingStrategy.kt create mode 100644 idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.after.kt create mode 100644 idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.kt create mode 100644 idea/testData/formatter/parameterList/ArgumentListDoNotWrap.after.kt create mode 100644 idea/testData/formatter/parameterList/ArgumentListDoNotWrap.kt create mode 100644 idea/testData/formatter/parameterList/ArgumentListWrapAlways.after.kt create mode 100644 idea/testData/formatter/parameterList/ArgumentListWrapAlways.kt create mode 100644 idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.after.kt create mode 100644 idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.kt create mode 100644 idea/testData/formatter/parameterList/ParameterListChopAsNeeded.after.kt create mode 100644 idea/testData/formatter/parameterList/ParameterListChopAsNeeded.kt create mode 100644 idea/testData/formatter/parameterList/ParameterListDoNotWrap.after.kt create mode 100644 idea/testData/formatter/parameterList/ParameterListDoNotWrap.kt create mode 100644 idea/testData/formatter/parameterList/ParameterListWrapAlways.after.kt create mode 100644 idea/testData/formatter/parameterList/ParameterListWrapAlways.kt create mode 100644 idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.after.kt create mode 100644 idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.kt diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java index 1e61cfee504..b18716c0cc4 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java @@ -120,6 +120,7 @@ public class JetBlock extends AbstractBlock { List blocks = new ArrayList(); NodeAlignmentStrategy childrenAlignmentStrategy = getChildrenAlignmentStrategy(); + WrappingStrategy wrappingStrategy = getWrappingStrategy(); for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) { IElementType childType = child.getElementType(); @@ -130,23 +131,28 @@ public class JetBlock extends AbstractBlock { continue; } - blocks.add(buildSubBlock(child, childrenAlignmentStrategy)); + blocks.add(buildSubBlock(child, childrenAlignmentStrategy, wrappingStrategy)); } return Collections.unmodifiableList(blocks); } @NotNull - private Block buildSubBlock(@NotNull ASTNode child, NodeAlignmentStrategy alignmentStrategy) { + private Block buildSubBlock( + @NotNull ASTNode child, + NodeAlignmentStrategy alignmentStrategy, + @NotNull WrappingStrategy wrappingStrategy) { + Wrap wrap = wrappingStrategy.getWrap(child.getElementType()); + // Skip one sub-level for operators, so type of block node is an element type of operator if (child.getElementType() == OPERATION_REFERENCE) { ASTNode operationNode = child.getFirstChildNode(); if (operationNode != null) { - return new JetBlock(operationNode, alignmentStrategy, createChildIndent(child), null, mySettings, mySpacingBuilder); + return new JetBlock(operationNode, alignmentStrategy, createChildIndent(child), wrap, mySettings, mySpacingBuilder); } } - return new JetBlock(child, alignmentStrategy, createChildIndent(child), null, mySettings, mySpacingBuilder); + return new JetBlock(child, alignmentStrategy, createChildIndent(child), wrap, mySettings, mySpacingBuilder); } private static ASTNode getPrevWithoutWhitespace(ASTNode node) { @@ -225,6 +231,36 @@ public class JetBlock extends AbstractBlock { return myNode.getFirstChildNode() == null; } + @NotNull + private static WrappingStrategy getWrappingStrategyForItemList(int wrapType, @NotNull final IElementType itemType) { + final Wrap itemWrap = Wrap.createWrap(wrapType, false); + return new WrappingStrategy() { + @Nullable + @Override + public Wrap getWrap(@NotNull IElementType childElementType) { + return childElementType == itemType ? itemWrap : null; + } + }; + } + + @NotNull + private WrappingStrategy getWrappingStrategy() { + CommonCodeStyleSettings commonSettings = mySettings.getCommonSettings(JetLanguage.INSTANCE); + IElementType elementType = myNode.getElementType(); + + if (elementType == VALUE_ARGUMENT_LIST) { + return getWrappingStrategyForItemList(commonSettings.CALL_PARAMETERS_WRAP, VALUE_ARGUMENT); + } + if (elementType == VALUE_PARAMETER_LIST) { + IElementType parentElementType = myNode.getTreeParent().getElementType(); + if (parentElementType == FUN || parentElementType == CLASS) { + return getWrappingStrategyForItemList(commonSettings.METHOD_PARAMETERS_WRAP, VALUE_PARAMETER); + } + } + + return WrappingStrategy.NoWrapping.INSTANCE$; + } + private NodeAlignmentStrategy getChildrenAlignmentStrategy() { final CommonCodeStyleSettings jetCommonSettings = mySettings.getCommonSettings(JetLanguage.INSTANCE); JetCodeStyleSettings jetSettings = mySettings.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 6a7967be625..3032980d169 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java @@ -38,9 +38,7 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti case WRAPPING_AND_BRACES_SETTINGS: return "public class ThisIsASampleClass {\n" + - " private fun foo1(i1: Int,\n" + - " i2: Int,\n" + - " i3: Int) : Int {\n" + + " fun foo1(i1: Int, i2: Int, i3: Int) : Int {\n" + " when (i1) {\n" + " is Number -> 0\n" + " else -> 1\n" + @@ -49,10 +47,7 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti " }\n" + " private fun foo2():Int {\n" + " try {" + - " return foo1(12,\n" + - " 13,\n" + - " 14\n" + - " )\n" + + " return foo1(12, 13, 14)\n" + " }" + " catch (e: Exception) {" + " return 0" + @@ -160,7 +155,9 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti "ELSE_ON_NEW_LINE", "WHILE_ON_NEW_LINE", "CATCH_ON_NEW_LINE", - "FINALLY_ON_NEW_LINE" + "FINALLY_ON_NEW_LINE", + "CALL_PARAMETERS_WRAP", + "METHOD_PARAMETERS_WRAP" ); consumer.renameStandardOption(CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT, "'when' statements"); consumer.showCustomOption(JetCodeStyleSettings.class, "ALIGN_IN_COLUMNS_CASE_BRANCH", "Align in columns 'case' branches", diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/WrappingStrategy.kt b/idea/src/org/jetbrains/jet/plugin/formatter/WrappingStrategy.kt new file mode 100644 index 00000000000..6b3f3d0c639 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/formatter/WrappingStrategy.kt @@ -0,0 +1,30 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.formatter + +import com.intellij.formatting.Wrap +import com.intellij.lang.ASTNode +import com.intellij.psi.tree.IElementType +import com.intellij.formatting.WrapType + +public trait WrappingStrategy { + fun getWrap(childElementType: IElementType): Wrap? + + object NoWrapping: WrappingStrategy { + override fun getWrap(childElementType: IElementType): Wrap? = null + } +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.after.kt b/idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.after.kt new file mode 100644 index 00000000000..57213a05f53 --- /dev/null +++ b/idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.after.kt @@ -0,0 +1,21 @@ +// SET_INT: CALL_PARAMETERS_WRAP = 4 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS +// RIGHT_MARGIN: 30 + +fun foo() { + testtest() + + testtesttesttesttesttesttesttesttest() + + testtest(foofoo) + + testtesttesttest( + foofoofoofoofoofoofoofoofoofoofoofoo) + + testtest(foobar, barfoo) + + testtesttesttest(foofoo, + barbar, + foobar, + barfoo) +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.kt b/idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.kt new file mode 100644 index 00000000000..39741e0072e --- /dev/null +++ b/idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.kt @@ -0,0 +1,17 @@ +// SET_INT: CALL_PARAMETERS_WRAP = 4 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS +// RIGHT_MARGIN: 30 + +fun foo() { + testtest() + + testtesttesttesttesttesttesttesttest() + + testtest(foofoo) + + testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo) + + testtest(foobar, barfoo) + + testtesttesttest(foofoo, barbar, foobar, barfoo) +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ArgumentListDoNotWrap.after.kt b/idea/testData/formatter/parameterList/ArgumentListDoNotWrap.after.kt new file mode 100644 index 00000000000..8847df417a7 --- /dev/null +++ b/idea/testData/formatter/parameterList/ArgumentListDoNotWrap.after.kt @@ -0,0 +1,17 @@ +// SET_INT: CALL_PARAMETERS_WRAP = 0 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS +// RIGHT_MARGIN: 30 + +fun foo() { + testtest() + + testtesttesttesttesttesttesttesttest() + + testtest(foofoo) + + testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo) + + testtest(foobar, barfoo) + + testtesttesttest(foofoo, barbar, foobar, barfoo) +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ArgumentListDoNotWrap.kt b/idea/testData/formatter/parameterList/ArgumentListDoNotWrap.kt new file mode 100644 index 00000000000..8847df417a7 --- /dev/null +++ b/idea/testData/formatter/parameterList/ArgumentListDoNotWrap.kt @@ -0,0 +1,17 @@ +// SET_INT: CALL_PARAMETERS_WRAP = 0 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS +// RIGHT_MARGIN: 30 + +fun foo() { + testtest() + + testtesttesttesttesttesttesttesttest() + + testtest(foofoo) + + testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo) + + testtest(foobar, barfoo) + + testtesttesttest(foofoo, barbar, foobar, barfoo) +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ArgumentListWrapAlways.after.kt b/idea/testData/formatter/parameterList/ArgumentListWrapAlways.after.kt new file mode 100644 index 00000000000..5d5a1d8d0c2 --- /dev/null +++ b/idea/testData/formatter/parameterList/ArgumentListWrapAlways.after.kt @@ -0,0 +1,21 @@ +// SET_INT: CALL_PARAMETERS_WRAP = 2 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS +// RIGHT_MARGIN: 30 + +fun foo() { + testtest() + + testtesttesttesttesttesttesttesttest() + + testtest(foofoo) + + testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo) + + testtest(foobar, + barfoo) + + testtesttesttest(foofoo, + barbar, + foobar, + barfoo) +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ArgumentListWrapAlways.kt b/idea/testData/formatter/parameterList/ArgumentListWrapAlways.kt new file mode 100644 index 00000000000..ec597d99e8d --- /dev/null +++ b/idea/testData/formatter/parameterList/ArgumentListWrapAlways.kt @@ -0,0 +1,17 @@ +// SET_INT: CALL_PARAMETERS_WRAP = 2 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS +// RIGHT_MARGIN: 30 + +fun foo() { + testtest() + + testtesttesttesttesttesttesttesttest() + + testtest(foofoo) + + testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo) + + testtest(foobar, barfoo) + + testtesttesttest(foofoo, barbar, foobar, barfoo) +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.after.kt b/idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.after.kt new file mode 100644 index 00000000000..41dbfd09778 --- /dev/null +++ b/idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.after.kt @@ -0,0 +1,21 @@ +// SET_INT: CALL_PARAMETERS_WRAP = 1 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS +// RIGHT_MARGIN: 30 + +fun foo() { + testtest() + + testtesttesttesttesttesttesttesttest() + + testtest(foofoo) + + testtesttesttest( + foofoofoofoofoofoofoofoofoofoofoofoo) + + testtest(foobar, barfoo) + + testtesttesttest(foofoo, + barbar, + foobar, + barfoo) +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.kt b/idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.kt new file mode 100644 index 00000000000..3dfd682618a --- /dev/null +++ b/idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.kt @@ -0,0 +1,17 @@ +// SET_INT: CALL_PARAMETERS_WRAP = 1 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS +// RIGHT_MARGIN: 30 + +fun foo() { + testtest() + + testtesttesttesttesttesttesttesttest() + + testtest(foofoo) + + testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo) + + testtest(foobar, barfoo) + + testtesttesttest(foofoo, barbar, foobar, barfoo) +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ParameterListChopAsNeeded.after.kt b/idea/testData/formatter/parameterList/ParameterListChopAsNeeded.after.kt new file mode 100644 index 00000000000..6bbc558a19c --- /dev/null +++ b/idea/testData/formatter/parameterList/ParameterListChopAsNeeded.after.kt @@ -0,0 +1,36 @@ +// SET_INT: METHOD_PARAMETERS_WRAP = 4 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS +// RIGHT_MARGIN: 30 + +fun testtest() + +fun testtesttesttesttesttesttesttesttest() + +fun testtest(foofoo: Int) + +fun testtesttesttest(foofoo: Int) + +fun test(foo: Int, bar: Int) + +fun testtesttesttest(foofoo: Int, + barbar: Int, + foobar: Int, + barfoo: Int) + +fun test() { + for (foo: Int in bar) { + + } + + for (foofoofoofoofoofoo: Int in bar) { + + } + + try { + + } catch (bar: Exception) { + + } catch (barbarbarbarbar: Exception) { + + } +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ParameterListChopAsNeeded.kt b/idea/testData/formatter/parameterList/ParameterListChopAsNeeded.kt new file mode 100644 index 00000000000..324f038333c --- /dev/null +++ b/idea/testData/formatter/parameterList/ParameterListChopAsNeeded.kt @@ -0,0 +1,33 @@ +// SET_INT: METHOD_PARAMETERS_WRAP = 4 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS +// RIGHT_MARGIN: 30 + +fun testtest() + +fun testtesttesttesttesttesttesttesttest() + +fun testtest(foofoo: Int) + +fun testtesttesttest(foofoo: Int) + +fun test(foo: Int, bar: Int) + +fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int) + +fun test() { + for (foo: Int in bar) { + + } + + for (foofoofoofoofoofoo: Int in bar) { + + } + + try { + + } catch (bar: Exception) { + + } catch (barbarbarbarbar: Exception) { + + } +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ParameterListDoNotWrap.after.kt b/idea/testData/formatter/parameterList/ParameterListDoNotWrap.after.kt new file mode 100644 index 00000000000..77701928fd0 --- /dev/null +++ b/idea/testData/formatter/parameterList/ParameterListDoNotWrap.after.kt @@ -0,0 +1,33 @@ +// SET_INT: METHOD_PARAMETERS_WRAP = 0 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS +// RIGHT_MARGIN: 30 + +fun testtest() + +fun testtesttesttesttesttesttesttesttest() + +fun testtest(foofoo: Int) + +fun testtesttesttest(foofoo: Int) + +fun test(foo: Int, bar: Int) + +fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int) + +fun test() { + for (foo: Int in bar) { + + } + + for (foofoofoofoofoofoo: Int in bar) { + + } + + try { + + } catch (bar: Exception) { + + } catch (barbarbarbarbar: Exception) { + + } +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ParameterListDoNotWrap.kt b/idea/testData/formatter/parameterList/ParameterListDoNotWrap.kt new file mode 100644 index 00000000000..77701928fd0 --- /dev/null +++ b/idea/testData/formatter/parameterList/ParameterListDoNotWrap.kt @@ -0,0 +1,33 @@ +// SET_INT: METHOD_PARAMETERS_WRAP = 0 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS +// RIGHT_MARGIN: 30 + +fun testtest() + +fun testtesttesttesttesttesttesttesttest() + +fun testtest(foofoo: Int) + +fun testtesttesttest(foofoo: Int) + +fun test(foo: Int, bar: Int) + +fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int) + +fun test() { + for (foo: Int in bar) { + + } + + for (foofoofoofoofoofoo: Int in bar) { + + } + + try { + + } catch (bar: Exception) { + + } catch (barbarbarbarbar: Exception) { + + } +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ParameterListWrapAlways.after.kt b/idea/testData/formatter/parameterList/ParameterListWrapAlways.after.kt new file mode 100644 index 00000000000..9c99ff6e6bf --- /dev/null +++ b/idea/testData/formatter/parameterList/ParameterListWrapAlways.after.kt @@ -0,0 +1,37 @@ +// SET_INT: METHOD_PARAMETERS_WRAP = 2 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS +// RIGHT_MARGIN: 30 + +fun testtest() + +fun testtesttesttesttesttesttesttesttest() + +fun testtest(foofoo: Int) + +fun testtesttesttest(foofoo: Int) + +fun test(foo: Int, + bar: Int) + +fun testtesttesttest(foofoo: Int, + barbar: Int, + foobar: Int, + barfoo: Int) + +fun test() { + for (foo: Int in bar) { + + } + + for (foofoofoofoofoofoo: Int in bar) { + + } + + try { + + } catch (bar: Exception) { + + } catch (barbarbarbarbar: Exception) { + + } +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ParameterListWrapAlways.kt b/idea/testData/formatter/parameterList/ParameterListWrapAlways.kt new file mode 100644 index 00000000000..ce407b7a0e3 --- /dev/null +++ b/idea/testData/formatter/parameterList/ParameterListWrapAlways.kt @@ -0,0 +1,33 @@ +// SET_INT: METHOD_PARAMETERS_WRAP = 2 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS +// RIGHT_MARGIN: 30 + +fun testtest() + +fun testtesttesttesttesttesttesttesttest() + +fun testtest(foofoo: Int) + +fun testtesttesttest(foofoo: Int) + +fun test(foo: Int, bar: Int) + +fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int) + +fun test() { + for (foo: Int in bar) { + + } + + for (foofoofoofoofoofoo: Int in bar) { + + } + + try { + + } catch (bar: Exception) { + + } catch (barbarbarbarbar: Exception) { + + } +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.after.kt b/idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.after.kt new file mode 100644 index 00000000000..617eb6a19c6 --- /dev/null +++ b/idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.after.kt @@ -0,0 +1,38 @@ +// SET_INT: METHOD_PARAMETERS_WRAP = 1 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS +// RIGHT_MARGIN: 30 + +fun testtest() + +fun testtesttesttesttesttesttesttesttest() + +fun testtest(foofoo: Int) + +fun testtesttesttest( + foofoo: Int) + +fun test(foo: Int, bar: Int) + +fun testtesttesttest( + foofoo: Int, + barbar: Int, + foobar: Int, + barfoo: Int) + +fun test() { + for (foo: Int in bar) { + + } + + for (foofoofoofoofoofoo: Int in bar) { + + } + + try { + + } catch (bar: Exception) { + + } catch (barbarbarbarbar: Exception) { + + } +} \ No newline at end of file diff --git a/idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.kt b/idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.kt new file mode 100644 index 00000000000..21253e8bef4 --- /dev/null +++ b/idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.kt @@ -0,0 +1,33 @@ +// SET_INT: METHOD_PARAMETERS_WRAP = 1 +// SET_TRUE: ALIGN_MULTILINE_PARAMETERS +// RIGHT_MARGIN: 30 + +fun testtest() + +fun testtesttesttesttesttesttesttesttest() + +fun testtest(foofoo: Int) + +fun testtesttesttest(foofoo: Int) + +fun test(foo: Int, bar: Int) + +fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int) + +fun test() { + for (foo: Int in bar) { + + } + + for (foofoofoofoofoofoo: Int in bar) { + + } + + try { + + } catch (bar: Exception) { + + } catch (barbarbarbarbar: Exception) { + + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java b/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java index 5f8809b2ed6..c508575da35 100644 --- a/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java +++ b/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java @@ -27,11 +27,14 @@ import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.testFramework.LightIdeaTestCase; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.InTextDirectivesUtils; import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.plugin.JetLanguage; import org.jetbrains.jet.plugin.PluginTestCaseBase; import org.jetbrains.jet.testing.SettingsConfigurator; @@ -138,16 +141,23 @@ public abstract class AbstractJetFormatterTest extends LightIdeaTestCase { String testFileName = expectedFileNameWithExtension.substring(0, expectedFileNameWithExtension.indexOf(".")); String testFileExtension = expectedFileNameWithExtension.substring(expectedFileNameWithExtension.lastIndexOf(".")); String originalFileText = FileUtil.loadFile(new File(testFileName + testFileExtension), true); - SettingsConfigurator configurator = JetFormatSettingsUtil.createConfigurator(originalFileText, JetFormatSettingsUtil.getSettings()); + CodeStyleSettings codeStyleSettings = JetFormatSettingsUtil.getSettings(); + Integer rightMargin = InTextDirectivesUtils.getPrefixedInt(originalFileText, "// RIGHT_MARGIN: "); + if (rightMargin != null) { + codeStyleSettings.setRightMargin(JetLanguage.INSTANCE, rightMargin); + } + + SettingsConfigurator configurator = JetFormatSettingsUtil.createConfigurator(originalFileText, codeStyleSettings); if (!inverted) { configurator.configureSettings(); } else { configurator.configureInvertedSettings(); } + doTextTest(originalFileText, new File(expectedFileNameWithExtension), testFileExtension); - JetFormatSettingsUtil.getSettings().clearCodeStyleSettings(); + codeStyleSettings.clearCodeStyleSettings(); } } diff --git a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java index fcfe01255cf..52d4094a9d1 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java @@ -33,7 +33,7 @@ import java.util.regex.Pattern; public class JetFormatterTestGenerated extends AbstractJetFormatterTest { @TestMetadata("idea/testData/formatter") @TestDataPath("$PROJECT_ROOT") - @InnerTestClasses({Formatter.ModifierList.class}) + @InnerTestClasses({Formatter.ModifierList.class, Formatter.ParameterList.class}) @RunWith(JUnit3RunnerWithInners.class) public static class Formatter extends AbstractJetFormatterTest { public void testAllFilesPresentInFormatter() throws Exception { @@ -612,6 +612,63 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest { doTest(fileName); } } + + @TestMetadata("idea/testData/formatter/parameterList") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ParameterList extends AbstractJetFormatterTest { + public void testAllFilesPresentInParameterList() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/formatter/parameterList"), Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), true); + } + + @TestMetadata("ArgumentListChopAsNeeded.after.kt") + public void testArgumentListChopAsNeeded() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.after.kt"); + doTest(fileName); + } + + @TestMetadata("ArgumentListDoNotWrap.after.kt") + public void testArgumentListDoNotWrap() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ArgumentListDoNotWrap.after.kt"); + doTest(fileName); + } + + @TestMetadata("ArgumentListWrapAlways.after.kt") + public void testArgumentListWrapAlways() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ArgumentListWrapAlways.after.kt"); + doTest(fileName); + } + + @TestMetadata("ArgumentListWrapAsNeeded.after.kt") + public void testArgumentListWrapAsNeeded() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.after.kt"); + doTest(fileName); + } + + @TestMetadata("ParameterListChopAsNeeded.after.kt") + public void testParameterListChopAsNeeded() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ParameterListChopAsNeeded.after.kt"); + doTest(fileName); + } + + @TestMetadata("ParameterListDoNotWrap.after.kt") + public void testParameterListDoNotWrap() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ParameterListDoNotWrap.after.kt"); + doTest(fileName); + } + + @TestMetadata("ParameterListWrapAlways.after.kt") + public void testParameterListWrapAlways() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ParameterListWrapAlways.after.kt"); + doTest(fileName); + } + + @TestMetadata("ParameterListWrapAsNeeded.after.kt") + public void testParameterListWrapAsNeeded() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.after.kt"); + doTest(fileName); + } + } } @TestMetadata("idea/testData/formatter") diff --git a/idea/tests/org/jetbrains/jet/testing/SettingsConfigurator.java b/idea/tests/org/jetbrains/jet/testing/SettingsConfigurator.java index 3e93e95461c..c6fe739068f 100644 --- a/idea/tests/org/jetbrains/jet/testing/SettingsConfigurator.java +++ b/idea/tests/org/jetbrains/jet/testing/SettingsConfigurator.java @@ -16,7 +16,13 @@ package org.jetbrains.jet.testing; +import com.intellij.lang.Language; +import com.intellij.util.Function; +import com.intellij.util.containers.ContainerUtil; +import kotlin.Pair; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.InTextDirectivesUtils; +import org.jetbrains.jet.plugin.JetLanguage; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -26,20 +32,35 @@ import java.util.Arrays; public class SettingsConfigurator { public static final String SET_TRUE_DIRECTIVE = "SET_TRUE:"; public static final String SET_FALSE_DIRECTIVE = "SET_FALSE:"; + public static final String SET_INT_DIRECTIVE = "SET_INT:"; private final String[] settingsToTrue; private final String[] settingsToFalse; + private final Pair[] settingsToIntValue; private final Object[] objects; public SettingsConfigurator(String fileText, Object... objects) { settingsToTrue = InTextDirectivesUtils.findArrayWithPrefixes(fileText, SET_TRUE_DIRECTIVE); settingsToFalse = InTextDirectivesUtils.findArrayWithPrefixes(fileText, SET_FALSE_DIRECTIVE); + //noinspection unchecked + settingsToIntValue = ContainerUtil.map2Array( + InTextDirectivesUtils.findArrayWithPrefixes(fileText, SET_INT_DIRECTIVE), + Pair.class, + new Function() { + @SuppressWarnings("unchecked") + @Override + public Pair fun(String s) { + String[] tokens = s.split("="); + return new Pair(tokens[0].trim(), Integer.valueOf(tokens[1].trim())); + } + } + ); this.objects = objects; } - public static void setBooleanSetting(String settingName, boolean value, Object... objects) { + public static void setSettingValue(String settingName, Object value, Class valueType, Object... objects) { for (Object object : objects) { - if (setSettingWithField(settingName, object, value) || setSettingWithMethod(settingName, object, value)) { + if (setSettingWithField(settingName, object, value) || setSettingWithMethod(settingName, object, value, valueType)) { return; } } @@ -48,6 +69,14 @@ public class SettingsConfigurator { "There's no property or method with name '%s' in given objects: %s", settingName, Arrays.toString(objects))); } + public static void setBooleanSetting(String setting, Boolean value, Object... objects) { + setSettingValue(setting, value, boolean.class, objects); + } + + public static void setIntSetting(String setting, Integer value, Object... objects) { + setSettingValue(setting, value, int.class, objects); + } + public void configureSettings() { for (String trueSetting : settingsToTrue) { setBooleanSetting(trueSetting, true, objects); @@ -56,6 +85,10 @@ public class SettingsConfigurator { for (String falseSetting : settingsToFalse) { setBooleanSetting(falseSetting, false, objects); } + + for (Pair setting : settingsToIntValue) { + setIntSetting(setting.getFirst(), setting.getSecond(), objects); + } } public void configureInvertedSettings() { @@ -66,12 +99,16 @@ public class SettingsConfigurator { for (String falseSetting : settingsToFalse) { setBooleanSetting(falseSetting, true, objects); } + + for (Pair setting : settingsToIntValue) { + setIntSetting(setting.getFirst(), setting.getSecond(), objects); + } } - private static boolean setSettingWithField(String settingName, Object object, boolean value) { + private static boolean setSettingWithField(String settingName, Object object, Object value) { try { Field field = object.getClass().getDeclaredField(settingName); - field.setBoolean(object, value); + field.set(object, value); return true; } catch (IllegalAccessException e) { @@ -84,9 +121,9 @@ public class SettingsConfigurator { return false; } - private static boolean setSettingWithMethod(String setterName, Object object, boolean value) { + private static boolean setSettingWithMethod(String setterName, Object object, Object value, Class valueType) { try { - Method method = object.getClass().getMethod(setterName, boolean.class); + Method method = object.getClass().getMethod(setterName, valueType); method.invoke(object, value); return true; }