FileStatusCache.kt
/**
* Dracon - An IntelliJ-Pijul integration.
* Copyright 2021 JonathanxD <jhrldev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.jonathanxd.dracon.cache
import com.github.jonathanxd.dracon.context.PijulVcsContext
import com.github.jonathanxd.dracon.pijul.PijulOperationResult
import com.github.jonathanxd.dracon.pijul.SuccessStatusCode
import com.github.jonathanxd.dracon.pijul.pijul
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.FileStatus
import com.intellij.openapi.vcs.FileStatusFactory
import java.io.Serializable
import java.nio.file.Path
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.relativeTo
@Service(Service.Level.PROJECT)
class FileStatusCache(val project: Project): CacheService<String, CacheablePijulFileStatus> {
override val cache = ActorDataCache<String, CacheablePijulFileStatus>(this.project, "file_status")
init {
// Pre-load status
this.cache.withLock {
val ctx = this.project.service<PijulVcsContext>()
val map = pijul(project).fileStatusMap(project)
if (map.result != null) {
for (change in map.result.changed) {
if (change.value.isNotEmpty()) {
val path = change.key.relativeTo(ctx.root)
this.cache.queryOrLoad(path.toString()) {
change.value.first().toPijul()
}
}
}
for (tracked in map.result.tracked) {
val path = tracked.relativeTo(ctx.root)
this.cache.queryOrLoad(path.toString()) {
FileStatus.NOT_CHANGED.toPijul()
}
}
}
}
}
fun load(path: Path): PijulOperationResult<FileStatus> {
val ctx = this.project.service<PijulVcsContext>()
return this.cache.queryOrLoad(path.relativeTo(ctx.root).toString(),
{ pijul(project).fileStatus(project, path) },
{ it.statusCode is SuccessStatusCode },
{ it.result!!.toPijul() },
{ PijulOperationResult("file_status", SuccessStatusCode, it.toFileStatus()) }
)
}
fun unload(path: Path) {
val ctx = this.project.service<PijulVcsContext>()
val relativePath = path.relativeTo(ctx.root).toString()
this.invalidate(relativePath)
}
fun unloadAll(paths: Iterable<Path>) {
val ctx = this.project.service<PijulVcsContext>()
val relativePaths = paths.map { it.relativeTo(ctx.root).toString() }
this.invalidateAll(relativePaths)
}
fun updateCache(path: Path, newValueCompute: () -> CacheablePijulFileStatus) {
val ctx = this.project.service<PijulVcsContext>()
val relativePath = path.relativeTo(ctx.root).toString()
super.updateCache(relativePath, newValueCompute)
}
fun updateCacheForPaths(paths: List<Path>, newValueCompute: (Path) -> CacheablePijulFileStatus) {
val ctx = this.project.service<PijulVcsContext>()
val relativePathMap = paths.associateBy { it.relativeTo(ctx.root).toString() }
super.updateCache(relativePathMap.keys.toList()) {
newValueCompute(relativePathMap[it]!!)
}
}
}
data class CacheablePijulFileStatus(val id: String) : Serializable {
companion object {
private const val serialVersionUID: Long = 1
}
}
fun FileStatus.toPijul(): CacheablePijulFileStatus = CacheablePijulFileStatus(this.id)
fun CacheablePijulFileStatus.toFileStatus(): FileStatus =
try {
FileStatus::class.java.getDeclaredField(this.id).get(null) as FileStatus
} catch (t: Throwable) {
FileStatusFactory.getInstance().allFileStatuses.first {
it.id == this.id
}
}