@@ -5,34 +5,60 @@ package software.aws.toolkits.jetbrains.services.amazonq.lsp.textdocument
55
66import com.intellij.openapi.Disposable
77import 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
1715import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLanguageServer
18- import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread
1916
2017class 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