@@ -86,20 +86,10 @@ class CompanionServer(
8686 return
8787 }
8888
89- val lspServer = getLuauLspServer()
90- if (lspServer == null ) {
89+ if (! sendLspNotification { it.pluginFull(tree) }) {
9190 LOG .debug(" LSP server not available, ignoring /full request" )
92- exchange.sendResponse(200 , " OK" )
93- return
94- }
95-
96- try {
97- lspServer.pluginFull(tree)
98- exchange.sendResponse(200 , " OK" )
99- } catch (e: Exception ) {
100- LOG .warn(" Failed to send $/plugin/full notification" , e)
101- exchange.sendResponse(500 , " Failed to send notification to LSP" )
10291 }
92+ exchange.sendResponse(200 , " OK" )
10393 }
10494 }
10595
@@ -110,20 +100,10 @@ class CompanionServer(
110100 return
111101 }
112102
113- val lspServer = getLuauLspServer()
114- if (lspServer == null ) {
103+ if (! sendLspNotification { it.pluginClear() }) {
115104 LOG .debug(" LSP server not available, ignoring /clear request" )
116- exchange.sendResponse(200 , " OK" )
117- return
118- }
119-
120- try {
121- lspServer.pluginClear()
122- exchange.sendResponse(200 , " OK" )
123- } catch (e: Exception ) {
124- LOG .warn(" Failed to send $/plugin/clear notification" , e)
125- exchange.sendResponse(500 , " Failed to send notification to LSP" )
126105 }
106+ exchange.sendResponse(200 , " OK" )
127107 }
128108 }
129109
@@ -162,12 +142,36 @@ class CompanionServer(
162142 }
163143 }
164144
165- private fun getLuauLspServer (): LuauLanguageServer ? {
166- val server = LspServerManager .getInstance(project)
145+ /* *
146+ * Send a notification to the Luau LSP server.
147+ * Uses reflection to support both old (lsp4jServer, 2024.x) and new (sendNotification, 2025.x+) IntelliJ APIs.
148+ */
149+ private fun sendLspNotification (action : (LuauLanguageServer ) -> Unit ): Boolean {
150+ val lspServer = LspServerManager .getInstance(project)
167151 .getServersForProvider(LuauLspServerSupportProvider ::class .java)
168- .firstOrNull() ? : return null
169- @Suppress(" UNCHECKED_CAST" , " DEPRECATION" )
170- return server.lsp4jServer as ? LuauLanguageServer
152+ .firstOrNull() ? : return false
153+
154+ try {
155+ val sendNotification = lspServer.javaClass.getMethod(
156+ " sendNotification" ,
157+ kotlin.jvm.functions.Function1 ::class .java
158+ )
159+ val callback: (org.eclipse.lsp4j.services.LanguageServer ) -> Unit = { languageServer ->
160+ (languageServer as ? LuauLanguageServer )?.let (action)
161+ }
162+ sendNotification.invoke(lspServer, callback)
163+ } catch (_: NoSuchMethodException ) {
164+ // Fallback for older IntelliJ versions with lsp4jServer
165+ try {
166+ val getLsp4jServer = lspServer.javaClass.getMethod(" getLsp4jServer" )
167+ val server = getLsp4jServer.invoke(lspServer) as ? LuauLanguageServer ? : return false
168+ action(server)
169+ } catch (e: Throwable ) {
170+ LOG .warn(" Failed to access LSP server API" , e)
171+ return false
172+ }
173+ }
174+ return true
171175 }
172176
173177 private fun HttpExchange.sendResponse (code : Int , body : String ) {
@@ -180,11 +184,11 @@ class CompanionServer(
180184 private inline fun HttpExchange.use (block : () -> Unit ) {
181185 try {
182186 block()
183- } catch (e: Exception ) {
187+ } catch (e: Throwable ) {
184188 LOG .warn(" Unhandled error in companion server" , e)
185189 try {
186190 sendResponse(500 , " Internal Server Error" )
187- } catch (_: Exception ) {
191+ } catch (_: Throwable ) {
188192 // Response may have already been sent
189193 }
190194 }
0 commit comments