Skip to content

Commit 5afc433

Browse files
authored
Merge pull request #536 from fwcd/disable-kts
Disable support for Kotlin scripts by default and add corresponding configuration options
2 parents 45cb21a + 9a439a8 commit 5afc433

File tree

11 files changed

+371
-298
lines changed

11 files changed

+371
-298
lines changed

detekt.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ config:
33
warningsAsErrors: false
44

55
comments:
6-
excludes: ["**/src/test/resources/**"]
6+
excludes: ["**/src/test/**"]
77

88
complexity:
9-
excludes: ["**/src/test/resources/**"]
9+
excludes: ["**/src/test/**"]
1010

1111
empty-blocks:
1212
excludes: ["**/src/test/**"]

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import java.nio.file.Path
1515
* Manages the class path (compiled JARs, etc), the Java source path
1616
* and the compiler. Note that Kotlin sources are stored in SourcePath.
1717
*/
18-
class CompilerClassPath(private val config: CompilerConfiguration, private val databaseService: DatabaseService) : Closeable {
18+
class CompilerClassPath(
19+
private val config: CompilerConfiguration,
20+
private val scriptsConfig: ScriptsConfiguration,
21+
private val databaseService: DatabaseService
22+
) : Closeable {
1923
val workspaceRoots = mutableSetOf<Path>()
2024

2125
private val javaSourcePath = mutableSetOf<Path>()
@@ -24,7 +28,13 @@ class CompilerClassPath(private val config: CompilerConfiguration, private val d
2428
val outputDirectory: File = Files.createTempDirectory("klsBuildOutput").toFile()
2529
val javaHome: String? = System.getProperty("java.home", null)
2630

27-
var compiler = Compiler(javaSourcePath, classPath.map { it.compiledJar }.toSet(), buildScriptClassPath, outputDirectory)
31+
var compiler = Compiler(
32+
javaSourcePath,
33+
classPath.map { it.compiledJar }.toSet(),
34+
buildScriptClassPath,
35+
scriptsConfig,
36+
outputDirectory
37+
)
2838
private set
2939

3040
private val async = AsyncExecutor()
@@ -72,7 +82,13 @@ class CompilerClassPath(private val config: CompilerConfiguration, private val d
7282
if (refreshCompiler) {
7383
LOG.info("Reinstantiating compiler")
7484
compiler.close()
75-
compiler = Compiler(javaSourcePath, classPath.map { it.compiledJar }.toSet(), buildScriptClassPath, outputDirectory)
85+
compiler = Compiler(
86+
javaSourcePath,
87+
classPath.map { it.compiledJar }.toSet(),
88+
buildScriptClassPath,
89+
scriptsConfig,
90+
outputDirectory
91+
)
7692
updateCompilerConfiguration()
7793
}
7894

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ 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+
3340
public data class JVMConfiguration(
3441
/** Which JVM target the Kotlin compiler uses. See Compiler.jvmTargetFrom for possible values. */
3542
var target: String = "default"
@@ -91,7 +98,8 @@ public data class Configuration(
9198
val compiler: CompilerConfiguration = CompilerConfiguration(),
9299
val completion: CompletionConfiguration = CompletionConfiguration(),
93100
val diagnostics: DiagnosticsConfiguration = DiagnosticsConfiguration(),
94-
var indexing: IndexingConfiguration = IndexingConfiguration(),
101+
val scripts: ScriptsConfiguration = ScriptsConfiguration(),
102+
val indexing: IndexingConfiguration = IndexingConfiguration(),
95103
val externalSources: ExternalSourcesConfiguration = ExternalSourcesConfiguration(),
96104
val hints: InlayHintsConfiguration = InlayHintsConfiguration()
97105
)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ import java.nio.file.Paths
2222
import java.util.concurrent.CompletableFuture
2323
import java.util.concurrent.CompletableFuture.completedFuture
2424

25-
class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
26-
val config = Configuration()
25+
class KotlinLanguageServer(
26+
val config: Configuration = Configuration()
27+
) : LanguageServer, LanguageClientAware, Closeable {
2728
val databaseService = DatabaseService()
28-
val classPath = CompilerClassPath(config.compiler, databaseService)
29+
val classPath = CompilerClassPath(config.compiler, config.scripts, databaseService)
2930

3031
private val tempDirectory = TemporaryDirectory()
3132
private val uriContentProvider = URIContentProvider(ClassContentProvider(config.externalSources, classPath, tempDirectory, CompositeSourceArchiveProvider(JdkSourceArchiveProvider(classPath), ClassPathSourceArchiveProvider(classPath))))
3233
val sourcePath = SourcePath(classPath, uriContentProvider, config.indexing, databaseService)
33-
val sourceFiles = SourceFiles(sourcePath, uriContentProvider)
34+
val sourceFiles = SourceFiles(sourcePath, uriContentProvider, config.scripts)
3435

3536
private val textDocuments = KotlinTextDocumentService(sourceFiles, sourcePath, config, tempDirectory, uriContentProvider, classPath)
3637
private val workspaces = KotlinWorkspaceService(sourceFiles, sourcePath, classPath, textDocuments, config)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ class KotlinWorkspaceService(
135135
}
136136
}
137137

138+
// Update scripts options
139+
get("scripts")?.asJsonObject?.apply {
140+
val scripts = config.scripts
141+
get("enabled")?.asBoolean?.let { scripts.enabled = it }
142+
get("buildScriptsEnabled")?.asBoolean?.let { scripts.buildScriptsEnabled = it }
143+
}
144+
138145
// Update code-completion options
139146
get("completion")?.asJsonObject?.apply {
140147
val completion = config.completion

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ private class NotifySourcePath(private val sp: SourcePath) {
6565
*/
6666
class SourceFiles(
6767
private val sp: SourcePath,
68-
private val contentProvider: URIContentProvider
68+
private val contentProvider: URIContentProvider,
69+
private val scriptsConfig: ScriptsConfiguration
6970
) {
7071
private val workspaceRoots = mutableSetOf<Path>()
7172
private var exclusions = SourceExclusions(workspaceRoots)
@@ -175,6 +176,19 @@ class SourceFiles(
175176
updateExclusions()
176177
}
177178

179+
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)
183+
.walkIncluded()
184+
.filter {
185+
sourceMatcher.matches(it.fileName)
186+
&& (scriptsConfig.buildScriptsEnabled || !it.endsWith(".gradle.kts"))
187+
}
188+
.map(Path::toUri)
189+
.toSet()
190+
}
191+
178192
private fun updateExclusions() {
179193
exclusions = SourceExclusions(workspaceRoots)
180194
}
@@ -222,15 +236,6 @@ private fun patch(sourceText: String, change: TextDocumentContentChangeEvent): S
222236
}
223237
}
224238

225-
private fun findSourceFiles(root: Path): Set<URI> {
226-
val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:*.{kt,kts}")
227-
return SourceExclusions(root)
228-
.walkIncluded()
229-
.filter { sourceMatcher.matches(it.fileName) }
230-
.map(Path::toUri)
231-
.toSet()
232-
}
233-
234239
private fun logAdded(sources: Collection<URI>, rootPath: Path?) {
235240
LOG.info("Adding {} under {} to source path", describeURIs(sources), rootPath)
236241
}

0 commit comments

Comments
 (0)