Skip to content

Commit 23a71e1

Browse files
committed
feat(devti-lang): improve directory creation logic for multiple levels #143
1 parent 60d885a commit 23a71e1

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

docs/devins/quick-start.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ ScreenShot
3737

3838
### File Command
3939

40+
based on [#143](https://github.com/unit-mesh/auto-dev/issues/143), we keep "/" as `File.separator` for macOS, Windows and Unix.
41+
4042
Read file content:
4143

4244
Explain code /file:src/main/java/com/example/Controller.java

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import com.intellij.openapi.vfs.VirtualFile
1515
import com.intellij.psi.PsiDocumentManager
1616
import com.intellij.psi.PsiFile
1717
import com.intellij.psi.PsiManager
18-
import java.io.File
1918

2019
class WriteInsCommand(val myProject: Project, val argument: String, val content: String) : InsCommand {
20+
private val pathSeparator = "/"
21+
2122
override suspend fun execute(): String? {
2223
val content = Code.parse(content).text
2324

@@ -29,13 +30,13 @@ class WriteInsCommand(val myProject: Project, val argument: String, val content:
2930
if (virtualFile == null) {
3031
return runWriteAction {
3132
// filepath maybe just a file name, so we need to create parent directory
32-
val hasChildPath = filepath.contains(File.separator)
33+
val hasChildPath = filepath.contains(pathSeparator)
3334
if (hasChildPath) {
34-
val parentPath = filepath.substringBeforeLast(File.separator)
35+
val parentPath = filepath.substringBeforeLast(pathSeparator)
3536
var parentDir = projectDir.findChild(parentPath)
3637
if (parentDir == null) {
3738
// parentDir maybe multiple level, so we need to create all parent directory
38-
val parentDirs = parentPath.split(File.separator)
39+
val parentDirs = parentPath.split(pathSeparator)
3940
parentDir = projectDir
4041
for (dir in parentDirs) {
4142
if (dir.isEmpty()) continue
@@ -67,7 +68,7 @@ class WriteInsCommand(val myProject: Project, val argument: String, val content:
6768
}
6869

6970
private fun createNewContent(parentDir: VirtualFile, filepath: String, content: String): Document? {
70-
val newFile = parentDir.createChildData(this, filepath.substringAfterLast(File.separator))
71+
val newFile = parentDir.createChildData(this, filepath.substringAfterLast(pathSeparator))
7172
val document = FileDocumentManager.getInstance().getDocument(newFile) ?: return null
7273

7374
document.setText(content)

0 commit comments

Comments
 (0)