Skip to content

Commit d95edfe

Browse files
committed
fix(amazonq): fix UI freeze when editing large files
1 parent 51d038e commit d95edfe

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "bugfix",
3+
"description" : "Fix UI freezes that may occur when interacting with large files in the editor"
4+
}

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

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

66
import com.intellij.openapi.Disposable
7+
import com.intellij.openapi.application.ApplicationManager
78
import com.intellij.openapi.editor.Document
9+
import com.intellij.openapi.editor.event.DocumentEvent
10+
import com.intellij.openapi.editor.event.DocumentListener
811
import com.intellij.openapi.fileEditor.FileDocumentManager
912
import com.intellij.openapi.fileEditor.FileDocumentManagerListener
1013
import com.intellij.openapi.fileEditor.FileEditorManager
@@ -29,10 +32,11 @@ import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread
2932

3033
class TextDocumentServiceHandler(
3134
private val project: Project,
32-
serverInstance: Disposable,
35+
private val serverInstance: Disposable,
3336
) : FileDocumentManagerListener,
3437
FileEditorManagerListener,
35-
BulkFileListener {
38+
BulkFileListener,
39+
DocumentListener {
3640

3741
init {
3842
// didOpen & didClose events
@@ -61,18 +65,30 @@ class TextDocumentServiceHandler(
6165
}
6266

6367
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+
}
6478
AmazonQLspService.executeIfRunning(project) { languageServer ->
6579
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+
}
7389
}
74-
}
75-
)
90+
)
91+
}
7692
}
7793
}
7894
}
@@ -81,14 +97,16 @@ class TextDocumentServiceHandler(
8197
AmazonQLspService.executeIfRunning(project) { languageServer ->
8298
val file = FileDocumentManager.getInstance().getFile(document) ?: return@executeIfRunning
8399
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
88107
}
89-
text = document.text
90-
}
91-
)
108+
)
109+
}
92110
}
93111
}
94112
}
@@ -141,4 +159,28 @@ class TextDocumentServiceHandler(
141159
}
142160
}
143161
}
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+
}
144186
}

0 commit comments

Comments
 (0)