Skip to content

Commit 5b44456

Browse files
committed
Model script exclusion via SourceExclusions
1 parent a18cd78 commit 5b44456

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,20 @@ class CompilerClassPath(
157157

158158
private fun isBuildScript(file: Path): Boolean = file.fileName.toString().let { it == "pom.xml" || it == "build.gradle" || it == "build.gradle.kts" }
159159

160+
private fun findJavaSourceFiles(root: Path): Set<Path> {
161+
val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:*.java")
162+
return SourceExclusions(listOf(root), scriptsConfig)
163+
.walkIncluded()
164+
.filter { sourceMatcher.matches(it.fileName) }
165+
.toSet()
166+
}
167+
160168
override fun close() {
161169
compiler.close()
162170
outputDirectory.delete()
163171
}
164172
}
165173

166-
private fun findJavaSourceFiles(root: Path): Set<Path> {
167-
val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:*.java")
168-
return SourceExclusions(root)
169-
.walkIncluded()
170-
.filter { sourceMatcher.matches(it.fileName) }
171-
.toSet()
172-
}
173-
174174
private fun logAdded(sources: Collection<Path>, name: String) {
175175
when {
176176
sources.isEmpty() -> return

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ public data class DiagnosticsConfiguration(
3030
var debounceTime: Long = 250L
3131
)
3232

33-
public data class ScriptsConfiguration(
34-
/** Whether .kts scripts are handled. */
35-
var enabled: Boolean = false,
36-
/** Whether .gradle.kts scripts are handled. Only considered if scripts are enabled in general. */
37-
var buildScriptsEnabled: Boolean = false
38-
)
39-
4033
public data class JVMConfiguration(
4134
/** Which JVM target the Kotlin compiler uses. See Compiler.jvmTargetFrom for possible values. */
4235
var target: String = "default"

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class SourceFiles(
6969
private val scriptsConfig: ScriptsConfiguration
7070
) {
7171
private val workspaceRoots = mutableSetOf<Path>()
72-
private var exclusions = SourceExclusions(workspaceRoots)
72+
private var exclusions = SourceExclusions(workspaceRoots, scriptsConfig)
7373
private val files = NotifySourcePath(sp)
7474
private val open = mutableSetOf<URI>()
7575

@@ -177,20 +177,16 @@ class SourceFiles(
177177
}
178178

179179
private fun findSourceFiles(root: Path): Set<URI> {
180-
val glob = if (scriptsConfig.enabled) "*.{kt,kts}" else "*.kt"
181-
val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:$glob")
182-
return SourceExclusions(root)
180+
val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:*.{kt,kts}")
181+
return SourceExclusions(listOf(root), scriptsConfig)
183182
.walkIncluded()
184-
.filter {
185-
sourceMatcher.matches(it.fileName)
186-
&& (scriptsConfig.buildScriptsEnabled || !it.endsWith(".gradle.kts"))
187-
}
183+
.filter { sourceMatcher.matches(it.fileName) }
188184
.map(Path::toUri)
189185
.toSet()
190186
}
191187

192188
private fun updateExclusions() {
193-
exclusions = SourceExclusions(workspaceRoots)
189+
exclusions = SourceExclusions(workspaceRoots, scriptsConfig)
194190
}
195191

196192
fun isOpen(uri: URI): Boolean = (uri in open)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.javacs.kt
2+
3+
public data class ScriptsConfiguration(
4+
/** Whether .kts scripts are handled. */
5+
var enabled: Boolean = false,
6+
/** Whether .gradle.kts scripts are handled. Only considered if scripts are enabled in general. */
7+
var buildScriptsEnabled: Boolean = false
8+
)

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@ import java.nio.file.Paths
99

1010
// TODO: Read exclusions from gitignore/settings.json/... instead of
1111
// hardcoding them
12-
class SourceExclusions(private val workspaceRoots: Collection<Path>) {
13-
private val excludedPatterns = listOf(".*", "bazel-*", "bin", "build", "node_modules", "target").map { FileSystems.getDefault().getPathMatcher("glob:$it") }
14-
15-
constructor(workspaceRoot: Path) : this(listOf(workspaceRoot)) {}
12+
class SourceExclusions(
13+
private val workspaceRoots: Collection<Path>,
14+
private val scriptsConfig: ScriptsConfiguration
15+
) {
16+
private val excludedPatterns = listOf(
17+
".*", "bazel-*", "bin", "build", "node_modules", "target",
18+
*(when {
19+
!scriptsConfig.enabled -> arrayOf("*.kts")
20+
!scriptsConfig.buildScriptsEnabled -> arrayOf("*.gradle.kts")
21+
else -> arrayOf()
22+
}),
23+
)
24+
.map { FileSystems.getDefault().getPathMatcher("glob:$it") }
1625

1726
/** Finds all non-excluded files recursively. */
1827
fun walkIncluded(): Sequence<Path> = workspaceRoots.asSequence().flatMap { root ->

0 commit comments

Comments
 (0)