Skip to content

Commit

Permalink
fix: Save dotfile in dumb background task (#169)
Browse files Browse the repository at this point in the history
* Save dotfile as dumb background task

* remove wrappers as they are included already inside save and remove methods

* merge main

* spotless

* Wait for smart mode in tests
  • Loading branch information
MarcinVaadin authored Nov 21, 2024
1 parent 8dd8114 commit 4c87dc6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 14 additions & 2 deletions src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.DumbModeTask
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.project.modules
import com.intellij.openapi.roots.CompilerModuleExtension
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.vfs.*
Expand All @@ -38,7 +40,7 @@ class CopilotPluginUtil {
val outputPath: String?
)

@JvmRecord data class ProjectInfo(val basePath: String?, val modules: List<CopilotPluginUtil.ModuleInfo>)
@JvmRecord data class ProjectInfo(val basePath: String?, val modules: List<ModuleInfo>)

companion object {

Expand Down Expand Up @@ -96,6 +98,16 @@ class CopilotPluginUtil {
}

fun saveDotFile(project: Project) {
val task =
object : DumbModeTask() {
override fun performInDumbMode(progressIndicator: ProgressIndicator) {
saveDotFileInternal(project)
}
}
DumbService.getInstance(project).queueTask(task)
}

private fun saveDotFileInternal(project: Project) {
val dotFileDirectory = getDotFileDirectory(project)
if (dotFileDirectory != null) {
val props = Properties()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.vaadin.plugin.copilot.listeners

import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.project.ProjectManagerListener
Expand All @@ -16,12 +14,8 @@ class CopilotVaadinProjectListener : VaadinProjectListener {
override fun vaadinProjectDetected(project: Project) {
if (!triggered) {
triggered = true
runInEdt {
WriteAction.run<Throwable> {
saveDotFile(project)
removeDotFileOnExit(project)
}
}
saveDotFile(project)
removeDotFileOnExit(project)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.command.UndoConfirmationPolicy
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.findDocument
Expand All @@ -31,6 +32,7 @@ class CopilotUndoManagerTest : BasePlatformTestCase() {
@BeforeEach
fun setup() {
super.setUp()
DumbService.getInstance(project).waitForSmartMode()
tempFile = File("${project.basePath}/${UUID.random().text}.tmp")
tempFile.deleteOnExit()
Files.createDirectories(Path.of(project.basePath))
Expand Down

0 comments on commit 4c87dc6

Please sign in to comment.