@@ -31,7 +31,7 @@ import org.jetbrains.kotlin.idea.base.plugin.artifacts.KotlinArtifacts
3131import org.jetbrains.kotlin.idea.compiler.configuration.KotlinPluginLayoutMode
3232import org.jetbrains.kotlin.idea.compiler.configuration.KotlinPluginLayoutModeProvider
3333import org.jetbrains.kotlin.idea.compiler.configuration.isRunningFromSources
34- import java.io.File
34+ import java.awt.Toolkit
3535import java.lang.invoke.MethodHandles
3636import java.net.URLDecoder
3737import java.nio.file.Path
@@ -61,6 +61,7 @@ private fun run(runConfig: KotlinLspServerRunConfig) {
6161 val mode = runConfig.mode
6262 initKotlinLspLogger(writeToStdOut = mode != KotlinLspServerMode .Stdio )
6363 initIdeaPaths(runConfig.systemPath)
64+ initHeadlessToolkit()
6465 setLspKotlinPluginModeIfRunningFromProductionLsp()
6566 val config = createConfiguration()
6667
@@ -106,7 +107,7 @@ private suspend fun handleRequests(connection: LspConnection, runConfig: KotlinL
106107 createCoroutineContext = { lspClient ->
107108 Client .contextElement(lspClient, runConfig)
108109 },
109- ) { lsp ->
110+ ) { _ ->
110111 exitSignal.await()
111112 }
112113 }
@@ -163,12 +164,28 @@ private fun isRunningFromProductionLsp(): Boolean {
163164 return ! isRunningFromSources()
164165}
165166
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+ }
166184
167185private fun getIJPathIfRunningFromSources (): String? {
168186 val serverClass = Class .forName(" com.jetbrains.ls.kotlinLsp.KotlinLspServerKt" )
169187 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" )
172189 if (! jar.endsWith(expectedOutDir)) return null
173190 return jar.removeSuffix(expectedOutDir)
174191}
0 commit comments