4
4
package software.aws.toolkits.jetbrains.services.amazonq.lsp.textdocument
5
5
6
6
import com.intellij.openapi.Disposable
7
+ import com.intellij.openapi.application.ApplicationManager
7
8
import com.intellij.openapi.editor.Document
9
+ import com.intellij.openapi.editor.event.DocumentEvent
10
+ import com.intellij.openapi.editor.event.DocumentListener
8
11
import com.intellij.openapi.fileEditor.FileDocumentManager
9
12
import com.intellij.openapi.fileEditor.FileDocumentManagerListener
10
13
import com.intellij.openapi.fileEditor.FileEditorManager
@@ -29,10 +32,11 @@ import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread
29
32
30
33
class TextDocumentServiceHandler (
31
34
private val project : Project ,
32
- serverInstance : Disposable ,
35
+ private val serverInstance : Disposable ,
33
36
) : FileDocumentManagerListener,
34
37
FileEditorManagerListener ,
35
- BulkFileListener {
38
+ BulkFileListener ,
39
+ DocumentListener {
36
40
37
41
init {
38
42
// didOpen & didClose events
@@ -61,18 +65,30 @@ class TextDocumentServiceHandler(
61
65
}
62
66
63
67
private fun handleFileOpened (file : VirtualFile ) {
68
+ ApplicationManager .getApplication().runReadAction {
69
+ FileDocumentManager .getInstance().getDocument(file)?.addDocumentListener(
70
+ object : DocumentListener {
71
+ override fun documentChanged (event : DocumentEvent ) {
72
+ realTimeEdit(event)
73
+ }
74
+ },
75
+ serverInstance
76
+ )
77
+ }
64
78
AmazonQLspService .executeIfRunning(project) { languageServer ->
65
79
toUriString(file)?.let { uri ->
66
- languageServer.textDocumentService.didOpen(
67
- DidOpenTextDocumentParams ().apply {
68
- textDocument = TextDocumentItem ().apply {
69
- this .uri = uri
70
- text = file.inputStream.readAllBytes().decodeToString()
71
- languageId = file.fileType.name.lowercase()
72
- version = file.modificationStamp.toInt()
80
+ pluginAwareExecuteOnPooledThread {
81
+ languageServer.textDocumentService.didOpen(
82
+ DidOpenTextDocumentParams ().apply {
83
+ textDocument = TextDocumentItem ().apply {
84
+ this .uri = uri
85
+ text = file.inputStream.readAllBytes().decodeToString()
86
+ languageId = file.fileType.name.lowercase()
87
+ version = file.modificationStamp.toInt()
88
+ }
73
89
}
74
- }
75
- )
90
+ )
91
+ }
76
92
}
77
93
}
78
94
}
@@ -81,14 +97,16 @@ class TextDocumentServiceHandler(
81
97
AmazonQLspService .executeIfRunning(project) { languageServer ->
82
98
val file = FileDocumentManager .getInstance().getFile(document) ? : return @executeIfRunning
83
99
toUriString(file)?.let { uri ->
84
- languageServer.textDocumentService.didSave(
85
- DidSaveTextDocumentParams ().apply {
86
- textDocument = TextDocumentIdentifier ().apply {
87
- this .uri = uri
100
+ pluginAwareExecuteOnPooledThread {
101
+ languageServer.textDocumentService.didSave(
102
+ DidSaveTextDocumentParams ().apply {
103
+ textDocument = TextDocumentIdentifier ().apply {
104
+ this .uri = uri
105
+ }
106
+ text = document.text
88
107
}
89
- text = document.text
90
- }
91
- )
108
+ )
109
+ }
92
110
}
93
111
}
94
112
}
@@ -141,4 +159,28 @@ class TextDocumentServiceHandler(
141
159
}
142
160
}
143
161
}
162
+
163
+ private fun realTimeEdit (event : DocumentEvent ) {
164
+ AmazonQLspService .executeIfRunning(project) { languageServer ->
165
+ pluginAwareExecuteOnPooledThread {
166
+ val vFile = FileDocumentManager .getInstance().getFile(event.document) ? : return @pluginAwareExecuteOnPooledThread
167
+ toUriString(vFile)?.let { uri ->
168
+ languageServer.textDocumentService.didChange(
169
+ DidChangeTextDocumentParams ().apply {
170
+ textDocument = VersionedTextDocumentIdentifier ().apply {
171
+ this .uri = uri
172
+ version = event.document.modificationStamp.toInt()
173
+ }
174
+ contentChanges = listOf (
175
+ TextDocumentContentChangeEvent ().apply {
176
+ text = event.document.text
177
+ }
178
+ )
179
+ }
180
+ )
181
+ }
182
+ }
183
+ }
184
+ // Process document changes here
185
+ }
144
186
}
0 commit comments