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

Prevent adjacent unary operators from merging #328

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 @@ -1075,19 +1075,33 @@ class KotlinInputAstVisitor(
builder.close()
}

override fun visitUnaryExpression(expression: KtUnaryExpression) {
override fun visitPostfixExpression(expression: KtPostfixExpression) {
builder.sync(expression)
builder.block(ZERO) {
visit(expression.baseExpression)
builder.token(expression.operationReference.text)
val baseExpression = expression.baseExpression
val operator = expression.operationReference.text

visit(baseExpression)
if (baseExpression is KtPostfixExpression &&
baseExpression.operationReference.text.last() == operator.first()) {
builder.space()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this exercised in any test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not currently. The only example I can think of would be a!! !!, but a!!!! has the same semantics. Do you have a preference on that case?

}
builder.token(operator)
}
}

override fun visitPrefixExpression(expression: KtPrefixExpression) {
builder.sync(expression)
builder.block(ZERO) {
builder.token(expression.operationReference.text)
visit(expression.baseExpression)
val baseExpression = expression.baseExpression
val operator = expression.operationReference.text

builder.token(operator)
if (baseExpression is KtPrefixExpression &&
operator.last() == baseExpression.operationReference.text.first()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this relies on the fact that there are no operators with a mixture of characters, right?
I'm wondering if we should always put a space instead of guessing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be better? I like not guessing.

How do you feel about these cases:

a--!!
!--a

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate Kotlin :)
let's add these test-cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

builder.space()
}
visit(baseExpression)
}
}

Expand Down
35 changes: 34 additions & 1 deletion core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2834,7 +2834,7 @@ class FormatterTest {
|""".trimMargin())

@Test
fun `Unary expressions`() =
fun `Unary prefix expressions`() =
assertFormatted(
"""
|fun f() {
Expand All @@ -2844,6 +2844,39 @@ class FormatterTest {
| +4
| ++a
| --a
|
| + +a
| +-a
| +!a
| -+a
| - -a
| -!a
| !+a
| !a
| ! !a
|
| + ++a
| +--a
| -++a
| - --a
| !++a
| !--a
|}
|""".trimMargin())

@Test
fun `Unary postfix expressions`() =
assertFormatted(
"""
|fun f() {
| a!!
| a++
| a--
|
| a--!!
| a++!!
|
| a!! !!
|}
|""".trimMargin())

Expand Down