Skip to content

Commit

Permalink
Fill LatexExternalPackageInclusionCache in background, fix #3607
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Dec 19, 2024
1 parent 97e3202 commit eb02676
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## [Unreleased]

### Added
* Improve performance when starting a run configuration
* Improve performance when starting a run configuration and when using autocompletion directly after starting the IDE
* Change order in structure view to match source file and sectioning level
* Add command redefinitions to command definition filter in structure view
* Add support for automatic language injection on the minted environment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nl.hannahsten.texifyidea.index.file

import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
Expand All @@ -8,6 +9,8 @@ import com.jetbrains.rd.util.concurrentMapOf
import nl.hannahsten.texifyidea.algorithm.DFS
import nl.hannahsten.texifyidea.lang.LatexPackage
import nl.hannahsten.texifyidea.util.files.removeFileExtension
import nl.hannahsten.texifyidea.util.runInBackground
import java.util.concurrent.atomic.AtomicBoolean

/**
* Compute and cache for each package style file all the other style files it includes (directly or indirectly).
Expand All @@ -18,11 +21,13 @@ object LatexExternalPackageInclusionCache {

private val cache = concurrentMapOf<LatexPackage, Set<LatexPackage>>()

private var isFillingCache = AtomicBoolean(false)

/**
* Map every LaTeX package style file to all the style files it includes, directly or indirectly.
*/
@Synchronized
fun getAllPackageInclusions(project: Project): Map<LatexPackage, Set<LatexPackage>> {
fun updateOrGetCache(project: Project): Map<LatexPackage, Set<LatexPackage>> {
if (cache.isNotEmpty() || DumbService.isDumb(project)) return cache

// Make sure the index is ready (#3754)
Expand All @@ -31,20 +36,30 @@ object LatexExternalPackageInclusionCache {
val directChildren = mutableMapOf<LatexPackage, MutableSet<LatexPackage>>()

// Get direct children from the index
FileBasedIndex.getInstance().getAllKeys(LatexExternalPackageInclusionIndex.Cache.id, project).forEach { indexKey ->
FileBasedIndex.getInstance().processValues(
LatexExternalPackageInclusionIndex.Cache.id, indexKey, null, { file, _ ->
val key = LatexPackage(file.name.removeFileExtension())
directChildren[key] = directChildren.getOrDefault(key, mutableSetOf()).also { it.add(LatexPackage((indexKey))) }
true
},
GlobalSearchScope.everythingScope(project)
)
}
if (isFillingCache.getAndSet(true)) return cache
runInBackground(project, "Retrieving LaTeX package inclusions...") { indicator ->
try {
runReadAction { FileBasedIndex.getInstance().getAllKeys(LatexExternalPackageInclusionIndex.Cache.id, project) }.forEach { indexKey ->
runReadAction {
FileBasedIndex.getInstance().processValues(
LatexExternalPackageInclusionIndex.Cache.id, indexKey, null, { file, _ ->
indicator.checkCanceled()
val key = LatexPackage(file.name.removeFileExtension())
directChildren[key] = directChildren.getOrDefault(key, mutableSetOf()).also { it.add(LatexPackage((indexKey))) }
true
},
GlobalSearchScope.everythingScope(project)
)
}
}

// Do some DFS for indirect inclusions
for (latexPackage in directChildren.keys) {
cache[latexPackage] = DFS(latexPackage) { parent -> directChildren[parent] ?: emptySet() }.execute()
// Do some DFS for indirect inclusions
for (latexPackage in directChildren.keys) {
cache[latexPackage] = DFS(latexPackage) { parent -> directChildren[parent] ?: emptySet() }.execute()
}
} finally {
isFillingCache.set(false)
}
}

return cache
Expand All @@ -55,7 +70,7 @@ object LatexExternalPackageInclusionCache {
*/
fun getAllIndirectlyIncludedPackages(packages: Collection<LatexPackage>, project: Project): Set<LatexPackage> {
val result = packages.toMutableSet()
val allInclusions = getAllPackageInclusions(project)
val allInclusions = updateOrGetCache(project)
for (latexPackage in packages) {
result.addAll(allInclusions[latexPackage] ?: emptySet())
}
Expand Down

0 comments on commit eb02676

Please sign in to comment.