Skip to content

Commit

Permalink
fix: replace use of ProjectLocator with project access (#75)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
r3mariano authored Jul 31, 2023
1 parent f483626 commit 36a77f2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
5 changes: 1 addition & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
id("idea")
id("java")
kotlin("kapt") version "1.8.10"
id("org.jetbrains.intellij") version "1.13.3"
id("org.jetbrains.intellij") version "1.15.0"
id("org.jetbrains.kotlin.jvm") version "1.8.10"
id("org.jetbrains.kotlin.plugin.serialization") version "1.4.32"
}
Expand Down Expand Up @@ -120,9 +120,6 @@ tasks {
failureLevel.set(
EnumSet.complementOf(
EnumSet.of(
// skipping compatibility problems due to potential false positive with EAP v232:
// 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.
RunPluginVerifierTask.FailureLevel.COMPATIBILITY_PROBLEMS,
// skipping missing dependencies as com.intellij.java provided by IJ raises a false warning
RunPluginVerifierTask.FailureLevel.MISSING_DEPENDENCIES,
// skipping experimental API usage, as delaying Gradle execution relies on experimental GradleExecutionAware.
Expand Down
12 changes: 7 additions & 5 deletions src/main/kotlin/com/squareup/cash/hermit/Hermit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,25 @@ object Hermit {
private val HANDLER_EP_NAME: ExtensionPointName<HermitPropertyHandler> =
ExtensionPointName("org.squareup.cash.hermit.idea-plugin.property-handler")

private val projects = ConcurrentHashMap<String, State>()
private val projects = ConcurrentHashMap<String, HermitState>()

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

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

fun allProjects(): Collection<HermitState> = projects.values

/**
* State maintains the Hermit state of a single project
*/
class State(private val project: Project) {
class HermitState(val project: Project) {
// Is this project a hermit enabled project?
private var isHermitProject = false
// Has the project been opened in the plugin?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.squareup.cash.hermit

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectLocator
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.newvfs.BulkFileListener
Expand All @@ -14,11 +13,17 @@ class HermitVFSChangeListener : BulkFileListener {
override fun after(events: MutableList<out VFileEvent>) {
val needsUpdating = HashMap<String, Project>()
events.forEach {
val file = it.file
if (file != null) {
val project = ProjectLocator.getInstance().guessProjectForFile(file)
if (project != null && file != null && isHermitChange(project, file)) {
needsUpdating[project.name] = project
it.file?.let { file ->
log.debug("Checking if file [${file.path}] is in projects " +
"${Hermit.allProjects().map { state -> state.project.name }}")
Hermit.allProjects().forEach { state ->
val project = state.project
// The project might have been disposed since looking up Hermit.allProjects.
log.debug("Project [${project.name}] is disposed: [${project.isDisposed}]")
if (!project.isDisposed && isHermitChange(project, file)) {
log.debug("Project [${project.name}] needs updating")
needsUpdating[project.name] = project
}
}
}
}
Expand Down

0 comments on commit 36a77f2

Please sign in to comment.