Skip to content

Feature/generated sources #629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CompilerClassPath(
compiler.updateConfiguration(config)
}

/** Updates and possibly reinstantiates the compiler using new paths. */
/** Updates and possibly re-instantiates the compiler using new paths. */
private fun refresh(
updateClassPath: Boolean = true,
updateBuildScriptClassPath: Boolean = true,
Expand Down
127 changes: 95 additions & 32 deletions shared/src/main/kotlin/org/javacs/kt/SourceExclusions.kt
Original file line number Diff line number Diff line change
@@ -1,48 +1,111 @@
package org.javacs.kt

import org.javacs.kt.util.filePath
import java.io.File
import java.net.URI
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import org.javacs.kt.util.filePath
import java.io.IOException

// TODO: Read exclusions from gitignore/settings.json/... instead of
// hardcoding them
class SourceExclusions(
private val workspaceRoots: Collection<Path>,
private val scriptsConfig: ScriptsConfiguration
scriptsConfig: ScriptsConfiguration,
) {
val excludedPatterns = (listOf(
".git", ".hg", ".svn", // Version control systems
".idea", ".idea_modules", ".vs", ".vscode", ".code-workspace", ".settings", // IDEs
"bazel-*", "bin", "build", "node_modules", "target", // Build systems
) + 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. */
fun walkIncluded(): Sequence<Path> = workspaceRoots.asSequence().flatMap { root ->
root.toFile()
.walk()
.onEnter { isPathIncluded(it.toPath()) }
.map { it.toPath() }
private val defaultPatterns =
listOf(
".git",
".hg",
".svn", // Version control systems
".idea",
".idea_modules",
".vs",
".vscode",
".code-workspace",
".settings", // IDEs
"bazel-*",
"bin",
"build",
"node_modules",
)

private val scriptPatterns =
when {
!scriptsConfig.enabled -> listOf("*.kts")
!scriptsConfig.buildScriptsEnabled -> listOf("*.gradle.kts")
else -> emptyList()
}

private val gitignorePatterns: List<String> = readGitignorePatterns()

val excludedPatterns = defaultPatterns + scriptPatterns + gitignorePatterns

private val exclusionMatchers =
excludedPatterns
.filter { !it.startsWith("!") } // Skip negated patterns for now
.map { FileSystems.getDefault().getPathMatcher("glob:$it") }

private fun readGitignorePatterns(): List<String> {
return workspaceRoots
.flatMap { root ->
val gitignore = root.resolve(".gitignore")
if (Files.exists(gitignore)) {
try {
Files.readAllLines(gitignore)
.asSequence()
.map { it.trim() }
.filter { it.isNotEmpty() && !it.startsWith("#") }
.map { it.removeSuffix("/") }
.toList()
.also { patterns ->
LOG.debug("Read {} patterns from {}", patterns.size, gitignore)
}
} catch (e: IOException) {
LOG.warn("Could not read .gitignore at $gitignore: ${e.message}")
emptyList()
} catch (e: SecurityException) {
LOG.warn("Security error reading .gitignore at $gitignore: ${e.message}")
emptyList()
}
} else {
emptyList()
}
}
.distinct() // Remove duplicates across workspace roots
}

/** Tests whether the given URI is not excluded. */
fun walkIncluded(): Sequence<Path> =
workspaceRoots.asSequence().flatMap { root ->
root.toFile().walk().onEnter { isPathIncluded(it.toPath()) }.map { it.toPath() }
}

fun isURIIncluded(uri: URI) = uri.filePath?.let(this::isPathIncluded) ?: false

/** Tests whether the given path is not excluded. */
fun isPathIncluded(file: Path): Boolean = workspaceRoots.any { file.startsWith(it) }
&& exclusionMatchers.none { matcher ->
fun isPathIncluded(file: Path): Boolean {
if (!workspaceRoots.any { file.startsWith(it) }) {
return false
}

val relativePaths =
workspaceRoots
.mapNotNull { if (file.startsWith(it)) it.relativize(file) else null }
.flatMap { it } // Extract path segments
.any(matcher::matches)
}
.flatten()

val isIncluded =
when {
// Check if we're in a target directory
relativePaths.contains(Path.of("target")) -> {
val pathList = relativePaths.toList()
val targetIndex = pathList.indexOf(Path.of("target"))
// Allow only target directory itself or if next directory is generated-sources
pathList.size <= targetIndex + 1 ||
pathList[targetIndex + 1] == Path.of("generated-sources")
}
// Check exclusion patterns
exclusionMatchers.any { matcher -> relativePaths.any(matcher::matches) } -> false
// Include paths outside target directory by default
else -> true
}

return isIncluded
}
}
Loading