Skip to content

Commit

Permalink
Merge pull request #545 from fwcd/improve-exclusions-v2
Browse files Browse the repository at this point in the history
Update exclusions upon config changes and log more verbosely
  • Loading branch information
fwcd authored Jan 15, 2024
2 parents 70dfeb4 + 84dc685 commit 8d0fc49
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ class KotlinTextDocumentService(

private fun recover(uriString: String, position: Position, recompile: Recompile): Pair<CompiledFile, Int>? {
val uri = parseURI(uriString)
if (!sf.isIncluded(uri)) return null
if (!sf.isIncluded(uri)) {
LOG.warn("URI is excluded, therefore cannot be recovered: $uri")
return null
}
val content = sp.content(uri)
val offset = offset(content, position.line, position.character)
val shouldRecompile = when (recompile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class KotlinWorkspaceService(
val scripts = config.scripts
get("enabled")?.asBoolean?.let { scripts.enabled = it }
get("buildScriptsEnabled")?.asBoolean?.let { scripts.buildScriptsEnabled = it }
sf.updateExclusions()
}

// Update code-completion options
Expand Down
10 changes: 6 additions & 4 deletions server/src/main/kotlin/org/javacs/kt/SourceFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class SourceFiles(
private val open = mutableSetOf<URI>()

fun open(uri: URI, content: String, version: Int) {
if (exclusions.isURIIncluded(uri)) {
if (isIncluded(uri)) {
files[uri] = SourceVersion(content, version, languageOf(uri), isTemporary = false)
open.add(uri)
}
Expand All @@ -98,7 +98,7 @@ class SourceFiles(
}

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

Expand Down Expand Up @@ -143,7 +143,7 @@ class SourceFiles(
null
}

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

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

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

logAdded(addSources, root)
Expand Down Expand Up @@ -187,8 +188,9 @@ class SourceFiles(
.toSet()
}

private fun updateExclusions() {
fun updateExclusions() {
exclusions = SourceExclusions(workspaceRoots, scriptsConfig)
LOG.info("Updated exclusions: ${exclusions.excludedPatterns}")
}

fun isOpen(uri: URI): Boolean = (uri in open)
Expand Down
8 changes: 5 additions & 3 deletions shared/src/main/kotlin/org/javacs/kt/SourceExclusions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class SourceExclusions(
private val workspaceRoots: Collection<Path>,
private val scriptsConfig: ScriptsConfiguration
) {
private val excludedPatterns = (listOf(
val excludedPatterns = (listOf(
".*", "bazel-*", "bin", "build", "node_modules", "target"
) + when {
!scriptsConfig.enabled -> listOf("*.kts")
!scriptsConfig.buildScriptsEnabled -> listOf("*.gradle.kts")
else -> emptyList()
})

private val exclusionMatchers = excludedPatterns
.map { FileSystems.getDefault().getPathMatcher("glob:$it") }

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

/** Tests whether the given path is not excluded. */
fun isPathIncluded(file: Path): Boolean = workspaceRoots.any { file.startsWith(it) }
&& excludedPatterns.none { pattern ->
&& exclusionMatchers.none { matcher ->
workspaceRoots
.mapNotNull { if (file.startsWith(it)) it.relativize(file) else null }
.flatMap { it } // Extract path segments
.any(pattern::matches)
.any(matcher::matches)
}
}

0 comments on commit 8d0fc49

Please sign in to comment.