@@ -6,7 +6,6 @@ import cc.unitmesh.devti.language.compiler.error.DEVINS_ERROR
6
6
import cc.unitmesh.devti.language.compiler.model.LineInfo
7
7
import cc.unitmesh.devti.language.psi.DevInUsed
8
8
import cc.unitmesh.devti.language.utils.lookupFile
9
- import com.intellij.openapi.application.ApplicationManager
10
9
import com.intellij.openapi.application.runInEdt
11
10
import com.intellij.openapi.application.runReadAction
12
11
import com.intellij.openapi.application.runWriteAction
@@ -19,10 +18,6 @@ import com.intellij.openapi.vfs.VirtualFile
19
18
import com.intellij.psi.PsiDocumentManager
20
19
import com.intellij.psi.PsiFile
21
20
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
26
21
27
22
class WriteInsCommand (val myProject : Project , val argument : String , val content : String , val used : DevInUsed ) :
28
23
InsCommand {
@@ -52,73 +47,65 @@ class WriteInsCommand(val myProject: Project, val argument: String, val content:
52
47
}
53
48
54
49
private fun writeToFile (filepath : String , projectDir : VirtualFile ): String {
55
- // filepath maybe just a file name, so we need to create parent directory
56
50
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)
74
68
}
75
- } else {
76
- parentDir = parentDir?.findChild(dir)
77
69
}
78
- }
79
-
80
- if (parentDir == null ) {
81
- return " $DEVINS_ERROR : Create Directory failed: $parentPath "
70
+ } else {
71
+ parentDir = parentDir.findChild(dir)
82
72
}
83
73
}
84
74
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
+ }
88
78
}
79
+
80
+ return createNewContent(parentDir, filepath, content) ? : " $DEVINS_ERROR : Create File failed: $argument "
89
81
}
90
82
91
83
private fun createNewContent (parentDir : VirtualFile , filepath : String , content : String ): String? {
92
84
var newFile: VirtualFile ? = null
85
+ var result: String? = null
93
86
runInEdt {
94
- WriteCommandAction .runWriteCommandAction(myProject) {
87
+ return @runInEdt WriteCommandAction .runWriteCommandAction(myProject) {
95
88
val name = filepath.substringAfterLast(pathSeparator)
96
89
if (name.isEmpty()) {
97
90
return @runWriteCommandAction
98
91
}
99
92
100
93
newFile = parentDir.createChildData(this , name)
101
- }
102
- }
103
-
104
- if (newFile == null ) {
105
- return " $DEVINS_ERROR : Create File failed: $argument "
106
- }
107
94
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
+ }
114
100
115
- runInEdt {
116
- WriteCommandAction .runWriteCommandAction(myProject) {
101
+ FileEditorManager .getInstance(myProject).openFile(newFile, true )
117
102
document.setText(content)
103
+
104
+ result = " Writing to file: $argument "
118
105
}
119
106
}
120
107
121
- return " Writing to file: $argument "
108
+ return result
122
109
}
123
110
124
111
private fun executeInsert (
0 commit comments