@@ -39,31 +39,50 @@ fun main(args: Array<String>) {
39
39
40
40
private class RunKotlinLspCommand : CliktCommand (name = " kotlin-lsp" ) {
41
41
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} `" )
43
44
val client: Boolean by option().flag()
44
45
.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} `" )
45
46
.validate { if (it && stdio) fail(" Can't use stdio mode with client mode" ) }
46
47
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
+
47
57
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 -> {
60
77
val stdout = System .out
61
78
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 -> {
64
83
logSystemInfo()
65
- tcpConnection(socket, client ) { input, output ->
66
- handleRequests(input, output, config, client )
84
+ tcpConnection(mode ) { input, output ->
85
+ handleRequests(input, output, config, mode )
67
86
}
68
87
}
69
88
}
@@ -72,11 +91,11 @@ private class RunKotlinLspCommand : CliktCommand(name = "kotlin-lsp") {
72
91
}
73
92
74
93
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 ) {
76
95
withBaseProtocolFraming(input, output) { incoming, outgoing ->
77
96
withServer {
78
97
val exitSignal = CompletableDeferred <Unit >()
79
- val handler = createLspHandlers(config, exitSignal, client )
98
+ val handler = createLspHandlers(config, exitSignal, clientMode = mode is KotlinLspServerMode . Socket . Client )
80
99
81
100
withLsp(
82
101
incoming,
@@ -163,12 +182,17 @@ private fun preloadKotlinStdlibWhenRunningFromSources() {
163
182
}
164
183
}
165
184
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
+ }
171
194
}
195
+ }
172
196
173
197
/* *
174
198
* VSC opens a **server** socket for LSP to connect to it.
0 commit comments