More correct code transformations: do not perform formatter's job on yourself

This commit is contained in:
Valentin Kipyatkov
2014-01-23 16:57:56 +04:00
parent ab7d8a7734
commit 3028cec4cd
10 changed files with 58 additions and 22 deletions
@@ -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<PsiElement, PsiElement> 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<PsiElement, PsiElement> 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
@@ -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)
@@ -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<PsiElement, PsiElement> 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
@@ -124,8 +124,8 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction<JetFunction>
// 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<PsiElement, PsiElement> 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
@@ -80,8 +80,8 @@ public class ChangeVariableTypeFix extends JetIntentionAction<JetVariableDeclara
SpecifyTypeExplicitlyAction.removeTypeAnnotation(element);
PsiElement nameIdentifier = element.getNameIdentifier();
assert nameIdentifier != null : "ChangeVariableTypeFix applied to variable without name";
Pair<PsiElement, PsiElement> 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();
@@ -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
@@ -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
+9
View File
@@ -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
@@ -2,7 +2,7 @@
import java.util.HashMap
fun foo(map : HashMap<String, Int>) {
for (entry : MutableMap.MutableEntry<String, Int><caret> in map.entrySet()) {
for (entry: MutableMap.MutableEntry<String, Int><caret> in map.entrySet()) {
}
}
@@ -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");