Skip to content

Commit ca0f32f

Browse files
committed
(#248) LSP: more robust service calls, log more errors
1 parent da0afde commit ca0f32f

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

src/main/kotlin/com/intellij/plugin/powershell/lang/lsp/ide/LSPRequestManager.kt

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
package com.intellij.plugin.powershell.lang.lsp.ide
55

6+
import com.intellij.openapi.diagnostic.logger
67
import com.intellij.openapi.diagnostic.thisLogger
78
import com.intellij.openapi.progress.ProcessCanceledException
89
import com.intellij.plugin.powershell.lang.lsp.languagehost.LanguageServerEndpoint
@@ -22,42 +23,36 @@ class LSPRequestManager(
2223
private fun checkStatus(): Boolean = serverEndpoint.isRunning
2324

2425
suspend fun didClose(params: DidCloseTextDocumentParams) {
25-
if (checkStatus()) {
26-
handleServerError {
27-
if (documentSyncOptions == null || documentSyncOptions.openClose) queue.didClose(params)
28-
}
26+
serviceCall {
27+
if (documentSyncOptions == null || documentSyncOptions.openClose) queue.didClose(params)
2928
}
3029
}
3130

3231
suspend fun didOpen(params: DidOpenTextDocumentParams) {
33-
if (checkStatus()) {
34-
handleServerError {
35-
if (documentSyncOptions == null || documentSyncOptions.openClose) queue.didOpen(params)
36-
}
32+
serviceCall {
33+
if (documentSyncOptions == null || documentSyncOptions.openClose) queue.didOpen(params)
3734
}
3835
}
3936

4037
suspend fun didChange(params: DidChangeTextDocumentParams) {
41-
if (checkStatus()) {
42-
handleServerError {
43-
if (documentSyncOptions == null || documentSyncOptions.change != null) queue.didChange(params)
44-
}
38+
serviceCall {
39+
if (documentSyncOptions == null || documentSyncOptions.change != null) queue.didChange(params)
4540
}
4641
}
4742

48-
suspend fun completion(params: CompletionParams): Either<List<CompletionItem>, CompletionList>? {
49-
if (checkStatus()) {
50-
return handleServerError {
51-
if (capabilities.completionProvider != null)
52-
queue.completion(params)
53-
else null
54-
}
43+
suspend fun completion(params: CompletionParams): Either<List<CompletionItem>, CompletionList>? =
44+
serviceCall {
45+
if (capabilities.completionProvider != null)
46+
queue.completion(params)
47+
else null
5548
}
5649

57-
return null
58-
}
50+
private suspend fun <T> serviceCall(action: suspend () -> T): T? {
51+
if (!checkStatus()) {
52+
logger.error("ServerEndpoint does not report isRunning == true")
53+
return null
54+
}
5955

60-
private suspend fun <T> handleServerError(action: suspend () -> T): T? {
6156
try {
6257
return action()
6358
} catch (e: Throwable) {
@@ -71,3 +66,5 @@ class LSPRequestManager(
7166
}
7267
}
7368
}
69+
70+
private val logger = logger<LSPRequestManager>()

src/main/kotlin/com/intellij/plugin/powershell/lang/lsp/languagehost/TextDocumentServiceQueue.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ private val logger = logger<TextDocumentServiceQueue>()
1515

1616
class TextDocumentServiceQueue(private val textDocumentService: () -> TextDocumentService?) {
1717

18-
private val service: TextDocumentService?
19-
get() = textDocumentService()
20-
2118
private val mutex = Mutex()
22-
private suspend fun <T> executeTask(id: String, logDescription: Any? = null, task: suspend () -> T): T {
19+
private suspend fun <T> executeTask(id: String, logDescription: Any? = null, task: suspend (TextDocumentService) -> T): T? {
2320
logger.trace {
2421
val summary = "$id: operation queued."
2522
val description = logDescription?.toString()
@@ -32,7 +29,10 @@ class TextDocumentServiceQueue(private val textDocumentService: () -> TextDocume
3229
withContext(Dispatchers.IO) {
3330
logger.trace { "$id: executing on the IO context." }
3431
try {
35-
task()
32+
textDocumentService()?.let { task(it) } ?: run {
33+
logger.error("Cannot get the service to perform task $id.")
34+
null
35+
}
3636
} finally {
3737
logger.trace { "$id: finished." }
3838
}
@@ -41,31 +41,31 @@ class TextDocumentServiceQueue(private val textDocumentService: () -> TextDocume
4141
}
4242

4343
suspend fun didOpen(params: DidOpenTextDocumentParams) {
44-
executeTask("didOpen notification", params) {
45-
service?.didOpen(params)
44+
executeTask("didOpen notification", params) { service ->
45+
service.didOpen(params)
4646
}
4747
}
4848

4949
suspend fun didClose(params: DidCloseTextDocumentParams) {
50-
executeTask("didClose notification", params) {
51-
service?.didClose(params)
50+
executeTask("didClose notification", params) { service ->
51+
service.didClose(params)
5252
}
5353
}
5454

5555
suspend fun didSave(params: DidSaveTextDocumentParams) {
56-
executeTask("didSave notification", params) {
57-
service?.didSave(params)
56+
executeTask("didSave notification", params) { service ->
57+
service.didSave(params)
5858
}
5959
}
6060

6161
suspend fun didChange(params: DidChangeTextDocumentParams) {
62-
executeTask("didChange notification", params) {
63-
service?.didChange(params)
62+
executeTask("didChange notification", params) { service ->
63+
service.didChange(params)
6464
}
6565
}
6666

6767
suspend fun completion(params: CompletionParams): Either<List<CompletionItem>, CompletionList>? =
68-
executeTask("completion request", params) {
69-
service?.completion(params)?.await()
68+
executeTask("completion request", params) { service ->
69+
service.completion(params)?.await()
7070
}
7171
}

0 commit comments

Comments
 (0)