Skip to content

Commit 79b1f38

Browse files
committed
init executeIfRunning implementation
1 parent 878b06e commit 79b1f38

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLspService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ internal class LSPProcessListener : ProcessListener {
8787

8888
@Service(Service.Level.PROJECT)
8989
class AmazonQLspService(private val project: Project, private val cs: CoroutineScope) : Disposable {
90-
private var instance: AmazonQServerInstance? = null
90+
internal var instance: AmazonQServerInstance? = null
9191

9292
init {
9393
cs.launch {
@@ -112,12 +112,12 @@ class AmazonQLspService(private val project: Project, private val cs: CoroutineS
112112
}
113113
}
114114

115-
private class AmazonQServerInstance(private val project: Project, private val cs: CoroutineScope) : Disposable {
115+
internal class AmazonQServerInstance(private val project: Project, private val cs: CoroutineScope) : Disposable {
116116
private val encryptionManager = JwtEncryptionManager()
117117

118118
private val launcher: Launcher<AmazonQLanguageServer>
119119

120-
private val languageServer: AmazonQLanguageServer
120+
internal val languageServer: AmazonQLanguageServer
121121
get() = launcher.remoteProxy
122122

123123
@Suppress("ForbiddenVoid")

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/textdocument/TextDocumentServiceHandler.kt

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.eclipse.lsp4j.DidOpenTextDocumentParams
1313
import org.eclipse.lsp4j.TextDocumentIdentifier
1414
import org.eclipse.lsp4j.TextDocumentItem
1515
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLanguageServer
16+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
1617

1718
class TextDocumentServiceHandler(
1819
private val project: Project,
@@ -31,31 +32,38 @@ class TextDocumentServiceHandler(
3132
)
3233
}
3334

35+
fun executeIfRunning(project: Project, runnable: (AmazonQLanguageServer) -> Unit) =
36+
AmazonQLspService.getInstance(project).instance?.languageServer?.let { runnable(it)}
37+
3438
override fun fileOpened(
3539
source: FileEditorManager,
36-
file: VirtualFile,
40+
file: VirtualFile
3741
) {
38-
languageServer.textDocumentService.didOpen(
39-
DidOpenTextDocumentParams().apply {
40-
textDocument = TextDocumentItem().apply {
41-
uri = file.url
42-
text = file.inputStream.readAllBytes().decodeToString()
42+
executeIfRunning(project) {
43+
it.textDocumentService.didOpen(
44+
DidOpenTextDocumentParams().apply {
45+
textDocument = TextDocumentItem().apply {
46+
uri = file.url
47+
text = file.inputStream.readAllBytes().decodeToString()
48+
}
4349
}
44-
}
45-
)
50+
)
51+
}
4652
}
4753

4854
override fun fileClosed(
4955
source: FileEditorManager,
5056
file: VirtualFile,
5157
) {
52-
languageServer.textDocumentService.didClose(
53-
DidCloseTextDocumentParams().apply {
54-
textDocument = TextDocumentIdentifier().apply {
55-
uri = file.url
58+
executeIfRunning(project) {
59+
it.textDocumentService.didClose(
60+
DidCloseTextDocumentParams().apply {
61+
textDocument = TextDocumentIdentifier().apply {
62+
uri = file.url
63+
}
5664
}
57-
}
58-
)
65+
)
66+
}
5967
}
6068

6169
private fun didChange() {

0 commit comments

Comments
 (0)