@@ -7,9 +7,11 @@ import cc.unitmesh.devti.util.parser.Code
7
7
import com.intellij.openapi.application.runReadAction
8
8
import com.intellij.openapi.application.runWriteAction
9
9
import com.intellij.openapi.command.WriteCommandAction
10
+ import com.intellij.openapi.editor.Document
10
11
import com.intellij.openapi.fileEditor.FileDocumentManager
11
12
import com.intellij.openapi.project.Project
12
13
import com.intellij.openapi.project.guessProjectDir
14
+ import com.intellij.openapi.vfs.VirtualFile
13
15
import com.intellij.psi.PsiDocumentManager
14
16
import com.intellij.psi.PsiFile
15
17
import com.intellij.psi.PsiManager
@@ -26,26 +28,42 @@ class WriteInsCommand(val myProject: Project, val argument: String, val content:
26
28
val virtualFile = myProject.lookupFile(filepath)
27
29
if (virtualFile == null ) {
28
30
return runWriteAction {
29
- val parentPath = filepath.substringBeforeLast(File .separator)
30
- var parentDir = projectDir.findChild(parentPath)
31
- if (parentDir == null ) {
32
- parentDir = projectDir.createChildDirectory(this , parentPath)
33
- }
34
- val newFile = parentDir.createChildData(this , filepath.substringAfterLast(File .separator))
35
- val document = FileDocumentManager .getInstance().getDocument(newFile)
36
- ? : return @runWriteAction " $DEVINS_ERROR : Create File failed: $argument "
31
+ // filepath maybe just a file name, so we need to create parent directory
32
+ val hasChildPath = filepath.contains(File .separator)
33
+ if (hasChildPath) {
34
+ val parentPath = filepath.substringBeforeLast(File .separator)
35
+ var parentDir = projectDir.findChild(parentPath)
36
+ if (parentDir == null ) {
37
+ parentDir = projectDir.createChildDirectory(this , parentPath)
38
+ }
39
+
40
+ createNewContent(parentDir, filepath, content)
41
+ ? : return @runWriteAction " $DEVINS_ERROR : Create File failed: $argument "
37
42
38
- document.setText(content)
43
+ return @runWriteAction " Create file: $argument "
44
+ } else {
45
+ createNewContent(projectDir, filepath, content)
46
+ ? : return @runWriteAction " $DEVINS_ERROR : Create File failed: $argument "
39
47
40
- return @runWriteAction " Create file: $argument "
48
+ return @runWriteAction " Create file: $argument "
49
+ }
41
50
}
42
51
}
43
52
44
53
val psiFile = PsiManager .getInstance(myProject).findFile(virtualFile)
45
54
? : return " $DEVINS_ERROR : File not found: $argument "
55
+
46
56
return executeInsert(psiFile, range, content)
47
57
}
48
58
59
+ private fun createNewContent (parentDir : VirtualFile , filepath : String , content : String ): Document ? {
60
+ val newFile = parentDir.createChildData(this , filepath.substringAfterLast(File .separator))
61
+ val document = FileDocumentManager .getInstance().getDocument(newFile) ? : return null
62
+
63
+ document.setText(content)
64
+ return document
65
+ }
66
+
49
67
private fun executeInsert (
50
68
psiFile : PsiFile ,
51
69
range : LineInfo ? ,
0 commit comments