@@ -13,33 +13,46 @@ import com.intellij.psi.PsiManager
13
13
14
14
15
15
/* *
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 .
18
18
*
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
21
25
*
22
- * Example output:
26
+ * Example output with compact mode :
23
27
* ```
24
28
* 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}/
31
33
* ```
32
34
*
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
36
41
*
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
39
51
*/
40
52
class DirInsCommand (private val myProject : Project , private val dir : String ) : InsCommand {
41
53
override val commandName: BuiltinCommand = BuiltinCommand .DIR
42
54
private val defaultMaxDepth = 2
55
+ private val compactMode = true
43
56
44
57
private val output = StringBuilder ()
45
58
@@ -60,13 +73,32 @@ class DirInsCommand(private val myProject: Project, private val dir: String) : I
60
73
val files = directory.files
61
74
val subdirectories = directory.subdirectories
62
75
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
+ }
65
83
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
+ }
70
102
}
71
103
}
72
104
0 commit comments