Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ class AutoCpDatabase(
) {
val problems get() = problemsFlow.value

/**
* true once the in-memory state diverged from what was loaded from disk,
* so an intentionally emptied database is distinguishable from a never-populated one
*/
@Volatile
var mutated = false

fun updateProblem(problem: Problem) {
mutated = true
val group = this.problems[problem.groupName]?.toMutableMap() ?: mutableMapOf()
group[problem.name] = problem
this.problemsFlow.value = problems.toMutableMap().apply { this[problem.groupName] = group }
}

fun modifySolutionFiles(action: MutableMap<String, SolutionFile>.() -> Unit) {
mutated = true
solutionFilesFlow.value = solutionFilesFlow.value.toMutableMap().apply(action)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.pushpavel.autocp.database

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vfs.newvfs.BulkFileListener
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent
import com.intellij.openapi.vfs.newvfs.events.VFileEvent

class AutoCpExternalReloader : BulkFileListener {

override fun after(events: List<VFileEvent>) {
val autocpEvents = events.asSequence()
.filter { it is VFileCreateEvent || (it is VFileContentChangeEvent && !it.isFromSave) }
.filter { it.path.endsWith("/.autocp") }
.toList()
if (autocpEvents.isEmpty()) return

val openProjects = ProjectManager.getInstanceIfCreated()?.openProjects ?: return
val affectedProjects = autocpEvents
.map { it.path.removeSuffix("/.autocp") }
.flatMap { parentPath ->
openProjects.filter { !it.isDefault && FileUtil.pathsEqual(it.basePath, parentPath) }
}
.toSet()

// defer disk I/O out of the write action that delivered the VFS events
for (project in affectedProjects) {
ApplicationManager.getApplication().invokeLater({
project.service<AutoCpStorage>().reloadFromDisk()
}, project.disposed)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AutoCpStorage(val project: Project) {

val log = Logger.getInstance(AutoCpStorage::class.java)

val database by lazy {
private val databaseDelegate = lazy {
if(!project.isDefault){
val converter = AutoCpFileConversion(project)
converter.convert()
Expand All @@ -51,6 +51,25 @@ class AutoCpStorage(val project: Project) {
AutoCpDatabase(MutableStateFlow(db.problems), MutableStateFlow(db.solutionFiles))
}

val database by databaseDelegate

fun reloadFromDisk() {
if (project.isDefault) return
if (!databaseDelegate.isInitialized()) return
val path = Paths.get(project.basePath!!, ".autocp")
if (!path.exists()) return
try {
val newDb = Migrations.migrateDB(runReadAction {
Json.parseToJsonElement(path.readText())
})
database.problemsFlow.value = newDb.problems
database.solutionFilesFlow.value = newDb.solutionFiles
database.mutated = false
} catch (e: Exception) {
log.warn("Failed to reload .autocp from disk", e)
}
}

val serializableDatabase
get() = database.run {
AutoCpDB(
Expand All @@ -70,7 +89,8 @@ class AutoCpStorageSaver : FileDocumentManagerListener {
return
val path = Paths.get(Path(project.basePath!!).pathString, ".autocp")
var virtualFile = VfsUtil.findFile(path, true)
val db = project.service<AutoCpStorage>().serializableDatabase
val storage = project.service<AutoCpStorage>()
val db = storage.serializableDatabase


if (virtualFile?.isValid != true && DEFAULT_AUTO_CP_DB != db) {
Expand All @@ -93,8 +113,15 @@ class AutoCpStorageSaver : FileDocumentManagerListener {
R.notify.couldNotWriteToAutoCpFile()
return@runReadAction
}
// in-memory state was never populated, prefer the on-disk content over clobbering it
if (!storage.database.mutated && db == DEFAULT_AUTO_CP_DB && document.text.isNotBlank()) {
storage.reloadFromDisk()
return@runReadAction
}
val newText = Json.encodeToString(db)
if (document.text == newText) return@runReadAction
runWriteAction {
document.setText(Json.encodeToString(db))
document.setText(newText)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
<programRunner implementation="com.github.pushpavel.autocp.config.AutoCpProgramRunner"/>
<executionTargetProvider implementation="com.github.pushpavel.autocp.config.AutoCpExecutionTargetProvider"/>
</extensions>
<applicationListeners>
<listener class="com.github.pushpavel.autocp.database.AutoCpExternalReloader"
topic="com.intellij.openapi.vfs.newvfs.BulkFileListener"/>
</applicationListeners>
<projectListeners>
<listener class="com.github.pushpavel.autocp.extend.cmake.CMakeAddExecutable"
topic="com.github.pushpavel.autocp.gather.filegen.FileGenerationListener"/>
Expand Down
Loading