Skip to content

Commit 72de224

Browse files
[tool] Add Kotlin autoformatting (#5374)
Updates the tooling to fetch and use `ktfmt` for Kotlin code, the same way it currently does for the Google Java formatter, and applies that formatting (in the default mode; see discussion in the linked issue) to the repository. In the future we could revisit the formatter or mode, but since this currently seems to be the most consistent with our other languages and to google3 formatting this is likely the option we'll want to stick with. Fixes flutter/flutter#118756
1 parent 17bd92e commit 72de224

File tree

31 files changed

+1819
-1263
lines changed

31 files changed

+1819
-1263
lines changed

Diff for: .ci.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ targets:
131131
target_file: repo_checks.yaml
132132
channel: master
133133
version_file: flutter_master.version
134-
# The format check requires clang-format.
134+
# The format check requires clang-format, and the current version of ktfmt requires JDK 11+.
135135
dependencies: >-
136136
[
137-
{"dependency": "clang", "version": "git_revision:5d5aba78dbbee75508f01bcaa69aedb2ab79065a"}
137+
{"dependency": "clang", "version": "git_revision:5d5aba78dbbee75508f01bcaa69aedb2ab79065a"},
138+
{"dependency": "open_jdk", "version": "version:17"}
138139
]
139140
140141
- name: Linux dart_unit_test_shard_1 master

Diff for: packages/animations/example/android/app/src/main/kotlin/dev/flutter/packages/animations/example/MainActivity.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ package dev.flutter.packages.animations.example
55

66
import io.flutter.embedding.android.FlutterActivity
77

8-
class MainActivity: FlutterActivity() {
9-
10-
}
8+
class MainActivity : FlutterActivity() {}

Diff for: packages/dynamic_layouts/example/android/app/src/main/kotlin/com/example/example/MainActivity.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ package com.example.example
55

66
import io.flutter.embedding.android.FlutterActivity
77

8-
class MainActivity: FlutterActivity() {
9-
}
8+
class MainActivity : FlutterActivity() {}

Diff for: packages/file_selector/file_selector/example/android/app/src/main/kotlin/dev/flutter/plugins/file_selector_example/MainActivity.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ package dev.flutter.plugins.file_selector_example
66

77
import io.flutter.embedding.android.FlutterActivity
88

9-
class MainActivity: FlutterActivity() {
10-
}
9+
class MainActivity : FlutterActivity() {}

Diff for: packages/flutter_adaptive_scaffold/example/android/app/src/main/kotlin/dev/flutter/example/MainActivity.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ package dev.flutter.example
66

77
import io.flutter.embedding.android.FlutterActivity
88

9-
class MainActivity: FlutterActivity() {
10-
}
9+
class MainActivity : FlutterActivity() {}

Diff for: packages/flutter_image/example/android/app/src/main/kotlin/com/example/example/MainActivity.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ package com.example.example
66

77
import io.flutter.embedding.android.FlutterActivity
88

9-
class MainActivity: FlutterActivity() {
10-
}
9+
class MainActivity : FlutterActivity() {}

Diff for: packages/flutter_markdown/example/android/app/src/main/kotlin/io/flutter/packages/flutter_markdown_example/MainActivity.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ package io.flutter.packages.flutter_markdown_example
55

66
import io.flutter.embedding.android.FlutterActivity
77

8-
class MainActivity: FlutterActivity() {
9-
}
8+
class MainActivity : FlutterActivity() {}

Diff for: packages/go_router/example/android/app/src/main/kotlin/com/example/example/MainActivity.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ package com.example.example
66

77
import io.flutter.embedding.android.FlutterActivity
88

9-
class MainActivity: FlutterActivity() {
10-
}
9+
class MainActivity : FlutterActivity() {}

Diff for: packages/palette_generator/example/android/app/src/main/kotlin/io/flutter/packages/palettegenerator/example/MainActivity.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ package io.flutter.packages.palettegenerator.imagecolors
55

66
import io.flutter.embedding.android.FlutterActivity
77

8-
class MainActivity: FlutterActivity() {
9-
10-
}
8+
class MainActivity : FlutterActivity() {}

Diff for: packages/pigeon/example/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ private class PigeonApiImplementation: ExampleHostApi {
146146
### Kotlin
147147
<?code-excerpt "android/app/src/main/kotlin/dev/flutter/pigeon_example_app/MainActivity.kt (kotlin-class)"?>
148148
```kotlin
149-
private class PigeonApiImplementation: ExampleHostApi {
149+
private class PigeonApiImplementation : ExampleHostApi {
150150
override fun getHostLanguage(): String {
151151
return "Kotlin"
152152
}
153153

154154
override fun add(a: Long, b: Long): Long {
155155
if (a < 0L || b < 0L) {
156-
throw FlutterError("code", "message", "details");
156+
throw FlutterError("code", "message", "details")
157157
}
158158
return a + b
159159
}
@@ -258,9 +258,7 @@ private class PigeonFlutterApi {
258258
}
259259

260260
fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) {
261-
flutterApi!!.flutterMethod(aString) {
262-
echo -> callback(Result.success(echo))
263-
}
261+
flutterApi!!.flutterMethod(aString) { echo -> callback(Result.success(echo)) }
264262
}
265263
}
266264
```

Diff for: packages/pigeon/example/app/android/app/src/main/kotlin/dev/flutter/pigeon_example_app/MainActivity.kt

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55
package dev.flutter.pigeon_example_app
66

77
import ExampleHostApi
8+
import FlutterError
89
import MessageData
910
import MessageFlutterApi
10-
import FlutterError
11-
1211
import androidx.annotation.NonNull
1312
import io.flutter.embedding.android.FlutterActivity
1413
import io.flutter.embedding.engine.FlutterEngine
1514
import io.flutter.embedding.engine.plugins.FlutterPlugin
1615

1716
// #docregion kotlin-class
18-
private class PigeonApiImplementation: ExampleHostApi {
17+
private class PigeonApiImplementation : ExampleHostApi {
1918
override fun getHostLanguage(): String {
2019
return "Kotlin"
2120
}
2221

2322
override fun add(a: Long, b: Long): Long {
2423
if (a < 0L || b < 0L) {
25-
throw FlutterError("code", "message", "details");
24+
throw FlutterError("code", "message", "details")
2625
}
2726
return a + b
2827
}
@@ -47,18 +46,16 @@ private class PigeonFlutterApi {
4746
}
4847

4948
fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) {
50-
flutterApi!!.flutterMethod(aString) {
51-
echo -> callback(Result.success(echo))
52-
}
49+
flutterApi!!.flutterMethod(aString) { echo -> callback(Result.success(echo)) }
5350
}
5451
}
5552
// #enddocregion kotlin-class-flutter
5653

57-
class MainActivity: FlutterActivity() {
54+
class MainActivity : FlutterActivity() {
5855
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
5956
super.configureFlutterEngine(flutterEngine)
6057

6158
val api = PigeonApiImplementation()
62-
ExampleHostApi.setUp(flutterEngine.dartExecutor.binaryMessenger, api);
59+
ExampleHostApi.setUp(flutterEngine.dartExecutor.binaryMessenger, api)
6360
}
6461
}

Diff for: packages/pigeon/example/app/android/app/src/main/kotlin/dev/flutter/pigeon_example_app/Messages.g.kt

+55-43
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Autogenerated from Pigeon, do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66

7-
87
import android.util.Log
98
import io.flutter.plugin.common.BasicMessageChannel
109
import io.flutter.plugin.common.BinaryMessenger
@@ -19,33 +18,31 @@ private fun wrapResult(result: Any?): List<Any?> {
1918

2019
private fun wrapError(exception: Throwable): List<Any?> {
2120
if (exception is FlutterError) {
22-
return listOf(
23-
exception.code,
24-
exception.message,
25-
exception.details
26-
)
21+
return listOf(exception.code, exception.message, exception.details)
2722
} else {
2823
return listOf(
29-
exception.javaClass.simpleName,
30-
exception.toString(),
31-
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception)
32-
)
24+
exception.javaClass.simpleName,
25+
exception.toString(),
26+
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception))
3327
}
3428
}
3529

3630
private fun createConnectionError(channelName: String): FlutterError {
37-
return FlutterError("channel-error", "Unable to establish connection on channel: '$channelName'.", "")}
31+
return FlutterError(
32+
"channel-error", "Unable to establish connection on channel: '$channelName'.", "")
33+
}
3834

3935
/**
4036
* Error class for passing custom error details to Flutter via a thrown PlatformException.
37+
*
4138
* @property code The error code.
4239
* @property message The error message.
4340
* @property details The error details. Must be a datatype supported by the api codec.
4441
*/
45-
class FlutterError (
46-
val code: String,
47-
override val message: String? = null,
48-
val details: Any? = null
42+
class FlutterError(
43+
val code: String,
44+
override val message: String? = null,
45+
val details: Any? = null
4946
) : Throwable()
5047

5148
enum class Code(val raw: Int) {
@@ -60,12 +57,11 @@ enum class Code(val raw: Int) {
6057
}
6158

6259
/** Generated class from Pigeon that represents data sent in messages. */
63-
data class MessageData (
64-
val name: String? = null,
65-
val description: String? = null,
66-
val code: Code,
67-
val data: Map<String?, String?>
68-
60+
data class MessageData(
61+
val name: String? = null,
62+
val description: String? = null,
63+
val code: Code,
64+
val data: Map<String?, String?>
6965
) {
7066
companion object {
7167
@Suppress("UNCHECKED_CAST")
@@ -77,12 +73,13 @@ data class MessageData (
7773
return MessageData(name, description, code, data)
7874
}
7975
}
76+
8077
fun toList(): List<Any?> {
8178
return listOf<Any?>(
82-
name,
83-
description,
84-
code.raw,
85-
data,
79+
name,
80+
description,
81+
code.raw,
82+
data,
8683
)
8784
}
8885
}
@@ -92,14 +89,13 @@ private object ExampleHostApiCodec : StandardMessageCodec() {
9289
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
9390
return when (type) {
9491
128.toByte() -> {
95-
return (readValue(buffer) as? List<Any?>)?.let {
96-
MessageData.fromList(it)
97-
}
92+
return (readValue(buffer) as? List<Any?>)?.let { MessageData.fromList(it) }
9893
}
9994
else -> super.readValueOfType(type, buffer)
10095
}
10196
}
102-
override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {
97+
98+
override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {
10399
when (value) {
104100
is MessageData -> {
105101
stream.write(128)
@@ -113,19 +109,23 @@ private object ExampleHostApiCodec : StandardMessageCodec() {
113109
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
114110
interface ExampleHostApi {
115111
fun getHostLanguage(): String
112+
116113
fun add(a: Long, b: Long): Long
114+
117115
fun sendMessage(message: MessageData, callback: (Result<Boolean>) -> Unit)
118116

119117
companion object {
120118
/** The codec used by ExampleHostApi. */
121-
val codec: MessageCodec<Any?> by lazy {
122-
ExampleHostApiCodec
123-
}
119+
val codec: MessageCodec<Any?> by lazy { ExampleHostApiCodec }
124120
/** Sets up an instance of `ExampleHostApi` to handle messages through the `binaryMessenger`. */
125121
@Suppress("UNCHECKED_CAST")
126122
fun setUp(binaryMessenger: BinaryMessenger, api: ExampleHostApi?) {
127123
run {
128-
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.getHostLanguage", codec)
124+
val channel =
125+
BasicMessageChannel<Any?>(
126+
binaryMessenger,
127+
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.getHostLanguage",
128+
codec)
129129
if (api != null) {
130130
channel.setMessageHandler { _, reply ->
131131
var wrapped: List<Any?>
@@ -141,7 +141,11 @@ interface ExampleHostApi {
141141
}
142142
}
143143
run {
144-
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add", codec)
144+
val channel =
145+
BasicMessageChannel<Any?>(
146+
binaryMessenger,
147+
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add",
148+
codec)
145149
if (api != null) {
146150
channel.setMessageHandler { message, reply ->
147151
val args = message as List<Any?>
@@ -160,7 +164,11 @@ interface ExampleHostApi {
160164
}
161165
}
162166
run {
163-
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage", codec)
167+
val channel =
168+
BasicMessageChannel<Any?>(
169+
binaryMessenger,
170+
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage",
171+
codec)
164172
if (api != null) {
165173
channel.setMessageHandler { message, reply ->
166174
val args = message as List<Any?>
@@ -187,26 +195,30 @@ interface ExampleHostApi {
187195
class MessageFlutterApi(private val binaryMessenger: BinaryMessenger) {
188196
companion object {
189197
/** The codec used by MessageFlutterApi. */
190-
val codec: MessageCodec<Any?> by lazy {
191-
StandardMessageCodec()
192-
}
198+
val codec: MessageCodec<Any?> by lazy { StandardMessageCodec() }
193199
}
200+
194201
fun flutterMethod(aStringArg: String?, callback: (Result<String>) -> Unit) {
195202
val channelName = "dev.flutter.pigeon.pigeon_example_package.MessageFlutterApi.flutterMethod"
196203
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
197204
channel.send(listOf(aStringArg)) {
198205
if (it is List<*>) {
199206
if (it.size > 1) {
200-
callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)));
207+
callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)))
201208
} else if (it[0] == null) {
202-
callback(Result.failure(FlutterError("null-error", "Flutter api returned null value for non-null return value.", "")));
209+
callback(
210+
Result.failure(
211+
FlutterError(
212+
"null-error",
213+
"Flutter api returned null value for non-null return value.",
214+
"")))
203215
} else {
204216
val output = it[0] as String
205-
callback(Result.success(output));
217+
callback(Result.success(output))
206218
}
207219
} else {
208-
callback(Result.failure(createConnectionError(channelName)));
209-
}
220+
callback(Result.failure(createConnectionError(channelName)))
221+
}
210222
}
211223
}
212224
}

0 commit comments

Comments
 (0)