Skip to content

Commit 5090d8c

Browse files
committed
fix(auto-save): change action update thread to EDT and improve file creation error handling
1 parent 2d0ed97 commit 5090d8c

File tree

2 files changed

+36
-49
lines changed

2 files changed

+36
-49
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/snippet/AutoDevSaveFileAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.intellij.openapi.vfs.LocalFileSystem
1616
import java.io.IOException
1717

1818
class AutoDevSaveFileAction : AnAction(AutoDevBundle.message("autodev.save.action")) {
19-
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
19+
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.EDT
2020

2121
override fun update(e: AnActionEvent) {
2222
val editor = e.getData(PlatformDataKeys.EDITOR) ?: return

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/WriteInsCommand.kt

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import cc.unitmesh.devti.language.compiler.error.DEVINS_ERROR
66
import cc.unitmesh.devti.language.compiler.model.LineInfo
77
import cc.unitmesh.devti.language.psi.DevInUsed
88
import cc.unitmesh.devti.language.utils.lookupFile
9-
import com.intellij.openapi.application.ApplicationManager
109
import com.intellij.openapi.application.runInEdt
1110
import com.intellij.openapi.application.runReadAction
1211
import com.intellij.openapi.application.runWriteAction
@@ -19,10 +18,6 @@ import com.intellij.openapi.vfs.VirtualFile
1918
import com.intellij.psi.PsiDocumentManager
2019
import com.intellij.psi.PsiFile
2120
import com.intellij.psi.PsiManager
22-
import com.intellij.util.concurrency.AppExecutorUtil
23-
import kotlinx.coroutines.CoroutineScope
24-
import kotlinx.coroutines.asCoroutineDispatcher
25-
import kotlinx.coroutines.launch
2621

2722
class WriteInsCommand(val myProject: Project, val argument: String, val content: String, val used: DevInUsed) :
2823
InsCommand {
@@ -52,73 +47,65 @@ class WriteInsCommand(val myProject: Project, val argument: String, val content:
5247
}
5348

5449
private fun writeToFile(filepath: String, projectDir: VirtualFile): String {
55-
// filepath maybe just a file name, so we need to create parent directory
5650
val hasChildPath = filepath.contains(pathSeparator)
57-
if (hasChildPath) {
58-
val parentPath = filepath.substringBeforeLast(pathSeparator)
59-
var parentDir = projectDir.findChild(parentPath)
60-
if (parentDir == null) {
61-
// parentDir maybe multiple level, so we need to create all parent directory
62-
val parentDirs = parentPath.split(pathSeparator)
63-
parentDir = projectDir
64-
for (dir in parentDirs) {
65-
if (dir.isEmpty()) continue
66-
67-
//check child folder if exist? if not, create it
68-
if (parentDir?.findChild(dir) == null) {
69-
var parentDir: VirtualFile? = null
70-
runInEdt {
71-
runWriteAction {
72-
parentDir = parentDir?.createChildDirectory(this, dir)
73-
}
51+
if (!hasChildPath) {
52+
return createNewContent(projectDir, filepath, content) ?: "$DEVINS_ERROR: Create File failed: $argument"
53+
}
54+
55+
val parentPath = filepath.substringBeforeLast(pathSeparator)
56+
var parentDir = projectDir.findChild(parentPath)
57+
if (parentDir == null) {
58+
val parentDirs = parentPath.split(pathSeparator)
59+
parentDir = projectDir
60+
for (dir in parentDirs) {
61+
if (dir.isEmpty()) continue
62+
63+
if (parentDir?.findChild(dir) == null) {
64+
var parentDir: VirtualFile? = null
65+
runInEdt {
66+
runWriteAction {
67+
parentDir = parentDir?.createChildDirectory(this, dir)
7468
}
75-
} else {
76-
parentDir = parentDir?.findChild(dir)
7769
}
78-
}
79-
80-
if (parentDir == null) {
81-
return "$DEVINS_ERROR: Create Directory failed: $parentPath"
70+
} else {
71+
parentDir = parentDir.findChild(dir)
8272
}
8373
}
8474

85-
return createNewContent(parentDir, filepath, content) ?: "$DEVINS_ERROR: Create File failed: $argument"
86-
} else {
87-
return createNewContent(projectDir, filepath, content) ?: "$DEVINS_ERROR: Create File failed: $argument"
75+
if (parentDir == null) {
76+
return "$DEVINS_ERROR: Create Directory failed: $parentPath"
77+
}
8878
}
79+
80+
return createNewContent(parentDir, filepath, content) ?: "$DEVINS_ERROR: Create File failed: $argument"
8981
}
9082

9183
private fun createNewContent(parentDir: VirtualFile, filepath: String, content: String): String? {
9284
var newFile: VirtualFile? = null
85+
var result: String? = null
9386
runInEdt {
94-
WriteCommandAction.runWriteCommandAction(myProject) {
87+
return@runInEdt WriteCommandAction.runWriteCommandAction(myProject) {
9588
val name = filepath.substringAfterLast(pathSeparator)
9689
if (name.isEmpty()) {
9790
return@runWriteCommandAction
9891
}
9992

10093
newFile = parentDir.createChildData(this, name)
101-
}
102-
}
103-
104-
if (newFile == null) {
105-
return "$DEVINS_ERROR: Create File failed: $argument"
106-
}
10794

108-
val document = runReadAction { FileDocumentManager.getInstance().getDocument(newFile) }
109-
?: return "$DEVINS_ERROR: File not found: $argument"
110-
111-
runInEdt {
112-
FileEditorManager.getInstance(myProject).openFile(newFile, true)
113-
}
95+
val document = runReadAction { FileDocumentManager.getInstance().getDocument(newFile) }
96+
if (document == null) {
97+
result = "$DEVINS_ERROR: Document not found: $argument"
98+
return@runWriteCommandAction
99+
}
114100

115-
runInEdt {
116-
WriteCommandAction.runWriteCommandAction(myProject) {
101+
FileEditorManager.getInstance(myProject).openFile(newFile, true)
117102
document.setText(content)
103+
104+
result = "Writing to file: $argument"
118105
}
119106
}
120107

121-
return "Writing to file: $argument"
108+
return result
122109
}
123110

124111
private fun executeInsert(

0 commit comments

Comments
 (0)