Skip to content

Commit b433e8d

Browse files
committed
feat(compiler): enhance DirInsCommand with smart depth control and compact mode
- Add default depth limit of 2 levels for cleaner output - Introduce compact mode for directories beyond depth limit - Display file sizes for regular files - Exclude build and configuration directories - Improve handling of deep Java package structures
1 parent b7d6b2a commit b433e8d

File tree

1 file changed

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

1 file changed

+54
-22
lines changed

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

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,46 @@ import com.intellij.psi.PsiManager
1313

1414

1515
/**
16-
* The `DirInsCommand` class is responsible for listing files and directories in a tree-like structure for a given directory path within a project.
17-
* It implements the `InsCommand` interface and provides an `execute` method to perform the directory listing operation asynchronously.
16+
* The `DirInsCommand` class provides a tree-like directory listing with smart depth control and file size information.
17+
* It implements the `InsCommand` interface for directory exploration within a project.
1818
*
19-
* The tree structure is visually represented using indentation and symbols (`├──`, `└──`) to denote files and subdirectories. Files are listed
20-
* first, followed by subdirectories, which are recursively processed to display their contents.
19+
* Features:
20+
* - Default depth limit of 2 levels for cleaner output
21+
* - Smart depth handling for Java-like source directories
22+
* - File size display for regular files
23+
* - Compact mode for directory display beyond depth limit
24+
* - Automatic exclusion of build and configuration directories
2125
*
22-
* Example output:
26+
* Example output with compact mode:
2327
* ```
2428
* myDirectory/
25-
* ├── file1.txt
26-
* ├── file2.txt
27-
* └── subDirectory/
28-
* ├── file3.txt
29-
* └── subSubDirectory/
30-
* └── file4.txt
29+
* ├── README.md (2.5 KB)
30+
* ├── package.json (1.2 KB)
31+
* ├── config.xml (0.5 KB)
32+
* └── src/{main,test,resources}/
3133
* ```
3234
*
33-
* About depth design:
34-
* In Java Langauge, the depth of dir are very long
35-
* In JavaScript Langauge, the dirs files are too many
35+
* Display rules:
36+
* - Files are always shown completely with their sizes
37+
* - Directories beyond depth limit are shown in compact format {dir1,dir2,dir3}
38+
* - For src directories:
39+
* - If only one subdirectory exists, traversal continues beyond max depth
40+
* - Helps handle deep Java package structures efficiently
3641
*
37-
* @param myProject The project instance in which the directory resides.
38-
* @param dir The path of the directory to list.
42+
* Excluded directories:
43+
* - .idea/
44+
* - build/
45+
* - target/
46+
* - node_modules/
47+
* - Any directory marked as ignored by VCS
48+
*
49+
* @param myProject The project instance in which the directory resides
50+
* @param dir The path of the directory to list
3951
*/
4052
class DirInsCommand(private val myProject: Project, private val dir: String) : InsCommand {
4153
override val commandName: BuiltinCommand = BuiltinCommand.DIR
4254
private val defaultMaxDepth = 2
55+
private val compactMode = true
4356

4457
private val output = StringBuilder()
4558

@@ -60,13 +73,32 @@ class DirInsCommand(private val myProject: Project, private val dir: String) : I
6073
val files = directory.files
6174
val subdirectories = directory.subdirectories
6275

63-
val items = files.map { it.name to StringUtilRt.formatFileSize(it.virtualFile.length) } +
64-
subdirectories.map { it.name + "/" to null }
76+
// 始终完整显示文件
77+
files.forEachIndexed { index, file ->
78+
val isLast = index == files.lastIndex && subdirectories.isEmpty()
79+
val prefix = if (isLast) "" else ""
80+
val size = StringUtilRt.formatFileSize(file.virtualFile.length)
81+
output.appendLine("${" ".repeat(depth)}$prefix── ${file.name}${size?.let { " ($it)" } ?: ""}")
82+
}
6583

66-
items.forEachIndexed { index, (name, size) ->
67-
val prefix = if (index == items.lastIndex) "" else ""
68-
output.appendLine("${" ".repeat(depth)}$prefix $name${size?.let { " ($it)" } ?: ""}")
69-
if (size == null) listDirectory(project, subdirectories[index - files.size], depth + 1)
84+
// 处理目录
85+
if (subdirectories.isNotEmpty()) {
86+
if (compactMode && depth >= defaultMaxDepth) {
87+
// 紧凑模式下只显示目录列表
88+
val dirNames = subdirectories
89+
.filter { !isExclude(project, it) }
90+
.map { it.name }
91+
if (dirNames.isNotEmpty()) {
92+
output.appendLine("${" ".repeat(depth)}└── {${dirNames.joinToString(",")}}/")
93+
}
94+
} else {
95+
subdirectories.forEachIndexed { index, subdir ->
96+
if (isExclude(project, subdir)) return@forEachIndexed
97+
val prefix = if (index == subdirectories.lastIndex) "" else ""
98+
output.appendLine("${" ".repeat(depth)}$prefix── ${subdir.name}/")
99+
listDirectory(project, subdir, depth + 1)
100+
}
101+
}
70102
}
71103
}
72104

0 commit comments

Comments
 (0)