Skip to content

Commit 62027d8

Browse files
author
Alexnader Chapchuk
committed
Improve http server.
1 parent 9fd1e7c commit 62027d8

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

src/main/kotlin/com/github/aleksandrsl/intellijluau/lsp/CompanionPluginService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ class CompanionPluginService(private val project: Project, private val coroutine
136136
override fun dispose() {
137137
messageBusConnection?.disconnect()
138138
messageBusConnection = null
139-
operationQueue.trySend(Operation.Stop)
140-
operationQueue.close()
139+
server?.stop()
141140
server = null
141+
operationQueue.close()
142142
}
143143

144144
private sealed class Operation {

src/main/kotlin/com/github/aleksandrsl/intellijluau/lsp/CompanionServer.kt

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/test/kotlin/com/github/aleksandrsl/intellijluau/declarations/LuauDeclarationsParserTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class LuauDeclarationsParserTest : TestCase() {
99

1010
fun `test parser processes valid luau file`() {
1111
val declarations = javaClass.classLoader.getResource("declarations/globalTypes.RobloxScriptSecurity.d.luau")
12-
?.let { parser.parse(it.path) }
12+
?.let { parser.parse(it.toURI().path) }
1313

1414
assertNotNull(declarations)
1515
assertTrue(declarations!!.enums.isNotEmpty())
@@ -137,7 +137,7 @@ end""".trimIndent()
137137

138138
assertNotNull(testFilePath)
139139
val startTime = System.currentTimeMillis()
140-
val declarations = parser.parse(testFilePath!!.path)
140+
val declarations = parser.parse(testFilePath!!.toURI().path)
141141
val duration = System.currentTimeMillis() - startTime
142142

143143
assertTrue(duration < 500)

0 commit comments

Comments
 (0)