From 3028cec4cd4e8a359a62ce03024e50c717129e8e Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 23 Jan 2014 16:57:56 +0400 Subject: [PATCH] More correct code transformations: do not perform formatter's job on yourself --- .../jetbrains/jet/lang/psi/JetPsiFactory.java | 21 +++++++------------ .../plugin/formatter/kotlinSpacingRules.kt | 6 ++++++ .../SpecifyTypeExplicitlyAction.java | 6 ++---- .../quickfix/ChangeFunctionReturnTypeFix.java | 4 ++-- .../quickfix/ChangeVariableTypeFix.java | 4 ++-- .../formatter/ColonSpaces.after.inv.kt | 9 ++++++++ idea/testData/formatter/ColonSpaces.after.kt | 9 ++++++++ idea/testData/formatter/ColonSpaces.kt | 9 ++++++++ .../specifyType/afterLoopParameter.kt | 2 +- .../formatter/JetFormatterTestGenerated.java | 10 +++++++++ 10 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 idea/testData/formatter/ColonSpaces.after.inv.kt create mode 100644 idea/testData/formatter/ColonSpaces.after.kt create mode 100644 idea/testData/formatter/ColonSpaces.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java index 43446672155..9d5c1012139 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java @@ -95,24 +95,19 @@ public class JetPsiFactory { return comma; } - //the pair contains the first and the last elements of a range @NotNull - public static Pair createColonAndWhiteSpaces(Project project) { - JetProperty property = createProperty(project, "val x : Int"); - return Pair.create(property.findElementAt(5), property.findElementAt(7)); - } - - //the pair contains the first and the last elements of a range - @NotNull - public static Pair createTypeWhiteSpaceAndColon(Project project, String type) { - JetProperty property = createProperty(project, "val x: " + type); - return Pair.create(property.findElementAt(5), (PsiElement) property.getTypeRef()); + public static PsiElement createEQ(Project project) { + PsiElement eq = createFunction(project, "fun foo() = foo").getEqualsToken(); + assert eq != null; + return eq; } @NotNull - public static ASTNode createColonNode(Project project) { + public static PsiElement createColon(Project project) { JetProperty property = createProperty(project, "val x: Int"); - return property.getNode().findChildByType(JetTokens.COLON); + PsiElement colon = property.findElementAt(5); + assert colon != null; + return colon; } @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/kotlinSpacingRules.kt b/idea/src/org/jetbrains/jet/plugin/formatter/kotlinSpacingRules.kt index 5f22c589fdf..4ac914bef7e 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/kotlinSpacingRules.kt +++ b/idea/src/org/jetbrains/jet/plugin/formatter/kotlinSpacingRules.kt @@ -79,6 +79,12 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder { afterInside(COLON, FUN).spaceIf(jetSettings.SPACE_AFTER_TYPE_COLON) beforeInside(COLON, VALUE_PARAMETER).spaceIf(jetSettings.SPACE_BEFORE_TYPE_COLON) afterInside(COLON, VALUE_PARAMETER).spaceIf(jetSettings.SPACE_AFTER_TYPE_COLON) + beforeInside(COLON, MULTI_VARIABLE_DECLARATION_ENTRY).spaceIf(jetSettings.SPACE_BEFORE_TYPE_COLON) + afterInside(COLON, MULTI_VARIABLE_DECLARATION_ENTRY).spaceIf(jetSettings.SPACE_AFTER_TYPE_COLON) + beforeInside(COLON, LOOP_PARAMETER).spaceIf(jetSettings.SPACE_BEFORE_TYPE_COLON) + afterInside(COLON, LOOP_PARAMETER).spaceIf(jetSettings.SPACE_AFTER_TYPE_COLON) + beforeInside(COLON, FUNCTION_LITERAL).spaceIf(jetSettings.SPACE_BEFORE_TYPE_COLON) + afterInside(COLON, FUNCTION_LITERAL).spaceIf(jetSettings.SPACE_AFTER_TYPE_COLON) // Extends or constraint colon beforeInside(COLON, TYPE_CONSTRAINT).spaceIf(jetSettings.SPACE_BEFORE_EXTEND_COLON) diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java b/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java index 8d2abccf6c6..9ad8961a89e 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java +++ b/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java @@ -263,10 +263,8 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction { } private static void addTypeAnnotationSilently(Project project, JetNamedDeclaration namedDeclaration, PsiElement anchor) { - JetTypeReference typeReference = JetPsiFactory.createType(project, "Any"); - namedDeclaration.addAfter(typeReference, anchor); - Pair colon = JetPsiFactory.createColonAndWhiteSpaces(project); - namedDeclaration.addRangeAfter(colon.getFirst(), colon.getSecond(), anchor); + namedDeclaration.addAfter(JetPsiFactory.createType(project, "Any"), anchor); + namedDeclaration.addAfter(JetPsiFactory.createColon(project), anchor); } @Nullable diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index cf22d69bf9c..25344a28575 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -124,8 +124,8 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction // if a function doesn't have a value parameter list, a syntax error is raised, and it should follow the function name elementToPrecedeType = elementToPrecedeType.getNextSibling(); } - Pair typeWhiteSpaceAndColon = JetPsiFactory.createTypeWhiteSpaceAndColon(project, typeText); - function.addRangeAfter(typeWhiteSpaceAndColon.first, typeWhiteSpaceAndColon.second, elementToPrecedeType); + function.addAfter(JetPsiFactory.createType(project, typeText), elementToPrecedeType); + function.addAfter(JetPsiFactory.createColon(project), elementToPrecedeType); } @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java index 0243e35a905..d84a0d37d42 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java @@ -80,8 +80,8 @@ public class ChangeVariableTypeFix extends JetIntentionAction typeWhiteSpaceAndColon = JetPsiFactory.createTypeWhiteSpaceAndColon(project, renderedType); - element.addRangeAfter(typeWhiteSpaceAndColon.first, typeWhiteSpaceAndColon.second, nameIdentifier); + element.addAfter(JetPsiFactory.createType(project, renderedType), nameIdentifier); + element.addAfter(JetPsiFactory.createColon(project), nameIdentifier); if (element instanceof JetProperty) { JetPropertyAccessor getter = ((JetProperty) element).getGetter(); diff --git a/idea/testData/formatter/ColonSpaces.after.inv.kt b/idea/testData/formatter/ColonSpaces.after.inv.kt new file mode 100644 index 00000000000..c4dcc8fbd4e --- /dev/null +++ b/idea/testData/formatter/ColonSpaces.after.inv.kt @@ -0,0 +1,9 @@ +fun foo() { + val (i :Int, s :String) = bar() + val h = {() :Unit -> bar() } + for (i :Int in collection) { + } +} + +// SET_FALSE: SPACE_BEFORE_TYPE_COLON +// SET_TRUE: SPACE_AFTER_TYPE_COLON \ No newline at end of file diff --git a/idea/testData/formatter/ColonSpaces.after.kt b/idea/testData/formatter/ColonSpaces.after.kt new file mode 100644 index 00000000000..a4cec627a6b --- /dev/null +++ b/idea/testData/formatter/ColonSpaces.after.kt @@ -0,0 +1,9 @@ +fun foo() { + val (i: Int, s: String) = bar() + val h = {(): Unit -> bar() } + for (i: Int in collection) { + } +} + +// SET_FALSE: SPACE_BEFORE_TYPE_COLON +// SET_TRUE: SPACE_AFTER_TYPE_COLON \ No newline at end of file diff --git a/idea/testData/formatter/ColonSpaces.kt b/idea/testData/formatter/ColonSpaces.kt new file mode 100644 index 00000000000..4d4e8c734fb --- /dev/null +++ b/idea/testData/formatter/ColonSpaces.kt @@ -0,0 +1,9 @@ +fun foo() { + val (i:Int,s:String) = bar() + val h = {():Unit -> bar()} + for (i:Int in collection) { + } +} + +// SET_FALSE: SPACE_BEFORE_TYPE_COLON +// SET_TRUE: SPACE_AFTER_TYPE_COLON \ No newline at end of file diff --git a/idea/testData/intentions/specifyType/afterLoopParameter.kt b/idea/testData/intentions/specifyType/afterLoopParameter.kt index e44e6887d14..3c80b9ea06b 100644 --- a/idea/testData/intentions/specifyType/afterLoopParameter.kt +++ b/idea/testData/intentions/specifyType/afterLoopParameter.kt @@ -2,7 +2,7 @@ import java.util.HashMap fun foo(map : HashMap) { - for (entry : MutableMap.MutableEntry in map.entrySet()) { + for (entry: MutableMap.MutableEntry in map.entrySet()) { } } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java index 182b8ff2ecd..d376a0cd5dd 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java @@ -59,6 +59,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest { doTest("idea/testData/formatter/ClassLineBreak.after.kt"); } + @TestMetadata("ColonSpaces.after.kt") + public void testColonSpaces() throws Exception { + doTest("idea/testData/formatter/ColonSpaces.after.kt"); + } + @TestMetadata("CommentInFunctionLiteral.after.kt") public void testCommentInFunctionLiteral() throws Exception { doTest("idea/testData/formatter/CommentInFunctionLiteral.after.kt"); @@ -397,6 +402,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest { doTestInverted("idea/testData/formatter/ClassLineBreak.after.inv.kt"); } + @TestMetadata("ColonSpaces.after.inv.kt") + public void testColonSpaces() throws Exception { + doTestInverted("idea/testData/formatter/ColonSpaces.after.inv.kt"); + } + @TestMetadata("DoWhileLineBreak.after.inv.kt") public void testDoWhileLineBreak() throws Exception { doTestInverted("idea/testData/formatter/DoWhileLineBreak.after.inv.kt");