[java] IJPL-213056 Move RepositoryLibrary WSM updates processing to background

(cherry picked from commit cbcf288d6ffb6fe018867373f052d3ddc40efda1)

IJ-CR-179746

GitOrigin-RevId: eefeaf0ff70c5ea3696ef9329afd8acbe4593d4c
This commit is contained in:
Vladislav.Yaroshchuk
2025-10-22 17:03:12 +04:00
committed by intellij-monorepo-bot
parent e0d45b0dd6
commit 12adea85e0
4 changed files with 64 additions and 47 deletions

View File

@@ -1,45 +0,0 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.jarRepository
import com.intellij.openapi.project.Project
import com.intellij.platform.backend.workspace.WorkspaceModelChangeListener
import com.intellij.platform.workspace.jps.entities.LibraryEntity
import com.intellij.platform.workspace.jps.entities.LibraryPropertiesEntity
import com.intellij.platform.workspace.storage.EntityChange
import com.intellij.platform.workspace.storage.VersionedStorageChange
private class RepositoryLibraryChangeListener(private val project: Project) : WorkspaceModelChangeListener {
override fun changed(event: VersionedStorageChange) {
val settings = RepositoryLibrarySettings.getInstanceOrDefaults(project)
val jarRepositoryAutoBindEnabled = settings.isJarRepositoryAutoBindEnabled()
val sha256ChecksumAutoBuildEnabled = settings.isSha256ChecksumAutoBuildEnabled()
// exit fast, avoid heavy computations if the feature is not enabled
if (!jarRepositoryAutoBindEnabled && !sha256ChecksumAutoBuildEnabled) {
return
}
val changedLibraryEntities = mutableSetOf<LibraryEntity?>()
// Handle library roots change
event.getChanges(LibraryEntity::class.java).forEach {
when (it) {
is EntityChange.Added, is EntityChange.Replaced -> changedLibraryEntities.add(it.newEntity)
else -> {}
}
}
// Handle library version change
event.getChanges(LibraryPropertiesEntity::class.java).forEach {
when (it) {
is EntityChange.Added, is EntityChange.Replaced -> changedLibraryEntities.add(it.newEntity?.library)
else -> {}
}
}
val utils = RepositoryLibraryUtils.getInstance(project)
utils.computeExtendedPropertiesFor(libraries = changedLibraryEntities.filterNotNull().toSet(),
buildSha256Checksum = sha256ChecksumAutoBuildEnabled,
guessAndBindRemoteRepository = jarRepositoryAutoBindEnabled)
}
}

View File

@@ -0,0 +1,21 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.jarRepository
import com.intellij.openapi.components.serviceAsync
import com.intellij.openapi.extensions.ExtensionNotApplicableException
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.util.application
class RepositoryLibraryExtendedPropertiesUpdateStartupActivity : ProjectActivity {
init {
if (application.isUnitTestMode() || application.isHeadlessEnvironment()) {
throw ExtensionNotApplicableException.create()
}
}
override suspend fun execute(project: Project) {
val service = project.serviceAsync<RepositoryLibraryUtils>()
service.subscribeToWorkspaceModelUpdates()
}
}

View File

@@ -29,6 +29,7 @@ import com.intellij.platform.ide.progress.withBackgroundProgress
import com.intellij.platform.util.progress.RawProgressReporter
import com.intellij.platform.util.progress.reportRawProgress
import com.intellij.platform.workspace.jps.entities.*
import com.intellij.platform.workspace.storage.EntityChange
import com.intellij.platform.workspace.storage.EntityStorage
import com.intellij.platform.workspace.storage.ImmutableEntityStorage
import com.intellij.platform.workspace.storage.MutableEntityStorage
@@ -78,6 +79,7 @@ class RepositoryLibraryUtils(private val project: Project, private val cs: Corou
private var testCoroutineScope: CoroutineScope? = null
private val myCoroutineScope: CoroutineScope get() = testCoroutineScope ?: cs
private val isSubscribedToWorkspaceModelChanges = AtomicBoolean(false)
/**
* Should be used only in tests. Required to promote errors to tests via [testScope].
@@ -92,6 +94,46 @@ class RepositoryLibraryUtils(private val project: Project, private val cs: Corou
testCoroutineScope = null
}
suspend fun subscribeToWorkspaceModelUpdates() {
if (isSubscribedToWorkspaceModelChanges.compareAndSet(false, true)) {
WorkspaceModel.getInstance(project).eventLog.collect { event ->
val settings = RepositoryLibrarySettings.getInstanceOrDefaults(project)
val jarRepositoryAutoBindEnabled = settings.isJarRepositoryAutoBindEnabled()
val sha256ChecksumAutoBuildEnabled = settings.isSha256ChecksumAutoBuildEnabled()
// exit fast, avoid heavy computations if the feature is not enabled
if (!jarRepositoryAutoBindEnabled && !sha256ChecksumAutoBuildEnabled) {
return@collect
}
val changedLibraryEntities = mutableSetOf<LibraryEntity?>()
// Handle library roots change
event.getChanges(LibraryEntity::class.java).forEach {
when (it) {
is EntityChange.Added, is EntityChange.Replaced -> changedLibraryEntities.add(it.newEntity)
else -> {}
}
}
// Handle library version change
event.getChanges(LibraryPropertiesEntity::class.java).forEach {
when (it) {
is EntityChange.Added, is EntityChange.Replaced -> changedLibraryEntities.add(it.newEntity?.library)
else -> {}
}
}
computeExtendedPropertiesFor(libraries = changedLibraryEntities.filterNotNull().toSet(),
buildSha256Checksum = sha256ChecksumAutoBuildEnabled,
guessAndBindRemoteRepository = jarRepositoryAutoBindEnabled)
}
} else {
logger.error("An attempt to subscribe to WSM updates more than once")
}
}
/**
* Tries to guess remote jar repository for each [RepositoryLibraryType] library and bind it to
* the library if found. Global libraries are excluded.

View File

@@ -333,6 +333,7 @@
<compilableFileTypesProvider implementation="com.intellij.compiler.impl.javaCompiler.JavaCompilableFileTypesProvider"/>
<postStartupActivity implementation="com.intellij.compiler.server.BuildManager$BuildManagerStartupActivity"/>
<postStartupActivity implementation="com.intellij.ide.SetupJavaProjectFromSourcesActivity"/>
<postStartupActivity implementation="com.intellij.jarRepository.RepositoryLibraryExtendedPropertiesUpdateStartupActivity"/>
<backgroundPostStartupActivity implementation="com.intellij.compiler.cache.CompilerCacheStartupActivity"/>
<console.folding implementation="com.intellij.execution.filters.StackTraceFolding"/>
@@ -2871,8 +2872,6 @@
</applicationListeners>
<projectListeners>
<listener class="com.intellij.jarRepository.RepositoryLibraryChangeListener"
topic="com.intellij.platform.backend.workspace.WorkspaceModelChangeListener"/>
<listener class="com.intellij.codeInsight.daemon.problems.pass.ProjectProblemFileInlaySelectionListenerSettingsListener"
topic="com.intellij.codeInsight.hints.InlayHintsSettings$SettingsListener"/>
<listener class="com.intellij.codeInsight.daemon.problems.pass.ProjectProblemFileRefactoringEventListener"