@@ -33,7 +33,10 @@ import org.eclipse.lsp4j.TextDocumentContentChangeEvent
33
33
import org.eclipse.lsp4j.TextDocumentIdentifier
34
34
import org.eclipse.lsp4j.TextDocumentItem
35
35
import org.eclipse.lsp4j.VersionedTextDocumentIdentifier
36
+ import software.aws.toolkits.core.utils.getLogger
36
37
import software.aws.toolkits.core.utils.tryOrNull
38
+ import software.aws.toolkits.core.utils.warn
39
+ import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLanguageServer
37
40
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
38
41
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ACTIVE_EDITOR_CHANGED_NOTIFICATION
39
42
import software.aws.toolkits.jetbrains.services.amazonq.lsp.util.LspEditorUtil.getCursorState
@@ -98,40 +101,36 @@ class TextDocumentServiceHandler(
98
101
}
99
102
}
100
103
101
- cs.launch {
102
- AmazonQLspService .executeAsyncIfRunning(project) { languageServer ->
103
- toUriString(file)?.let { uri ->
104
- languageServer.textDocumentService.didOpen(
105
- DidOpenTextDocumentParams ().apply {
106
- textDocument = TextDocumentItem ().apply {
107
- this .uri = uri
108
- text = file.inputStream.readAllBytes().decodeToString()
109
- languageId = file.fileType.name.lowercase()
110
- version = file.modificationStamp.toInt()
111
- }
104
+ trySendIfValid { languageServer ->
105
+ toUriString(file)?.let { uri ->
106
+ languageServer.textDocumentService.didOpen(
107
+ DidOpenTextDocumentParams ().apply {
108
+ textDocument = TextDocumentItem ().apply {
109
+ this .uri = uri
110
+ text = file.inputStream.readAllBytes().decodeToString()
111
+ languageId = file.fileType.name.lowercase()
112
+ version = file.modificationStamp.toInt()
112
113
}
113
- )
114
- }
114
+ }
115
+ )
115
116
}
116
117
}
117
118
}
118
119
}
119
120
120
121
override fun beforeDocumentSaving (document : Document ) {
121
- cs.launch {
122
- AmazonQLspService .executeAsyncIfRunning(project) { languageServer ->
123
- val file = FileDocumentManager .getInstance().getFile(document) ? : return @executeAsyncIfRunning
124
- toUriString(file)?.let { uri ->
125
- languageServer.textDocumentService.didSave(
126
- DidSaveTextDocumentParams ().apply {
127
- textDocument = TextDocumentIdentifier ().apply {
128
- this .uri = uri
129
- }
130
- // TODO: should respect `textDocumentSync.save.includeText` server capability config
131
- text = document.text
122
+ trySendIfValid { languageServer ->
123
+ val file = FileDocumentManager .getInstance().getFile(document) ? : return @trySendIfValid
124
+ toUriString(file)?.let { uri ->
125
+ languageServer.textDocumentService.didSave(
126
+ DidSaveTextDocumentParams ().apply {
127
+ textDocument = TextDocumentIdentifier ().apply {
128
+ this .uri = uri
132
129
}
133
- )
134
- }
130
+ // TODO: should respect `textDocumentSync.save.includeText` server capability config
131
+ text = document.text
132
+ }
133
+ )
135
134
}
136
135
}
137
136
}
@@ -141,23 +140,21 @@ class TextDocumentServiceHandler(
141
140
val document = FileDocumentManager .getInstance().getCachedDocument(event.file) ? : return @forEach
142
141
143
142
handleFileOpened(event.file)
144
- cs.launch {
145
- AmazonQLspService .executeAsyncIfRunning(project) { languageServer ->
146
- toUriString(event.file)?.let { uri ->
147
- languageServer.textDocumentService.didChange(
148
- DidChangeTextDocumentParams ().apply {
149
- textDocument = VersionedTextDocumentIdentifier ().apply {
150
- this .uri = uri
151
- version = document.modificationStamp.toInt()
152
- }
153
- contentChanges = listOf (
154
- TextDocumentContentChangeEvent ().apply {
155
- text = document.text
156
- }
157
- )
143
+ trySendIfValid { languageServer ->
144
+ toUriString(event.file)?.let { uri ->
145
+ languageServer.textDocumentService.didChange(
146
+ DidChangeTextDocumentParams ().apply {
147
+ textDocument = VersionedTextDocumentIdentifier ().apply {
148
+ this .uri = uri
149
+ version = document.modificationStamp.toInt()
158
150
}
159
- )
160
- }
151
+ contentChanges = listOf (
152
+ TextDocumentContentChangeEvent ().apply {
153
+ text = document.text
154
+ }
155
+ )
156
+ }
157
+ )
161
158
}
162
159
}
163
160
}
@@ -179,17 +176,15 @@ class TextDocumentServiceHandler(
179
176
tryOrNull { FileDocumentManager .getInstance().getDocument(file)?.removeDocumentListener(listener) }
180
177
file.putUserData(KEY_REAL_TIME_EDIT_LISTENER , null )
181
178
182
- cs.launch {
183
- AmazonQLspService .executeAsyncIfRunning(project) { languageServer ->
184
- toUriString(file)?.let { uri ->
185
- languageServer.textDocumentService.didClose(
186
- DidCloseTextDocumentParams ().apply {
187
- textDocument = TextDocumentIdentifier ().apply {
188
- this .uri = uri
189
- }
179
+ trySendIfValid { languageServer ->
180
+ toUriString(file)?.let { uri ->
181
+ languageServer.textDocumentService.didClose(
182
+ DidCloseTextDocumentParams ().apply {
183
+ textDocument = TextDocumentIdentifier ().apply {
184
+ this .uri = uri
190
185
}
191
- )
192
- }
186
+ }
187
+ )
193
188
}
194
189
}
195
190
}
@@ -221,24 +216,22 @@ class TextDocumentServiceHandler(
221
216
}
222
217
223
218
private fun realTimeEdit (event : DocumentEvent ) {
224
- cs.launch {
225
- AmazonQLspService .executeAsyncIfRunning(project) { languageServer ->
226
- val vFile = FileDocumentManager .getInstance().getFile(event.document) ? : return @executeAsyncIfRunning
227
- toUriString(vFile)?.let { uri ->
228
- languageServer.textDocumentService.didChange(
229
- DidChangeTextDocumentParams ().apply {
230
- textDocument = VersionedTextDocumentIdentifier ().apply {
231
- this .uri = uri
232
- version = event.document.modificationStamp.toInt()
233
- }
234
- contentChanges = listOf (
235
- TextDocumentContentChangeEvent ().apply {
236
- text = event.document.text
237
- }
238
- )
219
+ trySendIfValid { languageServer ->
220
+ val vFile = FileDocumentManager .getInstance().getFile(event.document) ? : return @trySendIfValid
221
+ toUriString(vFile)?.let { uri ->
222
+ languageServer.textDocumentService.didChange(
223
+ DidChangeTextDocumentParams ().apply {
224
+ textDocument = VersionedTextDocumentIdentifier ().apply {
225
+ this .uri = uri
226
+ version = event.document.modificationStamp.toInt()
239
227
}
240
- )
241
- }
228
+ contentChanges = listOf (
229
+ TextDocumentContentChangeEvent ().apply {
230
+ text = event.document.text
231
+ }
232
+ )
233
+ }
234
+ )
242
235
}
243
236
}
244
237
// Process document changes here
@@ -247,7 +240,20 @@ class TextDocumentServiceHandler(
247
240
override fun dispose () {
248
241
}
249
242
243
+ private fun trySendIfValid (runnable : (AmazonQLanguageServer ) -> Unit ) {
244
+ cs.launch {
245
+ AmazonQLspService .executeAsyncIfRunning(project) { languageServer ->
246
+ try {
247
+ runnable(languageServer)
248
+ } catch (e: Exception ) {
249
+ LOG .warn { " Invalid document: $e " }
250
+ }
251
+ }
252
+ }
253
+ }
254
+
250
255
companion object {
251
256
private val KEY_REAL_TIME_EDIT_LISTENER = Key .create<DocumentListener >(" amazonq.textdocument.realtimeedit.listener" )
257
+ private val LOG = getLogger<TextDocumentServiceHandler >()
252
258
}
253
259
}
0 commit comments