Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -68,6 +68,8 @@
textDocument = TextDocumentItem().apply {
this.uri = uri
text = file.inputStream.readAllBytes().decodeToString()
languageId = file.fileType.name.lowercase()
version = file.modificationStamp.toInt()

Check warning on line 72 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/textdocument/TextDocumentServiceHandler.kt

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L71 - L72 were not covered by tests
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
textDocument = TextDocumentItem().apply {
this.uri = uri
text = renamedFile.inputStream.readAllBytes().decodeToString()
languageId = renamedFile.fileType.name.lowercase()
version = renamedFile.modificationStamp.toInt()

Check warning on line 160 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/workspace/WorkspaceServiceHandler.kt

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L159 - L160 were not covered by tests
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.intellij.openapi.components.serviceIfCreated
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
Expand Down Expand Up @@ -149,27 +150,27 @@ class TextDocumentServiceHandlerTest {
with(paramsSlot.captured.textDocument) {
assertThat(this.uri).isEqualTo(normalizeFileUri(uri.toString()))
assertThat(text).isEqualTo(content)
assertThat(languageId).isEqualTo("java")
assertThat(version).isEqualTo(1)
}
}

@Test
fun `didOpen runs on fileOpened`() = runTest {
// Create test file
val uri = URI.create("file:///test/path/file.txt")
val content = "test content"

val file = createMockVirtualFile(uri, content)

// Call the handler method
sut.fileOpened(mockk(), file)

// Verify the correct LSP method was called with matching parameters
val paramsSlot = slot<DidOpenTextDocumentParams>()
verify { mockTextDocumentService.didOpen(capture(paramsSlot)) }

with(paramsSlot.captured.textDocument) {
assertThat(this.uri).isEqualTo(normalizeFileUri(uri.toString()))
assertThat(text).isEqualTo(content)
assertThat(languageId).isEqualTo("java")
assertThat(version).isEqualTo(1)
}
}

Expand Down Expand Up @@ -297,11 +298,21 @@ class TextDocumentServiceHandlerTest {
}
}

private fun createMockVirtualFile(uri: URI, content: String = ""): VirtualFile {
private fun createMockVirtualFile(
uri: URI,
content: String = "",
fileTypeName: String = "JAVA",
modificationStamp: Long = 1L,
): VirtualFile {
val path = mockk<Path> {
every { toUri() } returns uri
}
val inputStream = content.byteInputStream()

val mockFileType = mockk<FileType> {
every { name } returns fileTypeName
}

return mockk<VirtualFile> {
every { url } returns uri.path
every { toNioPath() } returns path
Expand All @@ -310,6 +321,8 @@ class TextDocumentServiceHandlerTest {
every { protocol } returns "file"
}
every { [email protected] } returns inputStream
every { fileType } returns mockFileType
every { [email protected] } returns modificationStamp
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.openapi.Disposable
import com.intellij.openapi.application.Application
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.serviceIfCreated
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.newvfs.events.VFileCopyEvent
Expand Down Expand Up @@ -394,6 +395,8 @@ class WorkspaceServiceHandlerTest {
oldName = oldName,
newName = newName,
isDirectory = false,
fileTypeName = "JAVA",
modificationStamp = 123L
)

// Act
Expand All @@ -405,8 +408,12 @@ class WorkspaceServiceHandlerTest {

val openParams = slot<DidOpenTextDocumentParams>()
verify { mockTextDocumentService.didOpen(capture(openParams)) }
assertThat(openParams.captured.textDocument.uri).isEqualTo(normalizeFileUri("file:///testDir/$newName"))
assertThat(openParams.captured.textDocument.text).isEqualTo(normalizeFileUri("content"))
with(openParams.captured.textDocument) {
assertThat(uri).isEqualTo(normalizeFileUri("file:///testDir/$newName"))
assertThat(text).isEqualTo("content")
assertThat(languageId).isEqualTo("java")
assertThat(version).isEqualTo(123)
}

// Assert
val paramsSlot = slot<RenameFilesParams>()
Expand Down Expand Up @@ -464,10 +471,14 @@ class WorkspaceServiceHandlerTest {
val event1 = createMockPropertyChangeEvent(
oldName = "old1.java",
newName = "new1.java",
fileTypeName = "JAVA",
modificationStamp = 123L
)
val event2 = createMockPropertyChangeEvent(
oldName = "old2.py",
newName = "new2.py",
fileTypeName = "Python",
modificationStamp = 456L
)

// Act
Expand All @@ -477,6 +488,17 @@ class WorkspaceServiceHandlerTest {
val paramsSlot = slot<RenameFilesParams>()
verify { mockWorkspaceService.didRenameFiles(capture(paramsSlot)) }
assertThat(paramsSlot.captured.files).hasSize(2)

// Verify didClose and didOpen for both files
verify(exactly = 2) { mockTextDocumentService.didClose(any()) }

val openParamsSlot = mutableListOf<DidOpenTextDocumentParams>()
verify(exactly = 2) { mockTextDocumentService.didOpen(capture(openParamsSlot)) }

assertThat(openParamsSlot[0].textDocument.languageId).isEqualTo("java")
assertThat(openParamsSlot[0].textDocument.version).isEqualTo(123)
assertThat(openParamsSlot[1].textDocument.languageId).isEqualTo("python")
assertThat(openParamsSlot[1].textDocument.version).isEqualTo(456)
}

@Test
Expand Down Expand Up @@ -635,10 +657,19 @@ class WorkspaceServiceHandlerTest {
assertThat(paramsSlot.captured.event.removed[0].name).isEqualTo("folder2")
}

private fun createMockVirtualFile(uri: URI, fileName: String, isDirectory: Boolean = false): VirtualFile {
private fun createMockVirtualFile(
uri: URI,
fileName: String,
isDirectory: Boolean = false,
fileTypeName: String = "PLAIN_TEXT",
modificationStamp: Long = 1L,
): VirtualFile {
val nioPath = mockk<Path> {
every { toUri() } returns uri
}
val mockFileType = mockk<FileType> {
every { name } returns fileTypeName
}
return mockk<VirtualFile> {
every { [email protected] } returns isDirectory
every { toNioPath() } returns nioPath
Expand All @@ -648,6 +679,8 @@ class WorkspaceServiceHandlerTest {
every { protocol } returns "file"
}
every { [email protected] } returns "content".byteInputStream()
every { fileType } returns mockFileType
every { [email protected] } returns modificationStamp
}
}

Expand All @@ -671,10 +704,12 @@ class WorkspaceServiceHandlerTest {
oldName: String,
newName: String,
isDirectory: Boolean = false,
fileTypeName: String = "PLAIN_TEXT",
modificationStamp: Long = 1L,
): VFilePropertyChangeEvent {
val parent = createMockVirtualFile(URI("file:///testDir/"), "testDir", true)
val newUri = URI("file:///testDir/$newName")
val file = createMockVirtualFile(newUri, newName, isDirectory)
val file = createMockVirtualFile(newUri, newName, isDirectory, fileTypeName, modificationStamp)
every { file.parent } returns parent

return mockk<VFilePropertyChangeEvent>().apply {
Expand Down
Loading