@@ -66,7 +66,7 @@ class DirInsCommand(private val myProject: Project, private val dir: String) : I
66
66
if (isExclude(project, directory)) return
67
67
68
68
val files = directory.files
69
- val subdirectories = directory.subdirectories
69
+ val subdirectories = directory.subdirectories.filter { ! isExclude(project, it) }.toList()
70
70
71
71
// 只在深度不超过默认最大深度时显示文件
72
72
if (depth <= defaultMaxDepth) {
@@ -78,17 +78,67 @@ class DirInsCommand(private val myProject: Project, private val dir: String) : I
78
78
}
79
79
}
80
80
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
+ // 常规目录显示逻辑
82
110
subdirectories.forEachIndexed { index, subdir ->
83
- if (isExclude(project, subdir)) return @forEachIndexed
84
111
val prefix = if (index == subdirectories.lastIndex) " └" else " ├"
85
112
output.appendLine(" ${" " .repeat(depth)}$prefix ── ${subdir.name} /" )
86
113
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
+ }
89
121
}
90
122
}
91
123
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
+
92
142
private fun isExclude (project : Project , directory : PsiDirectory ): Boolean {
93
143
if (directory.name == " .idea" ||
94
144
directory.name == " build" ||
0 commit comments