Skip to content

Commit 0bdb5e6

Browse files
committed
feat(compiler): add directory compression for deep nesting #308
Introduce logic to compress and display deeply nested directories when their depth exceeds a threshold. This improves readability by grouping subdirectories that meet specific conditions into a single compressed line, avoiding excessive recursion and clutter in the output.
1 parent 8e2e2c0 commit 0bdb5e6

File tree

1 file changed

+55
-5
lines changed
  • exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec

1 file changed

+55
-5
lines changed

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

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class DirInsCommand(private val myProject: Project, private val dir: String) : I
6666
if(isExclude(project, directory)) return
6767

6868
val files = directory.files
69-
val subdirectories = directory.subdirectories
69+
val subdirectories = directory.subdirectories.filter { !isExclude(project, it) }.toList()
7070

7171
// 只在深度不超过默认最大深度时显示文件
7272
if (depth <= defaultMaxDepth) {
@@ -78,17 +78,67 @@ class DirInsCommand(private val myProject: Project, private val dir: String) : I
7878
}
7979
}
8080

81-
// 无论深度如何,始终显示子目录
81+
// 如果子目录深度超过一定值,考虑压缩显示
82+
if (depth > defaultMaxDepth + 1) {
83+
// 检查是否所有子目录都已经达到最大深度可压缩显示
84+
val canCompressAllSubdirs = subdirectories.all {
85+
it.subdirectories.isNotEmpty() &&
86+
it.subdirectories.all { subdir ->
87+
!isExclude(project, subdir) && subdir.subdirectories.isEmpty()
88+
}
89+
}
90+
91+
if (canCompressAllSubdirs && subdirectories.isNotEmpty()) {
92+
// 收集所有叶节点目录名
93+
val compressedNames = mutableListOf<String>()
94+
subdirectories.forEach { subdir ->
95+
val leafDirs = subdir.subdirectories.filter { !isExclude(project, it) }
96+
if (leafDirs.isNotEmpty()) {
97+
compressedNames.add(subdir.name)
98+
}
99+
}
100+
101+
if (compressedNames.isNotEmpty()) {
102+
val prefix = "" // 这里可以根据实际情况决定是否是最后一项
103+
output.appendLine("${" ".repeat(depth)}$prefix── {${compressedNames.joinToString(",")}}/")
104+
return // 不再递归显示更深层次
105+
}
106+
}
107+
}
108+
109+
// 常规目录显示逻辑
82110
subdirectories.forEachIndexed { index, subdir ->
83-
if (isExclude(project, subdir)) return@forEachIndexed
84111
val prefix = if (index == subdirectories.lastIndex) "" else ""
85112
output.appendLine("${" ".repeat(depth)}$prefix── ${subdir.name}/")
86113

87-
// 继续递归,文件显示将受深度限制
88-
listDirectory(project, subdir, depth + 1)
114+
// 判断是否需要压缩显示子目录
115+
if (shouldCompressChildren(project, subdir, depth + 1)) {
116+
compressAndDisplayChildren(project, subdir, depth + 1)
117+
} else {
118+
// 继续递归,文件显示将受深度限制
119+
listDirectory(project, subdir, depth + 1)
120+
}
89121
}
90122
}
91123

124+
private fun shouldCompressChildren(project: Project, directory: PsiDirectory, depth: Int): Boolean {
125+
// 当深度超过阈值且子目录结构符合压缩条件时
126+
if (depth > defaultMaxDepth + 1) {
127+
val subdirs = directory.subdirectories.filter { !isExclude(project, it) }
128+
return subdirs.size > 1 && subdirs.all { it.subdirectories.isEmpty() }
129+
}
130+
return false
131+
}
132+
133+
private fun compressAndDisplayChildren(project: Project, directory: PsiDirectory, depth: Int) {
134+
val subdirs = directory.subdirectories.filter { !isExclude(project, it) }
135+
if (subdirs.isEmpty()) return
136+
137+
val subdirNames = subdirs.map { it.name }
138+
val prefix = ""
139+
output.appendLine("${" ".repeat(depth)}$prefix── {${subdirNames.joinToString(",")}}/")
140+
}
141+
92142
private fun isExclude(project: Project, directory: PsiDirectory): Boolean {
93143
if (directory.name == ".idea" ||
94144
directory.name == "build" ||

0 commit comments

Comments
 (0)