Skip to content

Commit 3abcd1f

Browse files
fix(amazonq): update didRenameFile to call didOpen & didClose for documents (#5524)
* add missing version/languageId to didOpen * remove extra params * tests * detekt
1 parent 5f752ca commit 3abcd1f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/workspace/WorkspaceServiceHandler.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ import org.eclipse.lsp4j.CreateFilesParams
2020
import org.eclipse.lsp4j.DeleteFilesParams
2121
import org.eclipse.lsp4j.DidChangeWatchedFilesParams
2222
import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams
23+
import org.eclipse.lsp4j.DidCloseTextDocumentParams
24+
import org.eclipse.lsp4j.DidOpenTextDocumentParams
2325
import org.eclipse.lsp4j.FileChangeType
2426
import org.eclipse.lsp4j.FileCreate
2527
import org.eclipse.lsp4j.FileDelete
2628
import org.eclipse.lsp4j.FileEvent
2729
import org.eclipse.lsp4j.FileRename
2830
import org.eclipse.lsp4j.RenameFilesParams
31+
import org.eclipse.lsp4j.TextDocumentIdentifier
32+
import org.eclipse.lsp4j.TextDocumentItem
2933
import org.eclipse.lsp4j.WorkspaceFolder
3034
import org.eclipse.lsp4j.WorkspaceFoldersChangeEvent
3135
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
@@ -135,6 +139,29 @@ class WorkspaceServiceHandler(
135139
val oldUri = toUriString(parentFile)?.let { parentUri -> "$parentUri/$oldFileName" }
136140
val newUri = toUriString(renamedFile)
137141

142+
if (!renamedFile.isDirectory) {
143+
oldUri?.let { uri ->
144+
languageServer.textDocumentService.didClose(
145+
DidCloseTextDocumentParams().apply {
146+
textDocument = TextDocumentIdentifier().apply {
147+
this.uri = uri
148+
}
149+
}
150+
)
151+
}
152+
153+
newUri?.let { uri ->
154+
languageServer.textDocumentService.didOpen(
155+
DidOpenTextDocumentParams().apply {
156+
textDocument = TextDocumentItem().apply {
157+
this.uri = uri
158+
text = renamedFile.inputStream.readAllBytes().decodeToString()
159+
}
160+
}
161+
)
162+
}
163+
}
164+
138165
FileRename().apply {
139166
this.oldUri = oldUri
140167
this.newUri = newUri

plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/workspace/WorkspaceServiceHandlerTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ import org.eclipse.lsp4j.CreateFilesParams
3131
import org.eclipse.lsp4j.DeleteFilesParams
3232
import org.eclipse.lsp4j.DidChangeWatchedFilesParams
3333
import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams
34+
import org.eclipse.lsp4j.DidCloseTextDocumentParams
35+
import org.eclipse.lsp4j.DidOpenTextDocumentParams
3436
import org.eclipse.lsp4j.FileChangeType
3537
import org.eclipse.lsp4j.RenameFilesParams
3638
import org.eclipse.lsp4j.WorkspaceFolder
3739
import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage
40+
import org.eclipse.lsp4j.services.TextDocumentService
3841
import org.eclipse.lsp4j.services.WorkspaceService
3942
import org.junit.jupiter.api.BeforeEach
4043
import org.junit.jupiter.api.Test
@@ -50,13 +53,15 @@ class WorkspaceServiceHandlerTest {
5053
private lateinit var project: Project
5154
private lateinit var mockLanguageServer: AmazonQLanguageServer
5255
private lateinit var mockWorkspaceService: WorkspaceService
56+
private lateinit var mockTextDocumentService: TextDocumentService
5357
private lateinit var sut: WorkspaceServiceHandler
5458
private lateinit var mockApplication: Application
5559

5660
@BeforeEach
5761
fun setup() {
5862
project = mockk<Project>()
5963
mockWorkspaceService = mockk<WorkspaceService>()
64+
mockTextDocumentService = mockk<TextDocumentService>()
6065
mockLanguageServer = mockk<AmazonQLanguageServer>()
6166

6267
mockApplication = mockk<Application>()
@@ -89,6 +94,11 @@ class WorkspaceServiceHandlerTest {
8994
every { mockWorkspaceService.didChangeWatchedFiles(any()) } returns Unit
9095
every { mockWorkspaceService.didChangeWorkspaceFolders(any()) } returns Unit
9196

97+
// Mock textDocument service (for didRename calls)
98+
every { mockLanguageServer.textDocumentService } returns mockTextDocumentService
99+
every { mockTextDocumentService.didOpen(any()) } returns Unit
100+
every { mockTextDocumentService.didClose(any()) } returns Unit
101+
92102
// Mock message bus
93103
val messageBus = mockk<MessageBus>()
94104
every { project.messageBus } returns messageBus
@@ -389,6 +399,15 @@ class WorkspaceServiceHandlerTest {
389399
// Act
390400
sut.after(listOf(propertyEvent))
391401

402+
val closeParams = slot<DidCloseTextDocumentParams>()
403+
verify { mockTextDocumentService.didClose(capture(closeParams)) }
404+
assertThat(closeParams.captured.textDocument.uri).isEqualTo(normalizeFileUri("file:///testDir/$oldName"))
405+
406+
val openParams = slot<DidOpenTextDocumentParams>()
407+
verify { mockTextDocumentService.didOpen(capture(openParams)) }
408+
assertThat(openParams.captured.textDocument.uri).isEqualTo(normalizeFileUri("file:///testDir/$newName"))
409+
assertThat(openParams.captured.textDocument.text).isEqualTo(normalizeFileUri("content"))
410+
392411
// Assert
393412
val paramsSlot = slot<RenameFilesParams>()
394413
verify { mockWorkspaceService.didRenameFiles(capture(paramsSlot)) }
@@ -411,6 +430,8 @@ class WorkspaceServiceHandlerTest {
411430
sut.after(listOf(propertyEvent))
412431

413432
// Assert
433+
verify(exactly = 0) { mockTextDocumentService.didClose(any()) }
434+
verify(exactly = 0) { mockTextDocumentService.didOpen(any()) }
414435
verify(exactly = 0) { mockWorkspaceService.didRenameFiles(any()) }
415436
}
416437

@@ -427,6 +448,8 @@ class WorkspaceServiceHandlerTest {
427448
sut.after(listOf(propertyEvent))
428449

429450
// Assert
451+
verify(exactly = 0) { mockTextDocumentService.didClose(any()) }
452+
verify(exactly = 0) { mockTextDocumentService.didOpen(any()) }
430453
val paramsSlot = slot<RenameFilesParams>()
431454
verify { mockWorkspaceService.didRenameFiles(capture(paramsSlot)) }
432455
with(paramsSlot.captured.files[0]) {
@@ -624,6 +647,7 @@ class WorkspaceServiceHandlerTest {
624647
every { fileSystem } returns mockk {
625648
every { protocol } returns "file"
626649
}
650+
every { this@mockk.inputStream } returns "content".byteInputStream()
627651
}
628652
}
629653

0 commit comments

Comments
 (0)