diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml
index d7a8bdafbff..b6d87385922 100644
--- a/.idea/codeStyleSettings.xml
+++ b/.idea/codeStyleSettings.xml
@@ -63,14 +63,57 @@
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -114,11 +157,13 @@
+
+
@@ -147,10 +192,31 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.idea/inspectionProfiles/idea_default.xml b/.idea/inspectionProfiles/idea_default.xml
index 4402d4e932c..87c20723c79 100644
--- a/.idea/inspectionProfiles/idea_default.xml
+++ b/.idea/inspectionProfiles/idea_default.xml
@@ -240,6 +240,10 @@
+
+
+
+
diff --git a/.idea/libraries/intellij_core.xml b/.idea/libraries/intellij_core.xml
index b5e58e5d23b..0d248c2a04e 100644
--- a/.idea/libraries/intellij_core.xml
+++ b/.idea/libraries/intellij_core.xml
@@ -57,6 +57,8 @@
+
+
diff --git a/.idea/runConfigurations/All_IDEA_Plugin_Tests.xml b/.idea/runConfigurations/All_IDEA_Plugin_Tests.xml
index 27790fe471e..56190dfe090 100644
--- a/.idea/runConfigurations/All_IDEA_Plugin_Tests.xml
+++ b/.idea/runConfigurations/All_IDEA_Plugin_Tests.xml
@@ -23,9 +23,17 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/AstAlignmentStrategy.java b/idea/src/org/jetbrains/jet/plugin/formatter/AstAlignmentStrategy.java
new file mode 100644
index 00000000000..d7961f20f3c
--- /dev/null
+++ b/idea/src/org/jetbrains/jet/plugin/formatter/AstAlignmentStrategy.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2010-2012 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.Alignment;
+import com.intellij.formatting.alignment.AlignmentStrategy;
+import com.intellij.lang.ASTNode;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ *
+ *
+ * @author Nikolay Krasko
+ */
+public abstract class ASTAlignmentStrategy {
+
+ private static final ASTAlignmentStrategy NULL_STRATEGY = fromTypes(AlignmentStrategy.wrap(null));
+
+ /** @return shared strategy instance that returns null all the time */
+ public static ASTAlignmentStrategy getNullStrategy() {
+ return NULL_STRATEGY;
+ }
+
+ public static ASTAlignmentStrategy fromTypes(AlignmentStrategy strategy) {
+ return new AlignmentStrategyWrapper(strategy);
+ }
+
+ @Nullable
+ public abstract Alignment getAlignment(ASTNode node);
+
+ public static class AlignmentStrategyWrapper extends ASTAlignmentStrategy {
+ private final AlignmentStrategy internalStrategy;
+
+ public AlignmentStrategyWrapper(@NotNull AlignmentStrategy internalStrategy) {
+ this.internalStrategy = internalStrategy;
+ }
+
+ @Nullable
+ @Override
+ public Alignment getAlignment(@NotNull ASTNode node) {
+ ASTNode parent = node.getTreeParent();
+ if (parent != null) {
+ return internalStrategy.getAlignment(parent.getElementType(), node.getElementType());
+ }
+
+ return internalStrategy.getAlignment(node.getElementType());
+ }
+ }
+}
diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java
index 2a8d99ec95d..773f432d7b3 100644
--- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java
+++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java
@@ -17,7 +17,6 @@
package org.jetbrains.jet.plugin.formatter;
import com.intellij.formatting.*;
-import com.intellij.formatting.alignment.AlignmentStrategy;
import com.intellij.lang.ASTNode;
import com.intellij.psi.TokenType;
import com.intellij.psi.codeStyle.CodeStyleSettings;
@@ -152,6 +151,15 @@ public class JetBlock extends AbstractBlock {
else if (type == JetNodeTypes.DOT_QUALIFIED_EXPRESSION) {
return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
}
+ else if (type == JetNodeTypes.VALUE_PARAMETER_LIST || type == JetNodeTypes.VALUE_ARGUMENT_LIST) {
+ // Child index 1 - cursor is after ( - parameter alignment should be recreated
+ // Child index 0 - before expression - know nothing about it
+ if (newChildIndex != 1 && newChildIndex != 0 && newChildIndex < getSubBlocks().size()) {
+ Block block = getSubBlocks().get(newChildIndex);
+ return new ChildAttributes(block.getIndent(), block.getAlignment());
+ }
+ return new ChildAttributes(Indent.getContinuationIndent(), null);
+ }
return new ChildAttributes(Indent.getNoneIndent(), null);
}
@@ -165,18 +173,18 @@ public class JetBlock extends AbstractBlock {
protected Map createChildrenAlignments() {
CommonCodeStyleSettings jetCommonSettings = mySettings.getCommonSettings(JetLanguage.INSTANCE);
- // Prepare strategies for setting up alignments - no alignment should be set by default
- AlignmentStrategy[] strategies = new AlignmentStrategy[] { AlignmentStrategy.getNullStrategy() };
+ // Prepare default null strategy
+ ASTAlignmentStrategy strategy = ASTAlignmentStrategy.getNullStrategy();
// Redefine list of strategies for some special elements
IElementType parentType = myNode.getElementType();
if (parentType == JetNodeTypes.VALUE_PARAMETER_LIST) {
- strategies = getAlignmentForChildInParenthesis(
+ strategy = getAlignmentForChildInParenthesis(
jetCommonSettings.ALIGN_MULTILINE_PARAMETERS, JetNodeTypes.VALUE_PARAMETER, JetTokens.COMMA,
jetCommonSettings.ALIGN_MULTILINE_METHOD_BRACKETS, JetTokens.LPAR, JetTokens.RPAR);
}
else if (parentType == JetNodeTypes.VALUE_ARGUMENT_LIST) {
- strategies = getAlignmentForChildInParenthesis(
+ strategy = getAlignmentForChildInParenthesis(
jetCommonSettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS, JetNodeTypes.VALUE_ARGUMENT, JetTokens.COMMA,
jetCommonSettings.ALIGN_MULTILINE_METHOD_BRACKETS, JetTokens.LPAR, JetTokens.RPAR);
}
@@ -193,32 +201,57 @@ public class JetBlock extends AbstractBlock {
continue;
}
- for (AlignmentStrategy strategy : strategies) {
- Alignment childAlignment = strategy.getAlignment(parentType, child.getElementType());
- if (childAlignment != null) {
- result.put(child, childAlignment);
- break;
- }
+ Alignment childAlignment = strategy.getAlignment(child);
+ if (childAlignment != null) {
+ result.put(child, childAlignment);
}
}
return result;
}
- private static AlignmentStrategy[] getAlignmentForChildInParenthesis(
- boolean shouldAlignChild, IElementType parameter, IElementType delimiter,
- boolean shouldAlignParenthesis, IElementType openParenth, IElementType closeParenth
+ private static ASTAlignmentStrategy getAlignmentForChildInParenthesis(
+ boolean shouldAlignChild, final IElementType parameter, final IElementType delimiter,
+ boolean shouldAlignParenthesis, final IElementType openParenth, final IElementType closeParenth
) {
- return new AlignmentStrategy[] {
- AlignmentStrategy.wrap(shouldAlignChild ? Alignment.createAlignment() : null, false, parameter),
- AlignmentStrategy.wrap(shouldAlignChild ? Alignment.createAlignment() : null, false, delimiter),
- AlignmentStrategy.wrap(shouldAlignParenthesis ? Alignment.createAlignment() : null, false, openParenth, closeParenth)
+ // TODO: Check this approach in other situations and refactor
+ final Alignment parameterAlignment = shouldAlignChild ? Alignment.createAlignment() : null;
+ final Alignment parenthesisAlignment = shouldAlignParenthesis ? Alignment.createAlignment() : null;
+
+ return new ASTAlignmentStrategy() {
+ @Override
+ public Alignment getAlignment(ASTNode node) {
+ IElementType childNodeType = node.getElementType();
+
+ ASTNode prev = getPrevWithoutWhitespace(node);
+ if ((prev != null && prev.getElementType() == TokenType.ERROR_ELEMENT) || childNodeType == TokenType.ERROR_ELEMENT) {
+ return parameterAlignment;
+ }
+
+ if (childNodeType == openParenth || childNodeType == closeParenth) {
+ return parenthesisAlignment;
+ }
+
+ if (childNodeType == parameter || childNodeType == delimiter) {
+ return parameterAlignment;
+ }
+
+ return null;
+ }
};
}
+ @Override
+ protected Indent getChildIndent() {
+ return super.getChildIndent(); //To change body of overridden methods use File | Settings | File Templates.
+ }
+
@Nullable
protected Indent createChildIndent(@NotNull ASTNode child) {
+ CommonCodeStyleSettings commonSettings = mySettings.getCommonSettings(JetLanguage.INSTANCE);
+
ASTNode childParent = child.getTreeParent();
+ IElementType childType = child.getElementType();
if (CODE_BLOCKS.contains(myNode.getElementType())) {
return indentIfNotBrace(child);
@@ -226,13 +259,13 @@ public class JetBlock extends AbstractBlock {
if (childParent != null &&
childParent.getElementType() == JetNodeTypes.BODY &&
- child.getElementType() != JetNodeTypes.BLOCK) {
+ childType != JetNodeTypes.BLOCK) {
// For a single statement if 'for'
return Indent.getNormalIndent();
}
- if (child.getElementType() == JetNodeTypes.WHEN_ENTRY) {
+ if (childType == JetNodeTypes.WHEN_ENTRY) {
// For the entry in when
// TODO: Add an option for configuration?
return Indent.getNormalIndent();
@@ -245,7 +278,7 @@ public class JetBlock extends AbstractBlock {
}
}
- if (STATEMENT_PARTS.contains(myNode.getElementType()) && child.getElementType() != JetNodeTypes.BLOCK) {
+ if (STATEMENT_PARTS.contains(myNode.getElementType()) && childType != JetNodeTypes.BLOCK) {
return Indent.getNormalIndent();
}
@@ -255,16 +288,21 @@ public class JetBlock extends AbstractBlock {
}
}
- if (childParent != null &&
- (childParent.getElementType() == JetNodeTypes.VALUE_PARAMETER_LIST ||
- childParent.getElementType() == JetNodeTypes.TYPE_PARAMETER_LIST ||
- childParent.getElementType() == JetNodeTypes.VALUE_ARGUMENT_LIST ||
- childParent.getElementType() == JetNodeTypes.TYPE_ARGUMENT_LIST)) {
- if (child.getElementType() == JetTokens.RPAR) {
- return Indent.getNoneIndent();
+ if (childParent != null) {
+ IElementType parentType = childParent.getElementType();
+
+ if (parentType == JetNodeTypes.VALUE_PARAMETER_LIST || parentType == JetNodeTypes.VALUE_ARGUMENT_LIST) {
+ ASTNode prev = getPrevWithoutWhitespace(child);
+ if (childType == JetTokens.RPAR && (prev == null || prev.getElementType() != TokenType.ERROR_ELEMENT)) {
+ return Indent.getNoneIndent();
+ }
+
+ return Indent.getContinuationWithoutFirstIndent();
}
- return Indent.getContinuationWithoutFirstIndent(false);
+ if (parentType == JetNodeTypes.TYPE_PARAMETER_LIST || parentType == JetNodeTypes.TYPE_ARGUMENT_LIST) {
+ return Indent.getContinuationWithoutFirstIndent();
+ }
}
return Indent.getNoneIndent();
diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java
index 641a1e44881..2a27b24c311 100644
--- a/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java
+++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetLanguageCodeStyleSettingsProvider.java
@@ -47,8 +47,7 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
" return 0\n" +
" }\n" +
" private fun foo2():Int {\n" +
- " return foo1(\n" +
- " 12,\n" +
+ " return foo1(12,\n" +
" 13,\n" +
" 14\n" +
" )\n" +
diff --git a/idea/testData/formatter/IndentationOnNewline/AfterImport.after.kt b/idea/testData/formatter/IndentationOnNewline/AfterImport_after.kt
similarity index 100%
rename from idea/testData/formatter/IndentationOnNewline/AfterImport.after.kt
rename to idea/testData/formatter/IndentationOnNewline/AfterImport_after.kt
diff --git a/idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.after.kt b/idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot_after.kt
similarity index 100%
rename from idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot.after.kt
rename to idea/testData/formatter/IndentationOnNewline/ConsecutiveCallsAfterDot_after.kt
diff --git a/idea/testData/formatter/IndentationOnNewline/DoInFun.after.kt b/idea/testData/formatter/IndentationOnNewline/DoInFun_after.kt
similarity index 100%
rename from idea/testData/formatter/IndentationOnNewline/DoInFun.after.kt
rename to idea/testData/formatter/IndentationOnNewline/DoInFun_after.kt
diff --git a/idea/testData/formatter/IndentationOnNewline/EmptyParameters.after.kt b/idea/testData/formatter/IndentationOnNewline/EmptyParameters.after.kt
deleted file mode 100644
index 1bf709254af..00000000000
--- a/idea/testData/formatter/IndentationOnNewline/EmptyParameters.after.kt
+++ /dev/null
@@ -1,3 +0,0 @@
-fun testParam(
- ) {
-}
\ No newline at end of file
diff --git a/idea/testData/formatter/IndentationOnNewline/EmptyParameters.kt b/idea/testData/formatter/IndentationOnNewline/EmptyParameters.kt
index 1bf709254af..17427fa56a4 100644
--- a/idea/testData/formatter/IndentationOnNewline/EmptyParameters.kt
+++ b/idea/testData/formatter/IndentationOnNewline/EmptyParameters.kt
@@ -1,3 +1,2 @@
-fun testParam(
- ) {
+fun testParam() {
}
\ No newline at end of file
diff --git a/idea/testData/formatter/IndentationOnNewline/EmptyParameters_after.kt b/idea/testData/formatter/IndentationOnNewline/EmptyParameters_after.kt
new file mode 100644
index 00000000000..9e46ff3a854
--- /dev/null
+++ b/idea/testData/formatter/IndentationOnNewline/EmptyParameters_after.kt
@@ -0,0 +1,4 @@
+fun testParam(
+
+) {
+}
\ No newline at end of file
diff --git a/idea/testData/formatter/IndentationOnNewline/For.after.kt b/idea/testData/formatter/IndentationOnNewline/For_after.kt
similarity index 100%
rename from idea/testData/formatter/IndentationOnNewline/For.after.kt
rename to idea/testData/formatter/IndentationOnNewline/For_after.kt
diff --git a/idea/testData/formatter/IndentationOnNewline/FunctionBlock.after.kt b/idea/testData/formatter/IndentationOnNewline/FunctionBlock_after.kt
similarity index 100%
rename from idea/testData/formatter/IndentationOnNewline/FunctionBlock.after.kt
rename to idea/testData/formatter/IndentationOnNewline/FunctionBlock_after.kt
diff --git a/idea/testData/formatter/IndentationOnNewline/If.after.kt b/idea/testData/formatter/IndentationOnNewline/If_after.kt
similarity index 100%
rename from idea/testData/formatter/IndentationOnNewline/If.after.kt
rename to idea/testData/formatter/IndentationOnNewline/If_after.kt
diff --git a/idea/testData/formatter/IndentationOnNewline/NotFirstParameter.after.kt b/idea/testData/formatter/IndentationOnNewline/NotFirstParameter_after.kt
similarity index 59%
rename from idea/testData/formatter/IndentationOnNewline/NotFirstParameter.after.kt
rename to idea/testData/formatter/IndentationOnNewline/NotFirstParameter_after.kt
index 7024e7dc4c7..5143bac94d3 100644
--- a/idea/testData/formatter/IndentationOnNewline/NotFirstParameter.after.kt
+++ b/idea/testData/formatter/IndentationOnNewline/NotFirstParameter_after.kt
@@ -1,3 +1,3 @@
fun testParam(a : String, b : Int,
- ) {
+ ) {
}
\ No newline at end of file
diff --git a/idea/testData/formatter/IndentationOnNewline/SettingAlignMultilineParametersInCalls.kt b/idea/testData/formatter/IndentationOnNewline/SettingAlignMultilineParametersInCalls.kt
new file mode 100644
index 00000000000..9e4d90e43cf
--- /dev/null
+++ b/idea/testData/formatter/IndentationOnNewline/SettingAlignMultilineParametersInCalls.kt
@@ -0,0 +1,5 @@
+fun test() {
+ testVeryLong(12,)
+}
+
+// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
\ No newline at end of file
diff --git a/idea/testData/formatter/IndentationOnNewline/SettingAlignMultilineParametersInCalls_after.kt b/idea/testData/formatter/IndentationOnNewline/SettingAlignMultilineParametersInCalls_after.kt
new file mode 100644
index 00000000000..f396effb050
--- /dev/null
+++ b/idea/testData/formatter/IndentationOnNewline/SettingAlignMultilineParametersInCalls_after.kt
@@ -0,0 +1,6 @@
+fun test() {
+ testVeryLong(12,
+ )
+}
+
+// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
\ No newline at end of file
diff --git a/idea/testData/formatter/IndentationOnNewline/SettingAlignMultilineParametersInCalls_after_inv.kt b/idea/testData/formatter/IndentationOnNewline/SettingAlignMultilineParametersInCalls_after_inv.kt
new file mode 100644
index 00000000000..affa3bd5d4e
--- /dev/null
+++ b/idea/testData/formatter/IndentationOnNewline/SettingAlignMultilineParametersInCalls_after_inv.kt
@@ -0,0 +1,6 @@
+fun test() {
+ testVeryLong(12,
+ )
+}
+
+// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
\ No newline at end of file
diff --git a/idea/testData/formatter/IndentationOnNewline/While.after.kt b/idea/testData/formatter/IndentationOnNewline/While_after.kt
similarity index 100%
rename from idea/testData/formatter/IndentationOnNewline/While.after.kt
rename to idea/testData/formatter/IndentationOnNewline/While_after.kt
diff --git a/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java b/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java
index f00e8020031..6a6f02ccef9 100644
--- a/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java
+++ b/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java
@@ -161,7 +161,7 @@ public abstract class AbstractJetFormatterTest extends LightIdeaTestCase {
return doc.getText();
}
- public static String loadFile(String name) throws Exception {
+ protected static String loadFile(String name) throws Exception {
String text = FileUtil.loadFile(new File(BASE_PATH, name));
text = StringUtil.convertLineSeparators(text);
return text;
diff --git a/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java b/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java
index 53be3ed71c0..696397f5a43 100644
--- a/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java
+++ b/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java
@@ -16,8 +16,12 @@
package org.jetbrains.jet.formatter;
+import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.psi.codeStyle.CodeStyleSettings;
+import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.testFramework.LightCodeInsightTestCase;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
+import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings;
import java.io.File;
@@ -38,8 +42,7 @@ public class JetTypingIndentationTest extends LightCodeInsightTestCase {
doFileNewlineTest();
}
- // TODO
- public void enabletestEmptyParameters() {
+ public void testEmptyParameters() {
doFileNewlineTest();
}
@@ -56,19 +59,59 @@ public class JetTypingIndentationTest extends LightCodeInsightTestCase {
doFileNewlineTest();
}
- // TODO
- public void enabletestNotFirstParameter() {
+ public void testNotFirstParameter() {
doFileNewlineTest();
}
+ public void testSettingAlignMultilineParametersInCalls() throws Exception {
+ doFileSettingNewLineTest();
+ }
+
public void testWhile() {
doFileNewlineTest();
}
public void doFileNewlineTest() {
- configureByFile(getTestName(false) + ".kt");
+ doNewlineTest(getBeforeFileName(), getAfterFileName());
+ }
+
+ public String getBeforeFileName() {
+ return getTestName(false) + ".kt";
+ }
+
+ public String getAfterFileName() {
+ return getTestName(false) + "_after.kt";
+ }
+
+ public String getInvertedAfterFileName() {
+ return getTestName(false) + "_after_inv.kt";
+ }
+
+ public void doFileSettingNewLineTest() throws Exception {
+ String originalFileText = FileUtil.loadFile(new File(getTestDataPath(), getBeforeFileName()));
+ FormattingSettingsConfigurator configurator = new FormattingSettingsConfigurator(originalFileText);
+
+ configurator.configureSettings(getSettings());
+ doNewlineTest(getBeforeFileName(), getAfterFileName());
+
+ configurator.configureInvertedSettings(getSettings());
+ doNewlineTest(getBeforeFileName(), getInvertedAfterFileName());
+
+ getSettings().clearCodeStyleSettings();
+ }
+
+ private void doNewlineTest(String beforeFileName, String afterFileName) {
+ configureByFile(beforeFileName);
type('\n');
- checkResultByFile(getTestName(false) + ".after.kt");
+ checkResultByFile(afterFileName);
+ }
+
+ public static JetCodeStyleSettings getJetSettings() {
+ return getSettings().getCustomSettings(JetCodeStyleSettings.class);
+ }
+
+ public static CodeStyleSettings getSettings() {
+ return CodeStyleSettingsManager.getSettings(getProject());
}
@Override