Skip to content

Commit 61759e8

Browse files
darthorimarintellij-monorepo-bot
authored andcommitted
[lsp] extract LSP config to class, so it's easier to pass around
LSP-166 GitOrigin-RevId: 1928883a2ac05b93d4971aa41a93fdf36cb21d9b
1 parent 844b8e9 commit 61759e8

File tree

2 files changed

+64
-24
lines changed

2 files changed

+64
-24
lines changed

kotlin-lsp/src/com/jetbrains/ls/kotlinLsp/KotlinLspServer.kt

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,50 @@ fun main(args: Array<String>) {
3939

4040
private class RunKotlinLspCommand : CliktCommand(name = "kotlin-lsp") {
4141
val socket: Int by option().int().default(9999).help("A port which will be used for a Kotlin LSP connection. Default is 9999")
42-
val stdio: Boolean by option().flag().help("Whether the Kotlin LSP server is used in stdio mode. If not set, server mode will be used with a port specified by `${::socket.name}`")
42+
val stdio: Boolean by option().flag()
43+
.help("Whether the Kotlin LSP server is used in stdio mode. If not set, server mode will be used with a port specified by `${::socket.name}`")
4344
val client: Boolean by option().flag()
4445
.help("Whether the Kotlin LSP server is used in client mode. If not set, server mode will be used with a port specified by `${::socket.name}`")
4546
.validate { if (it && stdio) fail("Can't use stdio mode with client mode") }
4647

48+
private fun createRunConfig(): KotlinLspServerRunConfig {
49+
val mode = when {
50+
stdio -> KotlinLspServerMode.Stdio
51+
client -> KotlinLspServerMode.Socket.Client(socket)
52+
else -> KotlinLspServerMode.Socket.Server(socket)
53+
}
54+
return KotlinLspServerRunConfig(mode)
55+
}
56+
4757
override fun run() {
48-
initKotlinLspLogger(writeToStdOut = !stdio)
49-
initIdeaPaths()
50-
setLspKotlinPluginModeIfRunningFromProductionLsp()
51-
52-
val config = createConfiguration()
53-
54-
val starter = createServerStarterAnalyzerImpl(config.plugins, isUnitTestMode = false)
55-
@Suppress("RAW_RUN_BLOCKING")
56-
runBlocking(Dispatchers.Default) {
57-
starter.start {
58-
preloadKotlinStdlibWhenRunningFromSources()
59-
if (stdio) {
58+
val runConfig = createRunConfig()
59+
run(runConfig)
60+
}
61+
}
62+
63+
private fun run(runConfig: KotlinLspServerRunConfig) {
64+
val mode = runConfig.mode
65+
initKotlinLspLogger(writeToStdOut = mode != KotlinLspServerMode.Stdio)
66+
initIdeaPaths()
67+
setLspKotlinPluginModeIfRunningFromProductionLsp()
68+
val config = createConfiguration()
69+
70+
val starter = createServerStarterAnalyzerImpl(config.plugins, isUnitTestMode = false)
71+
@Suppress("RAW_RUN_BLOCKING")
72+
runBlocking(Dispatchers.Default) {
73+
starter.start {
74+
preloadKotlinStdlibWhenRunningFromSources()
75+
when (mode) {
76+
KotlinLspServerMode.Stdio -> {
6077
val stdout = System.out
6178
System.setOut(System.err)
62-
handleRequests(System.`in`, stdout, config, true)
63-
} else {
79+
handleRequests(System.`in`, stdout, config, mode)
80+
}
81+
82+
is KotlinLspServerMode.Socket -> {
6483
logSystemInfo()
65-
tcpConnection(socket, client) { input, output ->
66-
handleRequests(input, output, config, client)
84+
tcpConnection(mode) { input, output ->
85+
handleRequests(input, output, config, mode)
6786
}
6887
}
6988
}
@@ -72,11 +91,11 @@ private class RunKotlinLspCommand : CliktCommand(name = "kotlin-lsp") {
7291
}
7392

7493
context(LSServerContext)
75-
private suspend fun handleRequests(input: InputStream, output: OutputStream, config: LSConfiguration, client: Boolean) {
94+
private suspend fun handleRequests(input: InputStream, output: OutputStream, config: LSConfiguration, mode: KotlinLspServerMode) {
7695
withBaseProtocolFraming(input, output) { incoming, outgoing ->
7796
withServer {
7897
val exitSignal = CompletableDeferred<Unit>()
79-
val handler = createLspHandlers(config, exitSignal, client)
98+
val handler = createLspHandlers(config, exitSignal, clientMode = mode is KotlinLspServerMode.Socket.Client)
8099

81100
withLsp(
82101
incoming,
@@ -163,12 +182,17 @@ private fun preloadKotlinStdlibWhenRunningFromSources() {
163182
}
164183
}
165184

166-
private suspend fun tcpConnection(port: Int, isClientMode: Boolean, body: suspend CoroutineScope.(InputStream, OutputStream) -> Unit) =
167-
if (isClientMode) {
168-
tcpClient(port, body)
169-
} else {
170-
tcpServer(port, body)
185+
private suspend fun tcpConnection(mode: KotlinLspServerMode.Socket, body: suspend CoroutineScope.(InputStream, OutputStream) -> Unit) {
186+
when (mode) {
187+
is KotlinLspServerMode.Socket.Client -> {
188+
tcpClient(mode.port, body)
189+
}
190+
191+
is KotlinLspServerMode.Socket.Server -> {
192+
tcpServer(mode.port, body)
193+
}
171194
}
195+
}
172196

173197
/**
174198
* VSC opens a **server** socket for LSP to connect to it.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.jetbrains.ls.kotlinLsp
2+
3+
class KotlinLspServerRunConfig(
4+
val mode: KotlinLspServerMode
5+
)
6+
7+
sealed interface KotlinLspServerMode {
8+
object Stdio : KotlinLspServerMode
9+
10+
sealed interface Socket : KotlinLspServerMode {
11+
val port: Int
12+
13+
class Server(override val port: Int) : Socket
14+
class Client(override val port: Int) : Socket
15+
}
16+
}

0 commit comments

Comments
 (0)