Skip to content

Commit 36a77f2

Browse files
authored
fix: replace use of ProjectLocator with project access (#75)
There was a compatibility problem tagged with ProjectLocator. Iterating through all open projects seems a bit more expensive, but we're not doing a lot per iteration.
1 parent f483626 commit 36a77f2

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

build.gradle.kts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
id("idea")
99
id("java")
1010
kotlin("kapt") version "1.8.10"
11-
id("org.jetbrains.intellij") version "1.13.3"
11+
id("org.jetbrains.intellij") version "1.15.0"
1212
id("org.jetbrains.kotlin.jvm") version "1.8.10"
1313
id("org.jetbrains.kotlin.plugin.serialization") version "1.4.32"
1414
}
@@ -120,9 +120,6 @@ tasks {
120120
failureLevel.set(
121121
EnumSet.complementOf(
122122
EnumSet.of(
123-
// skipping compatibility problems due to potential false positive with EAP v232:
124-
// Method com.squareup.cash.hermit.HermitVFSChangeListener.after(List events) : void references an unresolved class com.intellij.openapi.project.ProjectLocator.Companion. This can lead to **NoSuchClassError** exception at runtime.
125-
RunPluginVerifierTask.FailureLevel.COMPATIBILITY_PROBLEMS,
126123
// skipping missing dependencies as com.intellij.java provided by IJ raises a false warning
127124
RunPluginVerifierTask.FailureLevel.MISSING_DEPENDENCIES,
128125
// skipping experimental API usage, as delaying Gradle execution relies on experimental GradleExecutionAware.

src/main/kotlin/com/squareup/cash/hermit/Hermit.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@ object Hermit {
3333
private val HANDLER_EP_NAME: ExtensionPointName<HermitPropertyHandler> =
3434
ExtensionPointName("org.squareup.cash.hermit.idea-plugin.property-handler")
3535

36-
private val projects = ConcurrentHashMap<String, State>()
36+
private val projects = ConcurrentHashMap<String, HermitState>()
3737

38-
operator fun invoke(project: Project): State {
39-
val state = project.projectFilePath?.let { projects.getOrPut(it, { State(project) }) }
38+
operator fun invoke(project: Project): HermitState {
39+
val hermitState = project.projectFilePath?.let { projects.getOrPut(it) { HermitState(project) } }
4040
// projectFilePath can be null for the default project. Return an empty state for it.
41-
return state ?: State(project)
41+
return hermitState ?: HermitState(project)
4242
}
4343

4444
fun remove(project: Project) {
4545
log.debug(project.name + ": closing project")
4646
project.projectFilePath?.let { projects.remove(it) }
4747
}
4848

49+
fun allProjects(): Collection<HermitState> = projects.values
50+
4951
/**
5052
* State maintains the Hermit state of a single project
5153
*/
52-
class State(private val project: Project) {
54+
class HermitState(val project: Project) {
5355
// Is this project a hermit enabled project?
5456
private var isHermitProject = false
5557
// Has the project been opened in the plugin?

src/main/kotlin/com/squareup/cash/hermit/HermitVFSChangeListener.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.squareup.cash.hermit
22

33
import com.intellij.openapi.diagnostic.Logger
44
import com.intellij.openapi.project.Project
5-
import com.intellij.openapi.project.ProjectLocator
65
import com.intellij.openapi.project.guessProjectDir
76
import com.intellij.openapi.vfs.VirtualFile
87
import com.intellij.openapi.vfs.newvfs.BulkFileListener
@@ -14,11 +13,17 @@ class HermitVFSChangeListener : BulkFileListener {
1413
override fun after(events: MutableList<out VFileEvent>) {
1514
val needsUpdating = HashMap<String, Project>()
1615
events.forEach {
17-
val file = it.file
18-
if (file != null) {
19-
val project = ProjectLocator.getInstance().guessProjectForFile(file)
20-
if (project != null && file != null && isHermitChange(project, file)) {
21-
needsUpdating[project.name] = project
16+
it.file?.let { file ->
17+
log.debug("Checking if file [${file.path}] is in projects " +
18+
"${Hermit.allProjects().map { state -> state.project.name }}")
19+
Hermit.allProjects().forEach { state ->
20+
val project = state.project
21+
// The project might have been disposed since looking up Hermit.allProjects.
22+
log.debug("Project [${project.name}] is disposed: [${project.isDisposed}]")
23+
if (!project.isDisposed && isHermitChange(project, file)) {
24+
log.debug("Project [${project.name}] needs updating")
25+
needsUpdating[project.name] = project
26+
}
2227
}
2328
}
2429
}

0 commit comments

Comments
 (0)