Skip to content

Commit bbcfac1

Browse files
committed
fix(amazonq): add range field to indicate partial doc change
For the LSP's TextDocumentContentChangeEvent, if range is not specified, LSP would assume the contentChange is for the whole file and replace the file content with it, so if user is just making edits we need to specify range other wise file content will be replaced by this diff.
1 parent 43906bc commit bbcfac1

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import org.eclipse.lsp4j.DidChangeTextDocumentParams
2222
import org.eclipse.lsp4j.DidCloseTextDocumentParams
2323
import org.eclipse.lsp4j.DidOpenTextDocumentParams
2424
import org.eclipse.lsp4j.DidSaveTextDocumentParams
25+
import org.eclipse.lsp4j.Position
26+
import org.eclipse.lsp4j.Range
2527
import org.eclipse.lsp4j.TextDocumentContentChangeEvent
2628
import org.eclipse.lsp4j.TextDocumentIdentifier
2729
import org.eclipse.lsp4j.TextDocumentItem
@@ -164,6 +166,9 @@ class TextDocumentServiceHandler(
164166
pluginAwareExecuteOnPooledThread {
165167
val vFile = FileDocumentManager.getInstance().getFile(event.document) ?: return@pluginAwareExecuteOnPooledThread
166168
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)
167172
languageServer.textDocumentService.didChange(
168173
DidChangeTextDocumentParams().apply {
169174
textDocument = VersionedTextDocumentIdentifier().apply {
@@ -173,6 +178,7 @@ class TextDocumentServiceHandler(
173178
contentChanges = listOf(
174179
TextDocumentContentChangeEvent().apply {
175180
text = event.newFragment.toString()
181+
range = Range(Position(logicalPosition.line, logicalPosition.column), Position(newLogicalPosition.line, newLogicalPosition.column))
176182
}
177183
)
178184
}

0 commit comments

Comments
 (0)