@@ -13,6 +13,7 @@ import com.intellij.openapi.fileEditor.FileDocumentManagerListener
1313import  com.intellij.openapi.fileEditor.FileEditorManager 
1414import  com.intellij.openapi.fileEditor.FileEditorManagerListener 
1515import  com.intellij.openapi.project.Project 
16+ import  com.intellij.openapi.util.Key 
1617import  com.intellij.openapi.vfs.VirtualFile 
1718import  com.intellij.openapi.vfs.VirtualFileManager 
1819import  com.intellij.openapi.vfs.newvfs.BulkFileListener 
@@ -22,8 +23,6 @@ import org.eclipse.lsp4j.DidChangeTextDocumentParams
2223import  org.eclipse.lsp4j.DidCloseTextDocumentParams 
2324import  org.eclipse.lsp4j.DidOpenTextDocumentParams 
2425import  org.eclipse.lsp4j.DidSaveTextDocumentParams 
25- import  org.eclipse.lsp4j.Position 
26- import  org.eclipse.lsp4j.Range 
2726import  org.eclipse.lsp4j.TextDocumentContentChangeEvent 
2827import  org.eclipse.lsp4j.TextDocumentIdentifier 
2928import  org.eclipse.lsp4j.TextDocumentItem 
@@ -66,15 +65,16 @@ class TextDocumentServiceHandler(
6665    }
6766
6867    private  fun  handleFileOpened (file :  VirtualFile ) {
69-         ApplicationManager .getApplication().runReadAction {
70-             FileDocumentManager .getInstance().getDocument(file)?.addDocumentListener(
71-                 object  :  DocumentListener  {
72-                     override  fun  documentChanged (event :  DocumentEvent ) {
73-                         realTimeEdit(event)
74-                     }
75-                 },
76-                 this 
77-             )
68+         if  (file.getUserData(KEY_REAL_TIME_EDIT_LISTENER ) ==  null ) {
69+             val  listener =  object  :  DocumentListener  {
70+                 override  fun  documentChanged (event :  DocumentEvent ) {
71+                     realTimeEdit(event)
72+                 }
73+             }
74+             file.putUserData(KEY_REAL_TIME_EDIT_LISTENER , listener)
75+             ApplicationManager .getApplication().runReadAction {
76+                 FileDocumentManager .getInstance().getDocument(file)?.addDocumentListener(listener, this )
77+             }
7878        }
7979        AmazonQLspService .executeIfRunning(project) { languageServer -> 
8080            toUriString(file)?.let  { uri -> 
@@ -148,6 +148,11 @@ class TextDocumentServiceHandler(
148148        source :  FileEditorManager ,
149149        file :  VirtualFile ,
150150    ) {
151+         val  listener =  file.getUserData(KEY_REAL_TIME_EDIT_LISTENER )
152+         if  (listener !=  null ) {
153+             FileDocumentManager .getInstance().getDocument(file)?.removeDocumentListener(listener)
154+             file.putUserData(KEY_REAL_TIME_EDIT_LISTENER , null )
155+         }
151156        AmazonQLspService .executeIfRunning(project) { languageServer -> 
152157            toUriString(file)?.let  { uri -> 
153158                languageServer.textDocumentService.didClose(
@@ -166,9 +171,6 @@ class TextDocumentServiceHandler(
166171            pluginAwareExecuteOnPooledThread {
167172                val  vFile =  FileDocumentManager .getInstance().getFile(event.document) ? :  return @pluginAwareExecuteOnPooledThread
168173                toUriString(vFile)?.let  { uri -> 
169-                     val  editor =  FileEditorManager .getInstance(project).selectedTextEditor ? :  return @pluginAwareExecuteOnPooledThread
170-                     val  logicalPosition =  editor.offsetToLogicalPosition(event.offset)
171-                     val  newLogicalPosition =  editor.offsetToLogicalPosition(event.offset +  event.newLength)
172174                    languageServer.textDocumentService.didChange(
173175                        DidChangeTextDocumentParams ().apply  {
174176                            textDocument =  VersionedTextDocumentIdentifier ().apply  {
@@ -177,11 +179,7 @@ class TextDocumentServiceHandler(
177179                            }
178180                            contentChanges =  listOf (
179181                                TextDocumentContentChangeEvent ().apply  {
180-                                     text =  event.newFragment.toString()
181-                                     range =  Range (
182-                                         Position (logicalPosition.line, logicalPosition.column),
183-                                         Position (newLogicalPosition.line, newLogicalPosition.column)
184-                                     )
182+                                     text =  event.document.text
185183                                }
186184                            )
187185                        }
@@ -194,4 +192,8 @@ class TextDocumentServiceHandler(
194192
195193    override  fun  dispose () {
196194    }
195+ 
196+     companion  object  {
197+         private  val  KEY_REAL_TIME_EDIT_LISTENER  =  Key .create<DocumentListener >(" amazonq.textdocument.realtimeedit.listener"  )
198+     }
197199}
0 commit comments