@@ -4,20 +4,12 @@ import cc.unitmesh.devti.devin.InsCommand
4
4
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
5
5
import cc.unitmesh.devti.language.utils.lookupFile
6
6
import com.intellij.openapi.application.runReadAction
7
- import com.intellij.openapi.progress.ProgressIndicator
8
- import com.intellij.openapi.progress.ProgressManager
9
- import com.intellij.openapi.progress.Task
10
- import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
11
7
import com.intellij.openapi.project.Project
12
- import com.intellij.openapi.roots.ProjectFileIndex
13
8
import com.intellij.openapi.util.text.StringUtilRt
14
9
import com.intellij.openapi.vcs.FileStatus
15
10
import com.intellij.openapi.vcs.FileStatusManager
16
- import com.intellij.openapi.vfs.VirtualFile
17
11
import com.intellij.psi.PsiDirectory
18
12
import com.intellij.psi.PsiManager
19
- import java.util.concurrent.CompletableFuture
20
- import java.util.regex.Pattern
21
13
22
14
23
15
/* *
@@ -47,50 +39,25 @@ import java.util.regex.Pattern
47
39
*/
48
40
class DirInsCommand (private val myProject : Project , private val dir : String ) : InsCommand {
49
41
override val commandName: BuiltinCommand = BuiltinCommand .DIR
50
- private val HASH_FILE_PATTERN : Pattern = Pattern .compile(
51
- " ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(?:\\ .json|@[0-9a-f]+\\ .json)$" ,
52
- Pattern .CASE_INSENSITIVE
53
- )
54
-
55
- fun isHashJson (file : VirtualFile ? ): Boolean {
56
- return file != null && HASH_FILE_PATTERN .matcher(file.name).matches()
57
- }
42
+ private val defaultMaxDepth = 2
58
43
59
44
private val output = StringBuilder ()
60
- private val maxLength = 2
61
45
62
46
override suspend fun execute (): String? {
63
47
val virtualFile = myProject.lookupFile(dir) ? : return " File not found: $dir "
64
- val future = CompletableFuture <String >()
65
- val task = object : Task .Backgroundable (myProject, " Processing context" , false ) {
66
- override fun run (indicator : ProgressIndicator ) {
67
- val psiDirectory = runReadAction {
68
- PsiManager .getInstance(myProject!! ).findDirectory(virtualFile)
69
- }
70
-
71
- if (psiDirectory == null ) {
72
- future.complete(" Directory not found: $dir " )
73
- return
74
- }
75
-
76
- output.appendLine(" $dir /" )
77
- runReadAction { listDirectory(myProject!! , psiDirectory, 1 ) }
78
- future.complete(
79
- " Here is the directory tree (depth = 2) for $dir :\n $output "
80
- )
81
- }
82
- }
48
+ val psiDirectory = PsiManager .getInstance(myProject).findDirectory(virtualFile) ? : return null
83
49
84
- ProgressManager .getInstance( )
85
- .runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator (task))
50
+ output.appendLine( " $dir / " )
51
+ runReadAction { listDirectory(myProject, psiDirectory, 1 ) }
86
52
87
- return future.get ()
53
+ return output.toString ()
88
54
}
89
55
90
56
private fun listDirectory (project : Project , directory : PsiDirectory , depth : Int ) {
91
- if (depth > maxLength || isExclude(project, directory)) return
57
+ if (isExclude(project, directory)) return
58
+ if (depth > defaultMaxDepth && ! shouldContinueForPath(directory)) return
92
59
93
- val files = directory.files.filter { ! it.fileType.isBinary && ! isHashJson(it.virtualFile) }
60
+ val files = directory.files
94
61
val subdirectories = directory.subdirectories
95
62
96
63
val items = files.map { it.name to StringUtilRt .formatFileSize(it.virtualFile.length) } +
@@ -103,11 +70,24 @@ class DirInsCommand(private val myProject: Project, private val dir: String) : I
103
70
}
104
71
}
105
72
73
+ // / todo: replace to intellij source content dir
74
+ private fun shouldContinueForPath (directory : PsiDirectory ): Boolean {
75
+ // 如果是 src 目录,检查是否是单路径结构
76
+ if (directory.name == " src" || directory.parent?.name == " src" ) {
77
+ val subdirs = directory.subdirectories
78
+ // 对于只有一个子目录的情况,继续遍历
79
+ return subdirs.size == 1
80
+ }
81
+ return false
82
+ }
83
+
106
84
private fun isExclude (project : Project , directory : PsiDirectory ): Boolean {
107
- if (directory.name == " .idea" ) return true
85
+ if (directory.name == " .idea" ||
86
+ directory.name == " build" ||
87
+ directory.name == " target" ||
88
+ directory.name == " node_modules" ) return true
108
89
109
90
val status = FileStatusManager .getInstance(project).getStatus(directory.virtualFile)
110
91
return status == FileStatus .IGNORED
111
92
}
112
93
}
113
-
0 commit comments