Setup spacing rules for secondary constructors

#KT-6968 Fixed
This commit is contained in:
Denis Zharkov
2015-03-12 20:14:49 +03:00
parent 9e5192e0e3
commit 22425de5cb
11 changed files with 254 additions and 2 deletions
@@ -46,7 +46,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
val jetCommonSettings = settings.getCommonSettings(JetLanguage.INSTANCE)!!
return rules(settings) {
val DECLARATIONS = TokenSet.create(PROPERTY, FUN, CLASS, OBJECT_DECLARATION, ENUM_ENTRY)
val DECLARATIONS = TokenSet.create(PROPERTY, FUN, CLASS, OBJECT_DECLARATION, ENUM_ENTRY, SECONDARY_CONSTRUCTOR)
custom {
inPosition(left = CLASS, right = CLASS).emptyLinesIfLineBreakInLeft(1)
@@ -91,6 +91,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
between(PROPERTY, DECLARATIONS).blankLines(1)
between(OBJECT_DECLARATION, DECLARATIONS).blankLines(1)
between(SECONDARY_CONSTRUCTOR, DECLARATIONS).blankLines(1)
// ENUM_ENTRY - ENUM_ENTRY is exception
between(ENUM_ENTRY, DECLARATIONS).blankLines(1)
@@ -139,6 +140,9 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
betweenInside(DOT, IDENTIFIER, FUN).spacing(0, 0, 0, false, 0)
afterInside(IDENTIFIER, FUN).spacing(0, 0, 0, false, 0)
// before LPAR in constructor(): this() {}
after(CONSTRUCTOR_DELEGATION_REFERENCE).spacing(0, 0, 0, false, 0)
aroundInside(DOT, DOT_QUALIFIED_EXPRESSION).spaces(0)
aroundInside(SAFE_ACCESS, SAFE_ACCESS_EXPRESSION).spaces(0)
@@ -166,7 +170,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
beforeInside(COLON, TYPE_COLON_ELEMENTS) { spaceIf(jetSettings.SPACE_BEFORE_TYPE_COLON) }
afterInside(COLON, TYPE_COLON_ELEMENTS) { spaceIf(jetSettings.SPACE_AFTER_TYPE_COLON) }
val EXTEND_COLON_ELEMENTS = TokenSet.create(TYPE_CONSTRAINT, CLASS, OBJECT_DECLARATION, TYPE_PARAMETER, ENUM_ENTRY)
val EXTEND_COLON_ELEMENTS =
TokenSet.create(TYPE_CONSTRAINT, CLASS, OBJECT_DECLARATION, TYPE_PARAMETER, ENUM_ENTRY, SECONDARY_CONSTRUCTOR)
beforeInside(COLON, EXTEND_COLON_ELEMENTS) { spaceIf(jetSettings.SPACE_BEFORE_EXTEND_COLON) }
afterInside(COLON, EXTEND_COLON_ELEMENTS) { spaceIf(jetSettings.SPACE_AFTER_EXTEND_COLON) }
@@ -254,6 +259,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
inPosition(parent = FINALLY, right = BLOCK).customRule(leftBraceRule())
inPosition(parent = FUN, right = BLOCK).customRule(leftBraceRule())
inPosition(parent = SECONDARY_CONSTRUCTOR, right = BLOCK).customRule(leftBraceRule())
inPosition(parent = PROPERTY_ACCESSOR, right = BLOCK).customRule(leftBraceRule())
inPosition(right = CLASS_BODY).customRule(leftBraceRule(blockType = CLASS_BODY))
@@ -0,0 +1,63 @@
class A {
constructor() {
}
constructor() {
}
fun foo() {
}
constructor() {
}
fun foo() {
}
constructor() {
}
val x = 1
constructor() {
}
fun foo() {
}
val x = 1
constructor() {
}
val x = 1
val x = 1
constructor() {
}
constructor() {
x = 1
}
fun foo() = 1
constructor() {
}
fun foo() = 1
constructor() : this() {
}
constructor() : this() {
}
constructor() : this() {
}
constructor() : super() {
}
}
@@ -0,0 +1,31 @@
class A {
constructor() {}
constructor() {}
fun foo() {}
constructor() {}
fun foo() {}
constructor() {}
val x = 1
constructor() {}
fun foo() {}
val x = 1
constructor() {}
val x = 1
val x = 1
constructor() {}
constructor() {
x = 1
}
fun foo() = 1
constructor() {}
fun foo() = 1
constructor() : this() {}
constructor():this() {}
constructor(): this () {}
constructor(): super(){}
}
@@ -0,0 +1,16 @@
class A {
constructor() {
}
constructor(): this() {
}
constructor(): super() {
}
constructor(): super() {
}
}
// SET_TRUE: SPACE_BEFORE_EXTEND_COLON
// SET_FALSE: SPACE_AFTER_EXTEND_COLON
@@ -0,0 +1,16 @@
class A {
constructor() {
}
constructor() :this() {
}
constructor() :super() {
}
constructor() :super() {
}
}
// SET_TRUE: SPACE_BEFORE_EXTEND_COLON
// SET_FALSE: SPACE_AFTER_EXTEND_COLON
@@ -0,0 +1,9 @@
class A {
constructor() {}
constructor() : this() {}
constructor(): super() {}
constructor():super() {}
}
// SET_TRUE: SPACE_BEFORE_EXTEND_COLON
// SET_FALSE: SPACE_AFTER_EXTEND_COLON
@@ -0,0 +1,25 @@
class A {
constructor() {
}
constructor() : this() {
}
constructor() {
}
constructor() //eol comment
{
}
constructor() : super(1, 2, 3) //eol comment
{
}
constructor() {
}
}
// SET_TRUE: LBRACE_ON_NEXT_LINE
@@ -0,0 +1,30 @@
class A
{
constructor()
{
}
constructor() : this()
{
}
constructor()
{
}
constructor() //eol comment
{
}
constructor() : super(1, 2, 3) //eol comment
{
}
constructor()
{
}
}
// SET_TRUE: LBRACE_ON_NEXT_LINE
@@ -0,0 +1,25 @@
class A {
constructor() {}
constructor() : this()
{}
constructor()
{
}
constructor() //eol comment
{
}
constructor():super(1,2,3) //eol comment
{}
constructor() {
}
}
// SET_TRUE: LBRACE_ON_NEXT_LINE
@@ -400,6 +400,18 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTest(fileName);
}
@TestMetadata("SecondaryConstructors.after.kt")
public void testSecondaryConstructors() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/SecondaryConstructors.after.kt");
doTest(fileName);
}
@TestMetadata("secondaryCtorLineBreak.after.kt")
public void testSecondaryCtorLineBreak() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/secondaryCtorLineBreak.after.kt");
doTest(fileName);
}
@TestMetadata("SingleLineFunctionLiteral.after.kt")
public void testSingleLineFunctionLiteral() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/SingleLineFunctionLiteral.after.kt");
@@ -424,6 +436,12 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTest(fileName);
}
@TestMetadata("SpaceAroundExtendColonInSecondaryCtr.after.kt")
public void testSpaceAroundExtendColonInSecondaryCtr() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/SpaceAroundExtendColonInSecondaryCtr.after.kt");
doTest(fileName);
}
@TestMetadata("SpaceBeforeFunctionLiteral.after.kt")
public void testSpaceBeforeFunctionLiteral() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/SpaceBeforeFunctionLiteral.after.kt");
@@ -801,6 +819,12 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTestInverted(fileName);
}
@TestMetadata("secondaryCtorLineBreak.after.inv.kt")
public void testSecondaryCtorLineBreak() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/secondaryCtorLineBreak.after.inv.kt");
doTestInverted(fileName);
}
@TestMetadata("SingleLineFunctionLiteral.after.inv.kt")
public void testSingleLineFunctionLiteral() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/SingleLineFunctionLiteral.after.inv.kt");
@@ -825,6 +849,12 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTestInverted(fileName);
}
@TestMetadata("SpaceAroundExtendColonInSecondaryCtr.after.inv.kt")
public void testSpaceAroundExtendColonInSecondaryCtr() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/SpaceAroundExtendColonInSecondaryCtr.after.inv.kt");
doTestInverted(fileName);
}
@TestMetadata("SpacesAroundOperations.after.inv.kt")
public void testSpacesAroundOperations() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/SpacesAroundOperations.after.inv.kt");
@@ -7,6 +7,7 @@ import java.util.HashMap
class Test {
constructor() {
}
constructor(s: String) {
}
}