Skip to content

Commit

Permalink
feat(devti-lang): improve directory creation logic for multiple levels
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Apr 10, 2024
1 parent 60d885a commit 23a71e1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/devins/quick-start.md
Expand Up @@ -37,6 +37,8 @@ ScreenShot

### File Command

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

Read file content:

Explain code /file:src/main/java/com/example/Controller.java
Expand Down
Expand Up @@ -15,9 +15,10 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import java.io.File

class WriteInsCommand(val myProject: Project, val argument: String, val content: String) : InsCommand {
private val pathSeparator = "/"

override suspend fun execute(): String? {
val content = Code.parse(content).text

Expand All @@ -29,13 +30,13 @@ class WriteInsCommand(val myProject: Project, val argument: String, val content:
if (virtualFile == null) {
return runWriteAction {
// filepath maybe just a file name, so we need to create parent directory
val hasChildPath = filepath.contains(File.separator)
val hasChildPath = filepath.contains(pathSeparator)
if (hasChildPath) {
val parentPath = filepath.substringBeforeLast(File.separator)
val parentPath = filepath.substringBeforeLast(pathSeparator)
var parentDir = projectDir.findChild(parentPath)
if (parentDir == null) {
// parentDir maybe multiple level, so we need to create all parent directory
val parentDirs = parentPath.split(File.separator)
val parentDirs = parentPath.split(pathSeparator)
parentDir = projectDir
for (dir in parentDirs) {
if (dir.isEmpty()) continue
Expand Down Expand Up @@ -67,7 +68,7 @@ class WriteInsCommand(val myProject: Project, val argument: String, val content:
}

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

document.setText(content)
Expand Down

0 comments on commit 23a71e1

Please sign in to comment.