@@ -31,7 +31,7 @@ import org.jetbrains.kotlin.idea.base.plugin.artifacts.KotlinArtifacts
31
31
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinPluginLayoutMode
32
32
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinPluginLayoutModeProvider
33
33
import org.jetbrains.kotlin.idea.compiler.configuration.isRunningFromSources
34
- import java.io.File
34
+ import java.awt.Toolkit
35
35
import java.lang.invoke.MethodHandles
36
36
import java.net.URLDecoder
37
37
import java.nio.file.Path
@@ -61,6 +61,7 @@ private fun run(runConfig: KotlinLspServerRunConfig) {
61
61
val mode = runConfig.mode
62
62
initKotlinLspLogger(writeToStdOut = mode != KotlinLspServerMode .Stdio )
63
63
initIdeaPaths(runConfig.systemPath)
64
+ initHeadlessToolkit()
64
65
setLspKotlinPluginModeIfRunningFromProductionLsp()
65
66
val config = createConfiguration()
66
67
@@ -106,7 +107,7 @@ private suspend fun handleRequests(connection: LspConnection, runConfig: KotlinL
106
107
createCoroutineContext = { lspClient ->
107
108
Client .contextElement(lspClient, runConfig)
108
109
},
109
- ) { lsp ->
110
+ ) { _ ->
110
111
exitSignal.await()
111
112
}
112
113
}
@@ -163,12 +164,28 @@ private fun isRunningFromProductionLsp(): Boolean {
163
164
return ! isRunningFromSources()
164
165
}
165
166
167
+ private fun initHeadlessToolkit () {
168
+ // System.setProperty("apple.awt.UIElement", "true")
169
+ val field = ClassLoader .getPlatformClassLoader().loadClass(" java.awt.GraphicsEnvironment" )
170
+ .declaredFields.find { it.name == " headless" }!!
171
+ field.setAccessible(true )
172
+ val prev = field.get(null ) as Boolean?
173
+ field.set(null , true )
174
+ val current: Toolkit ?
175
+ try {
176
+ current = Toolkit .getDefaultToolkit()
177
+ if (! current::class .java.name.endsWith(" HeadlessToolkit" )) {
178
+ throw AssertionError (" Unexpected awt.Toolkit: ${current::class .java.name} " )
179
+ }
180
+ } finally {
181
+ field.set(null , prev)
182
+ }
183
+ }
166
184
167
185
private fun getIJPathIfRunningFromSources (): String? {
168
186
val serverClass = Class .forName(" com.jetbrains.ls.kotlinLsp.KotlinLspServerKt" )
169
187
val jar = PathManager .getJarForClass(serverClass)?.absolutePathString() ? : return null
170
- val SEP = File .separator
171
- val expectedOutDir = " ${SEP } out${SEP } classes${SEP } production${SEP } language-server.kotlin-lsp"
188
+ val expectedOutDir = FileUtilRt .toSystemDependentName(" /out/classes/production/language-server.kotlin-lsp" )
172
189
if (! jar.endsWith(expectedOutDir)) return null
173
190
return jar.removeSuffix(expectedOutDir)
174
191
}
0 commit comments