@@ -13,6 +13,7 @@ import com.intellij.openapi.fileEditor.FileDocumentManagerListener
13
13
import com.intellij.openapi.fileEditor.FileEditorManager
14
14
import com.intellij.openapi.fileEditor.FileEditorManagerListener
15
15
import com.intellij.openapi.project.Project
16
+ import com.intellij.openapi.util.Key
16
17
import com.intellij.openapi.vfs.VirtualFile
17
18
import com.intellij.openapi.vfs.VirtualFileManager
18
19
import com.intellij.openapi.vfs.newvfs.BulkFileListener
@@ -22,8 +23,6 @@ import org.eclipse.lsp4j.DidChangeTextDocumentParams
22
23
import org.eclipse.lsp4j.DidCloseTextDocumentParams
23
24
import org.eclipse.lsp4j.DidOpenTextDocumentParams
24
25
import org.eclipse.lsp4j.DidSaveTextDocumentParams
25
- import org.eclipse.lsp4j.Position
26
- import org.eclipse.lsp4j.Range
27
26
import org.eclipse.lsp4j.TextDocumentContentChangeEvent
28
27
import org.eclipse.lsp4j.TextDocumentIdentifier
29
28
import org.eclipse.lsp4j.TextDocumentItem
@@ -66,15 +65,16 @@ class TextDocumentServiceHandler(
66
65
}
67
66
68
67
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
+ }
78
78
}
79
79
AmazonQLspService .executeIfRunning(project) { languageServer ->
80
80
toUriString(file)?.let { uri ->
@@ -148,6 +148,11 @@ class TextDocumentServiceHandler(
148
148
source : FileEditorManager ,
149
149
file : VirtualFile ,
150
150
) {
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
+ }
151
156
AmazonQLspService .executeIfRunning(project) { languageServer ->
152
157
toUriString(file)?.let { uri ->
153
158
languageServer.textDocumentService.didClose(
@@ -166,9 +171,6 @@ class TextDocumentServiceHandler(
166
171
pluginAwareExecuteOnPooledThread {
167
172
val vFile = FileDocumentManager .getInstance().getFile(event.document) ? : return @pluginAwareExecuteOnPooledThread
168
173
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)
172
174
languageServer.textDocumentService.didChange(
173
175
DidChangeTextDocumentParams ().apply {
174
176
textDocument = VersionedTextDocumentIdentifier ().apply {
@@ -177,11 +179,7 @@ class TextDocumentServiceHandler(
177
179
}
178
180
contentChanges = listOf (
179
181
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
185
183
}
186
184
)
187
185
}
@@ -194,4 +192,8 @@ class TextDocumentServiceHandler(
194
192
195
193
override fun dispose () {
196
194
}
195
+
196
+ companion object {
197
+ private val KEY_REAL_TIME_EDIT_LISTENER = Key .create<DocumentListener >(" amazonq.textdocument.realtimeedit.listener" )
198
+ }
197
199
}
0 commit comments