Skip to content

Commit

Permalink
Merged branch idea243.release into idea243.x
Browse files Browse the repository at this point in the history
  • Loading branch information
builduser committed Dec 13, 2024
2 parents 42fb27f + ea894af commit 6545dd7
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.jetbrains.plugins.scala.compiler.references

import com.intellij.openapi.module.Module
import com.intellij.util.messages.Topic

import java.util.EventListener

trait CompilerReferenceServiceStatusListener extends EventListener {
def onIndexingPhaseStarted(): Unit = ()
def onCompilationInfoIndexed(modules: Set[String]): Unit = ()
def onCompilationInfoIndexed(modules: Set[(Module, ModuleScope)]): Unit = ()
def onIndexingPhaseFinished(success: Boolean): Unit = ()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ final private[references] class ScalaCompilerReferenceService(project: Project)
}

override def processCompilationInfo(info: CompilationInfo, isOffline: Boolean): Unit = {
val modules = info.affectedModules(project).map(_.getName)
val modules = info.affectedModules(project)
logger.debug(s"[compiler indices] processCompilationInfo. offline: $isOffline")

indexerScheduler.schedule(ProcessCompilationInfo(info, () => {
if (!isOffline) { // do not mark modules as up-to-date when indexing 'offline' sbt compilations
modules.foreach(compilationTimestamps.put(_, info.startTimestamp))
modules.foreach { case (m, _) => compilationTimestamps.put(m.getName, info.startTimestamp) }
logger.debug(s"[compiler indices] Reindexed ${info.generatedClasses.size} classfiles.")
dirtyScopeHolder.compilationInfoIndexed(info)
messageBus.syncPublisher(CompilerReferenceServiceStatusListener.topic).onCompilationInfoIndexed(modules)
Expand Down Expand Up @@ -202,8 +202,8 @@ final private[references] class ScalaCompilerReferenceService(project: Project)
)
onIndexCorruption()
} else {
reader = ScalaCompilerReferenceReaderFactory(project)
messageBus.syncPublisher(CompilerReferenceServiceStatusListener.topic).onIndexingPhaseFinished(indexingSuccessful)
reader = ScalaCompilerReferenceReaderFactory(project)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private class ScalaDirtyScopeHolder(
}

private[references] def compilationInfoIndexed(info: CompilationInfo): Unit = {
val modules = info.affectedModules(project)
val modules = info.affectedModules(project).map(_._1)

val scopes = info match {
case sbti: SbtCompilationInfo => modules.map(ScopedModule(_, sbti.configuration))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package org.jetbrains.plugins.scala.compiler.references.indices

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import org.jetbrains.jps.backwardRefs.index.CompilerReferenceIndex
import org.jetbrains.plugins.scala.compiler.references.indexDir

private[references] object ScalaCompilerReferenceReaderFactory {
val expectedIndexVersion: Int = ScalaCompilerIndices.version

def apply(project: Project): Option[ScalaCompilerReferenceReader] =
for {
dir <- indexDir(project)
if CompilerReferenceIndex.exists(dir) &&
!CompilerReferenceIndex.versionDiffers(dir, expectedIndexVersion)
} yield new ScalaCompilerReferenceReader(dir)
val Log: Logger = Logger.getInstance(classOf[ScalaCompilerReferenceReaderFactory.type])

//noinspection UnstableApiUsage
def apply(project: Project): Option[ScalaCompilerReferenceReader] = {
try {
for {
dir <- indexDir(project)
if CompilerReferenceIndex.exists(dir) &&
!CompilerReferenceIndex.versionDiffers(dir, expectedIndexVersion)
} yield new ScalaCompilerReferenceReader(dir)
} catch {
case e: Exception =>
indexDir(project).foreach(CompilerReferenceIndex.removeIndexFiles)
Log.error(e)
None
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.annotations.Nls
import org.jetbrains.jps.backwardRefs.index.CompilerReferenceIndex
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.indices.protocol.CompilationInfo
import org.jetbrains.plugins.scala.indices.protocol.jps.JpsCompilationInfo
import org.jetbrains.plugins.scala.indices.protocol.sbt.SbtCompilationInfo
import org.jetbrains.plugins.scala.indices.protocol.sbt.{Configuration, SbtCompilationInfo}
import org.jetbrains.sbt.project.data.ModuleNode

import java.io.File
Expand Down Expand Up @@ -42,13 +41,22 @@ package object references {
}

implicit class CompilationInfoExt(private val info: CompilationInfo) extends AnyVal {
def affectedModules(project: Project): Set[Module] = {
def affectedModules(project: Project): Set[(Module, ModuleScope)] = {
val manager = ModuleManager.getInstance(project)

info match {
case jinfo: JpsCompilationInfo => jinfo.affectedModules.flatMap(manager.findModuleByName(_).toOption)
case sbtInfo: SbtCompilationInfo => findIdeaModule(project, sbtInfo.projectId).toSet
case _ => Set.empty
case jinfo: JpsCompilationInfo =>
jinfo.affectedModules
.flatMap(ModuleScope.parse)
.flatMap { case (name, scope) => Option(manager.findModuleByName(name)).map((_, scope)) }
case sbtInfo: SbtCompilationInfo =>
val scope = sbtInfo.configuration match {
case Configuration.Compile => ModuleScope.Production
case Configuration.Test => ModuleScope.Test
}
findIdeaModule(project, sbtInfo.projectId).map((_, scope)).toSet
case _ =>
Set.empty
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.intellij.find.FindManager
import com.intellij.find.impl.FindManagerImpl
import com.intellij.openapi.module.Module
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.{DumbService, Project}
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.ui.{DialogWrapper, Messages}
import com.intellij.psi._
Expand All @@ -15,10 +15,11 @@ import com.intellij.util.containers.ContainerUtil
import com.intellij.util.messages.MessageBusConnection
import com.intellij.util.ui.EDT
import org.jetbrains.plugins.scala.compiler.CompilerIntegrationBundle
import org.jetbrains.plugins.scala.compiler.references.{CompilerReferenceServiceStatusListener, ScalaCompilerReferenceService, UsagesInFile, task, upToDateCompilerIndexExists}
import org.jetbrains.plugins.scala.compiler.references.{CompilerReferenceServiceStatusListener, ModuleScope, ScalaCompilerReferenceService, UsagesInFile, task, upToDateCompilerIndexExists}
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.findUsages.SearchTargetExtractors.SAMType
import org.jetbrains.plugins.scala.findUsages.factory.ScalaFindUsagesConfiguration

//noinspection ApiStatus
import org.jetbrains.plugins.scala.compiler.references.indices.ScalaCompilerIndices
import org.jetbrains.plugins.scala.compiler.references.search.ImplicitUsagesSearchDialogs._
Expand Down Expand Up @@ -163,42 +164,39 @@ object CompilerIndicesReferencesSearcher extends ExternalSearchScopeChecker {
pendingConnection = project.getMessageBus.connect(project.unloadAwareDisposable)

pendingConnection.subscribe(CompilerReferenceServiceStatusListener.topic, new CompilerReferenceServiceStatusListener {
private[this] val targetModuleNames = ContainerUtil.newConcurrentSet[String]
private[this] val modulesInCurrentBuild = ContainerUtil.newConcurrentSet[(String, ModuleScope)]

targetModuleNames.addAll(targetModules.collect {
modulesInCurrentBuild.addAll(targetModules.collect {
case module if module.isSourceModule => module.getName
}.asJavaCollection)
}.flatMap(m => Seq((m, ModuleScope.Production), (m, ModuleScope.Test))).asJavaCollection)

override def onIndexingPhaseStarted(): Unit = showProgressIndicator(project)

override def onCompilationInfoIndexed(modules: Set[String]): Unit =
targetModuleNames.removeAll(modules.asJava)
override def onCompilationInfoIndexed(modules: Set[(Module, ModuleScope)]): Unit =
modulesInCurrentBuild.removeAll(modules.map { case (m, scope) => (m.getName, scope) }.asJava)

override def onIndexingPhaseFinished(success: Boolean): Unit = {
if (success) {
if (targetModuleNames.isEmpty) {
lock.withLock(indexingFinishedCondition.signal())
val findManager = FindManager.getInstance(project).asInstanceOf[FindManagerImpl]
val config = ScalaFindUsagesConfiguration.getInstance(project)

val handler = inReadAction(target match {
case SAMType(_) => new ScalaFindUsagesHandler(target, config)
case _ => new CompilerIndicesFindUsagesHandler(target, config)
})

pendingConnection.disconnect()
invokeWhenSmart(project) {
findManager.getFindUsagesManager.findUsages(
handler.getPrimaryElements,
handler.getSecondaryElements,
handler,
handler.getFindUsagesOptions(),
false
)
}
lock.withLock(indexingFinishedCondition.signal())
if (success && modulesInCurrentBuild.isEmpty) {
val findManager = FindManager.getInstance(project).asInstanceOf[FindManagerImpl]
val config = ScalaFindUsagesConfiguration.getInstance(project)

val handler = inReadAction(target match {
case SAMType(_) => new ScalaFindUsagesHandler(target, config)
case _ => new CompilerIndicesFindUsagesHandler(target, config)
})

pendingConnection.disconnect()
invokeWhenSmart(project) {
findManager.getFindUsagesManager.findUsages(
handler.getPrimaryElements,
handler.getSecondaryElements,
handler,
handler.getFindUsagesOptions(),
false
)
}
} else {
lock.withLock(indexingFinishedCondition.signal())
pendingConnection.disconnect()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.jetbrains.jps.incremental.scala

import java.io.File
import java.{util => jutil}
import com.intellij.util.containers.ContainerUtil
import org.jetbrains.jps.ModuleChunk
import org.jetbrains.jps.builders.java.{JavaModuleBuildTargetType, JavaSourceRootDescriptor}
Expand All @@ -11,9 +9,12 @@ import org.jetbrains.jps.incremental.scala.InitialScalaBuilder.{hasScala, isScal
import org.jetbrains.jps.incremental.{BuilderCategory, CompileContext, ModuleBuildTarget, ModuleLevelBuilder}
import org.jetbrains.plugins.scala.compiler.references.Builder.rebuildPropertyKey
import org.jetbrains.plugins.scala.compiler.references.Messages._
import org.jetbrains.plugins.scala.compiler.references.ModuleScope
import org.jetbrains.plugins.scala.indices.protocol.CompiledClass
import org.jetbrains.plugins.scala.indices.protocol.jps.JpsCompilationInfo

import java.io.File
import java.{util => jutil}
import scala.jdk.CollectionConverters._

class ScalaCompilerReferenceIndexBuilder extends ModuleLevelBuilder(BuilderCategory.CLASS_POST_PROCESSOR) {
Expand All @@ -38,7 +39,12 @@ class ScalaCompilerReferenceIndexBuilder extends ModuleLevelBuilder(BuilderCateg
if (shouldBeNonIncremental) {
val pd = context.getProjectDescriptor
val (allClasses, timestamp) = getAllClassesInfo(context)
val allModules = pd.getProject.getModules.asScala.filter(hasScala(context, _)).map(_.getName).toSet
val allModules = pd.getProject.getModules.asScala.filter(hasScala(context, _))
.flatMap { m =>
val name = m.getName
Seq(ModuleScope.Production, ModuleScope.Test).map(_.appendScopeSuffix(name))
}
.toSet

val info = JpsCompilationInfo(
allModules,
Expand Down Expand Up @@ -72,7 +78,11 @@ class ScalaCompilerReferenceIndexBuilder extends ModuleLevelBuilder(BuilderCateg
return ExitCode.OK

if (!shouldBeNonIncremental) {
val affectedModules = chunk.getModules.asScala.filter(hasScala(context, _)).map(_.getName).toSet
val affectedModules = chunk.getTargets.asScala.map { target =>
val scope = if (target.isTests) ModuleScope.Test else ModuleScope.Production
val name = target.getModule.getName
scope.appendScopeSuffix(name)
}.toSet

val compiledClasses =
outputConsumer.getCompiledClasses
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jetbrains.plugins.scala.compiler.references

sealed trait ModuleScope {
val suffix: String

def appendScopeSuffix(name: String): String = name + suffix
}

object ModuleScope {

case object Production extends ModuleScope {
override val suffix: String = "_$production$"
}

case object Test extends ModuleScope {
override val suffix: String = "_$test$"
}

def parse(string: String): Option[(String, ModuleScope)] = string match {
case s if s.endsWith(Production.suffix) => Some(s.stripSuffix(Production.suffix), Production)
case s if s.endsWith(Test.suffix) => Some(s.stripSuffix(Test.suffix), Test)
case _ => None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1809,12 +1809,9 @@ package object extensions {

implicit class LockExtensions(private val lock: java.util.concurrent.locks.Lock) extends AnyVal {
def withLock[A](body: => A): A = {
try {
lock.lock()
body
} finally {
lock.unlock()
}
lock.lock()
try body
finally lock.unlock()
}
}

Expand Down

0 comments on commit 6545dd7

Please sign in to comment.