Skip to content

Commit 49e7477

Browse files
committed
refactor(commands): improve file search and directory listing
- Use `runReadAction` for thread-safe operations in `LocalSearchInsCommand` and `DirShireCommand`. - Add line numbers to search results and exclude ignored files in `LocalSearchInsCommand`. - Skip `.idea` directory and ignored files in directory listing in `DirShireCommand`.
1 parent 2cf0aea commit 49e7477

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

shirelang/src/main/kotlin/com/phodal/shirelang/compiler/execute/command/DirShireCommand.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.phodal.shirelang.compiler.execute.command
22

3+
import com.intellij.openapi.application.runReadAction
34
import com.intellij.openapi.project.Project
5+
import com.intellij.openapi.roots.ProjectFileIndex
46
import com.intellij.psi.PsiDirectory
57
import com.intellij.psi.PsiManager
68
import com.phodal.shirecore.lookupFile
@@ -34,12 +36,17 @@ class DirShireCommand(private val myProject: Project, private val dir: String) :
3436
val psiDirectory = PsiManager.getInstance(myProject).findDirectory(virtualFile) ?: return null
3537

3638
output.appendLine("$dir/")
37-
listDirectory(psiDirectory, 1)
39+
runReadAction { listDirectory(myProject, psiDirectory, 1) }
3840

3941
return output.toString()
4042
}
4143

42-
private fun listDirectory(directory: PsiDirectory, depth: Int) {
44+
private fun listDirectory(project: Project, directory: PsiDirectory, depth: Int) {
45+
val isExclude = ProjectFileIndex.getInstance(project).isUnderIgnored(directory.virtualFile)
46+
if (isExclude) {
47+
return
48+
}
49+
4350
val files = directory.files
4451
val subdirectories = directory.subdirectories
4552

@@ -57,7 +64,7 @@ class DirShireCommand(private val myProject: Project, private val dir: String) :
5764
} else {
5865
output.appendLine("${" ".repeat(depth)}├── ${subdirectory.name}/")
5966
}
60-
listDirectory(subdirectory, depth + 1)
67+
listDirectory(project, subdirectory, depth + 1)
6168
}
6269
}
6370
}

shirelang/src/main/kotlin/com/phodal/shirelang/compiler/execute/command/LocalSearchInsCommand.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.phodal.shirelang.compiler.execute.command
22

3+
import com.intellij.openapi.application.runReadAction
34
import com.intellij.openapi.project.Project
45
import com.intellij.openapi.roots.ProjectFileIndex
56
import com.intellij.openapi.vfs.VirtualFile
67
import com.phodal.shirecore.canBeAdded
8+
import com.phodal.shirecore.relativePath
9+
import kotlin.collections.component1
10+
import kotlin.collections.component2
711

812
/**
913
* Todo: Spike different search API in Intellij
@@ -18,7 +22,7 @@ import com.phodal.shirecore.canBeAdded
1822
* ```
1923
*
2024
*/
21-
class LocalSearchInsCommand(val myProject: Project, private val scope: String, val text: String?) : ShireCommand {
25+
class LocalSearchInsCommandLocalSearchInsCommand(val myProject: Project, private val scope: String, val text: String?) : ShireCommand {
2226
private val MAX_LINE_SIZE = 180
2327
private val OVERLAP = 4
2428

@@ -29,11 +33,11 @@ class LocalSearchInsCommand(val myProject: Project, private val scope: String, v
2933
throw IllegalArgumentException("Text length should be more than 4")
3034
}
3135

32-
val textSearch = search(myProject, text, OVERLAP)
36+
val textSearch = runReadAction { search(myProject, text, OVERLAP) }
3337
return textSearch.map { (file, lines) ->
34-
val filePath = file.path
38+
val filePath = file.relativePath(myProject)
3539
val linesWithContext = lines.joinToString("\n")
36-
"$filePath\n$linesWithContext"
40+
"file: $filePath\n$linesWithContext"
3741
}.joinToString("\n")
3842
}
3943

@@ -57,6 +61,10 @@ class LocalSearchInsCommand(val myProject: Project, private val scope: String, v
5761
return@iterateContent true
5862
}
5963

64+
if (ProjectFileIndex.getInstance(project).isUnderIgnored(file)) return@iterateContent true
65+
/// skip for .idea/
66+
if (file.path.contains(".idea")) return@iterateContent true
67+
6068
val content = file.contentsToByteArray().toString(Charsets.UTF_8).lines()
6169
val matchedIndices = content.withIndex()
6270
.filter { (_, line) ->
@@ -68,7 +76,9 @@ class LocalSearchInsCommand(val myProject: Project, private val scope: String, v
6876
val linesWithContext = matchedIndices.flatMap { index ->
6977
val start = (index - overlap).coerceAtLeast(0)
7078
val end = (index + overlap).coerceAtMost(content.size - 1)
71-
content.subList(start, end + 1)
79+
content.subList(start, end + 1).mapIndexed { offset, line ->
80+
"${start + offset + 1} $line"
81+
}
7282
}.distinct()
7383

7484
result[file] = linesWithContext

0 commit comments

Comments
 (0)