Skip to content

Commit 5ec8ac2

Browse files
committed
Move file walking logic into SourceExclusions
1 parent f599ef6 commit 5ec8ac2

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import java.io.Closeable
55
import java.nio.file.Files
66
import java.nio.file.FileSystems
77
import java.nio.file.Path
8-
import java.util.stream.Collectors
98

109
/**
1110
* Manages the class path (compiled JARs, etc), the Java source path
@@ -129,14 +128,9 @@ class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
129128

130129
private fun findJavaSourceFiles(root: Path): Set<Path> {
131130
val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:*.java")
132-
val exclusions = SourceExclusions(root)
133-
return root.toFile().walk()
134-
.onEnter { exclusions.isPathIncluded(it.toPath()) }
135-
.map { it.toPath() }
136-
.filter {
137-
// LOG.info("At $it")
138-
sourceMatcher.matches(it)
139-
}
131+
return SourceExclusions(root)
132+
.walkIncluded()
133+
.filter { sourceMatcher.matches(it) }
140134
.toSet()
141135
}
142136

server/src/main/kotlin/org/javacs/kt/SourceFiles.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import java.nio.file.FileSystems
2020
import java.nio.file.Files
2121
import java.nio.file.Path
2222
import java.nio.file.Paths
23-
import java.util.stream.Collectors
2423

2524
private class SourceVersion(val content: String, val version: Int, val language: Language?, val isTemporary: Boolean)
2625

@@ -215,14 +214,13 @@ private fun patch(sourceText: String, change: TextDocumentContentChangeEvent): S
215214
}
216215
}
217216

218-
// TODO: Cut off branches that are excluded in the walker directly
219217
private fun findSourceFiles(root: Path): Set<URI> {
220218
val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:*.{kt,kts}")
221-
val exclusions = SourceExclusions(root)
222-
return Files.walk(root)
223-
.filter { exclusions.isPathIncluded(it) && sourceMatcher.matches(it.fileName) }
219+
return SourceExclusions(root)
220+
.walkIncluded()
221+
.filter { sourceMatcher.matches(it.fileName) }
224222
.map(Path::toUri)
225-
.collect(Collectors.toSet())
223+
.toSet()
226224
}
227225

228226
private fun logAdded(sources: Collection<URI>, rootPath: Path?) {

shared/src/main/kotlin/org/javacs/kt/SourceExclusions.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ import java.nio.file.Paths
1111
class SourceExclusions(private val workspaceRoots: Collection<Path>) {
1212
private val excludedFolders = listOf(".git", "bin", "build", "target", "node_modules")
1313

14-
constructor(workspaceRoot: Path) : this(listOf(workspaceRoot)) {}
14+
constructor(workspaceRoot: Path) : this(listOf(workspaceRoot)) {}
1515

16+
/** Finds all non-excluded files recursively. */
17+
fun walkIncluded(): Sequence<Path> = workspaceRoots.asSequence().flatMap { root ->
18+
root.toFile()
19+
.walk()
20+
.onEnter { isPathIncluded(it.toPath()) }
21+
.map { it.toPath() }
22+
}
23+
24+
/** Tests whether the given URI is not excluded. */
1625
fun isURIIncluded(uri: URI) = uri.filePath?.let(this::isPathIncluded) ?: false
1726

27+
/** Tests whether the given path is not excluded. */
1828
fun isPathIncluded(file: Path): Boolean = workspaceRoots.any { file.startsWith(it) }
1929
&& excludedFolders.none {
2030
workspaceRoots

0 commit comments

Comments
 (0)