From 7896f64c8b3c3f923d8b14a050e0babd6accc199 Mon Sep 17 00:00:00 2001 From: Steven Chun Date: Thu, 9 May 2024 01:31:42 -0700 Subject: [PATCH] Read exclusions from init_options --- .../kotlin/org/javacs/kt/CompilerClassPath.kt | 2 +- .../main/kotlin/org/javacs/kt/Configuration.kt | 8 +++++--- .../org/javacs/kt/KotlinLanguageServer.kt | 12 +++++++++--- .../main/kotlin/org/javacs/kt/SourceFiles.kt | 18 ++++++++++++++---- .../kotlin/org/javacs/kt/SourceExclusions.kt | 5 +++-- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt b/server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt index d3ef00e2a..472253468 100644 --- a/server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt +++ b/server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt @@ -159,7 +159,7 @@ class CompilerClassPath( private fun findJavaSourceFiles(root: Path): Set { val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:*.java") - return SourceExclusions(listOf(root), scriptsConfig) + return SourceExclusions(listOf(root), scriptsConfig, mutableListOf()) .walkIncluded() .filter { sourceMatcher.matches(it.fileName) } .toSet() diff --git a/server/src/main/kotlin/org/javacs/kt/Configuration.kt b/server/src/main/kotlin/org/javacs/kt/Configuration.kt index 13774c38e..1251a8597 100644 --- a/server/src/main/kotlin/org/javacs/kt/Configuration.kt +++ b/server/src/main/kotlin/org/javacs/kt/Configuration.kt @@ -70,12 +70,12 @@ data class FormattingConfiguration( var ktfmt: KtfmtConfiguration = KtfmtConfiguration() ) -fun getStoragePath(params: InitializeParams): Path? { +fun getInitializationOptions(params: InitializeParams): InitializationOptions? { params.initializationOptions?.let { initializationOptions -> val gson = GsonBuilder().registerTypeHierarchyAdapter(Path::class.java, GsonPathConverter()).create() val options = gson.fromJson(initializationOptions as JsonElement, InitializationOptions::class.java) - return options?.storagePath + return options } return null @@ -83,7 +83,9 @@ fun getStoragePath(params: InitializeParams): Path? { data class InitializationOptions( // A path to a directory used by the language server to store data. Used for caching purposes. - val storagePath: Path? + val storagePath: Path?, + // Additional paths to exclude from source files. + val additionalSourceExclusions: List? ) class GsonPathConverter : JsonDeserializer { diff --git a/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt b/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt index 29998dc3a..72e49f156 100644 --- a/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt +++ b/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt @@ -31,7 +31,7 @@ class KotlinLanguageServer( private val tempDirectory = TemporaryDirectory() private val uriContentProvider = URIContentProvider(ClassContentProvider(config.externalSources, classPath, tempDirectory, CompositeSourceArchiveProvider(JdkSourceArchiveProvider(classPath), ClassPathSourceArchiveProvider(classPath)))) val sourcePath = SourcePath(classPath, uriContentProvider, config.indexing, databaseService) - val sourceFiles = SourceFiles(sourcePath, uriContentProvider, config.scripts) + val sourceFiles = SourceFiles(sourcePath, uriContentProvider, config.scripts, mutableListOf()) private val textDocuments = KotlinTextDocumentService(sourceFiles, sourcePath, config, tempDirectory, uriContentProvider, classPath) private val workspaces = KotlinWorkspaceService(sourceFiles, sourcePath, classPath, textDocuments, config) @@ -72,6 +72,14 @@ class KotlinLanguageServer( fun getProtocolExtensionService(): KotlinProtocolExtensions = protocolExtensions override fun initialize(params: InitializeParams): CompletableFuture = async.compute { + // Parse initialization options + val options = getInitializationOptions(params) + databaseService.setup(options?.storagePath) + + if(options?.additionalSourceExclusions != null) { + sourceFiles.updateAdditionalExclusions(options.additionalSourceExclusions.toMutableList()) + } + val serverCapabilities = ServerCapabilities() serverCapabilities.setTextDocumentSync(TextDocumentSyncKind.Incremental) serverCapabilities.workspace = WorkspaceServerCapabilities() @@ -94,8 +102,6 @@ class KotlinLanguageServer( serverCapabilities.executeCommandProvider = ExecuteCommandOptions(ALL_COMMANDS) serverCapabilities.documentHighlightProvider = Either.forLeft(true) - val storagePath = getStoragePath(params) - databaseService.setup(storagePath) val clientCapabilities = params.capabilities config.completion.snippets.enabled = clientCapabilities?.textDocument?.completion?.completionItem?.snippetSupport ?: false diff --git a/server/src/main/kotlin/org/javacs/kt/SourceFiles.kt b/server/src/main/kotlin/org/javacs/kt/SourceFiles.kt index d7bb84968..1bbc19f67 100644 --- a/server/src/main/kotlin/org/javacs/kt/SourceFiles.kt +++ b/server/src/main/kotlin/org/javacs/kt/SourceFiles.kt @@ -66,10 +66,15 @@ private class NotifySourcePath(private val sp: SourcePath) { class SourceFiles( private val sp: SourcePath, private val contentProvider: URIContentProvider, - private val scriptsConfig: ScriptsConfiguration + private val scriptsConfig: ScriptsConfiguration, + private val additionalSourceExclusions: MutableList, ) { private val workspaceRoots = mutableSetOf() - private var exclusions = SourceExclusions(workspaceRoots, scriptsConfig) + private var exclusions = SourceExclusions( + workspaceRoots, + scriptsConfig, + additionalSourceExclusions + ) private val files = NotifySourcePath(sp) private val open = mutableSetOf() @@ -181,7 +186,7 @@ class SourceFiles( private fun findSourceFiles(root: Path): Set { val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:*.{kt,kts}") - return SourceExclusions(listOf(root), scriptsConfig) + return SourceExclusions(listOf(root), scriptsConfig, additionalSourceExclusions) .walkIncluded() .filter { sourceMatcher.matches(it.fileName) } .map(Path::toUri) @@ -189,10 +194,15 @@ class SourceFiles( } fun updateExclusions() { - exclusions = SourceExclusions(workspaceRoots, scriptsConfig) + exclusions = SourceExclusions(workspaceRoots, scriptsConfig, additionalSourceExclusions) LOG.info("Updated exclusions: ${exclusions.excludedPatterns}") } + fun updateAdditionalExclusions(exclusions: MutableList) { + additionalSourceExclusions.addAll(exclusions) + updateExclusions() + } + fun isOpen(uri: URI): Boolean = (uri in open) fun isIncluded(uri: URI): Boolean = exclusions.isURIIncluded(uri) diff --git a/shared/src/main/kotlin/org/javacs/kt/SourceExclusions.kt b/shared/src/main/kotlin/org/javacs/kt/SourceExclusions.kt index de4a512c2..0ff185c79 100644 --- a/shared/src/main/kotlin/org/javacs/kt/SourceExclusions.kt +++ b/shared/src/main/kotlin/org/javacs/kt/SourceExclusions.kt @@ -11,13 +11,14 @@ import java.nio.file.Paths // hardcoding them class SourceExclusions( private val workspaceRoots: Collection, - private val scriptsConfig: ScriptsConfiguration + private val scriptsConfig: ScriptsConfiguration, + private val additionalSourceExclusions: List ) { 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 { + ) + additionalSourceExclusions + when { !scriptsConfig.enabled -> listOf("*.kts") !scriptsConfig.buildScriptsEnabled -> listOf("*.gradle.kts") else -> emptyList()