Skip to content

Commit

Permalink
[tool] Add Kotlin autoformatting (flutter#5374)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stuartmorgan authored and HugoOlthof committed Dec 13, 2023
1 parent 7088c07 commit c32ff83
Show file tree
Hide file tree
Showing 31 changed files with 1,819 additions and 1,263 deletions.
5 changes: 3 additions & 2 deletions .ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ targets:
target_file: repo_checks.yaml
channel: master
version_file: flutter_master.version
# The format check requires clang-format.
# The format check requires clang-format, and the current version of ktfmt requires JDK 11+.
dependencies: >-
[
{"dependency": "clang", "version": "git_revision:5d5aba78dbbee75508f01bcaa69aedb2ab79065a"}
{"dependency": "clang", "version": "git_revision:5d5aba78dbbee75508f01bcaa69aedb2ab79065a"},
{"dependency": "open_jdk", "version": "version:17"}
]
- name: Linux dart_unit_test_shard_1 master
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ package dev.flutter.packages.animations.example

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {

}
class MainActivity : FlutterActivity() {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ package com.example.example

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
class MainActivity : FlutterActivity() {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ package dev.flutter.plugins.file_selector_example

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
class MainActivity : FlutterActivity() {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ package dev.flutter.example

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
class MainActivity : FlutterActivity() {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ package com.example.example

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
class MainActivity : FlutterActivity() {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ package io.flutter.packages.flutter_markdown_example

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
class MainActivity : FlutterActivity() {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ package com.example.example

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
class MainActivity : FlutterActivity() {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ package io.flutter.packages.palettegenerator.imagecolors

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {

}
class MainActivity : FlutterActivity() {}
8 changes: 3 additions & 5 deletions packages/pigeon/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ private class PigeonApiImplementation: ExampleHostApi {
### Kotlin
<?code-excerpt "android/app/src/main/kotlin/dev/flutter/pigeon_example_app/MainActivity.kt (kotlin-class)"?>
```kotlin
private class PigeonApiImplementation: ExampleHostApi {
private class PigeonApiImplementation : ExampleHostApi {
override fun getHostLanguage(): String {
return "Kotlin"
}

override fun add(a: Long, b: Long): Long {
if (a < 0L || b < 0L) {
throw FlutterError("code", "message", "details");
throw FlutterError("code", "message", "details")
}
return a + b
}
Expand Down Expand Up @@ -258,9 +258,7 @@ private class PigeonFlutterApi {
}

fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) {
flutterApi!!.flutterMethod(aString) {
echo -> callback(Result.success(echo))
}
flutterApi!!.flutterMethod(aString) { echo -> callback(Result.success(echo)) }
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
package dev.flutter.pigeon_example_app

import ExampleHostApi
import FlutterError
import MessageData
import MessageFlutterApi
import FlutterError

import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.FlutterPlugin

// #docregion kotlin-class
private class PigeonApiImplementation: ExampleHostApi {
private class PigeonApiImplementation : ExampleHostApi {
override fun getHostLanguage(): String {
return "Kotlin"
}

override fun add(a: Long, b: Long): Long {
if (a < 0L || b < 0L) {
throw FlutterError("code", "message", "details");
throw FlutterError("code", "message", "details")
}
return a + b
}
Expand All @@ -47,18 +46,16 @@ private class PigeonFlutterApi {
}

fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) {
flutterApi!!.flutterMethod(aString) {
echo -> callback(Result.success(echo))
}
flutterApi!!.flutterMethod(aString) { echo -> callback(Result.success(echo)) }
}
}
// #enddocregion kotlin-class-flutter

class MainActivity: FlutterActivity() {
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)

val api = PigeonApiImplementation()
ExampleHostApi.setUp(flutterEngine.dartExecutor.binaryMessenger, api);
ExampleHostApi.setUp(flutterEngine.dartExecutor.binaryMessenger, api)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Autogenerated from Pigeon, do not edit directly.
// See also: https://pub.dev/packages/pigeon


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

private fun wrapError(exception: Throwable): List<Any?> {
if (exception is FlutterError) {
return listOf(
exception.code,
exception.message,
exception.details
)
return listOf(exception.code, exception.message, exception.details)
} else {
return listOf(
exception.javaClass.simpleName,
exception.toString(),
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception)
)
exception.javaClass.simpleName,
exception.toString(),
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception))
}
}

private fun createConnectionError(channelName: String): FlutterError {
return FlutterError("channel-error", "Unable to establish connection on channel: '$channelName'.", "")}
return FlutterError(
"channel-error", "Unable to establish connection on channel: '$channelName'.", "")
}

/**
* Error class for passing custom error details to Flutter via a thrown PlatformException.
*
* @property code The error code.
* @property message The error message.
* @property details The error details. Must be a datatype supported by the api codec.
*/
class FlutterError (
val code: String,
override val message: String? = null,
val details: Any? = null
class FlutterError(
val code: String,
override val message: String? = null,
val details: Any? = null
) : Throwable()

enum class Code(val raw: Int) {
Expand All @@ -60,12 +57,11 @@ enum class Code(val raw: Int) {
}

/** Generated class from Pigeon that represents data sent in messages. */
data class MessageData (
val name: String? = null,
val description: String? = null,
val code: Code,
val data: Map<String?, String?>

data class MessageData(
val name: String? = null,
val description: String? = null,
val code: Code,
val data: Map<String?, String?>
) {
companion object {
@Suppress("UNCHECKED_CAST")
Expand All @@ -77,12 +73,13 @@ data class MessageData (
return MessageData(name, description, code, data)
}
}

fun toList(): List<Any?> {
return listOf<Any?>(
name,
description,
code.raw,
data,
name,
description,
code.raw,
data,
)
}
}
Expand All @@ -92,14 +89,13 @@ private object ExampleHostApiCodec : StandardMessageCodec() {
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
return when (type) {
128.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
MessageData.fromList(it)
}
return (readValue(buffer) as? List<Any?>)?.let { MessageData.fromList(it) }
}
else -> super.readValueOfType(type, buffer)
}
}
override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {

override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {
when (value) {
is MessageData -> {
stream.write(128)
Expand All @@ -113,19 +109,23 @@ private object ExampleHostApiCodec : StandardMessageCodec() {
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
interface ExampleHostApi {
fun getHostLanguage(): String

fun add(a: Long, b: Long): Long

fun sendMessage(message: MessageData, callback: (Result<Boolean>) -> Unit)

companion object {
/** The codec used by ExampleHostApi. */
val codec: MessageCodec<Any?> by lazy {
ExampleHostApiCodec
}
val codec: MessageCodec<Any?> by lazy { ExampleHostApiCodec }
/** Sets up an instance of `ExampleHostApi` to handle messages through the `binaryMessenger`. */
@Suppress("UNCHECKED_CAST")
fun setUp(binaryMessenger: BinaryMessenger, api: ExampleHostApi?) {
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.getHostLanguage", codec)
val channel =
BasicMessageChannel<Any?>(
binaryMessenger,
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.getHostLanguage",
codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
var wrapped: List<Any?>
Expand All @@ -141,7 +141,11 @@ interface ExampleHostApi {
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add", codec)
val channel =
BasicMessageChannel<Any?>(
binaryMessenger,
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add",
codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
Expand All @@ -160,7 +164,11 @@ interface ExampleHostApi {
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage", codec)
val channel =
BasicMessageChannel<Any?>(
binaryMessenger,
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage",
codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
Expand All @@ -187,26 +195,30 @@ interface ExampleHostApi {
class MessageFlutterApi(private val binaryMessenger: BinaryMessenger) {
companion object {
/** The codec used by MessageFlutterApi. */
val codec: MessageCodec<Any?> by lazy {
StandardMessageCodec()
}
val codec: MessageCodec<Any?> by lazy { StandardMessageCodec() }
}

fun flutterMethod(aStringArg: String?, callback: (Result<String>) -> Unit) {
val channelName = "dev.flutter.pigeon.pigeon_example_package.MessageFlutterApi.flutterMethod"
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
channel.send(listOf(aStringArg)) {
if (it is List<*>) {
if (it.size > 1) {
callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)));
callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)))
} else if (it[0] == null) {
callback(Result.failure(FlutterError("null-error", "Flutter api returned null value for non-null return value.", "")));
callback(
Result.failure(
FlutterError(
"null-error",
"Flutter api returned null value for non-null return value.",
"")))
} else {
val output = it[0] as String
callback(Result.success(output));
callback(Result.success(output))
}
} else {
callback(Result.failure(createConnectionError(channelName)));
}
callback(Result.failure(createConnectionError(channelName)))
}
}
}
}
Loading

0 comments on commit c32ff83

Please sign in to comment.