From 7354a4e504167c3d41c37ad30726ed126c7bc4d9 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 20 May 2025 08:22:33 +0200 Subject: [PATCH] rename ModuleEx.init to initNewlyAddedModule and note for the method GitOrigin-RevId: 8683ae0e2e0ed7ef86802ea56a71dd2ee97fc4a7 --- .../module/impl/ModuleComponentManager.kt | 29 ++++++++++---- .../openapi/module/impl/ModuleImpl.kt | 7 ++-- .../module/ModuleManagerComponentBridge.kt | 11 +++--- .../openapi/module/impl/ModuleEx.java | 3 +- .../PrecomputedExtensionModel.kt | 10 ++++- .../module/ModifiableModuleModelBridgeImpl.kt | 39 ++++++++++++------- .../serviceContainer/ComponentManagerImpl.kt | 24 +++--------- 7 files changed, 72 insertions(+), 51 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/openapi/module/impl/ModuleComponentManager.kt b/platform/lang-impl/src/com/intellij/openapi/module/impl/ModuleComponentManager.kt index fc20cafb7d8f..63fcfbefcbe3 100644 --- a/platform/lang-impl/src/com/intellij/openapi/module/impl/ModuleComponentManager.kt +++ b/platform/lang-impl/src/com/intellij/openapi/module/impl/ModuleComponentManager.kt @@ -20,10 +20,8 @@ import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.extensions.PluginDescriptor import com.intellij.openapi.module.Module import com.intellij.openapi.util.NlsSafe -import com.intellij.serviceContainer.ComponentManagerImpl -import com.intellij.serviceContainer.PrecomputedExtensionModel -import com.intellij.serviceContainer.emptyConstructorMethodType -import com.intellij.serviceContainer.findConstructorOrNull +import com.intellij.serviceContainer.* +import com.intellij.util.concurrency.annotations.RequiresBlockingContext import com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModuleBridgeImpl import org.jetbrains.annotations.ApiStatus import org.jetbrains.annotations.TestOnly @@ -54,6 +52,22 @@ class ModuleComponentManager(parent: ComponentManagerImpl) : ComponentManagerImp emptyConstructorMethodType, ) + internal fun initModuleContainer(plugins: List, precomputedExtensionModel: PrecomputedExtensionModel) { + // register services before registering extensions because plugins can access services in their extensions, + // which can be invoked right away if the plugin is loaded dynamically + for (plugin in plugins) { + registerServices(plugin.moduleContainerDescriptor.services, plugin) + for (content in plugin.contentModules) { + val services = content.descriptor.moduleContainerDescriptor.services + if (services.isNotEmpty()) { + registerServices(services, plugin) + } + } + } + + registerExtensionPointsAndExtensionByPrecomputedModel(precomputedExtensionModel, null) + } + fun initForModule(module: Module) { this.module = module registerServiceInstance(serviceInterface = Module::class.java, instance = module, pluginDescriptor = fakeCorePluginDescriptor) @@ -104,6 +118,7 @@ class ModuleComponentManager(parent: ComponentManagerImpl) : ComponentManagerImp // expose to call it via ModuleImpl @Suppress("OVERRIDE_DEPRECATION", "DEPRECATION") + @RequiresBlockingContext public override fun createComponents() { super.createComponents() } @@ -114,13 +129,13 @@ class ModuleComponentManager(parent: ComponentManagerImpl) : ComponentManagerImp precomputedExtensionModel: PrecomputedExtensionModel?, listenerCallbacks: MutableList?, ) { + assert(listenerCallbacks.isNullOrEmpty()) if (precomputedExtensionModel == null) { LOG.error("precomputedExtensionModel must not be null") - super.registerComponents(modules, app, null, listenerCallbacks) + initModuleContainer(modules, precomputeModuleLevelExtensionModel()) } else { - assert(listenerCallbacks.isNullOrEmpty()) - super.initModuleContainer(modules, precomputedExtensionModel) + initModuleContainer(modules, precomputedExtensionModel) } if (modules.any { it.pluginId == PluginManagerCore.CORE_ID }) { diff --git a/platform/lang-impl/src/com/intellij/openapi/module/impl/ModuleImpl.kt b/platform/lang-impl/src/com/intellij/openapi/module/impl/ModuleImpl.kt index b4e02e12406d..557e02895dfb 100644 --- a/platform/lang-impl/src/com/intellij/openapi/module/impl/ModuleImpl.kt +++ b/platform/lang-impl/src/com/intellij/openapi/module/impl/ModuleImpl.kt @@ -6,6 +6,7 @@ package com.intellij.openapi.module.impl import com.intellij.configurationStore.NonPersistentModuleStore import com.intellij.configurationStore.RenameableStateStorageManager import com.intellij.ide.highlighter.ModuleFileType +import com.intellij.ide.plugins.PluginManagerCore import com.intellij.openapi.components.* import com.intellij.openapi.components.impl.stores.ComponentStoreOwner import com.intellij.openapi.components.impl.stores.IComponentStore @@ -22,8 +23,8 @@ import com.intellij.openapi.util.SimpleModificationTracker import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.pointers.VirtualFilePointer import com.intellij.psi.search.GlobalSearchScope -import com.intellij.serviceContainer.ComponentManagerImpl.Companion.fakeCorePluginDescriptor import com.intellij.serviceContainer.getComponentManagerImpl +import com.intellij.serviceContainer.precomputeModuleLevelExtensionModel import com.intellij.util.messages.MessageBus import com.intellij.util.xmlb.annotations.MapAnnotation import com.intellij.util.xmlb.annotations.Property @@ -71,11 +72,11 @@ open class ModuleImpl( internal fun getModuleComponentManager(): ModuleComponentManager = componentManager.getComponentManagerImpl() as ModuleComponentManager - override fun init() { + override fun initNewlyAddedModule() { // do not measure (activityNamePrefix method not overridden by this class) // because there are a lot of modules and no need to measure each one val moduleComponentManager = getModuleComponentManager() - moduleComponentManager.registerComponents() + moduleComponentManager.initModuleContainer(PluginManagerCore.getPluginSet().enabledPlugins, precomputeModuleLevelExtensionModel()) @Suppress("DEPRECATION") moduleComponentManager.createComponents() } diff --git a/platform/lang-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/module/ModuleManagerComponentBridge.kt b/platform/lang-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/module/ModuleManagerComponentBridge.kt index 93a1a1019678..832c0dee35f6 100644 --- a/platform/lang-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/module/ModuleManagerComponentBridge.kt +++ b/platform/lang-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/module/ModuleManagerComponentBridge.kt @@ -107,6 +107,7 @@ open class ModuleManagerComponentBridge(private val project: Project, coroutineS else { null } + for (change in moduleChanges) { if (change !is EntityChange.Added) { continue @@ -189,7 +190,7 @@ open class ModuleManagerComponentBridge(private val project: Project, coroutineS init: (ModuleBridge) -> Unit, ): ModuleBridge { val componentManager = ModuleComponentManager(project.getComponentManagerImpl()) - return ModuleBridgeImpl( + val moduleBridge = ModuleBridgeImpl( moduleEntityId = symbolicId, name = name, project = project, @@ -197,10 +198,10 @@ open class ModuleManagerComponentBridge(private val project: Project, coroutineS entityStorage = entityStorage, diff = diff, componentManager = componentManager, - ).also { - componentManager.initForModule(it) - init(it) - } + ) + componentManager.initForModule(moduleBridge) + init(moduleBridge) + return moduleBridge } } diff --git a/platform/projectModel-impl/src/com/intellij/openapi/module/impl/ModuleEx.java b/platform/projectModel-impl/src/com/intellij/openapi/module/impl/ModuleEx.java index d34d219acf06..142ffc2a4f72 100644 --- a/platform/projectModel-impl/src/com/intellij/openapi/module/impl/ModuleEx.java +++ b/platform/projectModel-impl/src/com/intellij/openapi/module/impl/ModuleEx.java @@ -7,7 +7,8 @@ import org.jetbrains.annotations.NotNull; @ApiStatus.Internal public interface ModuleEx extends Module { - default void init() { + // not called for module on loading, only if module created after project opening + default void initNewlyAddedModule() { } default void markAsLoaded() { diff --git a/platform/projectModel-impl/src/com/intellij/serviceContainer/PrecomputedExtensionModel.kt b/platform/projectModel-impl/src/com/intellij/serviceContainer/PrecomputedExtensionModel.kt index 4762c3b54901..cd4537785d35 100644 --- a/platform/projectModel-impl/src/com/intellij/serviceContainer/PrecomputedExtensionModel.kt +++ b/platform/projectModel-impl/src/com/intellij/serviceContainer/PrecomputedExtensionModel.kt @@ -1,5 +1,5 @@ // Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -@file:Suppress("ReplaceGetOrSet", "ReplacePutWithAssignment") +@file:Suppress("ReplaceGetOrSet", "ReplacePutWithAssignment", "ReplaceJavaStaticMethodWithKotlinAnalog") package com.intellij.serviceContainer @@ -16,9 +16,11 @@ class PrecomputedExtensionModel( @JvmField val nameToExtensions: Map>>>, ) +private val EMPTY = PrecomputedExtensionModel(extensionPoints = java.util.List.of(), nameToExtensions = java.util.Map.of()) + @ApiStatus.Internal fun precomputeModuleLevelExtensionModel(): PrecomputedExtensionModel { - val modules = PluginManagerCore.getPluginSet().getEnabledModules() + val modules = PluginManagerCore.getPluginSet().enabledPlugins var extensionPointTotalCount = 0 val mutableNameToExtensions = HashMap>>>() @@ -36,6 +38,10 @@ fun precomputeModuleLevelExtensionModel(): PrecomputedExtensionModel { } } + if (extensionPointDescriptors.isEmpty() || mutableNameToExtensions.isEmpty()) { + return EMPTY + } + val nameToExtensions = java.util.Map.copyOf(mutableNameToExtensions) // step 2 - collect container level extensions executeRegisterTask(modules) { pluginDescriptor -> diff --git a/platform/projectModel-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/module/ModifiableModuleModelBridgeImpl.kt b/platform/projectModel-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/module/ModifiableModuleModelBridgeImpl.kt index 2b8b3e0b59ab..6b0b3f993c39 100644 --- a/platform/projectModel-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/module/ModifiableModuleModelBridgeImpl.kt +++ b/platform/projectModel-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/module/ModifiableModuleModelBridgeImpl.kt @@ -13,6 +13,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.util.Disposer import com.intellij.openapi.util.SystemInfo import com.intellij.openapi.util.io.FileUtil +import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.vfs.VfsUtilCore import com.intellij.platform.backend.workspace.WorkspaceModel import com.intellij.platform.diagnostic.telemetry.helpers.MillisecondsMeasurer @@ -60,17 +61,25 @@ internal class ModifiableModuleModelBridgeImpl( override fun getModules(): Array = currentModuleSet.toTypedArray() override fun newNonPersistentModule(moduleName: String, moduleTypeId: String): Module { - val moduleEntity = diff addEntity ModuleEntity(name = moduleName, - dependencies = listOf(ModuleSourceDependency), - entitySource = NonPersistentEntitySource - ) + val moduleEntity = diff.addEntity(ModuleEntity( + name = moduleName, + dependencies = listOf(ModuleSourceDependency), + entitySource = NonPersistentEntitySource, + )) - val module = moduleManager.createModule(moduleEntity.symbolicId, moduleName, null, entityStorageOnDiff, diff) {} + val module = moduleManager.createModule( + symbolicId = moduleEntity.symbolicId, + name = moduleName, + virtualFileUrl = null, + entityStorage = entityStorageOnDiff, + diff = diff, + init = {}, + ) diff.mutableModuleMap.addMapping(moduleEntity, module) modulesToAdd.put(moduleName, module) currentModuleSet.add(module) - module.init() + module.initNewlyAddedModule() module.setModuleType(moduleTypeId) return module } @@ -79,7 +88,7 @@ internal class ModifiableModuleModelBridgeImpl( // TODO Handle filePath, add correct iml source with a path // TODO Must be in sync with module loading. It is not now - val canonicalPath = FileUtil.toSystemIndependentName(resolveShortWindowsName(filePath)) + val canonicalPath = FileUtilRt.toSystemIndependentName(resolveShortWindowsName(filePath)) val existingModule = getModuleByFilePath(canonicalPath) if (existingModule != null) { @@ -118,16 +127,16 @@ internal class ModifiableModuleModelBridgeImpl( } private fun createModuleInstance(moduleEntity: ModuleEntity, isNew: Boolean): ModuleBridge { - val plugins = PluginManagerCore.getPluginSet().getEnabledModules() - val moduleInstance = moduleManager.createModuleInstance(moduleEntity = moduleEntity, - versionedStorage = entityStorageOnDiff, - diff = diff, - isNew = isNew, - precomputedExtensionModel = precomputeModuleLevelExtensionModel(), - plugins = plugins + val moduleInstance = moduleManager.createModuleInstance( + moduleEntity = moduleEntity, + versionedStorage = entityStorageOnDiff, + diff = diff, + isNew = isNew, + precomputedExtensionModel = precomputeModuleLevelExtensionModel(), + plugins = PluginManagerCore.getPluginSet().enabledPlugins, ) diff.mutableModuleMap.addMapping(moduleEntity, moduleInstance) - modulesToAdd[moduleEntity.name] = moduleInstance + modulesToAdd.put(moduleEntity.name, moduleInstance) currentModuleSet.add(moduleInstance) return moduleInstance } diff --git a/platform/service-container/src/com/intellij/serviceContainer/ComponentManagerImpl.kt b/platform/service-container/src/com/intellij/serviceContainer/ComponentManagerImpl.kt index 28cee8f1aef1..09aafdf76154 100644 --- a/platform/service-container/src/com/intellij/serviceContainer/ComponentManagerImpl.kt +++ b/platform/service-container/src/com/intellij/serviceContainer/ComponentManagerImpl.kt @@ -41,6 +41,7 @@ import com.intellij.openapi.util.* import com.intellij.platform.instanceContainer.internal.* import com.intellij.platform.util.coroutines.childScope import com.intellij.util.concurrency.ThreadingAssertions +import com.intellij.util.concurrency.annotations.RequiresBlockingContext import com.intellij.util.containers.UList import com.intellij.util.messages.MessageBus import com.intellij.util.messages.MessageBusFactory @@ -394,24 +395,10 @@ abstract class ComponentManagerImpl( } } - protected fun initModuleContainer(plugins: List, precomputedExtensionModel: PrecomputedExtensionModel) { - // register services before registering extensions because plugins can access services in their extensions, - // which can be invoked right away if the plugin is loaded dynamically - for (plugin in plugins) { - registerServices(plugin.moduleContainerDescriptor.services, plugin) - for (content in plugin.contentModules) { - val services = content.descriptor.moduleContainerDescriptor.services - if (services.isNotEmpty()) { - registerServices(services, plugin) - } - } - } - - registerExtensionPointsAndExtensionByPrecomputedModel(precomputedExtensionModel, null) - } - - private fun registerExtensionPointsAndExtensionByPrecomputedModel(precomputedExtensionModel: PrecomputedExtensionModel, - listenerCallbacks: MutableList?) { + protected fun registerExtensionPointsAndExtensionByPrecomputedModel( + precomputedExtensionModel: PrecomputedExtensionModel, + listenerCallbacks: MutableList?, + ) { if (precomputedExtensionModel.extensionPoints.isEmpty()) { return } @@ -503,6 +490,7 @@ abstract class ComponentManagerImpl( @Suppress("DuplicatedCode") @Deprecated(message = "Use createComponentsNonBlocking") + @RequiresBlockingContext protected open fun createComponents() { LOG.assertTrue(containerState.get() == ContainerState.PRE_INIT)