Skip to content

Commit 04bf6b3

Browse files
committed
Enable ktfmt to format Kotlin code
Before this commit, I spent a while manually formatting code to reduce diffs in the codebase. By using ktfmt, we can focus more on the code and less on trivia like formatting. The formatted output isn't always perfect, but it's better than not having a code formatter. We use the Dropbox style instead of the default style because we want to use 4-space indent instead of 2-space indent, for consistency with the official Kotlin style guide and google-java-format (which we use in lsif-java)
1 parent 7268cd8 commit 04bf6b3

File tree

27 files changed

+1419
-960
lines changed

27 files changed

+1419
-960
lines changed

build.gradle.kts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@ plugins {
77
id("com.github.johnrengelman.shadow") version "6.1.0"
88
id("com.palantir.git-version") version "0.12.3"
99
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
10+
id("com.diffplug.spotless") version "5.17.0"
1011
}
1112

13+
1214
val versionDetails: Closure<VersionDetails> by extra
1315

1416
allprojects {
17+
if (name !in setOf("minimized", "semanticdb-kotlin")) {
18+
apply(plugin = "com.diffplug.spotless")
19+
spotless {
20+
kotlin {
21+
ktfmt().dropboxStyle()
22+
}
23+
}
24+
}
25+
1526
group = "com.sourcegraph"
1627
version = (project.properties["version"] as String).let {
1728
if (it != "unspecified" && !it.startsWith("refs"))
@@ -23,6 +34,7 @@ allprojects {
2334
val lastNum = tag.split(".").last().toInt() + 1
2435
"${tag.split(".").subList(0, 2).joinToString(".")}.$lastNum-SNAPSHOT"
2536
}
37+
2638
}
2739

2840
repositories {
@@ -65,4 +77,4 @@ allprojects {
6577
sourceCompatibility = "1.8"
6678
}
6779
}
68-
}
80+
}

debug-project/src/main/kotlin/sample/Main.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@ class Main<T> {
1010
return null
1111
}
1212

13-
fun method(burger: String) {
14-
}
15-
16-
val helloWorld = object {
17-
val hello = "Hello"
18-
val world = "World"
13+
fun method(burger: String) {}
1914

20-
override fun toString() = "$hello $world"
21-
}
15+
val helloWorld =
16+
object {
17+
val hello = "Hello"
18+
val world = "World"
2219

20+
override fun toString() = "$hello $world"
21+
}
2322
}
2423

2524
val bananas = 1
2625

2726
fun test() = Unit
2827

29-
typealias Stringer = String
28+
typealias Stringer = String
Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package snapshots
22
// ^^^^^^^^^ reference snapshots/
33

4-
class Class constructor(private var banana: Int, apple: String): Throwable(banana.toString() + apple) {
4+
class Class constructor(private var banana: Int, apple: String) :
55
// ^^^^^ definition snapshots/Class# Class
66
// ^^^^^^^^^^^ definition snapshots/Class#`<init>`(). Class
77
// ^^^^^^ definition snapshots/Class#banana. banana
@@ -11,46 +11,48 @@ class Class constructor(private var banana: Int, apple: String): Throwable(banan
1111
// ^^^ reference kotlin/Int#
1212
// ^^^^^ definition snapshots/Class#`<init>`().(apple) apple
1313
// ^^^^^^ reference kotlin/String#
14-
// ^^^^^^^^^ reference kotlin/Throwable#`<init>`().
15-
// ^^^^^^ reference snapshots/Class#`<init>`().(banana)
16-
// ^^^^^^^^ reference kotlin/Int#toString().
17-
// ^ reference kotlin/String#plus().
18-
// ^^^^^ reference snapshots/Class#`<init>`().(apple)
19-
init {
20-
println("")
21-
// ^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
22-
}
14+
Throwable(banana.toString() + apple) {
15+
// ^^^^^^^^^ reference kotlin/Throwable#`<init>`().
16+
// ^^^^^^ reference snapshots/Class#`<init>`().(banana)
17+
// ^^^^^^^^ reference kotlin/Int#toString().
18+
// ^ reference kotlin/String#plus().
19+
// ^^^^^ reference snapshots/Class#`<init>`().(apple)
20+
init {
21+
println("")
22+
// ^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
23+
}
2324

24-
val asdf = object {
25-
// ^^^^ definition snapshots/Class#asdf. asdf
26-
// ^^^^ definition snapshots/Class#getAsdf(). asdf
25+
val asdf =
26+
// ^^^^ definition snapshots/Class#asdf. asdf
27+
// ^^^^ definition snapshots/Class#getAsdf(). asdf
28+
object {
2729
fun doStuff() = Unit
2830
// ^^^^^^^ definition local0 doStuff
2931
// ^^^^ reference kotlin/Unit#
30-
}
32+
}
3133

32-
constructor(): this(1, "")
33-
// ^^^^^^^^^^^ definition snapshots/Class#`<init>`(+1). Class
34+
constructor() : this(1, "")
35+
//^^^^^^^^^^^ definition snapshots/Class#`<init>`(+1). Class
3436

35-
constructor(banana: Int): this(banana, "")
36-
// ^^^^^^^^^^^ definition snapshots/Class#`<init>`(+2). Class
37-
// ^^^^^^ definition snapshots/Class#`<init>`(+2).(banana) banana
38-
// ^^^ reference kotlin/Int#
39-
// ^^^^^^ reference snapshots/Class#`<init>`(+2).(banana)
37+
constructor(banana: Int) : this(banana, "")
38+
//^^^^^^^^^^^ definition snapshots/Class#`<init>`(+2). Class
39+
// ^^^^^^ definition snapshots/Class#`<init>`(+2).(banana) banana
40+
// ^^^ reference kotlin/Int#
41+
// ^^^^^^ reference snapshots/Class#`<init>`(+2).(banana)
4042

41-
fun run() {
42-
// ^^^ definition snapshots/Class#run(). run
43-
println(Class::class)
44-
// ^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
45-
// ^^^^^ reference snapshots/Class#
46-
println("I eat $banana for lunch")
47-
// ^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
48-
// ^^^^^^ reference snapshots/Class#banana.
49-
// ^^^^^^ reference snapshots/Class#getBanana().
50-
// ^^^^^^ reference snapshots/Class#setBanana().
51-
banana = 42
52-
// ^^^^^^ reference snapshots/Class#banana.
53-
// ^^^^^^ reference snapshots/Class#getBanana().
54-
// ^^^^^^ reference snapshots/Class#setBanana().
55-
}
43+
fun run() {
44+
// ^^^ definition snapshots/Class#run(). run
45+
println(Class::class)
46+
// ^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
47+
// ^^^^^ reference snapshots/Class#
48+
println("I eat $banana for lunch")
49+
// ^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
50+
// ^^^^^^ reference snapshots/Class#banana.
51+
// ^^^^^^ reference snapshots/Class#getBanana().
52+
// ^^^^^^ reference snapshots/Class#setBanana().
53+
banana = 42
54+
// ^^^^^^ reference snapshots/Class#banana.
55+
// ^^^^^^ reference snapshots/Class#getBanana().
56+
// ^^^^^^ reference snapshots/Class#setBanana().
57+
}
5658
}

semanticdb-kotlinc/minimized/src/generatedSnapshots/resources/kotlin/snapshots/CompanionOwner.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ package snapshots
44
class CompanionOwner {
55
// ^^^^^^^^^^^^^^ definition snapshots/CompanionOwner# CompanionOwner
66
// ^^^^^^^^^^^^^^ definition snapshots/CompanionOwner#`<init>`(). CompanionOwner
7-
companion object {
8-
// ^^^^^^^^^ definition snapshots/CompanionOwner#Companion# Companion
9-
fun create(): CompanionOwner = CompanionOwner()
10-
// ^^^^^^ definition snapshots/CompanionOwner#Companion#create(). create
11-
// ^^^^^^^^^^^^^^ reference snapshots/CompanionOwner#
12-
// ^^^^^^^^^^^^^^ reference snapshots/CompanionOwner#`<init>`().
13-
}
14-
fun create(): Int = CompanionOwner.create().hashCode()
15-
// ^^^^^^ definition snapshots/CompanionOwner#create(). create
16-
// ^^^ reference kotlin/Int#
17-
// ^^^^^^^^^^^^^^ reference snapshots/CompanionOwner#Companion#
18-
// ^^^^^^ reference snapshots/CompanionOwner#Companion#create().
19-
// ^^^^^^^^ reference snapshots/CompanionOwner#hashCode(+-1).
7+
companion object {
8+
// ^^^^^^^^^ definition snapshots/CompanionOwner#Companion# Companion
9+
fun create(): CompanionOwner = CompanionOwner()
10+
// ^^^^^^ definition snapshots/CompanionOwner#Companion#create(). create
11+
// ^^^^^^^^^^^^^^ reference snapshots/CompanionOwner#
12+
// ^^^^^^^^^^^^^^ reference snapshots/CompanionOwner#`<init>`().
13+
}
14+
fun create(): Int = CompanionOwner.create().hashCode()
15+
// ^^^^^^ definition snapshots/CompanionOwner#create(). create
16+
// ^^^ reference kotlin/Int#
17+
// ^^^^^^^^^^^^^^ reference snapshots/CompanionOwner#Companion#
18+
// ^^^^^^ reference snapshots/CompanionOwner#Companion#create().
19+
// ^^^^^^^^ reference snapshots/CompanionOwner#hashCode(+-1).
2020
}

semanticdb-kotlinc/minimized/src/generatedSnapshots/resources/kotlin/snapshots/Functions.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fun sampleText(x: String = "") {
55
// ^^^^^^^^^^ definition snapshots/FunctionsKt#sampleText(). sampleText
66
// ^ definition snapshots/FunctionsKt#sampleText().(x) x
77
// ^^^^^^ reference kotlin/String#
8-
println(x)
9-
// ^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
10-
// ^ reference snapshots/FunctionsKt#sampleText().(x)
11-
}
8+
println(x)
9+
//^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
10+
// ^ reference snapshots/FunctionsKt#sampleText().(x)
11+
}
Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
package snapshots
22
// ^^^^^^^^^ reference snapshots/
33

4-
val x = arrayListOf<String>().forEachIndexed { i, s ->
4+
val x = arrayListOf<String>().forEachIndexed { i, s -> println("$i $s") }
55
// ^ definition snapshots/LambdasKt#x. x
66
// ^ definition snapshots/LambdasKt#getX(). x
77
// ^^^^^^^^^^^ reference kotlin/collections/CollectionsKt#arrayListOf().
88
// ^^^^^^ reference kotlin/String#
99
// ^^^^^^^^^^^^^^ reference kotlin/collections/CollectionsKt#forEachIndexed(+9).
1010
// ^ definition local0 i
1111
// ^ definition local1 s
12-
println("$i $s")
13-
// ^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
14-
// ^ reference local0
15-
// ^ reference local1
16-
}
12+
// ^^^^^^^ reference kotlin/io/ConsoleKt#println(+1).
13+
// ^ reference local0
14+
// ^ reference local1
1715

18-
val y = "fdsa".run {
16+
val y = "fdsa".run { this.toByteArray() }
1917
// ^ definition snapshots/LambdasKt#y. y
2018
// ^ definition snapshots/LambdasKt#getY(). y
2119
// ^^^ reference kotlin/StandardKt#run(+1).
22-
this.toByteArray()
23-
// ^^^^ reference
24-
// ^^^^^^^^^^^ reference kotlin/text/StringsKt#toByteArray().
25-
}
20+
// ^^^^ reference
21+
// ^^^^^^^^^^^ reference kotlin/text/StringsKt#toByteArray().
2622

27-
val z = y.let {
23+
val z = y.let { it.size }
2824
// ^ definition snapshots/LambdasKt#z. z
2925
// ^ definition snapshots/LambdasKt#getZ(). z
3026
// ^ reference snapshots/LambdasKt#y.
3127
// ^ reference snapshots/LambdasKt#getY().
3228
// ^^^ reference kotlin/StandardKt#let().
33-
it.size
34-
// ^^ reference local2
35-
// ^^^^ reference kotlin/ByteArray#size.
36-
// ^^^^ reference kotlin/ByteArray#getSize().
37-
}
29+
// ^^ reference local2
30+
// ^^^^ reference kotlin/ByteArray#size.
31+
// ^^^^ reference kotlin/ByteArray#getSize().

semanticdb-kotlinc/minimized/src/generatedSnapshots/resources/kotlin/snapshots/ObjectKt.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import java.lang.RuntimeException
88

99
object ObjectKt {
1010
// ^^^^^^^^ definition snapshots/ObjectKt# ObjectKt
11-
fun fail(message: String?): Nothing {
12-
// ^^^^ definition snapshots/ObjectKt#fail(). fail
13-
// ^^^^^^^ definition snapshots/ObjectKt#fail().(message) message
14-
// ^^^^^^ reference kotlin/String#
15-
// ^^^^^^^ reference kotlin/Nothing#
16-
throw RuntimeException(message)
17-
// ^^^^^^^^^^^^^^^^ reference java/lang/RuntimeException#`<init>`(+1).
18-
// ^^^^^^^ reference snapshots/ObjectKt#fail().(message)
19-
}
11+
fun fail(message: String?): Nothing {
12+
// ^^^^ definition snapshots/ObjectKt#fail(). fail
13+
// ^^^^^^^ definition snapshots/ObjectKt#fail().(message) message
14+
// ^^^^^^ reference kotlin/String#
15+
// ^^^^^^^ reference kotlin/Nothing#
16+
throw RuntimeException(message)
17+
// ^^^^^^^^^^^^^^^^ reference java/lang/RuntimeException#`<init>`(+1).
18+
// ^^^^^^^ reference snapshots/ObjectKt#fail().(message)
19+
}
2020
}
Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
package snapshots
2-
3-
class Class constructor(private var banana: Int, apple: String): Throwable(banana.toString() + apple) {
4-
init {
5-
println("")
6-
}
7-
8-
val asdf = object {
9-
fun doStuff() = Unit
10-
}
11-
12-
constructor(): this(1, "")
13-
14-
constructor(banana: Int): this(banana, "")
15-
16-
fun run() {
17-
println(Class::class)
18-
println("I eat $banana for lunch")
19-
banana = 42
20-
}
21-
}
1+
package snapshots
2+
3+
class Class constructor(private var banana: Int, apple: String) :
4+
Throwable(banana.toString() + apple) {
5+
init {
6+
println("")
7+
}
8+
9+
val asdf =
10+
object {
11+
fun doStuff() = Unit
12+
}
13+
14+
constructor() : this(1, "")
15+
16+
constructor(banana: Int) : this(banana, "")
17+
18+
fun run() {
19+
println(Class::class)
20+
println("I eat $banana for lunch")
21+
banana = 42
22+
}
23+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package snapshots
2-
3-
class CompanionOwner {
4-
companion object {
5-
fun create(): CompanionOwner = CompanionOwner()
6-
}
7-
fun create(): Int = CompanionOwner.create().hashCode()
8-
}
1+
package snapshots
2+
3+
class CompanionOwner {
4+
companion object {
5+
fun create(): CompanionOwner = CompanionOwner()
6+
}
7+
fun create(): Int = CompanionOwner.create().hashCode()
8+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
package snapshots
2-
3-
fun sampleText(x: String = "") {
4-
println(x)
5-
}
1+
package snapshots
2+
3+
fun sampleText(x: String = "") {
4+
println(x)
5+
}

0 commit comments

Comments
 (0)