Skip to content

fix(amazonq): fix UI freeze when editing large files #5685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type" : "bugfix",
"description" : "Fix UI freezes that may occur when interacting with large files in the editor"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package software.aws.toolkits.jetbrains.services.amazonq.lsp.textdocument

import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.event.DocumentEvent
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileDocumentManagerListener
import com.intellij.openapi.fileEditor.FileEditorManager
Expand All @@ -29,10 +32,11 @@ import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread

class TextDocumentServiceHandler(
private val project: Project,
serverInstance: Disposable,
private val serverInstance: Disposable,
) : FileDocumentManagerListener,
FileEditorManagerListener,
BulkFileListener {
BulkFileListener,
DocumentListener {

init {
// didOpen & didClose events
Expand Down Expand Up @@ -61,18 +65,30 @@ class TextDocumentServiceHandler(
}

private fun handleFileOpened(file: VirtualFile) {
ApplicationManager.getApplication().runReadAction {
FileDocumentManager.getInstance().getDocument(file)?.addDocumentListener(
object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
realTimeEdit(event)
}
},
serverInstance
)
}
AmazonQLspService.executeIfRunning(project) { languageServer ->
toUriString(file)?.let { uri ->
languageServer.textDocumentService.didOpen(
DidOpenTextDocumentParams().apply {
textDocument = TextDocumentItem().apply {
this.uri = uri
text = file.inputStream.readAllBytes().decodeToString()
languageId = file.fileType.name.lowercase()
version = file.modificationStamp.toInt()
pluginAwareExecuteOnPooledThread {
languageServer.textDocumentService.didOpen(
DidOpenTextDocumentParams().apply {
textDocument = TextDocumentItem().apply {
this.uri = uri
text = file.inputStream.readAllBytes().decodeToString()
languageId = file.fileType.name.lowercase()
version = file.modificationStamp.toInt()
}
}
}
)
)
}
}
}
}
Expand All @@ -81,14 +97,16 @@ class TextDocumentServiceHandler(
AmazonQLspService.executeIfRunning(project) { languageServer ->
val file = FileDocumentManager.getInstance().getFile(document) ?: return@executeIfRunning
toUriString(file)?.let { uri ->
languageServer.textDocumentService.didSave(
DidSaveTextDocumentParams().apply {
textDocument = TextDocumentIdentifier().apply {
this.uri = uri
pluginAwareExecuteOnPooledThread {
languageServer.textDocumentService.didSave(
DidSaveTextDocumentParams().apply {
textDocument = TextDocumentIdentifier().apply {
this.uri = uri
}
text = document.text
}
text = document.text
}
)
)
}
}
}
}
Expand Down Expand Up @@ -141,4 +159,28 @@ class TextDocumentServiceHandler(
}
}
}

private fun realTimeEdit(event: DocumentEvent) {
AmazonQLspService.executeIfRunning(project) { languageServer ->
pluginAwareExecuteOnPooledThread {
val vFile = FileDocumentManager.getInstance().getFile(event.document) ?: return@pluginAwareExecuteOnPooledThread
toUriString(vFile)?.let { uri ->
languageServer.textDocumentService.didChange(
DidChangeTextDocumentParams().apply {
textDocument = VersionedTextDocumentIdentifier().apply {
this.uri = uri
version = event.document.modificationStamp.toInt()
}
contentChanges = listOf(
TextDocumentContentChangeEvent().apply {
text = event.document.text
}
)
}
)
}
}
}
// Process document changes here
}
}
Loading