Skip to content

Commit fb33dde

Browse files
authored
Migrate to ktfmt (#2064)
1 parent 2829be0 commit fb33dde

File tree

85 files changed

+3459
-3501
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3459
-3501
lines changed

build.gradle.kts

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import com.diffplug.gradle.spotless.JavaExtension
22
import com.vanniktech.maven.publish.MavenPublishBaseExtension
3+
import java.net.URI
34
import org.jetbrains.dokka.gradle.DokkaExtension
45
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
56
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
67
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
7-
import java.net.URI
88

99
buildscript {
1010
dependencies {
11-
val kotlinVersion =
12-
System.getenv("MOSHI_KOTLIN_VERSION")
13-
?: libs.versions.kotlin.get()
14-
val kspVersion =
15-
System.getenv("MOSHI_KSP_VERSION")
16-
?: libs.versions.ksp.get()
11+
val kotlinVersion = System.getenv("MOSHI_KOTLIN_VERSION") ?: libs.versions.kotlin.get()
12+
val kspVersion = System.getenv("MOSHI_KSP_VERSION") ?: libs.versions.ksp.get()
1713
classpath(kotlin("gradle-plugin", version = kotlinVersion))
1814
classpath("com.google.devtools.ksp:symbol-processing-gradle-plugin:$kspVersion")
1915
}
@@ -31,9 +27,7 @@ allprojects {
3127
group = "com.squareup.moshi"
3228
version = "2.0.0-SNAPSHOT"
3329

34-
repositories {
35-
mavenCentral()
36-
}
30+
repositories { mavenCentral() }
3731
}
3832

3933
spotless {
@@ -52,20 +46,14 @@ spotless {
5246
targetExclude("**/build/**")
5347
}
5448
kotlin {
55-
ktlint(libs.ktlint.get().version).editorConfigOverride(
56-
mapOf(
57-
"ktlint_standard_filename" to "disabled",
58-
// Making something an expression body should be a choice around readability.
59-
"ktlint_standard_function-expression-body" to "disabled",
60-
),
61-
)
49+
ktfmt(libs.ktfmt.get().version).googleStyle()
6250
target("**/*.kt")
6351
trimTrailingWhitespace()
6452
endWithNewline()
6553
targetExclude("**/Dependencies.kt", "**/build/**")
6654
}
6755
kotlinGradle {
68-
ktlint(libs.ktlint.get().version)
56+
ktfmt(libs.ktfmt.get().version).googleStyle()
6957
target("**/*.gradle.kts")
7058
trimTrailingWhitespace()
7159
endWithNewline()
@@ -76,14 +64,10 @@ subprojects {
7664
// Apply with "java" instead of just "java-library" so kotlin projects get it too
7765
pluginManager.withPlugin("java") {
7866
configure<JavaPluginExtension> {
79-
toolchain {
80-
languageVersion.set(libs.versions.jdk.map(JavaLanguageVersion::of))
81-
}
67+
toolchain { languageVersion.set(libs.versions.jdk.map(JavaLanguageVersion::of)) }
8268
}
8369
if (project.name != "records-tests") {
84-
tasks.withType<JavaCompile>().configureEach {
85-
options.release.set(8)
86-
}
70+
tasks.withType<JavaCompile>().configureEach { options.release.set(8) }
8771
}
8872
}
8973

@@ -110,11 +94,7 @@ dependencies {
11094
dokka(project(":moshi-kotlin-codegen"))
11195
}
11296

113-
dokka {
114-
dokkaPublications.html {
115-
outputDirectory.set(layout.projectDirectory.dir("docs/2.x"))
116-
}
117-
}
97+
dokka { dokkaPublications.html { outputDirectory.set(layout.projectDirectory.dir("docs/2.x")) } }
11898

11999
subprojects {
120100
plugins.withId("org.jetbrains.dokka") {
@@ -135,9 +115,7 @@ subprojects {
135115
sourceLink {
136116
localDirectory.set(layout.projectDirectory.dir("src"))
137117
val relPath =
138-
rootProject.isolated.projectDirectory.asFile
139-
.toPath()
140-
.relativize(projectDir.toPath())
118+
rootProject.isolated.projectDirectory.asFile.toPath().relativize(projectDir.toPath())
141119
remoteUrl("https://github.com/square/moshi/tree/main/$relPath/src")
142120
remoteLineSuffix.set("#L")
143121
}

examples/build.gradle.kts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,5 @@ dependencies {
1313
}
1414

1515
tasks.withType<KotlinCompile>().configureEach {
16-
compilerOptions {
17-
freeCompilerArgs.add(
18-
"-opt-in=kotlin.ExperimentalStdlibApi",
19-
)
20-
}
16+
compilerOptions { freeCompilerArgs.add("-opt-in=kotlin.ExperimentalStdlibApi") }
2117
}

examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ import com.squareup.moshi.JsonWriter
2323
import com.squareup.moshi.Moshi
2424
import com.squareup.moshi.Moshi.Builder
2525
import com.squareup.moshi.Types
26-
import okio.BufferedSource
2726
import java.lang.reflect.Type
2827
import kotlin.annotation.AnnotationRetention.RUNTIME
28+
import okio.BufferedSource
2929

3030
@JsonClass(generateAdapter = true)
3131
data class ExampleClass(val type: Int, @JsonString val rawJson: String)
3232

33-
@Retention(RUNTIME)
34-
@JsonQualifier
35-
annotation class JsonString
33+
@Retention(RUNTIME) @JsonQualifier annotation class JsonString
3634

3735
class JsonStringJsonAdapterFactory : JsonAdapter.Factory {
3836
override fun create(type: Type, annotations: Set<Annotation>, moshi: Moshi): JsonAdapter<*>? {
@@ -52,17 +50,15 @@ class JsonStringJsonAdapterFactory : JsonAdapter.Factory {
5250
}
5351

5452
fun main() {
55-
//language=JSON
53+
// language=JSON
5654
val json = "{\"type\":1,\"rawJson\":{\"a\":2,\"b\":3,\"c\":[1,2,3]}}"
5755

58-
val moshi = Builder()
59-
.add(JsonStringJsonAdapterFactory())
60-
.build()
56+
val moshi = Builder().add(JsonStringJsonAdapterFactory()).build()
6157

6258
val example: ExampleClass = moshi.adapter<ExampleClass>().fromJson(json)
6359

6460
check(example.type == 1)
6561

66-
//language=JSON
62+
// language=JSON
6763
check(example.rawJson == "{\"a\":2,\"b\":3,\"c\":[1,2,3]}")
6864
}

examples/src/main/java/com/squareup/moshi/recipes/ReadJsonListKt.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import com.squareup.moshi.recipes.models.Card
2121

2222
class ReadJsonListKt {
2323

24-
//language=JSON
25-
private val jsonString = """
24+
// language=JSON
25+
private val jsonString =
26+
"""
2627
[{"rank": "4",
2728
"suit": "CLUBS"
2829
},
@@ -32,7 +33,8 @@ class ReadJsonListKt {
3233
{"rank": "J",
3334
"suit": "SPADES"
3435
}]
35-
""".trimIndent()
36+
"""
37+
.trimIndent()
3638

3739
fun readJsonList() {
3840
val jsonAdapter = Moshi.Builder().build().adapter<List<Card>>()

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ kotlinCompileTesting = { module = "dev.zacsweers.kctfork:core", version.ref = "k
3737
kotlinCompileTesting-ksp = { module = "dev.zacsweers.kctfork:ksp", version.ref ="kotlinCompileTesting" }
3838
truth = "com.google.truth:truth:1.4.5"
3939
googleJavaFormat = "com.google.googlejavaformat:google-java-format:1.33.0"
40-
ktlint = "com.pinterest.ktlint:ktlint-cli:1.8.0"
40+
ktfmt = "com.facebook:ktfmt:0.59"

moshi-adapters/build.gradle.kts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ dependencies {
1919
}
2020

2121
tasks.withType<Jar>().configureEach {
22-
manifest {
23-
attributes("Automatic-Module-Name" to "com.squareup.moshi.adapters")
24-
}
22+
manifest { attributes("Automatic-Module-Name" to "com.squareup.moshi.adapters") }
2523
}
2624

27-
configure<MavenPublishBaseExtension> {
28-
configure(KotlinJvm(javadocJar = None()))
29-
}
25+
configure<MavenPublishBaseExtension> { configure(KotlinJvm(javadocJar = None())) }

moshi-adapters/japicmp/build.gradle.kts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ val latest = configurations.create("latest")
1111
dependencies {
1212
baseline("com.squareup.moshi:moshi-adapters:1.15.2") {
1313
isTransitive = false
14-
version {
15-
strictly("1.14.0")
16-
}
14+
version { strictly("1.14.0") }
1715
}
1816
latest(project(":moshi-adapters"))
1917
}
@@ -30,6 +28,4 @@ val japicmp =
3028
includeSynthetic.set(true)
3129
}
3230

33-
tasks.named("check").configure {
34-
dependsOn(japicmp)
35-
}
31+
tasks.named("check").configure { dependsOn(japicmp) }

moshi-adapters/src/main/java/com/squareup/moshi/Rfc3339DateJsonAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package com.squareup.moshi
1717

1818
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
19-
import okio.IOException
2019
import java.util.Date
20+
import okio.IOException
2121

2222
@Deprecated(
2323
"""This class moved to avoid a package name conflict in the Java Platform Module System.

moshi-adapters/src/main/java/com/squareup/moshi/adapters/EnumJsonAdapter.kt

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,26 @@ import okio.IOException
2828
* A JsonAdapter for enums that allows having a fallback enum value when a deserialized string does
2929
* not match any enum value. To use, add this as an adapter for your enum type on your
3030
* [Moshi.Builder][com.squareup.moshi.Moshi.Builder]:
31-
*
3231
* ```
3332
* Moshi moshi = new Moshi.Builder()
3433
* .add(CurrencyCode.class, EnumJsonAdapter.create(CurrencyCode.class)
3534
* .withUnknownFallback(CurrencyCode.USD))
3635
* .build();
3736
* ```
3837
*/
39-
public class EnumJsonAdapter<T : Enum<T>> internal constructor(
38+
public class EnumJsonAdapter<T : Enum<T>>
39+
internal constructor(
4040
private val enumType: Class<T>,
4141
private val fallbackValue: T?,
4242
private val useFallbackValue: Boolean,
4343
) : JsonAdapter<T?>() {
4444

4545
private val constants = enumType.enumConstants
46-
private val nameStrings = Array(constants.size) { i ->
47-
val constantName = constants[i].name
48-
enumType.getField(constantName).jsonName(constantName)
49-
}
46+
private val nameStrings =
47+
Array(constants.size) { i ->
48+
val constantName = constants[i].name
49+
enumType.getField(constantName).jsonName(constantName)
50+
}
5051
private val options = Options.of(*nameStrings)
5152

5253
/**
@@ -66,13 +67,11 @@ public class EnumJsonAdapter<T : Enum<T>> internal constructor(
6667
if (!useFallbackValue) {
6768
val name = reader.nextString()
6869
throw JsonDataException(
69-
"Expected one of ${nameStrings.toList()} but was $name at path ${reader.path}",
70+
"Expected one of ${nameStrings.toList()} but was $name at path ${reader.path}"
7071
)
7172
}
7273
if (reader.peek() != STRING) {
73-
throw JsonDataException(
74-
"Expected a string but was ${reader.peek()} at path ${reader.path}",
75-
)
74+
throw JsonDataException("Expected a string but was ${reader.peek()} at path ${reader.path}")
7675
}
7776
reader.skipValue()
7877
return fallbackValue
@@ -81,9 +80,7 @@ public class EnumJsonAdapter<T : Enum<T>> internal constructor(
8180
@Throws(IOException::class)
8281
override fun toJson(writer: JsonWriter, value: T?) {
8382
if (value == null) {
84-
throw NullPointerException(
85-
"value was null! Wrap in .nullSafe() to write nullable values.",
86-
)
83+
throw NullPointerException("value was null! Wrap in .nullSafe() to write nullable values.")
8784
}
8885
writer.value(nameStrings[value.ordinal])
8986
}

0 commit comments

Comments
 (0)