Skip to content

Commit 82efc16

Browse files
committed
Exclude coroutine lib from IntelliJ dependency
The IntelliJ dependency does not provide sources for the coroutine library, which can make it very frustrating to debug / view source code. We specify our own coroutine library, but IntelliJ doesn't seem to choose to use it over the IntelliJ library, I assume since the IntelliJ library is defined first. Using an artifact transform we can filter out the coroutine jar from the IntelliJ library, though, so the IDE will only see the dependency we define.
1 parent a5b8be6 commit 82efc16

File tree

8 files changed

+160
-14
lines changed

8 files changed

+160
-14
lines changed

build.gradle.kts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,33 @@ dependencies {
112112
testRuntimeOnly(libs.junit.entine)
113113
}
114114

115+
val artifactType = Attribute.of("artifactType", String::class.java)
116+
val filtered = Attribute.of("filtered", Boolean::class.javaObjectType)
117+
118+
dependencies {
119+
attributesSchema {
120+
attribute(filtered)
121+
}
122+
artifactTypes.getByName("jar") {
123+
attributes.attribute(filtered, false)
124+
}
125+
126+
registerTransform(Filter::class) {
127+
from.attribute(filtered, false).attribute(artifactType, "jar")
128+
to.attribute(filtered, true).attribute(artifactType, "jar")
129+
130+
parameters {
131+
ideaVersion.set(providers.gradleProperty("ideaVersion"))
132+
ideaVersionName.set(providers.gradleProperty("ideaVersionName"))
133+
depsFile.set(layout.projectDirectory.file(".gradle/intellij-deps.json"))
134+
}
135+
}
136+
}
137+
138+
configurations.compileClasspath {
139+
attributes.attribute(filtered, true)
140+
}
141+
115142
intellij {
116143
// IntelliJ IDEA dependency
117144
version.set(providers.gradleProperty("ideaVersion"))
@@ -240,6 +267,8 @@ idea {
240267
module {
241268
generatedSourceDirs.add(file("build/gen"))
242269
excludeDirs.add(file(intellij.sandboxDir.get()))
270+
isDownloadJavadoc = true
271+
isDownloadSources = true
243272
}
244273
}
245274

buildSrc/settings.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Minecraft Dev for IntelliJ
3+
*
4+
* https://minecraftdev.org
5+
*
6+
* Copyright (c) 2021 minecraft-dev
7+
*
8+
* MIT License
9+
*/
10+
11+
rootProject.name = "buildSrc"

buildSrc/src/main/kotlin/Filter.kt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Minecraft Dev for IntelliJ
3+
*
4+
* https://minecraftdev.org
5+
*
6+
* Copyright (c) 2021 minecraft-dev
7+
*
8+
* MIT License
9+
*/
10+
11+
import com.google.gson.Gson
12+
import com.google.gson.reflect.TypeToken
13+
import java.nio.file.Files
14+
import java.nio.file.Path
15+
import javax.inject.Inject
16+
import org.gradle.api.artifacts.transform.CacheableTransform
17+
import org.gradle.api.artifacts.transform.InputArtifact
18+
import org.gradle.api.artifacts.transform.TransformAction
19+
import org.gradle.api.artifacts.transform.TransformOutputs
20+
import org.gradle.api.artifacts.transform.TransformParameters
21+
import org.gradle.api.file.FileSystemLocation
22+
import org.gradle.api.file.ProjectLayout
23+
import org.gradle.api.file.RegularFileProperty
24+
import org.gradle.api.provider.Property
25+
import org.gradle.api.provider.Provider
26+
import org.gradle.api.tasks.Input
27+
import org.gradle.api.tasks.InputFile
28+
import org.gradle.api.tasks.Internal
29+
import org.gradle.api.tasks.PathSensitive
30+
import org.gradle.api.tasks.PathSensitivity
31+
32+
abstract class Filter : TransformAction<Filter.Params> {
33+
interface Params : TransformParameters {
34+
@get:Input
35+
val ideaVersion: Property<String>
36+
@get:Input
37+
val ideaVersionName: Property<String>
38+
@get:PathSensitive(PathSensitivity.NONE)
39+
@get:InputFile
40+
val depsFile: RegularFileProperty
41+
}
42+
43+
@get:PathSensitive(PathSensitivity.NONE)
44+
@get:InputArtifact
45+
abstract val inputArtifact: Provider<FileSystemLocation>
46+
47+
@get:Inject
48+
abstract val layout: ProjectLayout
49+
50+
private val deps: List<Dep>?
51+
52+
init {
53+
deps = run {
54+
val depsFile = parameters.depsFile.orNull?.asFile ?: return@run null
55+
if (!depsFile.exists()) {
56+
return@run null
57+
}
58+
59+
val depList: DepList = depsFile.bufferedReader().use { reader ->
60+
Gson().fromJson(reader, DepList::class.java)
61+
}
62+
63+
if (
64+
parameters.ideaVersion.orNull == depList.intellijVersion &&
65+
parameters.ideaVersionName.orNull == depList.intellijVersionName
66+
) {
67+
depList.deps
68+
} else {
69+
null
70+
}
71+
}
72+
}
73+
74+
override fun transform(outputs: TransformOutputs) {
75+
val input = inputArtifact.get().asFile.toPath()
76+
77+
// exclude the coroutines jar
78+
// We include our own - but also IntelliJ's jar breaks sources
79+
val inputParts = input.map { it.toString() }
80+
if (!inputParts.containsAll(pathParts)) {
81+
outputs.file(inputArtifact)
82+
return
83+
}
84+
85+
val fileName = inputParts.last()
86+
if (fileName.startsWith("kotlinx-coroutines")) {
87+
return
88+
}
89+
90+
deps?.forEach { d ->
91+
if (fileName == "${d.artifactId}-${d.version}.jar") {
92+
return
93+
}
94+
}
95+
96+
outputs.file(inputArtifact)
97+
}
98+
99+
companion object {
100+
private val pathParts = listOf("com.jetbrains.intellij.idea", "ideaIC", "lib")
101+
}
102+
}

buildSrc/src/main/kotlin/mcdev.gradle.kts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ val jsonFile = file("$projectDir/$fileName")
2828
val ideaVersion: String by project
2929
val ideaVersionName: String by project
3030

31-
data class DepList(val intellijVersion: String, val intellijVersionName: String, val deps: List<Dep>)
32-
data class Dep(val groupId: String, val artifactId: String, val version: String)
33-
3431
if (jsonFile.exists()) {
3532
val deps: DepList = jsonFile.bufferedReader().use { reader ->
36-
Gson().fromJson(reader, object : TypeToken<DepList>() {}.type)
33+
Gson().fromJson(reader, DepList::class.java)
3734
}
3835
if (ideaVersion != deps.intellijVersion || ideaVersionName != deps.intellijVersionName) {
3936
println("IntelliJ library sources file definition is out of date, deleting")

buildSrc/src/main/kotlin/util.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,6 @@ fun Project.parser(bnf: String, pack: String): TaskDelegate<JavaExec> {
9595
)
9696
}
9797
}
98+
99+
data class DepList(val intellijVersion: String, val intellijVersionName: String, val deps: List<Dep>)
100+
data class Dep(val groupId: String, val artifactId: String, val version: String)

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ asm = "9.3"
55

66
[libraries]
77
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
8+
coroutines-jdk8 = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8", version.ref = "coroutines" }
89
coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "coroutines" }
910

1011
templateMakerFabric = "com.extracraftx.minecraft:TemplateMakerFabric:0.4.1"
@@ -34,5 +35,5 @@ junit-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "jun
3435
junit-entine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }
3536

3637
[bundles]
37-
coroutines = ["coroutines-core", "coroutines-swing"]
38+
coroutines = ["coroutines-core", "coroutines-jdk8", "coroutines-swing"]
3839
asm = ["asm", "asm-tree", "asm-analysis"]

src/main/kotlin/util/utils.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ package com.demonwav.mcdev.util
1313
import com.google.gson.Gson
1414
import com.google.gson.reflect.TypeToken
1515
import com.intellij.lang.java.lexer.JavaLexer
16+
import com.intellij.openapi.application.AppUIExecutor
1617
import com.intellij.openapi.application.ApplicationManager
1718
import com.intellij.openapi.application.ModalityState
19+
import com.intellij.openapi.application.impl.coroutineDispatchingContext
1820
import com.intellij.openapi.application.runReadAction
1921
import com.intellij.openapi.command.WriteCommandAction
2022
import com.intellij.openapi.module.Module
@@ -31,6 +33,8 @@ import com.intellij.pom.java.LanguageLevel
3133
import com.intellij.psi.PsiDocumentManager
3234
import com.intellij.psi.PsiFile
3335
import java.util.Locale
36+
import kotlinx.coroutines.CoroutineScope
37+
import kotlinx.coroutines.runBlocking
3438

3539
inline fun <T : Any?> runWriteTask(crossinline func: () -> T): T {
3640
return invokeAndWait {
@@ -88,6 +92,10 @@ fun invokeLaterAny(func: () -> Unit) {
8892
ApplicationManager.getApplication().invokeLater(func, ModalityState.any())
8993
}
9094

95+
fun <T> edtCoroutineScope(block: suspend CoroutineScope.() -> T): T {
96+
return runBlocking(AppUIExecutor.onUiThread().coroutineDispatchingContext(), block)
97+
}
98+
9199
inline fun <T : Any?> PsiFile.runWriteAction(crossinline func: () -> T) =
92100
applyWriteAction { func() }
93101

src/test/kotlin/framework/EdtInterceptor.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
package com.demonwav.mcdev.framework
1212

13-
import com.intellij.openapi.util.Ref
14-
import com.intellij.testFramework.runInEdtAndWait
13+
import com.demonwav.mcdev.util.edtCoroutineScope
1514
import java.lang.reflect.Method
1615
import org.junit.jupiter.api.extension.ExtensionContext
1716
import org.junit.jupiter.api.extension.InvocationInterceptor
@@ -51,15 +50,11 @@ class EdtInterceptor : InvocationInterceptor {
5150
return
5251
}
5352

54-
val ref = Ref<Throwable>()
55-
runInEdtAndWait {
56-
try {
53+
val thrown = edtCoroutineScope {
54+
runCatching {
5755
invocation.proceed()
58-
} catch (t: Throwable) {
59-
ref.set(t)
60-
}
56+
}.exceptionOrNull()
6157
}
62-
val thrown = ref.get()
6358
if (thrown != null) {
6459
throw thrown
6560
}

0 commit comments

Comments
 (0)