Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
Expand Up @@ -20,12 +20,16 @@ import org.eclipse.lsp4j.CreateFilesParams
import org.eclipse.lsp4j.DeleteFilesParams
import org.eclipse.lsp4j.DidChangeWatchedFilesParams
import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams
import org.eclipse.lsp4j.DidCloseTextDocumentParams
import org.eclipse.lsp4j.DidOpenTextDocumentParams
import org.eclipse.lsp4j.FileChangeType
import org.eclipse.lsp4j.FileCreate
import org.eclipse.lsp4j.FileDelete
import org.eclipse.lsp4j.FileEvent
import org.eclipse.lsp4j.FileRename
import org.eclipse.lsp4j.RenameFilesParams
import org.eclipse.lsp4j.TextDocumentIdentifier
import org.eclipse.lsp4j.TextDocumentItem
import org.eclipse.lsp4j.WorkspaceFolder
import org.eclipse.lsp4j.WorkspaceFoldersChangeEvent
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
Expand Down Expand Up @@ -135,6 +139,29 @@ class WorkspaceServiceHandler(
val oldUri = toUriString(parentFile)?.let { parentUri -> "$parentUri/$oldFileName" }
val newUri = toUriString(renamedFile)

if (!renamedFile.isDirectory) {
oldUri?.let { uri ->
languageServer.textDocumentService.didClose(
DidCloseTextDocumentParams().apply {
textDocument = TextDocumentIdentifier().apply {
this.uri = uri
}
}
)
}

newUri?.let { uri ->
languageServer.textDocumentService.didOpen(
DidOpenTextDocumentParams().apply {
textDocument = TextDocumentItem().apply {
this.uri = uri
text = renamedFile.inputStream.readAllBytes().decodeToString()
}
}
)
}
}

FileRename().apply {
this.oldUri = oldUri
this.newUri = newUri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ import org.eclipse.lsp4j.CreateFilesParams
import org.eclipse.lsp4j.DeleteFilesParams
import org.eclipse.lsp4j.DidChangeWatchedFilesParams
import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams
import org.eclipse.lsp4j.DidCloseTextDocumentParams
import org.eclipse.lsp4j.DidOpenTextDocumentParams
import org.eclipse.lsp4j.FileChangeType
import org.eclipse.lsp4j.RenameFilesParams
import org.eclipse.lsp4j.WorkspaceFolder
import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage
import org.eclipse.lsp4j.services.TextDocumentService
import org.eclipse.lsp4j.services.WorkspaceService
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
Expand All @@ -50,13 +53,15 @@ class WorkspaceServiceHandlerTest {
private lateinit var project: Project
private lateinit var mockLanguageServer: AmazonQLanguageServer
private lateinit var mockWorkspaceService: WorkspaceService
private lateinit var mockTextDocumentService: TextDocumentService
private lateinit var sut: WorkspaceServiceHandler
private lateinit var mockApplication: Application

@BeforeEach
fun setup() {
project = mockk<Project>()
mockWorkspaceService = mockk<WorkspaceService>()
mockTextDocumentService = mockk<TextDocumentService>()
mockLanguageServer = mockk<AmazonQLanguageServer>()

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

// Mock textDocument service (for didRename calls)
every { mockLanguageServer.textDocumentService } returns mockTextDocumentService
every { mockTextDocumentService.didOpen(any()) } returns Unit
every { mockTextDocumentService.didClose(any()) } returns Unit

// Mock message bus
val messageBus = mockk<MessageBus>()
every { project.messageBus } returns messageBus
Expand Down Expand Up @@ -389,10 +399,19 @@ class WorkspaceServiceHandlerTest {
// Act
sut.after(listOf(propertyEvent))

val closeParams = slot<DidCloseTextDocumentParams>()
verify { mockTextDocumentService.didClose(capture(closeParams)) }
assertEquals(normalizeFileUri("file:///testDir/$oldName"), closeParams.captured.textDocument.uri)

val openParams = slot<DidOpenTextDocumentParams>()
verify { mockTextDocumentService.didOpen(capture(openParams)) }
assertEquals(normalizeFileUri("file:///testDir/$newName"), openParams.captured.textDocument.uri)
assertEquals(normalizeFileUri("content"), openParams.captured.textDocument.text)

// Assert
val paramsSlot = slot<RenameFilesParams>()
verify { mockWorkspaceService.didRenameFiles(capture(paramsSlot)) }
with(paramsSlot.captured.files[0]) {
val renameParams = slot<RenameFilesParams>()
verify { mockWorkspaceService.didRenameFiles(capture(renameParams)) }
with(renameParams.captured.files[0]) {
assertEquals(normalizeFileUri("file:///testDir/$oldName"), oldUri)
assertEquals(normalizeFileUri("file:///testDir/$newName"), newUri)
}
Expand All @@ -411,6 +430,8 @@ class WorkspaceServiceHandlerTest {
sut.after(listOf(propertyEvent))

// Assert
verify(exactly = 0) { mockTextDocumentService.didClose(any()) }
verify(exactly = 0) { mockTextDocumentService.didOpen(any()) }
verify(exactly = 0) { mockWorkspaceService.didRenameFiles(any()) }
}

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

// Assert
verify(exactly = 0) { mockTextDocumentService.didClose(any()) }
verify(exactly = 0) { mockTextDocumentService.didOpen(any()) }
val paramsSlot = slot<RenameFilesParams>()
verify { mockWorkspaceService.didRenameFiles(capture(paramsSlot)) }
with(paramsSlot.captured.files[0]) {
Expand Down Expand Up @@ -624,6 +647,7 @@ class WorkspaceServiceHandlerTest {
every { fileSystem } returns mockk {
every { protocol } returns "file"
}
every { [email protected] } returns "content".byteInputStream()
}
}

Expand Down
Loading