Skip to content

Commit 1cd8d67

Browse files
committed
Setup custom KotlinLanguageClient
1 parent db5ba9f commit 1cd8d67

File tree

8 files changed

+33
-21
lines changed

8 files changed

+33
-21
lines changed

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import org.javacs.kt.util.AsyncExecutor
77
import java.io.Closeable
88
import java.io.File
99
import java.nio.file.FileSystems
10+
import java.nio.file.Files
1011
import java.nio.file.Path
11-
import java.nio.file.Paths
1212

1313
/**
1414
* Manages the class path (compiled JARs, etc), the Java source path
@@ -19,9 +19,9 @@ class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
1919
private val javaSourcePath = mutableSetOf<Path>()
2020
private val buildScriptClassPath = mutableSetOf<Path>()
2121
val classPath = mutableSetOf<ClassPathEntry>()
22-
private var outputDirectory: File? = null
22+
val outputDirectory: File = Files.createTempDirectory("klsBuildOutput").toFile()
2323

24-
var compiler = Compiler(javaSourcePath, classPath.map { it.compiledJar }.toSet(), buildScriptClassPath)
24+
var compiler = Compiler(javaSourcePath, classPath.map { it.compiledJar }.toSet(), buildScriptClassPath, outputDirectory)
2525
private set
2626

2727
private val async = AsyncExecutor()
@@ -98,10 +98,6 @@ class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
9898
workspaceRoots.add(root)
9999
javaSourcePath.addAll(findJavaSourceFiles(root))
100100

101-
val outputDir = Paths.get(root.toString(), "kls").toFile()
102-
outputDirectory = outputDir
103-
compiler.outputDirectory = outputDir
104-
105101
return refresh()
106102
}
107103

@@ -110,8 +106,6 @@ class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
110106

111107
workspaceRoots.remove(root)
112108
javaSourcePath.removeAll(findJavaSourceFiles(root))
113-
outputDirectory = null
114-
compiler.outputDirectory = null
115109

116110
return refresh()
117111
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.javacs.kt
2+
3+
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification
4+
import org.eclipse.lsp4j.jsonrpc.services.JsonSegment
5+
import org.eclipse.lsp4j.services.LanguageClient
6+
7+
@JsonSegment("kotlin")
8+
interface KotlinLanguageClient : LanguageClient {
9+
10+
@JsonNotification
11+
fun buildOutputLocationSet(buildOutputLocation: String) {
12+
throw UnsupportedOperationException()
13+
}
14+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import java.nio.file.Paths
2121
import java.util.concurrent.CompletableFuture
2222
import java.util.concurrent.CompletableFuture.completedFuture
2323

24-
class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
24+
class KotlinLanguageServer : LanguageServer, Closeable {
2525
val config = Configuration()
2626
val classPath = CompilerClassPath(config.compiler)
2727

@@ -34,7 +34,7 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
3434
private val workspaces = KotlinWorkspaceService(sourceFiles, sourcePath, classPath, textDocuments, config)
3535
private val protocolExtensions = KotlinProtocolExtensionService(uriContentProvider)
3636

37-
private lateinit var client: LanguageClient
37+
private lateinit var client: KotlinLanguageClient
3838

3939
private val async = AsyncExecutor()
4040
private var progressFactory: Progress.Factory = Progress.Factory.None
@@ -51,7 +51,7 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
5151
LOG.info("Kotlin Language Server: Version ${VERSION ?: "?"}")
5252
}
5353

54-
override fun connect(client: LanguageClient) {
54+
fun connect(client: KotlinLanguageClient) {
5555
this.client = client
5656
connectLoggingBackend()
5757

@@ -132,6 +132,10 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
132132
InitializeResult(serverCapabilities, serverInfo)
133133
}
134134

135+
override fun initialized(params: InitializedParams?) {
136+
client.buildOutputLocationSet(classPath.outputDirectory.absolutePath)
137+
}
138+
135139
private fun connectLoggingBackend() {
136140
val backend: (LogMessage) -> Unit = {
137141
client.logMessage(MessageParams().apply {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.util.concurrent.Executors
66
import org.eclipse.lsp4j.launch.LSPLauncher
77
import org.eclipse.lsp4j.ConfigurationParams
88
import org.eclipse.lsp4j.ConfigurationItem
9+
import org.eclipse.lsp4j.jsonrpc.Launcher
910
import org.javacs.kt.util.ExitingInputStream
1011
import org.javacs.kt.util.tcpStartServer
1112
import org.javacs.kt.util.tcpConnectToClient
@@ -43,7 +44,7 @@ fun main(argv: Array<String>) {
4344

4445
val server = KotlinLanguageServer()
4546
val threads = Executors.newSingleThreadExecutor { Thread(it, "client") }
46-
val launcher = LSPLauncher.createServerLauncher(server, ExitingInputStream(inStream), outStream, threads, { it })
47+
val launcher = Launcher.createLauncher(server, KotlinLanguageClient::class.java, ExitingInputStream(inStream), outStream, threads) { it }
4748

4849
server.connect(launcher.remoteProxy)
4950
launcher.startListening()

server/src/main/kotlin/org/javacs/kt/compiler/Compiler.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ enum class CompilationKind {
440440
* Incrementally compiles files and expressions.
441441
* The basic strategy for compiling one file at-a-time is outlined in OneFilePerformance.
442442
*/
443-
class Compiler(javaSourcePath: Set<Path>, classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet(), var outputDirectory: File? = null) : Closeable {
443+
class Compiler(javaSourcePath: Set<Path>, classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet(), private val outputDirectory: File) : Closeable {
444444
private var closed = false
445445
private val localFileSystem: VirtualFileSystem
446446

@@ -551,15 +551,15 @@ class Compiler(javaSourcePath: Set<Path>, classPath: Set<Path>, buildScriptClass
551551
fun removeGeneratedCode(files: Collection<KtFile>) {
552552
files.forEach { file ->
553553
file.declarations.forEach { declaration ->
554-
outputDirectory?.resolve(
554+
outputDirectory.resolve(
555555
file.packageFqName.asString().replace(".", File.separator) + File.separator + declaration.name + ".class"
556-
)?.delete()
556+
).delete()
557557
}
558558
}
559559
}
560560

561561
fun generateCode(container: ComponentProvider, bindingContext: BindingContext, files: Collection<KtFile>) {
562-
outputDirectory?.let {
562+
outputDirectory.let {
563563
compileLock.withLock {
564564
val compileEnv = compileEnvironmentFor(CompilationKind.DEFAULT)
565565
val state = GenerationState.Builder(

server/src/test/kotlin/org/javacs/kt/CompiledFileTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CompiledFileTest {
1616
}
1717
}
1818

19-
fun compileFile(): CompiledFile = Compiler(setOf(), setOf()).use { compiler ->
19+
fun compileFile(): CompiledFile = Compiler(setOf(), setOf(), outputDirectory = Files.createTempDirectory("klsBuildOutput").toFile()).use { compiler ->
2020
val file = testResourcesRoot().resolve("compiledFile/CompiledFileExample.kt")
2121
val content = Files.readAllLines(file).joinToString("\n")
2222
val parse = compiler.createKtFile(content, file)

server/src/test/kotlin/org/javacs/kt/CompilerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.junit.BeforeClass
1414
import java.nio.file.Files
1515

1616
class CompilerTest {
17-
val compiler = Compiler(setOf(), setOf())
17+
val compiler = Compiler(setOf(), setOf(), outputDirectory = Files.createTempDirectory("klsBuildOutput").toFile())
1818
val myTestResources = testResourcesRoot().resolve("compiler")
1919
val file = myTestResources.resolve("FileToEdit.kt")
2020
val editedText = """

server/src/test/kotlin/org/javacs/kt/LanguageServerTestFixture.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package org.javacs.kt
22

33
import org.eclipse.lsp4j.*
4-
import org.eclipse.lsp4j.services.LanguageClient
54
import org.junit.Before
65
import org.junit.After
76
import java.nio.file.Path
87
import java.nio.file.Paths
98
import java.util.concurrent.CompletableFuture
109

11-
abstract class LanguageServerTestFixture(relativeWorkspaceRoot: String) : LanguageClient {
10+
abstract class LanguageServerTestFixture(relativeWorkspaceRoot: String) : KotlinLanguageClient {
1211
val workspaceRoot = absoluteWorkspaceRoot(relativeWorkspaceRoot)
1312
val languageServer = createLanguageServer()
1413
val diagnostics = mutableListOf<Diagnostic>()

0 commit comments

Comments
 (0)