Skip to content

Commit 9e94d78

Browse files
vepanimasintellij-monorepo-bot
authored andcommitted
[graphql] WEB-64772 add project level libraries support
GitOrigin-RevId: aa4b18de5a50f789edbd187d7593f8808782436f
1 parent 64e8227 commit 9e94d78

File tree

13 files changed

+106
-47
lines changed

13 files changed

+106
-47
lines changed

src/gen/com/intellij/lang/jsgraphql/schema/library/impl/GraphQLLibraryEntityImpl.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package com.intellij.lang.jsgraphql.schema.library.impl
22

3+
import com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryAttachmentScope
34
import com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntity
4-
import com.intellij.platform.workspace.storage.ConnectionId
5-
import com.intellij.platform.workspace.storage.EntitySource
6-
import com.intellij.platform.workspace.storage.EntityType
7-
import com.intellij.platform.workspace.storage.GeneratedCodeApiVersion
8-
import com.intellij.platform.workspace.storage.GeneratedCodeImplVersion
9-
import com.intellij.platform.workspace.storage.MutableEntityStorage
10-
import com.intellij.platform.workspace.storage.WorkspaceEntity
11-
import com.intellij.platform.workspace.storage.WorkspaceEntityInternalApi
5+
import com.intellij.platform.workspace.storage.*
6+
import com.intellij.platform.workspace.storage.annotations.Default
127
import com.intellij.platform.workspace.storage.impl.ModifiableWorkspaceEntityBase
138
import com.intellij.platform.workspace.storage.impl.WorkspaceEntityBase
149
import com.intellij.platform.workspace.storage.impl.WorkspaceEntityData
@@ -53,6 +48,8 @@ internal class GraphQLLibraryEntityImpl(private val dataSource: GraphQLLibraryEn
5348
return dataSource.description
5449
}
5550

51+
override var attachmentScope: GraphQLLibraryAttachmentScope = dataSource.attachmentScope
52+
5653
override val roots: Set<VirtualFileUrl>
5754
get() {
5855
readField("roots")
@@ -132,6 +129,7 @@ internal class GraphQLLibraryEntityImpl(private val dataSource: GraphQLLibraryEn
132129
if (this.identifier != dataSource.identifier) this.identifier = dataSource.identifier
133130
if (this.displayName != dataSource.displayName) this.displayName = dataSource.displayName
134131
if (this.description != dataSource?.description) this.description = dataSource.description
132+
if (this.attachmentScope != dataSource.attachmentScope) this.attachmentScope = dataSource.attachmentScope
135133
if (this.roots != dataSource.roots) this.roots = dataSource.roots.toMutableSet()
136134
updateChildToParentReferences(parents)
137135
}
@@ -170,6 +168,15 @@ internal class GraphQLLibraryEntityImpl(private val dataSource: GraphQLLibraryEn
170168
changedProperty.add("description")
171169
}
172170

171+
override var attachmentScope: GraphQLLibraryAttachmentScope
172+
get() = getEntityData().attachmentScope
173+
set(value) {
174+
checkModificationAllowed()
175+
getEntityData(true).attachmentScope = value
176+
changedProperty.add("attachmentScope")
177+
178+
}
179+
173180
private val rootsUpdater: (value: Set<VirtualFileUrl>) -> Unit = { value ->
174181
val _diff = diff
175182
if (_diff != null) index(this, "roots", value)
@@ -202,6 +209,7 @@ internal class GraphQLLibraryEntityData : WorkspaceEntityData<GraphQLLibraryEnti
202209
lateinit var identifier: String
203210
lateinit var displayName: String
204211
var description: String? = null
212+
var attachmentScope: GraphQLLibraryAttachmentScope = GraphQLLibraryAttachmentScope.GLOBAL
205213
lateinit var roots: MutableSet<VirtualFileUrl>
206214

207215
internal fun isIdentifierInitialized(): Boolean = ::identifier.isInitialized
@@ -244,6 +252,7 @@ internal class GraphQLLibraryEntityData : WorkspaceEntityData<GraphQLLibraryEnti
244252
override fun createDetachedEntity(parents: List<WorkspaceEntity.Builder<*>>): WorkspaceEntity.Builder<*> {
245253
return GraphQLLibraryEntity(identifier, displayName, roots, entitySource) {
246254
this.description = this@GraphQLLibraryEntityData.description
255+
this.attachmentScope = this@GraphQLLibraryEntityData.attachmentScope
247256
}
248257
}
249258

@@ -262,6 +271,7 @@ internal class GraphQLLibraryEntityData : WorkspaceEntityData<GraphQLLibraryEnti
262271
if (this.identifier != other.identifier) return false
263272
if (this.displayName != other.displayName) return false
264273
if (this.description != other.description) return false
274+
if (this.attachmentScope != other.attachmentScope) return false
265275
if (this.roots != other.roots) return false
266276
return true
267277
}
@@ -275,6 +285,7 @@ internal class GraphQLLibraryEntityData : WorkspaceEntityData<GraphQLLibraryEnti
275285
if (this.identifier != other.identifier) return false
276286
if (this.displayName != other.displayName) return false
277287
if (this.description != other.description) return false
288+
if (this.attachmentScope != other.attachmentScope) return false
278289
if (this.roots != other.roots) return false
279290
return true
280291
}
@@ -284,6 +295,7 @@ internal class GraphQLLibraryEntityData : WorkspaceEntityData<GraphQLLibraryEnti
284295
result = 31 * result + identifier.hashCode()
285296
result = 31 * result + displayName.hashCode()
286297
result = 31 * result + description.hashCode()
298+
result = 31 * result + attachmentScope.hashCode()
287299
result = 31 * result + roots.hashCode()
288300
return result
289301
}
@@ -293,6 +305,7 @@ internal class GraphQLLibraryEntityData : WorkspaceEntityData<GraphQLLibraryEnti
293305
result = 31 * result + identifier.hashCode()
294306
result = 31 * result + displayName.hashCode()
295307
result = 31 * result + description.hashCode()
308+
result = 31 * result + attachmentScope.hashCode()
296309
result = 31 * result + roots.hashCode()
297310
return result
298311
}

src/gen/com/intellij/lang/jsgraphql/schema/library/impl/MetadataStorageImpl.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ internal object MetadataStorageImpl: MetadataStorageBase() {
2525
OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "identifier", valueType = primitiveTypeStringNotNullable, withDefault = false),
2626
OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "displayName", valueType = primitiveTypeStringNotNullable, withDefault = false),
2727
OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "description", valueType = primitiveTypeStringNullable, withDefault = false),
28+
OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "attachmentScope", valueType = ValueTypeMetadata.SimpleType.CustomType(isNullable = false, typeMetadata = FinalClassMetadata.EnumClassMetadata(fqName = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryAttachmentScope", properties = listOf(), supertypes = listOf("java.io.Serializable",
29+
"kotlin.Comparable",
30+
"kotlin.Enum"), values = listOf("GLOBAL",
31+
"PROJECT"))), withDefault = true),
2832
OwnPropertyMetadata(isComputable = false, isKey = false, isOpen = false, name = "roots", valueType = ValueTypeMetadata.ParameterizedType(generics = listOf(ValueTypeMetadata.SimpleType.CustomType(isNullable = false, typeMetadata = FinalClassMetadata.KnownClass(fqName = "com.intellij.platform.workspace.storage.url.VirtualFileUrl"))), primitive = primitiveTypeSetNotNullable), withDefault = false)), extProperties = listOf(), isAbstract = false)
2933

3034
addMetadata(typeMetadata)
3135
}
3236

3337
override fun initializeMetadataHash() {
34-
addMetadataHash(typeFqn = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntity", metadataHash = 87991351)
38+
addMetadataHash(typeFqn = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntity", metadataHash = -1849614646)
39+
addMetadataHash(typeFqn = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryAttachmentScope", metadataHash = -912984699)
3540
addMetadataHash(typeFqn = "com.intellij.platform.workspace.storage.EntitySource", metadataHash = 212848188)
3641
addMetadataHash(typeFqn = "com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryEntitySource", metadataHash = 619168016)
3742
}

src/main/com/intellij/lang/jsgraphql/ide/config/GraphQLConfigContributor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.intellij.openapi.project.Project
77
interface GraphQLConfigContributor {
88
companion object {
99
@JvmField
10-
val EP_NAME =
10+
val EP_NAME: ExtensionPointName<GraphQLConfigContributor> =
1111
ExtensionPointName.create<GraphQLConfigContributor>("com.intellij.lang.jsgraphql.configContributor")
1212
}
1313

src/main/com/intellij/lang/jsgraphql/ide/config/GraphQLConfigProvider.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class GraphQLConfigProvider(private val project: Project, cs: CoroutineScope) :
7878
private const val CONFIG_RELOAD_DELAY = 500
7979

8080
@JvmStatic
81-
fun getInstance(project: Project) = project.service<GraphQLConfigProvider>()
81+
fun getInstance(project: Project): GraphQLConfigProvider = project.service<GraphQLConfigProvider>()
8282
}
8383

8484
private val generatedSourcesManager = GraphQLGeneratedSourcesManager.getInstance(project)
@@ -95,7 +95,7 @@ class GraphQLConfigProvider(private val project: Project, cs: CoroutineScope) :
9595

9696
/**
9797
* Use this service as a dependency for tracking content changes in configuration files.
98-
* Note, that calculations which depends on the scope structure should probably depend on
98+
* Note that calculations that depend on the scope structure should probably depend on
9999
* [GraphQLScopeDependency] instead, which tracks this service state and some more stuff,
100100
* which could affect resolve and type evaluation.
101101
*/
@@ -182,10 +182,10 @@ class GraphQLConfigProvider(private val project: Project, cs: CoroutineScope) :
182182
return ConfigEvaluationState(entry.status, entry.error)
183183
}
184184

185-
val isInitialized
185+
val isInitialized: Boolean
186186
get() = initialized
187187

188-
val hasExplicitConfiguration
188+
val hasExplicitConfiguration: Boolean
189189
get() = configData.isNotEmpty()
190190

191191
private fun findOverriddenConfig(file: PsiFile): GraphQLConfigOverride? {
@@ -476,9 +476,7 @@ class GraphQLConfigProvider(private val project: Project, cs: CoroutineScope) :
476476

477477
modificationTracker.incModificationCount()
478478
scopeDependency.update()
479-
WriteAction.run<Throwable> {
480-
PsiManager.getInstance(project).dropPsiCaches()
481-
}
479+
PsiManager.getInstance(project).dropPsiCaches()
482480

483481
DaemonCodeAnalyzer.getInstance(project).restart()
484482
project.messageBus.syncPublisher(GraphQLConfigListener.TOPIC).onConfigurationChanged()

src/main/com/intellij/lang/jsgraphql/ide/config/model/GraphQLProjectConfig.kt

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import com.intellij.lang.jsgraphql.ide.introspection.source.GraphQLGeneratedSour
1818
import com.intellij.lang.jsgraphql.ide.resolve.GraphQLScopeDependency
1919
import com.intellij.lang.jsgraphql.ide.resolve.GraphQLScopeProvider
2020
import com.intellij.lang.jsgraphql.psi.getPhysicalVirtualFile
21+
import com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryAttachmentScope
22+
import com.intellij.lang.jsgraphql.schema.library.GraphQLLibraryManager
2123
import com.intellij.openapi.project.Project
24+
import com.intellij.openapi.roots.ProjectFileIndex
2225
import com.intellij.openapi.util.io.FileUtil
2326
import com.intellij.openapi.vfs.VirtualFile
2427
import com.intellij.psi.PsiFile
@@ -37,9 +40,6 @@ class GraphQLProjectConfig(
3740
val environment: GraphQLEnvironmentSnapshot,
3841
val rootConfig: GraphQLConfig,
3942
) {
40-
private val generatedSourcesManager = GraphQLGeneratedSourcesManager.getInstance(project)
41-
private val remoteSchemasRegistry = GraphQLRemoteSchemasRegistry.getInstance(project)
42-
4343
val dir: VirtualFile = rootConfig.dir
4444

4545
val file: VirtualFile? = rootConfig.file
@@ -51,7 +51,7 @@ class GraphQLProjectConfig(
5151
val schema: List<GraphQLSchemaPointer> = (rawData.schema ?: defaultData?.schema)?.map {
5252
GraphQLSchemaPointer(project, dir, it, isLegacy, environment).also { pointer ->
5353
if (pointer.isRemote && !pointer.outputPath.isNullOrEmpty()) {
54-
remoteSchemasRegistry.associate(pointer.outputPath, file?.path ?: dir.path)
54+
GraphQLRemoteSchemasRegistry.getInstance(project).associate(pointer.outputPath, file?.path ?: dir.path)
5555
}
5656
}
5757
} ?: emptyList()
@@ -78,23 +78,31 @@ class GraphQLProjectConfig(
7878
private val expandContext
7979
get() = GraphQLExpandVariableContext(project, dir, isLegacy, environment)
8080

81-
private val outOfScopePaths: List<String> by lazy {
81+
private val outOfScopePaths: List<String> by lazy(LazyThreadSafetyMode.PUBLICATION) {
8282
schema.asSequence()
8383
.mapNotNull { it.filePath }
8484
.filter { it.startsWith("..") }
8585
.map { FileUtil.toCanonicalPath(FileUtil.join(dir.path, FileUtil.toSystemIndependentName(it))) }
8686
.toList()
8787
}
8888

89+
private val absolutePaths: List<String> by lazy(LazyThreadSafetyMode.PUBLICATION) {
90+
schema.asSequence()
91+
.mapNotNull { it.filePath }
92+
.filter { FileUtil.isAbsolute(it) }
93+
.toList()
94+
}
95+
8996
private val matchingCache = GraphQLFileMatcherCache.newInstance(project)
9097

9198
private val matchingSchemaCache = GraphQLFileMatcherCache.newInstance(project)
9299

93100
private val baseScope
94101
get() = GlobalSearchScope
95102
.allScope(project)
96-
.union(generatedSourcesManager.createGeneratedSourcesScope())
97-
.union(remoteSchemasRegistry.createRemoteIntrospectionScope())
103+
.union(GraphQLGeneratedSourcesManager.getInstance(project).createGeneratedSourcesScope())
104+
.union(GraphQLRemoteSchemasRegistry.getInstance(project).createRemoteIntrospectionScope())
105+
.union(GraphQLLibraryManager.getInstance(project).createScope(project, GraphQLLibraryAttachmentScope.PROJECT))
98106

99107
private val scopeCached: CachedValue<GlobalSearchScope> =
100108
CachedValuesManager.getManager(project).createCachedValue {
@@ -127,6 +135,7 @@ class GraphQLProjectConfig(
127135
}
128136

129137
private fun matchesImpl(virtualFile: VirtualFile): Boolean {
138+
val generatedSourcesManager = GraphQLGeneratedSourcesManager.getInstance(project)
130139
if (generatedSourcesManager.isGeneratedFile(virtualFile)) {
131140
return generatedSourcesManager.getSourceFile(virtualFile)?.let { matches(it) } ?: false
132141
}
@@ -148,6 +157,7 @@ class GraphQLProjectConfig(
148157
}
149158

150159
private fun matchesSchemaImpl(virtualFile: VirtualFile): Boolean {
160+
val generatedSourcesManager = GraphQLGeneratedSourcesManager.getInstance(project)
151161
if (generatedSourcesManager.isGeneratedFile(virtualFile)) {
152162
return generatedSourcesManager.getSourceFile(virtualFile)?.let { matchesSchema(it) } ?: false
153163
}
@@ -177,6 +187,12 @@ class GraphQLProjectConfig(
177187
return outOfScopePaths.any { FileUtil.pathsEqual(it, virtualFile.path) }
178188
}
179189

190+
fun isInProjectLibrary(file: VirtualFile): Boolean {
191+
return ProjectFileIndex.getInstance(project).isInLibrary(file)
192+
&& GraphQLLibraryManager.getInstance(project).isLibraryRoot(file, GraphQLLibraryAttachmentScope.PROJECT)
193+
&& absolutePaths.any { FileUtil.pathsEqual(it, file.path) }
194+
}
195+
180196
private fun matchPattern(candidate: VirtualFile, pointer: Any?): Boolean {
181197
return when (pointer) {
182198
is List<*> -> pointer.any { matchPattern(candidate, it) }

src/main/com/intellij/lang/jsgraphql/ide/config/scope/GraphQLConfigScope.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ open class GraphQLConfigScope(
3535
return true
3636
}
3737

38-
val matchingConfig = configProvider.resolveProjectConfig(psiFile) ?: return false
38+
val matchingConfig = configProvider.resolveProjectConfig(psiFile)
39+
if (matchingConfig == null) {
40+
return projectConfig.isInProjectLibrary(file)
41+
}
42+
3943
if (projectConfig == matchingConfig) {
4044
return true
4145
}

src/main/com/intellij/lang/jsgraphql/ide/resolve/GraphQLScopeProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class GraphQLScopeProvider(private val project: Project) : Disposable {
9898

9999
@JvmStatic
100100
fun createScope(project: Project, baseScope: GlobalSearchScope, file: VirtualFile? = null): GlobalSearchScope {
101-
var scope = baseScope.union(GraphQLLibraryManager.getInstance(project).createScope(project))
101+
var scope = baseScope.union(GraphQLLibraryManager.getInstance(project).createGlobalScope(project))
102102

103103
if (GraphQLModuleLibrariesScope.isEnabled) {
104104
scope = scope.union(GraphQLModuleLibrariesScope.create(project, file))

src/main/com/intellij/lang/jsgraphql/ide/validation/GraphQLGeneralErrorFilter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class GraphQLGeneralErrorFilter : GraphQLErrorFilter {
4343
}
4444

4545
if (error is TypeExtensionMissingBaseTypeError && namedNode?.name == GraphQLKnownTypes.QUERY_TYPE) {
46-
val roots = GraphQLLibraryManager.getInstance(project).libraryRoots.map { it.path }.toSet()
46+
val roots = GraphQLLibraryManager.getInstance(project).getLibraryRoots().map { it.path }.toSet()
4747
return namedNode.sourceLocation?.sourceName in roots
4848
}
4949

src/main/com/intellij/lang/jsgraphql/schema/library/GraphQLLibrary.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ data class GraphQLLibrary(
1515
val sourceRoots: Collection<VirtualFile>
1616
get() = rootUrls.mapNotNull { url -> url.virtualFile?.takeIf { it.isValid } }.toSet()
1717

18-
override fun getPresentableText(): String? =
18+
override fun getPresentableText(): String =
1919
GraphQLBundle.message("graphql.library.prefix", descriptor.displayName)
2020

2121
override fun getIcon(unused: Boolean): Icon = GraphQLIcons.Logos.GraphQL
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.intellij.lang.jsgraphql.schema.library
2+
3+
enum class GraphQLLibraryAttachmentScope {
4+
GLOBAL,
5+
PROJECT;
6+
7+
companion object {
8+
@JvmField
9+
val ALL: Array<GraphQLLibraryAttachmentScope> = GraphQLLibraryAttachmentScope.entries.toTypedArray()
10+
}
11+
}

0 commit comments

Comments
 (0)