Skip to content

Commit 8d0fc49

Browse files
authored
Merge pull request #545 from fwcd/improve-exclusions-v2
Update exclusions upon config changes and log more verbosely
2 parents 70dfeb4 + 84dc685 commit 8d0fc49

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ class KotlinTextDocumentService(
8080

8181
private fun recover(uriString: String, position: Position, recompile: Recompile): Pair<CompiledFile, Int>? {
8282
val uri = parseURI(uriString)
83-
if (!sf.isIncluded(uri)) return null
83+
if (!sf.isIncluded(uri)) {
84+
LOG.warn("URI is excluded, therefore cannot be recovered: $uri")
85+
return null
86+
}
8487
val content = sp.content(uri)
8588
val offset = offset(content, position.line, position.character)
8689
val shouldRecompile = when (recompile) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class KotlinWorkspaceService(
140140
val scripts = config.scripts
141141
get("enabled")?.asBoolean?.let { scripts.enabled = it }
142142
get("buildScriptsEnabled")?.asBoolean?.let { scripts.buildScriptsEnabled = it }
143+
sf.updateExclusions()
143144
}
144145

145146
// Update code-completion options

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class SourceFiles(
7474
private val open = mutableSetOf<URI>()
7575

7676
fun open(uri: URI, content: String, version: Int) {
77-
if (exclusions.isURIIncluded(uri)) {
77+
if (isIncluded(uri)) {
7878
files[uri] = SourceVersion(content, version, languageOf(uri), isTemporary = false)
7979
open.add(uri)
8080
}
@@ -98,7 +98,7 @@ class SourceFiles(
9898
}
9999

100100
fun edit(uri: URI, newVersion: Int, contentChanges: List<TextDocumentContentChangeEvent>) {
101-
if (exclusions.isURIIncluded(uri)) {
101+
if (isIncluded(uri)) {
102102
val existing = files[uri]!!
103103
var newText = existing.content
104104

@@ -143,7 +143,7 @@ class SourceFiles(
143143
null
144144
}
145145

146-
private fun isSource(uri: URI): Boolean = exclusions.isURIIncluded(uri) && languageOf(uri) != null
146+
private fun isSource(uri: URI): Boolean = isIncluded(uri) && languageOf(uri) != null
147147

148148
private fun languageOf(uri: URI): Language? {
149149
val fileName = uri.filePath?.fileName?.toString() ?: return null
@@ -154,6 +154,7 @@ class SourceFiles(
154154
}
155155

156156
fun addWorkspaceRoot(root: Path) {
157+
LOG.info("Searching $root using exclusions: ${exclusions.excludedPatterns}")
157158
val addSources = findSourceFiles(root)
158159

159160
logAdded(addSources, root)
@@ -187,8 +188,9 @@ class SourceFiles(
187188
.toSet()
188189
}
189190

190-
private fun updateExclusions() {
191+
fun updateExclusions() {
191192
exclusions = SourceExclusions(workspaceRoots, scriptsConfig)
193+
LOG.info("Updated exclusions: ${exclusions.excludedPatterns}")
192194
}
193195

194196
fun isOpen(uri: URI): Boolean = (uri in open)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ class SourceExclusions(
1313
private val workspaceRoots: Collection<Path>,
1414
private val scriptsConfig: ScriptsConfiguration
1515
) {
16-
private val excludedPatterns = (listOf(
16+
val excludedPatterns = (listOf(
1717
".*", "bazel-*", "bin", "build", "node_modules", "target"
1818
) + when {
1919
!scriptsConfig.enabled -> listOf("*.kts")
2020
!scriptsConfig.buildScriptsEnabled -> listOf("*.gradle.kts")
2121
else -> emptyList()
2222
})
23+
24+
private val exclusionMatchers = excludedPatterns
2325
.map { FileSystems.getDefault().getPathMatcher("glob:$it") }
2426

2527
/** Finds all non-excluded files recursively. */
@@ -35,10 +37,10 @@ class SourceExclusions(
3537

3638
/** Tests whether the given path is not excluded. */
3739
fun isPathIncluded(file: Path): Boolean = workspaceRoots.any { file.startsWith(it) }
38-
&& excludedPatterns.none { pattern ->
40+
&& exclusionMatchers.none { matcher ->
3941
workspaceRoots
4042
.mapNotNull { if (file.startsWith(it)) it.relativize(file) else null }
4143
.flatMap { it } // Extract path segments
42-
.any(pattern::matches)
44+
.any(matcher::matches)
4345
}
4446
}

0 commit comments

Comments
 (0)