Skip to content

Commit 1eb78da

Browse files
feat: allow walking generated sources inside target folder
1 parent 8418fb5 commit 1eb78da

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CompilerClassPath(
4545
compiler.updateConfiguration(config)
4646
}
4747

48-
/** Updates and possibly reinstantiates the compiler using new paths. */
48+
/** Updates and possibly re-instantiates the compiler using new paths. */
4949
private fun refresh(
5050
updateClassPath: Boolean = true,
5151
updateBuildScriptClassPath: Boolean = true,
Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,78 @@
11
package org.javacs.kt
22

3-
import org.javacs.kt.util.filePath
4-
import java.io.File
53
import java.net.URI
64
import java.nio.file.FileSystems
75
import java.nio.file.Path
8-
import java.nio.file.Paths
6+
import org.javacs.kt.util.filePath
97

108
// TODO: Read exclusions from gitignore/settings.json/... instead of
119
// hardcoding them
1210
class SourceExclusions(
1311
private val workspaceRoots: Collection<Path>,
14-
private val scriptsConfig: ScriptsConfiguration
12+
scriptsConfig: ScriptsConfiguration,
1513
) {
16-
val excludedPatterns = (listOf(
17-
".git", ".hg", ".svn", // Version control systems
18-
".idea", ".idea_modules", ".vs", ".vscode", ".code-workspace", ".settings", // IDEs
19-
"bazel-*", "bin", "build", "node_modules", "target", // Build systems
20-
) + when {
21-
!scriptsConfig.enabled -> listOf("*.kts")
22-
!scriptsConfig.buildScriptsEnabled -> listOf("*.gradle.kts")
23-
else -> emptyList()
24-
})
14+
val excludedPatterns =
15+
(listOf(
16+
".git",
17+
".hg",
18+
".svn", // Version control systems
19+
".idea",
20+
".idea_modules",
21+
".vs",
22+
".vscode",
23+
".code-workspace",
24+
".settings", // IDEs
25+
"bazel-*",
26+
"bin",
27+
"build",
28+
"node_modules",
29+
) +
30+
when {
31+
!scriptsConfig.enabled -> listOf("*.kts")
32+
!scriptsConfig.buildScriptsEnabled -> listOf("*.gradle.kts")
33+
else -> emptyList()
34+
})
2535

2636
private val exclusionMatchers = excludedPatterns
37+
.filter { !it.startsWith("!") }
2738
.map { FileSystems.getDefault().getPathMatcher("glob:$it") }
2839

29-
/** Finds all non-excluded files recursively. */
30-
fun walkIncluded(): Sequence<Path> = workspaceRoots.asSequence().flatMap { root ->
31-
root.toFile()
32-
.walk()
33-
.onEnter { isPathIncluded(it.toPath()) }
34-
.map { it.toPath() }
35-
}
40+
fun walkIncluded(): Sequence<Path> =
41+
workspaceRoots.asSequence().flatMap { root ->
42+
root.toFile().walk().onEnter { isPathIncluded(it.toPath()) }.map { it.toPath() }
43+
}
3644

37-
/** Tests whether the given URI is not excluded. */
3845
fun isURIIncluded(uri: URI) = uri.filePath?.let(this::isPathIncluded) ?: false
3946

40-
/** Tests whether the given path is not excluded. */
41-
fun isPathIncluded(file: Path): Boolean = workspaceRoots.any { file.startsWith(it) }
42-
&& exclusionMatchers.none { matcher ->
43-
workspaceRoots
44-
.mapNotNull { if (file.startsWith(it)) it.relativize(file) else null }
45-
.flatMap { it } // Extract path segments
46-
.any(matcher::matches)
47+
fun isPathIncluded(file: Path): Boolean {
48+
if (!workspaceRoots.any { file.startsWith(it) }) {
49+
return false
50+
}
51+
52+
val relativePaths = workspaceRoots
53+
.mapNotNull { if (file.startsWith(it)) it.relativize(file) else null }
54+
.flatten()
55+
56+
// Check if we're in a target directory
57+
if (relativePaths.contains(Path.of("target"))) {
58+
val pathList = relativePaths.toList()
59+
val targetIndex = pathList.indexOf(Path.of("target"))
60+
61+
// Allow only target directory itself or if next directory is generated-sources
62+
return pathList.size <= targetIndex + 1 ||
63+
pathList[targetIndex + 1] == Path.of("generated-sources")
64+
}
65+
66+
// If path matches any exclusion pattern, exclude it
67+
if (exclusionMatchers.any { matcher ->
68+
relativePaths.any(matcher::matches)
69+
}) {
70+
return false
4771
}
72+
73+
// Include paths outside target directory by default
74+
return true
75+
}
76+
4877
}
78+

0 commit comments

Comments
 (0)