Skip to content

Commit 9f72b70

Browse files
committed
[formatter] Add more cases for annotations
1 parent 444862b commit 9f72b70

4 files changed

Lines changed: 92 additions & 1 deletion

File tree

core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2240,7 +2240,14 @@ open class KotlinInputAstVisitor(
22402240
visit(psi)
22412241
}
22422242

2243-
if (onlyAnnotationsSoFar && forceAnnotationBreaks && psi is KtAnnotationEntry) {
2243+
val shouldForceBreak = forceAnnotationBreaks &&
2244+
// don't force break on parameter annotations
2245+
list.parent !is KtParameter &&
2246+
// don't force break on receiver type annotations
2247+
!(list.parent is KtTypeReference && list.parent.parent is KtFunction) &&
2248+
// don't force break on parameter type annotations
2249+
!(list.parent is KtTypeReference && list.parent.parent is KtParameter)
2250+
if (onlyAnnotationsSoFar && shouldForceBreak) {
22442251
builder.forcedBreak()
22452252
} else if (onlyAnnotationsSoFar) {
22462253
builder.breakOp(" ")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
private class AnnotatedConstructor(
2+
@JvmField @Volatile private var state: Int,
3+
@get:JvmName("ctx") val context: CoroutineContext,
4+
@Suppress("UNUSED_PARAMETER") unused: String = "",
5+
@property:Inject val dependency: Dependency,
6+
@Deprecated("Use context instead", ReplaceWith("context")) val legacyContext: CoroutineContext,
7+
)
8+
9+
private class AnnotatedParameters {
10+
fun single(@NotNull a: String) = a
11+
12+
fun multiple(@NotNull @Size(min = 1) a: String, @Nullable b: String?) = a + b
13+
14+
fun wrapped(
15+
@NotNull @Size(min = 1, max = 100) someVeryLongParameterName: String,
16+
@Nullable anotherRatherLongParameterName: String? = null,
17+
) = someVeryLongParameterName + anotherRatherLongParameterName
18+
19+
fun varargs(@NotNull vararg values: String) = values.size
20+
21+
fun higherOrder(@NotNull block: (@Nullable Int) -> Unit) = block(1)
22+
23+
fun @receiver:NotNull String.extension(@NotNull other: String) = this + other
24+
25+
fun withLambda(@NotNull name: String, @Nullable block: () -> Unit = {}) {
26+
block()
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
private class AnnotatedConstructor(
2+
@JvmField @Volatile private var state: Int,
3+
@get:JvmName("ctx") val context: CoroutineContext,
4+
@Suppress("UNUSED_PARAMETER") unused: String = "",
5+
@property:Inject val dependency: Dependency,
6+
@Deprecated("Use context instead", ReplaceWith("context")) val legacyContext: CoroutineContext,
7+
)
8+
9+
private class AnnotatedParameters {
10+
fun single(@NotNull a: String) = a
11+
12+
fun multiple(@NotNull @Size(min = 1) a: String, @Nullable b: String?) = a + b
13+
14+
fun wrapped(
15+
@NotNull @Size(min = 1, max = 100) someVeryLongParameterName: String,
16+
@Nullable anotherRatherLongParameterName: String? = null,
17+
) = someVeryLongParameterName + anotherRatherLongParameterName
18+
19+
fun varargs(@NotNull vararg values: String) = values.size
20+
21+
fun higherOrder(@NotNull block: (@Nullable Int) -> Unit) = block(1)
22+
23+
fun @receiver:NotNull String.extension(@NotNull other: String) = this + other
24+
25+
fun withLambda(@NotNull name: String, @Nullable block: () -> Unit = {}) {
26+
block()
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
private class AnnotatedConstructor(
2+
@JvmField @Volatile private var state: Int,
3+
@get:JvmName("ctx") val context: CoroutineContext,
4+
@Suppress("UNUSED_PARAMETER") unused: String = "",
5+
@property:Inject val dependency: Dependency,
6+
@Deprecated("Use context instead", ReplaceWith("context")) val legacyContext: CoroutineContext,
7+
)
8+
9+
private class AnnotatedParameters {
10+
fun single(@NotNull a: String) = a
11+
12+
fun multiple(@NotNull @Size(min = 1) a: String, @Nullable b: String?) = a + b
13+
14+
fun wrapped(
15+
@NotNull @Size(min = 1, max = 100) someVeryLongParameterName: String,
16+
@Nullable anotherRatherLongParameterName: String? = null,
17+
) = someVeryLongParameterName + anotherRatherLongParameterName
18+
19+
fun varargs(@NotNull vararg values: String) = values.size
20+
21+
fun higherOrder(@NotNull block: (@Nullable Int) -> Unit) = block(1)
22+
23+
fun @receiver:NotNull String.extension(@NotNull other: String) = this + other
24+
25+
fun withLambda(@NotNull name: String, @Nullable block: () -> Unit = {}) {
26+
block()
27+
}
28+
}

0 commit comments

Comments
 (0)