Skip to content

Commit 60378f7

Browse files
committed
feat(compiler): add DIR command for listing files and directories #257
- Introduce DIR command to list files and directories in a tree-like structure. - Implement DirInsCommand to handle directory listing logic. - Update BuiltinCommand enum to include DIR command.
1 parent 1399b02 commit 60378f7

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ class DevInsCompiler(
231231
RefactorInsCommand(myProject, prop, nextTextSegment)
232232
}
233233

234+
BuiltinCommand.DIR -> {
235+
result.isLocalCommand = true
236+
DirInsCommand(myProject, prop)
237+
}
238+
234239
else -> {
235240
PrintInsCommand("/" + commandNode.commandName + ":" + prop)
236241
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cc.unitmesh.devti.language.compiler.exec
2+
3+
import cc.unitmesh.devti.language.utils.lookupFile
4+
import com.intellij.openapi.project.Project
5+
import com.intellij.psi.PsiDirectory
6+
import com.intellij.psi.PsiManager
7+
8+
/**
9+
* Dir List files and directories in a tree-like structure
10+
*/
11+
class DirInsCommand(private val myProject: Project, private val dir: String) : InsCommand {
12+
private val output = StringBuilder()
13+
14+
override suspend fun execute(): String? {
15+
val virtualFile = myProject.lookupFile(dir) ?: return "File not found: $dir"
16+
val psiDirectory = PsiManager.getInstance(myProject).findDirectory(virtualFile) ?: return null
17+
18+
output.appendLine("$dir/")
19+
listDirectory(psiDirectory, 1)
20+
21+
return output.toString()
22+
}
23+
24+
private fun listDirectory(directory: PsiDirectory, depth: Int) {
25+
val files = directory.files
26+
val subdirectories = directory.subdirectories
27+
28+
for ((index, file) in files.withIndex()) {
29+
if (index == files.size - 1) {
30+
output.appendLine("${" ".repeat(depth)}└── ${file.name}")
31+
} else {
32+
output.appendLine("${" ".repeat(depth)}├── ${file.name}")
33+
}
34+
}
35+
36+
for ((index, subdirectory) in subdirectories.withIndex()) {
37+
if (index == subdirectories.size - 1) {
38+
output.appendLine("${" ".repeat(depth)}└── ${subdirectory.name}/")
39+
} else {
40+
output.appendLine("${" ".repeat(depth)}├── ${subdirectory.name}/")
41+
}
42+
listDirectory(subdirectory, depth + 1)
43+
}
44+
}
45+
}
46+

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/completion/dataprovider/BuiltinCommand.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,9 @@ enum class BuiltinCommand(
4141
true,
4242
true
4343
),
44-
BROWSE(
45-
"browse",
46-
"Get the content of a given URL",
47-
AllIcons.Toolwindows.WebToolWindow,
48-
true,
49-
true
50-
),
51-
Refactor(
52-
"refactor",
53-
"Refactor the content of a file",
54-
AutoDevIcons.Idea,
55-
true,
56-
true
57-
),
44+
BROWSE("browse", "Get the content of a given URL", AllIcons.Toolwindows.WebToolWindow, true, true),
45+
Refactor("refactor", "Refactor the content of a file", AutoDevIcons.Idea, true, true),
46+
DIR("dir", "List files and directories in a tree-like structure", AllIcons.Actions.ProjectDirectory, true, true),
5847
;
5948

6049
companion object {

0 commit comments

Comments
 (0)