Skip to content

Commit 334feda

Browse files
committed
implement file open and close
1 parent 4da2951 commit 334feda

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import software.aws.toolkits.core.utils.warn
3939
import software.aws.toolkits.jetbrains.isDeveloperMode
4040
import software.aws.toolkits.jetbrains.services.amazonq.lsp.encryption.JwtEncryptionManager
4141
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.createExtendedClientMetadata
42+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.textdocument.TextDocumentServiceHandler
4243
import software.aws.toolkits.jetbrains.services.telemetry.ClientMetadata
4344
import java.io.IOException
4445
import java.io.OutputStreamWriter
@@ -235,6 +236,12 @@ private class AmazonQServerInstance(private val project: Project, private val cs
235236
}
236237
languageServer.initialized(InitializedParams())
237238
}
239+
240+
TextDocumentServiceHandler(
241+
project,
242+
languageServer,
243+
this
244+
)
238245
}
239246

240247
override fun dispose() {

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

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,60 @@ package software.aws.toolkits.jetbrains.services.amazonq.lsp.textdocument
55

66
import com.intellij.openapi.Disposable
77
import com.intellij.openapi.project.Project
8-
import com.intellij.openapi.vfs.VirtualFileManager
9-
import com.intellij.openapi.vfs.newvfs.BulkFileListener
10-
import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent
11-
import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent
12-
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
13-
import org.eclipse.lsp4j.CreateFilesParams
14-
import org.eclipse.lsp4j.DeleteFilesParams
15-
import org.eclipse.lsp4j.FileCreate
16-
import org.eclipse.lsp4j.FileDelete
8+
import com.intellij.openapi.fileEditor.FileEditorManager
9+
import com.intellij.openapi.fileEditor.FileEditorManagerListener
10+
import com.intellij.openapi.vfs.VirtualFile
11+
import org.eclipse.lsp4j.DidCloseTextDocumentParams
12+
import org.eclipse.lsp4j.DidOpenTextDocumentParams
13+
import org.eclipse.lsp4j.TextDocumentIdentifier
14+
import org.eclipse.lsp4j.TextDocumentItem
1715
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLanguageServer
18-
import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread
1916

2017
class TextDocumentServiceHandler(
2118
private val project: Project,
22-
private val languageServer: AmazonQLanguageServer
23-
) : Disposable {
24-
25-
fun startTextDocumentServiceListeners() {
19+
private val languageServer: AmazonQLanguageServer,
20+
private val serverInstance: Disposable
21+
) {
2622

23+
init {
24+
subscribeToFileEditorEvents()
2725
}
2826

29-
private fun didClose() {
27+
// for textDocument/ didOpen and didClose
28+
private fun subscribeToFileEditorEvents() {
29+
project.messageBus.connect(serverInstance).subscribe(
30+
FileEditorManagerListener.FILE_EDITOR_MANAGER,
31+
object: FileEditorManagerListener {
32+
override fun fileOpened(
33+
source: FileEditorManager,
34+
file: VirtualFile
35+
) {
36+
languageServer.textDocumentService.didOpen(
37+
DidOpenTextDocumentParams().apply {
38+
textDocument = TextDocumentItem().apply {
39+
uri = file.url
40+
text = file.inputStream.readAllBytes().decodeToString()
41+
}
42+
}
43+
)
44+
}
3045

46+
override fun fileClosed(
47+
source: FileEditorManager,
48+
file: VirtualFile
49+
) {
50+
languageServer.textDocumentService.didClose(
51+
DidCloseTextDocumentParams().apply {
52+
textDocument = TextDocumentIdentifier().apply {
53+
uri = file.url
54+
}
55+
}
56+
)
57+
}
58+
}
59+
)
3160
}
3261

33-
private fun didOpen() {
34-
35-
}
3662
private fun didChange() {
3763

3864
}
@@ -41,6 +67,4 @@ class TextDocumentServiceHandler(
4167

4268
}
4369

44-
override fun dispose() {
45-
}
4670
}

0 commit comments

Comments
 (0)