Skip to content

Commit 03fcbce

Browse files
committed
refactor(sketch): replace EditFileCommand with async execution
- Remove EditFileCommand import and use scratch file execution - Add runWriteAction wrapper for async file execution - Add file cleanup in finally block to prevent resource leaks - Switch from runFile to runFileAsync for better performance
1 parent 825d224 commit 03fcbce

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/code/CodeHighlightSketch.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cc.unitmesh.devti.sketch.ui.code
33
import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.AutoDevIcons
55
import cc.unitmesh.devti.AutoDevNotifications
6-
import cc.unitmesh.devti.command.EditFileCommand
76
import cc.unitmesh.devti.command.dataprovider.BuiltinCommand
87
import cc.unitmesh.devti.gui.chat.ui.AutoInputService
98
import cc.unitmesh.devti.provider.BuildSystemProvider
@@ -25,6 +24,7 @@ import com.intellij.openapi.actionSystem.impl.ActionButton
2524
import com.intellij.openapi.application.ReadAction
2625
import com.intellij.openapi.application.runInEdt
2726
import com.intellij.openapi.application.runReadAction
27+
import com.intellij.openapi.application.runWriteAction
2828
import com.intellij.openapi.command.WriteCommandAction
2929
import com.intellij.openapi.diagnostic.logger
3030
import com.intellij.openapi.editor.Document
@@ -38,7 +38,6 @@ import com.intellij.openapi.project.Project
3838
import com.intellij.openapi.util.Disposer
3939
import com.intellij.openapi.util.text.StringUtil
4040
import com.intellij.openapi.vfs.VirtualFile
41-
import com.intellij.psi.PsiFile
4241
import com.intellij.psi.PsiManager
4342
import com.intellij.ui.JBColor
4443
import com.intellij.ui.components.JBLabel
@@ -139,7 +138,6 @@ open class CodeHighlightSketch(
139138
}
140139

141140

142-
143141
private val plainText = PlainTextLanguage.INSTANCE.displayName
144142

145143
private val devinLanguageId = "devin"
@@ -517,22 +515,26 @@ private fun CodeHighlightSketch.handleExecutionResult(result: String?, button: J
517515
}
518516

519517
private suspend fun executeEditFileCommand(project: Project, currentText: String, callback: (String?) -> Unit) {
518+
var file: VirtualFile? = null
520519
try {
521520
val newFileName = "DevIn-${System.currentTimeMillis()}.devin"
522521
val language = Language.findLanguageByID("DevIn")
523-
val file = ScratchRootType.getInstance()
522+
file = ScratchRootType.getInstance()
524523
.createScratchFile(project, newFileName, language, currentText)
525524

526525
if (file == null) return callback("DEVINS_ERROR: Failed to create scratch file")
527526

528527
val psiFile = runReadAction { PsiManager.getInstance(project).findFile(file)!! }
529528

530-
RunService.provider(project, file)
531-
?.runFile(project, file, psiFile, isFromToolAction = true)
532-
?: RunService.runInCli(project, psiFile)
533-
?: AutoDevNotifications.notify(project, "No run service found for ${file.name}")
529+
runWriteAction {
530+
RunService.provider(project, file)
531+
?.runFileAsync(project, file, psiFile)
532+
?: AutoDevNotifications.notify(project, "No run service found for ${file.name}")
533+
}
534534
} catch (e: Exception) {
535535
callback("DEVINS_ERROR: ${e.message}")
536+
} finally {
537+
file?.delete(project)
536538
}
537539
}
538540

0 commit comments

Comments
 (0)