Skip to content

Commit 2e37bfe

Browse files
committed
refactor(dir): simplify directory listing logic and improve output format
- Combine conditions for early return in `listDirectory`. - Filter out binary and hashed JSON files before processing. - Refactor file and subdirectory listing into a single loop with formatted output. - Add file size display for non-directory items.
1 parent d3637f6 commit 2e37bfe

File tree

2 files changed

+13
-25
lines changed
  • exts/devins-lang/src/main

2 files changed

+13
-25
lines changed

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

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.openapi.progress.Task
1010
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
1111
import com.intellij.openapi.project.Project
1212
import com.intellij.openapi.roots.ProjectFileIndex
13+
import com.intellij.openapi.util.text.StringUtilRt
1314
import com.intellij.openapi.vcs.FileStatus
1415
import com.intellij.openapi.vcs.FileStatusManager
1516
import com.intellij.openapi.vfs.VirtualFile
@@ -87,33 +88,18 @@ class DirInsCommand(private val myProject: Project, private val dir: String) : I
8788
}
8889

8990
private fun listDirectory(project: Project, directory: PsiDirectory, depth: Int) {
90-
if (depth > maxLength) return
91-
if (isExclude(project, directory)) return
91+
if (depth > maxLength || isExclude(project, directory)) return
9292

93-
val files = directory.files
93+
val files = directory.files.filter { !it.fileType.isBinary && !isHashJson(it.virtualFile) }
9494
val subdirectories = directory.subdirectories
9595

96-
for ((index, file) in files.withIndex()) {
97-
/// skip binary files? ignore hashed file names, like `f5086740-a1a1-491b-82c9-ab065a9d1754.json`
98-
if (file.fileType.isBinary) continue
99-
if (isHashJson(file.virtualFile)) continue
96+
val items = files.map { it.name to StringUtilRt.formatFileSize(it.virtualFile.length) } +
97+
subdirectories.map { it.name + "/" to null }
10098

101-
if (index == files.size - 1) {
102-
output.appendLine("${" ".repeat(depth)}└── ${file.name}")
103-
} else {
104-
output.appendLine("${" ".repeat(depth)}├── ${file.name}")
105-
}
106-
}
107-
108-
for ((index, subdirectory) in subdirectories.withIndex()) {
109-
if (isExclude(project, directory)) continue
110-
111-
if (index == subdirectories.size - 1) {
112-
output.appendLine("${" ".repeat(depth)}└── ${subdirectory.name}/")
113-
} else {
114-
output.appendLine("${" ".repeat(depth)}├── ${subdirectory.name}/")
115-
}
116-
listDirectory(project, subdirectory, depth + 1)
99+
items.forEachIndexed { index, (name, size) ->
100+
val prefix = if (index == items.lastIndex) "" else ""
101+
output.appendLine("${" ".repeat(depth)}$prefix $name${size?.let { " ($it)" } ?: ""}")
102+
if (size == null) listDirectory(project, subdirectories[index - files.size], depth + 1)
117103
}
118104
}
119105

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
List the contents of a directory in 2 depth
2-
/dir:src
1+
List the contents of a directory up to a depth of 2 (the default depth)
2+
/dir:.
3+
If navigating deeper, list contents up to a depth of 2 for each selected directory:
4+
/dir:src/components

0 commit comments

Comments
 (0)