Skip to content

Commit 72638aa

Browse files
Tomasz Pasternaklukaszwawrzyk
authored andcommitted
Workaround for "Find Usages broken" bug in Scala Plugin
Bug tracker: https://youtrack.jetbrains.com/issue/SCL-16768 Correct initialization of `ScalaCompilerReferenceService` is required for "Find usages" feature to work correcly. Unfortunately, it's broken in that way, it does nothing when there are no modules in the project right after the moment, when the project is opened. This happens when a project is imported, the module creation starts after opening then.
1 parent 5b983db commit 72638aa

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

src/com/twitter/intellij/pants/service/scala/PantsScalaDataService.scala

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ import java.io.File
77
import java.util
88
import java.util.Collections
99

10+
import com.intellij.ProjectTopics
1011
import com.intellij.openapi.diagnostic.Logger
1112
import com.intellij.openapi.externalSystem.model.project.ProjectData
1213
import com.intellij.openapi.externalSystem.model.{DataNode, ExternalSystemException, Key, ProjectKeys}
1314
import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider
1415
import com.intellij.openapi.externalSystem.service.project.manage.ProjectDataService
15-
import com.intellij.openapi.project.Project
16+
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
17+
import com.intellij.openapi.module.Module
18+
import com.intellij.openapi.project.{ModuleListener, Project}
1619
import com.intellij.openapi.roots.impl.libraries.LibraryEx.ModifiableModelEx
1720
import com.intellij.openapi.roots.libraries.Library
1821
import com.intellij.openapi.util.Computable
19-
import org.jetbrains.plugins.scala.project.{LibraryExt, ScalaLanguageLevel, ScalaLibraryProperties, ScalaLibraryType, Version}
22+
import com.twitter.intellij.pants.util.PantsUtil
23+
import org.jetbrains.plugins.scala.findUsages.compilerReferences.ScalaCompilerReferenceService
24+
import org.jetbrains.plugins.scala.project.{LibraryExt, ScalaLibraryProperties, ScalaLibraryType, Version}
25+
import com.twitter.intellij.pants.settings.PantsSettings
26+
import com.twitter.intellij.pants.util.PantsConstants
2027

2128
import scala.collection.JavaConverters.{asScalaSetConverter, iterableAsScalaIterableConverter}
2229

@@ -37,6 +44,34 @@ class PantsScalaDataService extends ProjectDataService[ScalaModelData, Library]
3744
modelsProvider: IdeModifiableModelsProvider
3845
): Unit = {
3946
toImport.asScala.toSet.foreach[Unit](doImport(_, modelsProvider))
47+
48+
// Workaround for bug in Scala Plugin https://youtrack.jetbrains.com/issue/SCL-16768
49+
//
50+
// Correct initialization of `ScalaCompilerReferenceService` is required for "Find usages"
51+
// feature to work correctly. Unfortunately, it's broken in that way, it does nothing when
52+
// there are no modules in the project right after the moment, when the project is opened.
53+
// This happens when a project is imported, the module creation starts after opening project.
54+
val pantsSettings = ExternalSystemApiUtil.getSettings(project, PantsConstants.SYSTEM_ID).asInstanceOf[PantsSettings]
55+
if (pantsSettings.isUseIntellijCompiler) {
56+
doOnceOnPantsModuleAdded(project) {
57+
ScalaCompilerReferenceService(project).initializeReferenceService()
58+
}
59+
}
60+
}
61+
62+
private def doOnceOnPantsModuleAdded(project: Project)(action: => Unit): Unit = {
63+
project.getMessageBus.connect().subscribe(ProjectTopics.MODULES, new ModuleListener {
64+
var done = false
65+
66+
override def moduleAdded(project: Project, module: Module): Unit = {
67+
if (!done) {
68+
if (PantsUtil.isPantsModule(module)) {
69+
action
70+
done = true;
71+
}
72+
}
73+
}
74+
})
4075
}
4176

4277
private def doImport(scalaNode: DataNode[ScalaModelData], modelsProvider: IdeModifiableModelsProvider) {
@@ -74,9 +109,7 @@ class PantsScalaDataService extends ProjectDataService[ScalaModelData, Library]
74109
project: Project,
75110
modelsProvider: IdeModifiableModelsProvider
76111
): Computable[util.Collection[Library]] =
77-
new Computable[util.Collection[Library]] {
78-
override def compute(): util.Collection[Library] = Collections.emptyList()
79-
}
112+
() => Collections.emptyList()
80113

81114
override def removeData(toRemove: Computable[util.Collection[Library]],
82115
toIgnore: util.Collection[DataNode[ScalaModelData]],

0 commit comments

Comments
 (0)