Formatter: left brace formatting in class/trait/class object/object/object literal declarations
This commit is contained in:
@@ -195,9 +195,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
}
|
||||
custom {
|
||||
|
||||
fun spacingForLeftBrace(block: ASTNode?): Spacing? {
|
||||
val noBlockSpacing = Spacing.createSpacing(1, 1, 0, settings.KEEP_LINE_BREAKS, settings.KEEP_BLANK_LINES_IN_CODE)
|
||||
if (block != null && block.getElementType() == BLOCK) {
|
||||
fun spacingForLeftBrace(block: ASTNode?, blockType: IElementType = BLOCK): Spacing? {
|
||||
if (block != null && block.getElementType() == blockType) {
|
||||
val leftBrace = block.getFirstChildNode()
|
||||
if (leftBrace != null && leftBrace.getElementType() == LBRACE) {
|
||||
val previousLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(leftBrace)
|
||||
@@ -207,12 +206,12 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
return Spacing.createSpacing(1, 1, minimumLF, keepLineBreaks, 0)
|
||||
}
|
||||
}
|
||||
return noBlockSpacing
|
||||
return Spacing.createSpacing(1, 1, 0, settings.KEEP_LINE_BREAKS, settings.KEEP_BLANK_LINES_IN_CODE)
|
||||
}
|
||||
|
||||
val leftBraceRule = {
|
||||
fun leftBraceRule(blockType: IElementType = BLOCK) = {
|
||||
(parent: ASTBlock, left: ASTBlock, right: ASTBlock) ->
|
||||
spacingForLeftBrace(right.getNode())
|
||||
spacingForLeftBrace(right.getNode(), blockType)
|
||||
}
|
||||
|
||||
val leftBraceRuleIfBlockIsWrapped = {
|
||||
@@ -227,12 +226,13 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
inPosition(parent = WHILE, right = BODY).customRule(leftBraceRuleIfBlockIsWrapped)
|
||||
inPosition(parent = DO_WHILE, right = BODY).customRule(leftBraceRuleIfBlockIsWrapped)
|
||||
|
||||
inPosition(parent = TRY, right = BLOCK).customRule(leftBraceRule)
|
||||
inPosition(parent = CATCH, right = BLOCK).customRule(leftBraceRule)
|
||||
inPosition(parent = FINALLY, right = BLOCK).customRule(leftBraceRule)
|
||||
inPosition(parent = TRY, right = BLOCK).customRule(leftBraceRule())
|
||||
inPosition(parent = CATCH, right = BLOCK).customRule(leftBraceRule())
|
||||
inPosition(parent = FINALLY, right = BLOCK).customRule(leftBraceRule())
|
||||
|
||||
inPosition(parent = FUN, right = BLOCK).customRule(leftBraceRule)
|
||||
inPosition(parent = FUN, right = BLOCK).customRule(leftBraceRule())
|
||||
|
||||
inPosition(right = CLASS_BODY).customRule(leftBraceRule(blockType = CLASS_BODY))
|
||||
|
||||
val spacesInSimpleFunction = if (jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD) 1 else 0
|
||||
inPosition(parent = FUNCTION_LITERAL,
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
class A {
|
||||
class object {
|
||||
}
|
||||
}
|
||||
|
||||
class A {
|
||||
class object {
|
||||
}
|
||||
}
|
||||
|
||||
trait A {
|
||||
}
|
||||
|
||||
trait A {
|
||||
}
|
||||
|
||||
object A {
|
||||
}
|
||||
|
||||
object A {
|
||||
}
|
||||
|
||||
val a = object {
|
||||
}
|
||||
|
||||
val a = object {
|
||||
}
|
||||
|
||||
// SET_TRUE: LBRACE_ON_NEXT_LINE
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
class A
|
||||
{
|
||||
class object
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class A
|
||||
{
|
||||
class object
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
trait A
|
||||
{
|
||||
}
|
||||
|
||||
trait A
|
||||
{
|
||||
}
|
||||
|
||||
object A
|
||||
{
|
||||
}
|
||||
|
||||
object A
|
||||
{
|
||||
}
|
||||
|
||||
val a = object
|
||||
{
|
||||
}
|
||||
|
||||
val a = object
|
||||
{
|
||||
}
|
||||
|
||||
// SET_TRUE: LBRACE_ON_NEXT_LINE
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
class A {
|
||||
class object {
|
||||
}
|
||||
}
|
||||
|
||||
class A
|
||||
{
|
||||
class object
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
trait A {
|
||||
}
|
||||
|
||||
trait A
|
||||
{
|
||||
}
|
||||
|
||||
object A {
|
||||
}
|
||||
|
||||
object A
|
||||
{
|
||||
}
|
||||
|
||||
val a = object {
|
||||
}
|
||||
|
||||
val a = object
|
||||
{
|
||||
}
|
||||
|
||||
// SET_TRUE: LBRACE_ON_NEXT_LINE
|
||||
|
||||
@@ -18,7 +18,8 @@ fun f() //eol comment
|
||||
{
|
||||
}
|
||||
|
||||
class A {
|
||||
class A
|
||||
{
|
||||
fun f()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class C1 protected (val x: Any){}
|
||||
class C1 protected (val x: Any) {}
|
||||
|
||||
fun f() {
|
||||
val c = C1(12);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class <caret>C1{}
|
||||
class <caret>C1 {}
|
||||
|
||||
fun f() {
|
||||
val c = C1();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
open class C1 protected (x3: ((Int) -> Int)?,
|
||||
var _x2: Float,
|
||||
val _x1: Int = 1){
|
||||
val _x1: Int = 1) {
|
||||
fun bar() {
|
||||
val y1 = _x1;
|
||||
val y2 = _x2;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
open class C1 protected (<caret>val x1: Int = 1, var x2: Float, x3: ((Int) -> Int)?){
|
||||
open class C1 protected (<caret>val x1: Int = 1, var x2: Float, x3: ((Int) -> Int)?) {
|
||||
fun bar() {
|
||||
val y1 = x1;
|
||||
val y2 = x2;
|
||||
|
||||
@@ -49,6 +49,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
|
||||
doTest("idea/testData/formatter/Class.after.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("ClassLineBreak.after.kt")
|
||||
public void testClassLineBreak() throws Exception {
|
||||
doTest("idea/testData/formatter/ClassLineBreak.after.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("CommentInFunctionLiteral.after.kt")
|
||||
public void testCommentInFunctionLiteral() throws Exception {
|
||||
doTest("idea/testData/formatter/CommentInFunctionLiteral.after.kt");
|
||||
@@ -362,6 +367,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/formatter"), Pattern.compile("^([^\\.]+)\\.after.inv.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("ClassLineBreak.after.inv.kt")
|
||||
public void testClassLineBreak() throws Exception {
|
||||
doTestInverted("idea/testData/formatter/ClassLineBreak.after.inv.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("DoWhileLineBreak.after.inv.kt")
|
||||
public void testDoWhileLineBreak() throws Exception {
|
||||
doTestInverted("idea/testData/formatter/DoWhileLineBreak.after.inv.kt");
|
||||
|
||||
Reference in New Issue
Block a user