Skip to content

Commit 6af89ed

Browse files
BoDmartinbonnin
andcommitted
Fix intellij plugin
Co-authored-by: Martin Bonnin <[email protected]>
1 parent 3fb7fec commit 6af89ed

File tree

59 files changed

+315
-313
lines changed

Some content is hidden

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

59 files changed

+315
-313
lines changed

.github/workflows/pr.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ jobs:
9999
- name: Maximize Build Space
100100
run: |
101101
sudo rm -rf /usr/share/dotnet
102-
sudo rm -rf /usr/local/lib/android
103102
sudo rm -rf /opt/ghc
104103
105104
# Check out current repository

intellij-plugin/build.gradle.kts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ intellij {
5757
// }
5858
}
5959

60+
val apolloDependencies = configurations.create("apolloDependencies").apply {
61+
listOf(":apollo-annotations", ":apollo-api", ":apollo-runtime").forEach {
62+
dependencies.add(project.dependencies.project(it, "jvmApiElements"))
63+
}
64+
}
65+
6066
tasks {
6167
withType<KotlinCompile> {
6268
kotlinOptions {
@@ -144,6 +150,7 @@ tasks {
144150
events.add(TestLogEvent.FAILED)
145151
showStandardStreams = true
146152
}
153+
inputs.files(apolloDependencies)
147154
}
148155
}
149156

@@ -185,7 +192,7 @@ fun isSnapshotBuild() = System.getenv("IJ_PLUGIN_SNAPSHOT").toBoolean()
185192

186193
apollo {
187194
service("apolloDebug") {
188-
packageName.set("com.apollographql.apollo.debug")
195+
packageName.set("com.apollographql.apollo3.debug")
189196
schemaFiles.from(file("../libraries/apollo-debug-server/graphql/schema.graphqls"))
190197
introspection {
191198
endpointUrl.set("http://localhost:12200/")

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/apollodebugserver/ApolloDebugClient.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package com.apollographql.ijplugin.apollodebugserver
22

33
import com.android.ddmlib.IDevice
44
import com.android.tools.idea.adb.AdbShellCommandsUtil
5-
import com.apollographql.apollo.ApolloClient
6-
import com.apollographql.apollo.debug.GetApolloClientsQuery
7-
import com.apollographql.apollo.debug.GetNormalizedCacheQuery
8-
import com.apollographql.apollo.network.http.LoggingInterceptor
5+
import com.apollographql.apollo3.ApolloClient
6+
import com.apollographql.apollo3.debug.GetApolloClientsQuery
7+
import com.apollographql.apollo3.debug.GetNormalizedCacheQuery
8+
import com.apollographql.apollo3.network.http.LoggingInterceptor
9+
import com.apollographql.ijplugin.util.apollo4
910
import com.apollographql.ijplugin.util.executeCatching
1011
import com.apollographql.ijplugin.util.logd
1112
import com.apollographql.ijplugin.util.logw
@@ -118,7 +119,7 @@ class ApolloDebugClient(
118119

119120
val String.normalizedCacheSimpleName: String
120121
get() = when (this) {
121-
"com.apollographql.apollo.cache.normalized.api.MemoryCache" -> "MemoryCache"
122-
"com.apollographql.apollo.cache.normalized.sql.SqlNormalizedCache" -> "SqlNormalizedCache"
122+
"$apollo4.cache.normalized.api.MemoryCache" -> "MemoryCache"
123+
"$apollo4.cache.normalized.sql.SqlNormalizedCache" -> "SqlNormalizedCache"
123124
else -> this
124125
}

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/inspection/Apollo4AvailableInspection.kt

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.apollographql.ijplugin.project.apolloProjectService
66
import com.apollographql.ijplugin.refactoring.migration.v3tov4.ApolloV3ToV4MigrationProcessor
77
import com.apollographql.ijplugin.telemetry.TelemetryEvent
88
import com.apollographql.ijplugin.telemetry.telemetryService
9+
import com.apollographql.ijplugin.util.apollo3
910
import com.apollographql.ijplugin.util.getMethodName
1011
import com.apollographql.ijplugin.util.unquoted
1112
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
@@ -22,17 +23,11 @@ import org.jetbrains.kotlin.idea.util.application.isUnitTestMode
2223
import org.jetbrains.kotlin.psi.KtBinaryExpression
2324
import org.jetbrains.kotlin.psi.KtCallExpression
2425
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
25-
import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
26-
import org.jetbrains.kotlin.psi.KtStringTemplateEntry
2726
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
28-
import org.toml.lang.psi.TomlInlineTable
2927
import org.toml.lang.psi.TomlLiteral
30-
import org.toml.lang.psi.TomlTable
3128
import org.toml.lang.psi.ext.TomlLiteralKind
3229
import org.toml.lang.psi.ext.kind
3330

34-
private const val apollo3 = "com.apollographql.apollo"
35-
3631
class Apollo4AvailableInspection : LocalInspectionTool() {
3732
// XXX kts files are not highlighted in tests
3833
private val buildGradleFileName = if (isUnitTestMode()) "build.gradle.kt" else "build.gradle.kts"
@@ -42,8 +37,6 @@ class Apollo4AvailableInspection : LocalInspectionTool() {
4237

4338
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
4439
return object : PsiElementVisitor() {
45-
private val registeredTomlVersionValues = mutableSetOf<PsiElement>()
46-
4740
override fun visitElement(element: PsiElement) {
4841
if (!isEnabled) return
4942
if (!element.project.apolloProjectService.apolloVersion.isAtLeastV3) return
@@ -62,97 +55,55 @@ class Apollo4AvailableInspection : LocalInspectionTool() {
6255
if (element.kind !is TomlLiteralKind.String) return
6356
val dependencyText = element.text.unquoted()
6457
if (dependencyText == apollo3 || dependencyText.startsWith("$apollo3:")) {
65-
// Find the associated version
66-
val versionEntry = (element.parent.parent as? TomlInlineTable)?.entries
67-
?.first { it.key.text == "version" || it.key.text == "version.ref" } ?: return
68-
if (versionEntry.key.text == "version") {
69-
val version = versionEntry.value?.firstChild?.text?.unquoted() ?: return
70-
if (!version.startsWith("4")) {
71-
holder.registerProblem(element.parent.parent.parent, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
72-
}
73-
} else {
74-
// Resolve the reference
75-
val versionsTable = element.containingFile.children.filterIsInstance<TomlTable>()
76-
.firstOrNull { it.header.key?.text == "versions" } ?: return
77-
val versionRefKey = versionEntry.value?.text?.unquoted()
78-
val refTarget = versionsTable.entries.firstOrNull { it.key.text == versionRefKey } ?: return
79-
val version = refTarget.value?.firstChild?.text?.unquoted() ?: return
80-
if (!version.startsWith("4")) {
81-
// Do not highlight the same element several times
82-
if (refTarget.value!! !in registeredTomlVersionValues) {
83-
holder.registerProblem(refTarget.value!!, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
84-
registeredTomlVersionValues.add(refTarget.value!!)
85-
}
86-
}
87-
}
58+
holder.registerProblem(element.parent.parent.parent, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
8859
}
8960
}
9061

9162
private fun visitBuildGradleKts(callExpression: KtCallExpression, holder: ProblemsHolder) {
9263
when (callExpression.getMethodName()) {
9364
"id" -> {
9465
// id("xxx")
95-
val dependencyText = callExpression.getArgumentAsStringTemplateEntries(0)?.getSingleEntry() ?: return
66+
val dependencyText = callExpression.getArgumentAsString(0) ?: return
9667
if (dependencyText != apollo3) return
97-
when (val element = callExpression.parent) {
98-
is KtBinaryExpression -> {
99-
// id("xxx") version yyy
100-
val version = (element.right as? KtStringTemplateExpression)?.entries?.getSingleEntry() ?: return
101-
if (!version.startsWith("4")) {
102-
holder.registerProblem(element, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
103-
}
68+
val element = when (val parent = callExpression.parent) {
69+
is KtBinaryExpression, is KtDotQualifiedExpression -> {
70+
parent
10471
}
10572

106-
is KtDotQualifiedExpression -> {
107-
// id("xxx").version(yyy)
108-
val versionCallExpression = element.selectorExpression as? KtCallExpression
109-
val version = versionCallExpression?.getArgumentAsStringTemplateEntries(0)?.getSingleEntry() ?: return
110-
if (!version.startsWith("4")) {
111-
holder.registerProblem(element, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
112-
}
73+
else -> {
74+
callExpression
11375
}
11476
}
77+
holder.registerProblem(element, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
11578
}
11679

11780
"implementation", "api", "testImplementation", "testApi" -> {
11881
when (callExpression.valueArguments.size) {
11982
// implementation("xxx:yyy:zzz")
12083
1 -> {
121-
val dependency = callExpression.getArgumentAsStringTemplateEntries(0)?.getSingleEntry() ?: return
84+
val dependency = callExpression.getArgumentAsString(0) ?: return
12285
val dependencyElements = dependency.split(":")
123-
if (dependencyElements.size != 3) return
12486
val groupId = dependencyElements[0]
12587
if (groupId != apollo3) return
126-
val version = dependencyElements[2]
127-
if (!version.startsWith("4")) {
128-
holder.registerProblem(callExpression, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
129-
}
88+
holder.registerProblem(callExpression, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
13089
}
13190

13291

92+
// implementation("xxx", "yyy")
13393
// implementation("xxx", "yyy", "zzz")
134-
3 -> {
135-
val groupId = callExpression.getArgumentAsStringTemplateEntries(0)?.getSingleEntry() ?: return
94+
2, 3 -> {
95+
val groupId = callExpression.getArgumentAsString(0) ?: return
13696
if (groupId != apollo3) return
137-
val version = callExpression.getArgumentAsStringTemplateEntries(2)?.getSingleEntry() ?: return
138-
if (!version.startsWith("4")) {
139-
holder.registerProblem(callExpression, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
140-
}
97+
holder.registerProblem(callExpression, ApolloBundle.message("inspection.apollo4Available.reportText"), Apollo4AvailableQuickFix)
14198
}
14299
}
143100
}
144101
}
145102
}
146103

147-
private fun KtCallExpression.getArgumentAsStringTemplateEntries(index: Int): Array<KtStringTemplateEntry>? =
148-
(valueArgumentList?.arguments?.getOrNull(index)
149-
?.children?.firstOrNull() as? KtStringTemplateExpression)?.entries
150-
151-
// Only consider simple strings (no templates)
152-
private fun Array<KtStringTemplateEntry>.getSingleEntry(): String? {
153-
if (size != 1 || this[0] !is KtLiteralStringTemplateEntry) return null
154-
return this[0].text.unquoted()
155-
}
104+
private fun KtCallExpression.getArgumentAsString(index: Int): String? =
105+
(valueArgumentList?.arguments?.getOrNull(index)
106+
?.children?.firstOrNull() as? KtStringTemplateExpression)?.text?.unquoted()
156107
}
157108
}
158109
}

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/inspection/ApolloInputConstructorNamedArgsInspection.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.apollographql.ijplugin.navigation.isApolloInputClassReference
55
import com.apollographql.ijplugin.project.apolloProjectService
66
import com.apollographql.ijplugin.telemetry.TelemetryEvent
77
import com.apollographql.ijplugin.telemetry.telemetryService
8+
import com.apollographql.ijplugin.util.apollo3
9+
import com.apollographql.ijplugin.util.apollo4
810
import com.apollographql.ijplugin.util.cast
911
import com.apollographql.ijplugin.util.originalClassName
1012
import com.apollographql.ijplugin.util.registerProblem
@@ -105,8 +107,8 @@ object ChangeToBuilderQuickFix : LocalQuickFix {
105107
val argumentExpression = getArgumentExpression() ?: return "?"
106108
val dotQualifiedExpression = argumentExpression.cast<KtDotQualifiedExpression>()
107109
val receiverClassName = dotQualifiedExpression?.receiverExpression?.mainReference?.resolve()?.kotlinFqName?.asString()
108-
val isOptional = receiverClassName == "com.apollographql.apollo.api.Optional"
109-
val isOptionalCompanion = receiverClassName == "com.apollographql.apollo.api.Optional.Companion"
110+
val isOptional = receiverClassName == "$apollo3.api.Optional" || receiverClassName == "$apollo4.api.Optional"
111+
val isOptionalCompanion = receiverClassName == "$apollo3.api.Optional.Companion" || receiverClassName == "$apollo4.api.Optional.Companion"
110112
val selectorCallExpression = dotQualifiedExpression?.selectorExpression.cast<KtCallExpression>()
111113
val selectorCallExpressionText = selectorCallExpression?.calleeExpression?.text
112114
val nameReferenceExpressionText = dotQualifiedExpression?.selectorExpression.cast<KtNameReferenceExpression>()?.text

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/inspection/ApolloOneOfInputCreationInspection.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.apollographql.ijplugin.navigation.findInputTypeGraphQLDefinitions
55
import com.apollographql.ijplugin.navigation.isApolloInputClass
66
import com.apollographql.ijplugin.navigation.isApolloInputClassReference
77
import com.apollographql.ijplugin.project.apolloProjectService
8+
import com.apollographql.ijplugin.util.apollo4
89
import com.apollographql.ijplugin.util.canBeNull
910
import com.apollographql.ijplugin.util.cast
1011
import com.apollographql.ijplugin.util.type
@@ -49,7 +50,7 @@ class ApolloOneOfInputCreationInspection : LocalInspectionTool() {
4950
return
5051
}
5152
val arg = expression.valueArguments.first()
52-
if (arg.getArgumentExpression()?.type()?.fqName?.asString() == "com.apollographql.apollo.api.Optional.Absent") {
53+
if (arg.getArgumentExpression()?.type()?.fqName?.asString() == "$apollo4.api.Optional.Absent") {
5354
holder.registerProblem(expression.calleeExpression!!, ApolloBundle.message("inspection.oneOfInputCreation.reportText.constructor.argIsAbsent"))
5455
}
5556
}

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/navigation/GraphQLNavigation.kt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.apollographql.ijplugin.navigation
22

3+
import com.apollographql.ijplugin.util.apollo3
4+
import com.apollographql.ijplugin.util.apollo4
35
import com.apollographql.ijplugin.util.asKtClass
46
import com.apollographql.ijplugin.util.capitalizeFirstLetter
57
import com.apollographql.ijplugin.util.containingKtFile
@@ -41,14 +43,24 @@ import org.jetbrains.kotlin.psi.KtSuperTypeCallEntry
4143
import org.jetbrains.kotlin.psi.psiUtil.containingClass
4244

4345
private val APOLLO_OPERATION_TYPES = setOf(
44-
FqName("com.apollographql.apollo.api.Query"),
45-
FqName("com.apollographql.apollo.api.Mutation"),
46-
FqName("com.apollographql.apollo.api.Subscription"),
46+
FqName("$apollo3.api.Query"),
47+
FqName("$apollo3.api.Mutation"),
48+
FqName("$apollo3.api.Subscription"),
49+
50+
FqName("$apollo4.api.Query"),
51+
FqName("$apollo4.api.Mutation"),
52+
FqName("$apollo4.api.Subscription"),
4753
)
4854

49-
private val APOLLO_FRAGMENT_TYPE = FqName("com.apollographql.apollo.api.Fragment.Data")
55+
private val APOLLO_FRAGMENT_TYPE = setOf(
56+
FqName("$apollo3.api.Fragment.Data"),
57+
FqName("$apollo4.api.Fragment.Data"),
58+
)
5059

51-
private val APOLLO_ENUM_TYPE = FqName("com.apollographql.apollo.api.EnumType")
60+
private val APOLLO_ENUM_TYPE = setOf(
61+
FqName("$apollo3.api.EnumType"),
62+
FqName("$apollo4.api.EnumType"),
63+
)
5264

5365
fun KtNameReferenceExpression.isApolloOperationOrFragmentReference(): Boolean {
5466
return resolveKtName()?.asKtClass()?.isApolloOperationOrFragment() == true
@@ -75,7 +87,7 @@ fun KtClass.isApolloOperation(): Boolean {
7587
fun KtClass.isApolloFragment(): Boolean {
7688
return superTypeListEntries.any {
7789
val superType = it.typeAsUserType?.referenceExpression?.resolveKtName()?.kotlinFqName
78-
superType == APOLLO_FRAGMENT_TYPE
90+
superType in APOLLO_FRAGMENT_TYPE
7991
} ||
8092
// Fallback for fragments in responseBased codegen: they are interfaces generated in a .fragment package.
8193
// This can lead to false positives, but consequences are not dire.
@@ -85,7 +97,7 @@ fun KtClass.isApolloFragment(): Boolean {
8597
fun KtClass.isApolloOperationOrFragment(): Boolean {
8698
return superTypeListEntries.any {
8799
val superType = it.typeAsUserType?.referenceExpression?.resolveKtName()?.kotlinFqName
88-
superType in APOLLO_OPERATION_TYPES || superType == APOLLO_FRAGMENT_TYPE
100+
superType in APOLLO_OPERATION_TYPES || superType in APOLLO_FRAGMENT_TYPE
89101
} ||
90102
// Fallback for fragments in responseBased codegen: they are interfaces generated in a .fragment package.
91103
// This can lead to false positives, but consequences are not dire.
@@ -102,7 +114,7 @@ fun KtClass.isApolloEnumClass() = isEnum() &&
102114
isEnum() && companionObjects.any { companion ->
103115
companion.declarations.filterIsInstance<KtProperty>().any { property ->
104116
property.name == "type" &&
105-
property.type()?.fqName == APOLLO_ENUM_TYPE
117+
property.type()?.fqName in APOLLO_ENUM_TYPE
106118
}
107119
}
108120

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/normalizedcache/PullFromDeviceDialog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.apollographql.ijplugin.normalizedcache
33
import android.annotation.SuppressLint
44
import com.android.ddmlib.Client
55
import com.android.ddmlib.IDevice
6-
import com.apollographql.apollo.debug.GetApolloClientsQuery
6+
import com.apollographql.apollo3.debug.GetApolloClientsQuery
77
import com.apollographql.ijplugin.ApolloBundle
88
import com.apollographql.ijplugin.apollodebugserver.ApolloDebugClient
99
import com.apollographql.ijplugin.apollodebugserver.ApolloDebugClient.Companion.getApolloDebugClients

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/normalizedcache/provider/ApolloDebugNormalizedCacheProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.apollographql.ijplugin.normalizedcache.provider
22

33
import com.apollographql.apollo.cache.normalized.api.CacheKey
4-
import com.apollographql.apollo.debug.GetNormalizedCacheQuery
4+
import com.apollographql.apollo3.debug.GetNormalizedCacheQuery
55
import com.apollographql.ijplugin.normalizedcache.NormalizedCache
66
import com.apollographql.ijplugin.normalizedcache.NormalizedCache.Field
77
import com.apollographql.ijplugin.normalizedcache.NormalizedCache.FieldValue

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/refactoring/migration/ApolloMigrationRefactoringProcessor.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ import org.jetbrains.kotlin.psi.KtPsiFactory
3131
import org.jetbrains.kotlin.resolve.ImportPath
3232
import org.jetbrains.plugins.gradle.util.GradleConstants
3333

34-
const val apollo2 = "com.apollographql.apollo"
35-
const val apollo3 = "com.apollographql.apollo"
36-
3734
/**
3835
* Generic processor for migrations.
3936
*

0 commit comments

Comments
 (0)