Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For --google_style, break between ( and long condition expressions #325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1687,18 +1687,8 @@ class KotlinInputAstVisitor(
override fun visitWhenExpression(expression: KtWhenExpression) {
builder.sync(expression)
builder.block(ZERO) {
builder.token("when")
expression.subjectExpression?.let { subjectExp ->
builder.space()
builder.block(ZERO) {
builder.token("(")
builder.block(if (isGoogleStyle) expressionBreakIndent else ZERO) { visit(subjectExp) }
if (isGoogleStyle) {
builder.breakOp(Doc.FillMode.UNIFIED, "", ZERO)
}
}
builder.token(")")
}
emitKeywordWithCondition("when", expression.subjectExpression)

builder.space()
builder.token("{", Doc.Token.RealOrImaginary.REAL, blockIndent, Optional.of(blockIndent))

Expand Down Expand Up @@ -1770,18 +1760,7 @@ class KotlinInputAstVisitor(
override fun visitIfExpression(expression: KtIfExpression) {
builder.sync(expression)
builder.block(ZERO) {
builder.block(ZERO) {
builder.token("if")
builder.space()
builder.token("(")
builder.block(if (isGoogleStyle) expressionBreakIndent else ZERO) {
visit(expression.condition)
}
if (isGoogleStyle) {
builder.breakOp(Doc.FillMode.UNIFIED, "", ZERO)
}
}
builder.token(")")
emitKeywordWithCondition("if", expression.condition)

if (expression.then is KtBlockExpression) {
builder.space()
Expand Down Expand Up @@ -1976,11 +1955,7 @@ class KotlinInputAstVisitor(
/** Example `while (a < b) { ... }` */
override fun visitWhileExpression(expression: KtWhileExpression) {
builder.sync(expression)
builder.token("while")
builder.space()
builder.token("(")
visit(expression.condition)
builder.token(")")
emitKeywordWithCondition("while", expression.condition)
builder.space()
visit(expression.body)
}
Expand All @@ -1994,11 +1969,7 @@ class KotlinInputAstVisitor(
visit(expression.body)
builder.space()
}
builder.token("while")
builder.space()
builder.token("(")
visit(expression.condition)
builder.token(")")
emitKeywordWithCondition("while", expression.condition)
}

/** Example `break` or `break@foo` in a loop */
Expand Down Expand Up @@ -2354,4 +2325,30 @@ class KotlinInputAstVisitor(
private fun visit(element: PsiElement?) {
element?.accept(this)
}

/** Emits a key word followed by a condition, e.g. `if (b)` or `while (c < d )` */
private fun emitKeywordWithCondition(keyword: String, condition: KtExpression?) {
if (condition == null) {
builder.token(keyword)
return
}

builder.block(ZERO) {
builder.token(keyword)
builder.space()
builder.token("(")
if (isGoogleStyle) {
builder.block(expressionBreakIndent) {
builder.breakOp(Doc.FillMode.UNIFIED, "", ZERO)
visit(condition)
builder.breakOp(Doc.FillMode.UNIFIED, "", expressionBreakNegativeIndent)
}
} else {
builder.block(ZERO) {
visit(condition)
}
}
}
builder.token(")")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -764,14 +764,16 @@ class GoogleStyleFormatterKtTest {
"""
|----------------------------
|fun foo() {
| if (expressions1 &&
| if (
| expressions1 &&
| expression2 &&
| expression3
| ) {
| bar()
| }
|
| if (foo(
| if (
| foo(
| expressions1 &&
| expression2 &&
| expression3
Expand All @@ -784,21 +786,39 @@ class GoogleStyleFormatterKtTest {
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `if expression with condition that exactly fits to line`() =
assertFormatted(
"""
|-------------------------
|fun foo() {
| if (
| e1 && e2 && e3 = e4
| ) {
| bar()
| }
|}
|""".trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `when() expression with multiline condition`() =
assertFormatted(
"""
|-----------------------
|fun foo() {
| when (expressions1 +
| when (
| expressions1 +
| expression2 +
| expression3
| ) {
| 1 -> print(1)
| 2 -> print(2)
| }
|
| when (foo(
| when (
| foo(
| expressions1 &&
| expression2 &&
| expression3
Expand All @@ -812,6 +832,67 @@ class GoogleStyleFormatterKtTest {
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `when expression with condition that exactly fits to line`() =
assertFormatted(
"""
|---------------------------
|fun foo() {
| when (
| e1 && e2 && e3 = e4
| ) {
| 1 -> print(1)
| 2 -> print(2)
| }
|}
|""".trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `while expression with multiline condition`() =
assertFormatted(
"""
|----------------------------
|fun foo() {
| while (
| expressions1 &&
| expression2 &&
| expression3
| ) {
| bar()
| }
|
| while (
| foo(
| expressions1 &&
| expression2 &&
| expression3
| )
| ) {
| bar()
| }
|}
|""".trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `while expression with condition that exactly fits to line`() =
assertFormatted(
"""
|----------------------------
|fun foo() {
| while (
| e1 && e2 && e3 = e4
| ) {
| bar()
| }
|}
|""".trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = true)

@Test
fun `handle destructuring declaration`() =
assertFormatted(
Expand Down